2017-10-30 12:40:34 +08:00
|
|
|
<?php
|
|
|
|
|
|
2018-08-17 15:25:08 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
|
2017-10-30 12:40:34 +08:00
|
|
|
use App\Events;
|
2018-07-15 17:42:03 +08:00
|
|
|
use App\Mail\ForgotPassword;
|
2019-12-14 11:10:37 +08:00
|
|
|
use App\Models\Player;
|
|
|
|
|
use App\Models\User;
|
2020-03-09 17:18:36 +08:00
|
|
|
use App\Rules\Captcha;
|
2020-04-06 11:13:56 +08:00
|
|
|
use Blessing\Rejection;
|
2020-06-02 10:49:06 +08:00
|
|
|
use Carbon\Carbon;
|
2017-10-30 12:40:34 +08:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2025-06-30 04:36:20 +08:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
use Illuminate\Support\Facades\Event;
|
2019-12-14 11:10:37 +08:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use Illuminate\Support\Facades\URL;
|
|
|
|
|
use Illuminate\Support\Str;
|
2020-03-02 15:02:39 +08:00
|
|
|
use Tests\Fakes\Filter;
|
2019-12-24 23:59:25 +08:00
|
|
|
use Vectorface\Whip\Whip;
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
class AuthControllerTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
|
2019-04-04 11:04:13 +08:00
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
2025-06-26 21:16:56 +08:00
|
|
|
app()->instance(Captcha::class, new class extends Captcha {
|
2019-04-04 11:04:13 +08:00
|
|
|
public function passes($attribute, $value)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 12:40:34 +08:00
|
|
|
public function testLogin()
|
|
|
|
|
{
|
2025-06-26 21:16:56 +08:00
|
|
|
$filter = Filter::fake();
|
2020-06-07 18:54:52 +08:00
|
|
|
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->get('/auth/login')->assertSee('Log in');
|
2020-06-07 18:54:52 +08:00
|
|
|
$filter->assertApplied('auth_page_rows:login');
|
2017-10-30 12:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHandleLogin()
|
|
|
|
|
{
|
2019-05-01 10:38:50 +08:00
|
|
|
Event::fake();
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2020-10-14 11:56:34 +08:00
|
|
|
$user = User::factory()->create();
|
2018-07-19 10:31:44 +08:00
|
|
|
$user->changePassword('12345678');
|
2020-10-14 11:56:34 +08:00
|
|
|
$player = Player::factory()->create(['uid' => $user->uid]);
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if `identification` is empty
|
2019-04-05 08:58:00 +08:00
|
|
|
$this->postJson('/auth/login')->assertJsonValidationErrors('identification');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if `password` is empty
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2019-03-14 00:30:53 +08:00
|
|
|
'/auth/login', ['identification' => $user->email]
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('password');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if length of `password` is lower than 6
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/login', [
|
2025-06-26 21:16:56 +08:00
|
|
|
'identification' => $user->email,
|
|
|
|
|
'password' => '123',
|
|
|
|
|
])->assertJsonValidationErrors('password');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2018-02-24 16:05:07 +08:00
|
|
|
// Should return a warning if length of `password` is greater than 32
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/login', [
|
2025-06-26 21:16:56 +08:00
|
|
|
'identification' => $user->email,
|
|
|
|
|
'password' => Str::random(80),
|
|
|
|
|
])->assertJsonValidationErrors('password');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
$this->flushSession();
|
|
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
// Should return a warning if user isn't existed
|
|
|
|
|
$this->postJson(
|
|
|
|
|
'/auth/login', [
|
2025-06-26 21:16:56 +08:00
|
|
|
'identification' => 'nope@nope.net',
|
|
|
|
|
'password' => '12345678',
|
|
|
|
|
])->assertJson([
|
|
|
|
|
'code' => 2,
|
|
|
|
|
'message' => trans('auth.validation.user'),
|
|
|
|
|
]);
|
2021-01-02 15:51:37 +08:00
|
|
|
Event::assertDispatched('auth.login.attempt', function ($event, $payload) {
|
2019-12-24 17:09:30 +08:00
|
|
|
$this->assertEquals('nope@nope.net', $payload[0]);
|
|
|
|
|
$this->assertEquals('12345678', $payload[1]);
|
|
|
|
|
$this->assertEquals('email', $payload[2]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertNotDispatched('auth.login.ready');
|
|
|
|
|
Event::assertNotDispatched('auth.login.succeeded');
|
|
|
|
|
Event::assertNotDispatched('auth.login.failed');
|
|
|
|
|
$this->flushSession();
|
|
|
|
|
|
|
|
|
|
Event::fake();
|
2020-03-02 15:02:39 +08:00
|
|
|
$filter = Filter::fake();
|
2019-12-24 23:59:25 +08:00
|
|
|
$whip = new Whip();
|
|
|
|
|
$ip = $whip->getValidIpAddress();
|
|
|
|
|
$loginFailsCacheKey = sha1('login_fails_'.$ip);
|
2018-08-16 17:57:24 +08:00
|
|
|
|
2017-10-30 12:40:34 +08:00
|
|
|
// Logging in should be failed if password is wrong
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/login', [
|
2025-06-26 21:16:56 +08:00
|
|
|
'identification' => $user->email,
|
|
|
|
|
'password' => 'wrong-password',
|
|
|
|
|
])->assertJson(
|
|
|
|
|
[
|
|
|
|
|
'code' => 1,
|
|
|
|
|
'message' => trans('auth.validation.password'),
|
|
|
|
|
'data' => ['login_fails' => 1],
|
|
|
|
|
]
|
|
|
|
|
);
|
2020-03-02 15:02:39 +08:00
|
|
|
$filter->assertApplied('client_ip', function ($value) use ($ip) {
|
|
|
|
|
$this->assertEquals($ip, $value);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2019-04-04 09:48:04 +08:00
|
|
|
$this->assertTrue(Cache::has($loginFailsCacheKey));
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::assertDispatched('auth.login.attempt', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->email, $payload[0]);
|
|
|
|
|
$this->assertEquals('wrong-password', $payload[1]);
|
|
|
|
|
$this->assertEquals('email', $payload[2]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertDispatched('auth.login.ready', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->uid, $payload[0]->uid);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertDispatched('auth.login.failed', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->uid, $payload[0]->uid);
|
|
|
|
|
$this->assertEquals(1, $payload[1]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
$this->flushSession();
|
|
|
|
|
|
|
|
|
|
// Should check captcha if there are too many fails
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::put($loginFailsCacheKey, 4);
|
|
|
|
|
$this->postJson(
|
2023-01-16 23:15:41 +08:00
|
|
|
'/auth/login', [
|
2025-06-26 21:16:56 +08:00
|
|
|
'identification' => $user->email,
|
|
|
|
|
'password' => '12345678',
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('captcha');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::flush();
|
2017-10-30 12:40:34 +08:00
|
|
|
$this->flushSession();
|
|
|
|
|
|
|
|
|
|
// Should clean the `login_fails` session if logged in successfully
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::put($loginFailsCacheKey, 1);
|
|
|
|
|
$this->postJson('/auth/login', [
|
2017-10-30 12:40:34 +08:00
|
|
|
'identification' => $user->email,
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => '12345678',
|
2018-07-13 15:13:35 +08:00
|
|
|
])->assertJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
[
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
|
'message' => trans('auth.login.success'),
|
2017-10-30 12:40:34 +08:00
|
|
|
]
|
2018-08-16 17:57:24 +08:00
|
|
|
);
|
2019-04-04 09:48:04 +08:00
|
|
|
$this->assertFalse(Cache::has($loginFailsCacheKey));
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::assertDispatched('auth.login.ready', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->uid, $payload[0]->uid);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertDispatched('auth.login.succeeded', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->uid, $payload[0]->uid);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2019-05-01 10:38:50 +08:00
|
|
|
Event::assertDispatched(Events\UserTryToLogin::class);
|
|
|
|
|
Event::assertDispatched(Events\UserLoggedIn::class);
|
|
|
|
|
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::flush();
|
2017-10-30 12:40:34 +08:00
|
|
|
$this->flushSession();
|
|
|
|
|
|
|
|
|
|
// Logged in should be in success if logged in with player name
|
2020-01-13 08:37:57 +08:00
|
|
|
auth()->logout();
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/login', [
|
2025-06-26 21:16:56 +08:00
|
|
|
'identification' => $player->name,
|
|
|
|
|
'password' => '12345678',
|
|
|
|
|
]
|
2018-07-13 15:13:35 +08:00
|
|
|
)->assertJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
[
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
|
'message' => trans('auth.login.success'),
|
2017-10-30 12:40:34 +08:00
|
|
|
]
|
2018-07-20 14:42:43 +08:00
|
|
|
);
|
|
|
|
|
$this->assertAuthenticated();
|
2020-04-06 11:13:56 +08:00
|
|
|
auth()->logout();
|
|
|
|
|
|
|
|
|
|
// rejected by filter
|
|
|
|
|
$filter->add('can_login', function () {
|
|
|
|
|
return new Rejection('banned');
|
|
|
|
|
});
|
|
|
|
|
$this->postJson('/auth/login', [
|
|
|
|
|
'identification' => $player->name,
|
|
|
|
|
'password' => '12345678',
|
|
|
|
|
])->assertJson(['code' => 1, 'message' => 'banned']);
|
|
|
|
|
$filter->assertApplied(
|
|
|
|
|
'can_login',
|
|
|
|
|
function ($can, $identification, $password) use ($player) {
|
|
|
|
|
$this->assertEquals($player->name, $identification);
|
|
|
|
|
$this->assertEquals('12345678', $password);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
);
|
2017-10-30 12:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLogout()
|
|
|
|
|
{
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::fake();
|
2018-07-20 14:42:43 +08:00
|
|
|
|
2020-10-14 11:56:34 +08:00
|
|
|
$user = User::factory()->create();
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->actingAs($user)->postJson('/auth/logout')->assertJson(
|
|
|
|
|
[
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
|
'message' => trans('auth.logout.success'),
|
2018-07-20 14:42:43 +08:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$this->assertGuest();
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::assertDispatched('auth.logout.before', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->uid, $payload[0]->uid);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertDispatched('auth.logout.after', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->uid, $payload[0]->uid);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2017-10-30 12:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRegister()
|
|
|
|
|
{
|
2025-06-26 21:16:56 +08:00
|
|
|
$filter = Filter::fake();
|
2020-06-07 18:54:52 +08:00
|
|
|
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->get('/auth/register')->assertSee('Register');
|
2020-06-07 18:54:52 +08:00
|
|
|
$filter->assertApplied('auth_page_rows:register');
|
2017-10-30 12:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHandleRegister()
|
|
|
|
|
{
|
2019-05-01 10:38:50 +08:00
|
|
|
Event::fake();
|
2020-03-02 15:02:39 +08:00
|
|
|
$filter = Filter::fake();
|
2019-12-24 23:59:25 +08:00
|
|
|
$whip = new Whip();
|
|
|
|
|
$ip = $whip->getValidIpAddress();
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if `email` is empty
|
2019-04-05 08:58:00 +08:00
|
|
|
$this->postJson('/auth/register')->assertJsonValidationErrors('email');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if `email` is invalid
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/register',
|
2019-03-14 00:30:53 +08:00
|
|
|
['email' => 'not_an_email']
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('email');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
// An existed user
|
2020-10-14 11:56:34 +08:00
|
|
|
$existedUser = User::factory()->create();
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->postJson(
|
|
|
|
|
'/auth/register',
|
2019-03-14 00:30:53 +08:00
|
|
|
['email' => $existedUser->email]
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('email');
|
2018-07-20 14:42:43 +08:00
|
|
|
|
2017-10-30 12:40:34 +08:00
|
|
|
// Should return a warning if `password` is empty
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/register',
|
2019-03-14 00:30:53 +08:00
|
|
|
['email' => 'a@b.c']
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('password');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if length of `password` is lower than 8
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/register',
|
|
|
|
|
[
|
|
|
|
|
'email' => 'a@b.c',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => '1',
|
2019-03-14 00:30:53 +08:00
|
|
|
]
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('password');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2018-02-24 16:05:07 +08:00
|
|
|
// Should return a warning if length of `password` is greater than 32
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/register',
|
|
|
|
|
[
|
|
|
|
|
'email' => 'a@b.c',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => Str::random(33),
|
2018-08-17 17:03:38 +08:00
|
|
|
]
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('password');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2018-08-17 17:03:38 +08:00
|
|
|
// The register_with_player_name option is set to true by default.
|
|
|
|
|
// Should return a warning if `player_name` is empty
|
|
|
|
|
$this->postJson(
|
|
|
|
|
'/auth/register',
|
|
|
|
|
[
|
|
|
|
|
'email' => 'a@b.c',
|
|
|
|
|
'password' => '12345678',
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2018-08-17 17:03:38 +08:00
|
|
|
]
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('player_name');
|
2018-08-17 17:03:38 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if `player_name` is invalid
|
|
|
|
|
option(['player_name_rule' => 'official']);
|
|
|
|
|
$this->postJson(
|
|
|
|
|
'/auth/register',
|
|
|
|
|
[
|
|
|
|
|
'email' => 'a@b.c',
|
|
|
|
|
'password' => '12345678',
|
|
|
|
|
'player_name' => '角色名',
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2018-08-17 17:03:38 +08:00
|
|
|
]
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('player_name');
|
2018-08-17 17:03:38 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if `player_name` is too long
|
|
|
|
|
$this->postJson(
|
|
|
|
|
'/auth/register',
|
|
|
|
|
[
|
|
|
|
|
'email' => 'a@b.c',
|
|
|
|
|
'password' => '12345678',
|
2019-02-27 23:44:50 +08:00
|
|
|
'player_name' => Str::random(option('player_name_length_max') + 10),
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2018-08-17 17:03:38 +08:00
|
|
|
]
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('player_name');
|
2018-08-17 17:03:38 +08:00
|
|
|
|
|
|
|
|
// Existed player
|
2020-10-14 11:56:34 +08:00
|
|
|
$player = Player::factory()->create();
|
2018-08-17 17:03:38 +08:00
|
|
|
$this->postJson(
|
|
|
|
|
'/auth/register',
|
|
|
|
|
[
|
|
|
|
|
'email' => 'a@b.c',
|
|
|
|
|
'password' => '12345678',
|
2019-03-13 13:16:51 +08:00
|
|
|
'player_name' => $player->name,
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2018-08-17 17:03:38 +08:00
|
|
|
]
|
|
|
|
|
)->assertJson([
|
2020-06-02 10:49:06 +08:00
|
|
|
'code' => 1,
|
2019-04-23 11:47:45 +08:00
|
|
|
'message' => trans('user.player.add.repeated'),
|
2018-08-17 17:03:38 +08:00
|
|
|
]);
|
2019-03-22 21:40:12 +08:00
|
|
|
$this->assertNull(User::where('email', 'a@b.c')->first());
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::assertDispatched('auth.registration.attempt', function ($event, $payload) {
|
|
|
|
|
[$data] = $payload;
|
|
|
|
|
$this->assertEquals('a@b.c', $data['email']);
|
|
|
|
|
$this->assertEquals('12345678', $data['password']);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertNotDispatched('auth.registration.ready');
|
|
|
|
|
Event::assertNotDispatched('auth.registration.completed');
|
2018-08-17 17:03:38 +08:00
|
|
|
|
|
|
|
|
option(['register_with_player_name' => false]);
|
|
|
|
|
|
2017-10-30 12:40:34 +08:00
|
|
|
// Should return a warning if `nickname` is empty
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/register',
|
|
|
|
|
[
|
|
|
|
|
'email' => 'a@b.c',
|
2018-08-17 17:03:38 +08:00
|
|
|
'password' => '12345678',
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2019-03-14 00:30:53 +08:00
|
|
|
]
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('nickname');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if `nickname` is too long
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/register',
|
|
|
|
|
[
|
|
|
|
|
'email' => 'a@b.c',
|
|
|
|
|
'password' => '12345678',
|
2019-02-27 23:44:50 +08:00
|
|
|
'nickname' => Str::random(256),
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2019-03-14 00:30:53 +08:00
|
|
|
]
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('nickname');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
// Should return a warning if `captcha` is empty
|
|
|
|
|
$this->postJson(
|
|
|
|
|
'/auth/register',
|
|
|
|
|
[
|
|
|
|
|
'email' => 'a@b.c',
|
|
|
|
|
'password' => '12345678',
|
2019-03-02 22:58:37 +08:00
|
|
|
'nickname' => 'nickname',
|
2019-03-14 00:30:53 +08:00
|
|
|
]
|
2019-04-05 08:58:00 +08:00
|
|
|
)->assertJsonValidationErrors('captcha');
|
2018-07-20 14:42:43 +08:00
|
|
|
|
2020-06-19 19:05:51 +08:00
|
|
|
option(['regs_per_ip' => -1]);
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/register',
|
|
|
|
|
[
|
|
|
|
|
'email' => 'a@b.c',
|
|
|
|
|
'password' => '12345678',
|
|
|
|
|
'nickname' => 'nickname',
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2017-10-30 12:40:34 +08:00
|
|
|
]
|
2018-07-13 15:13:35 +08:00
|
|
|
)->assertJson([
|
2020-06-02 10:49:06 +08:00
|
|
|
'code' => 1,
|
2019-04-23 11:47:45 +08:00
|
|
|
'message' => trans('auth.register.max', ['regs' => option('regs_per_ip')]),
|
2017-10-30 12:40:34 +08:00
|
|
|
]);
|
|
|
|
|
|
2020-06-02 10:49:06 +08:00
|
|
|
option(['regs_per_ip' => 100]);
|
2017-10-30 12:40:34 +08:00
|
|
|
// Database should be updated if succeeded
|
2018-07-13 15:13:35 +08:00
|
|
|
$response = $this->postJson(
|
2017-10-30 12:40:34 +08:00
|
|
|
'/auth/register',
|
|
|
|
|
[
|
|
|
|
|
'email' => 'a@b.c',
|
|
|
|
|
'password' => '12345678',
|
|
|
|
|
'nickname' => 'nickname',
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2017-10-30 12:40:34 +08:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$newUser = User::where('email', 'a@b.c')->first();
|
2018-07-13 15:13:35 +08:00
|
|
|
$response->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
|
'message' => trans('auth.register.success'),
|
2018-07-20 14:42:43 +08:00
|
|
|
]);
|
2020-03-02 15:02:39 +08:00
|
|
|
$filter->assertApplied('client_ip', function ($value) use ($ip) {
|
|
|
|
|
$this->assertEquals($ip, $value);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2020-06-02 10:49:06 +08:00
|
|
|
$filter->assertApplied('user_password', function ($password) {
|
|
|
|
|
return app('cipher')->verify('12345678', $password);
|
|
|
|
|
});
|
2017-10-30 12:40:34 +08:00
|
|
|
$this->assertTrue($newUser->verifyPassword('12345678'));
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->assertDatabaseHas('users', [
|
2017-10-30 12:40:34 +08:00
|
|
|
'email' => 'a@b.c',
|
|
|
|
|
'nickname' => 'nickname',
|
|
|
|
|
'score' => option('user_initial_score'),
|
2019-12-24 23:59:25 +08:00
|
|
|
'ip' => $ip,
|
2019-03-02 22:58:37 +08:00
|
|
|
'permission' => User::NORMAL,
|
2017-10-30 12:40:34 +08:00
|
|
|
]);
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->assertAuthenticated();
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::assertDispatched('auth.registration.attempt', function ($event, $payload) {
|
|
|
|
|
[$data] = $payload;
|
|
|
|
|
$this->assertEquals('a@b.c', $data['email']);
|
|
|
|
|
$this->assertEquals('12345678', $data['password']);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertDispatched('auth.registration.ready', function ($event, $payload) {
|
|
|
|
|
[$data] = $payload;
|
|
|
|
|
$this->assertEquals('a@b.c', $data['email']);
|
|
|
|
|
$this->assertEquals('12345678', $data['password']);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertDispatched('auth.registration.completed', function ($event, $payload) {
|
|
|
|
|
[$user] = $payload;
|
|
|
|
|
$this->assertEquals('a@b.c', $user->email);
|
|
|
|
|
$this->assertGreaterThan(0, $user->uid);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2019-05-01 10:38:50 +08:00
|
|
|
Event::assertDispatched(Events\UserRegistered::class);
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::assertDispatched('auth.login.ready', function ($event, $payload) {
|
|
|
|
|
[$user] = $payload;
|
|
|
|
|
$this->assertEquals('a@b.c', $user->email);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertDispatched('auth.login.succeeded', function ($event, $payload) {
|
|
|
|
|
[$user] = $payload;
|
|
|
|
|
$this->assertEquals('a@b.c', $user->email);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2018-08-17 17:03:38 +08:00
|
|
|
|
|
|
|
|
// Require player name
|
2020-06-02 10:49:06 +08:00
|
|
|
Event::fake();
|
2018-08-19 18:28:38 +08:00
|
|
|
option(['register_with_player_name' => true]);
|
2020-01-13 08:37:57 +08:00
|
|
|
auth()->logout();
|
2018-08-17 17:03:38 +08:00
|
|
|
$this->postJson(
|
|
|
|
|
'/auth/register',
|
|
|
|
|
[
|
2018-08-19 18:28:38 +08:00
|
|
|
'email' => 'abc@test.org',
|
2018-08-17 17:03:38 +08:00
|
|
|
'password' => '12345678',
|
|
|
|
|
'player_name' => 'name',
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2018-08-17 17:03:38 +08:00
|
|
|
]
|
2019-04-23 11:47:45 +08:00
|
|
|
)->assertJson(['code' => 0]);
|
2019-03-13 13:16:51 +08:00
|
|
|
$this->assertNotNull(Player::where('player', 'name'));
|
2020-06-02 10:49:06 +08:00
|
|
|
Event::assertDispatched('player.adding', function ($eventName, $payload) {
|
|
|
|
|
$this->assertEquals('name', $payload[0]);
|
|
|
|
|
$this->assertEquals('abc@test.org', $payload[1]->email);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertDispatched('player.added', function ($eventName, $payload) {
|
|
|
|
|
$this->assertEquals('name', $payload[0]->name);
|
|
|
|
|
$this->assertEquals('abc@test.org', $payload[1]->email);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2020-04-06 11:13:56 +08:00
|
|
|
auth()->logout();
|
|
|
|
|
|
|
|
|
|
// rejected by filter
|
|
|
|
|
$filter = Filter::fake();
|
|
|
|
|
$filter->add('can_register', function () {
|
|
|
|
|
return new Rejection('disabled');
|
|
|
|
|
});
|
|
|
|
|
$this->postJson('/auth/register', [])
|
|
|
|
|
->assertJson(['code' => 1, 'message' => 'disabled']);
|
2017-10-30 12:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testForgot()
|
|
|
|
|
{
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->get('/auth/forgot')->assertSee('Forgot Password');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2020-03-09 12:29:00 +08:00
|
|
|
config(['mail.default' => '']);
|
2018-08-17 12:32:44 +08:00
|
|
|
$this->get('/auth/forgot')->assertSee(trans('auth.forgot.disabled'));
|
2017-10-30 12:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHandleForgot()
|
|
|
|
|
{
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::fake();
|
2018-07-13 19:23:20 +08:00
|
|
|
Mail::fake();
|
2020-03-02 15:02:39 +08:00
|
|
|
$filter = Filter::fake();
|
2018-07-13 19:23:20 +08:00
|
|
|
|
2017-10-30 12:40:34 +08:00
|
|
|
// Should be forbidden if "forgot password" is closed
|
2020-03-09 12:29:00 +08:00
|
|
|
config(['mail.default' => '']);
|
2018-08-12 16:00:21 +08:00
|
|
|
$this->postJson('/auth/forgot', [
|
2019-12-24 17:09:30 +08:00
|
|
|
'email' => 'nope@nope.net',
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2018-07-13 15:13:35 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
|
'message' => trans('auth.forgot.disabled'),
|
2017-10-30 12:40:34 +08:00
|
|
|
]);
|
2020-03-09 12:29:00 +08:00
|
|
|
config(['mail.default' => 'smtp']);
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2019-12-24 23:59:25 +08:00
|
|
|
$whip = new Whip();
|
|
|
|
|
$ip = $whip->getValidIpAddress();
|
|
|
|
|
$lastMailCacheKey = sha1('last_mail_'.$ip);
|
2018-08-17 16:07:24 +08:00
|
|
|
|
2017-10-30 12:40:34 +08:00
|
|
|
// Should be forbidden if sending email frequently
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::put($lastMailCacheKey, time());
|
|
|
|
|
$this->postJson('/auth/forgot', [
|
2019-12-24 17:09:30 +08:00
|
|
|
'email' => 'nope@nope.net',
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2018-07-13 15:13:35 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 2,
|
|
|
|
|
'message' => trans('auth.forgot.frequent-mail'),
|
2017-10-30 12:40:34 +08:00
|
|
|
]);
|
2020-03-02 15:02:39 +08:00
|
|
|
$filter->assertApplied('client_ip', function ($value) use ($ip) {
|
|
|
|
|
$this->assertEquals($ip, $value);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::assertDispatched('auth.forgot.attempt', function ($event, $payload) {
|
|
|
|
|
$this->assertEquals('nope@nope.net', $payload[0]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertNotDispatched('auth.forgot.ready');
|
|
|
|
|
Event::assertNotDispatched('auth.forgot.sent');
|
|
|
|
|
Event::assertNotDispatched('auth.forgot.sent');
|
2019-04-04 09:48:04 +08:00
|
|
|
Cache::flush();
|
2018-08-17 16:07:24 +08:00
|
|
|
$this->flushSession();
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if user is not existed
|
2020-10-14 11:56:34 +08:00
|
|
|
$user = User::factory()->create();
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->withSession(['phrase' => 'a'])->postJson('/auth/forgot', [
|
2017-10-30 12:40:34 +08:00
|
|
|
'email' => 'nope@nope.net',
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2018-07-13 15:13:35 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
|
'message' => trans('auth.forgot.unregistered'),
|
2017-10-30 12:40:34 +08:00
|
|
|
]);
|
|
|
|
|
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::fake();
|
2018-07-13 15:13:35 +08:00
|
|
|
$this->postJson('/auth/forgot', [
|
2017-10-30 12:40:34 +08:00
|
|
|
'email' => $user->email,
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2018-07-13 15:13:35 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
|
'message' => trans('auth.forgot.success'),
|
2018-08-17 16:07:24 +08:00
|
|
|
]);
|
2019-04-04 09:48:04 +08:00
|
|
|
$this->assertTrue(Cache::has($lastMailCacheKey));
|
|
|
|
|
Cache::flush();
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::assertDispatched('auth.forgot.attempt', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->email, $payload[0]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertDispatched('auth.forgot.ready', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->email, $payload[0]->email);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2018-07-18 11:04:34 +08:00
|
|
|
Mail::assertSent(ForgotPassword::class, function ($mail) use ($user) {
|
|
|
|
|
return $mail->hasTo($user->email);
|
2018-07-15 17:42:03 +08:00
|
|
|
});
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::assertDispatched('auth.forgot.sent', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->email, $payload[0]->email);
|
|
|
|
|
$this->assertStringContainsString('auth/reset/'.$user->uid, $payload[1]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should handle exception when sending email
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::fake();
|
2018-07-15 17:42:03 +08:00
|
|
|
Mail::shouldReceive('to')
|
2017-10-30 12:40:34 +08:00
|
|
|
->once()
|
|
|
|
|
->andThrow(new \Mockery\Exception('A fake exception.'));
|
|
|
|
|
$this->flushSession();
|
|
|
|
|
$this->withSession(['phrase' => 'a'])
|
2018-07-13 15:13:35 +08:00
|
|
|
->postJson('/auth/forgot', [
|
2017-10-30 12:40:34 +08:00
|
|
|
'email' => $user->email,
|
2019-03-02 22:58:37 +08:00
|
|
|
'captcha' => 'a',
|
2018-07-13 15:13:35 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 2,
|
|
|
|
|
'message' => trans('auth.forgot.failed', ['msg' => 'A fake exception.']),
|
2017-10-30 12:40:34 +08:00
|
|
|
]);
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::assertNotDispatched('auth.forgot.sent');
|
|
|
|
|
Event::assertDispatched('auth.forgot.failed', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->email, $payload[0]->email);
|
|
|
|
|
$this->assertStringContainsString('auth/reset/'.$user->uid, $payload[1]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2018-07-23 09:33:55 +08:00
|
|
|
|
|
|
|
|
// Addition: Mailable test
|
|
|
|
|
$site_name = option_localized('site_name');
|
|
|
|
|
$mailable = new ForgotPassword('url');
|
|
|
|
|
$mailable->build();
|
2018-08-16 18:10:09 +08:00
|
|
|
$this->assertEquals(trans('auth.forgot.mail.title', ['sitename' => $site_name]), $mailable->subject);
|
|
|
|
|
$this->assertEquals('mails.password-reset', $mailable->view);
|
2017-10-30 12:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testReset()
|
|
|
|
|
{
|
2020-10-14 11:56:34 +08:00
|
|
|
$user = User::factory()->create();
|
2020-03-30 10:01:37 +08:00
|
|
|
$url = URL::temporarySignedRoute(
|
|
|
|
|
'auth.reset',
|
2020-06-02 10:49:06 +08:00
|
|
|
Carbon::now()->addHour(),
|
2020-03-30 10:01:37 +08:00
|
|
|
['uid' => $user->uid],
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
$this->get($url)->assertSuccessful();
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2020-03-30 10:01:37 +08:00
|
|
|
$url = URL::temporarySignedRoute(
|
|
|
|
|
'auth.reset',
|
2020-06-02 10:49:06 +08:00
|
|
|
Carbon::now()->addHour(),
|
2020-03-30 10:01:37 +08:00
|
|
|
['uid' => $user->uid]
|
|
|
|
|
);
|
|
|
|
|
$this->get($url)->assertForbidden();
|
2017-10-30 12:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHandleReset()
|
|
|
|
|
{
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::fake();
|
|
|
|
|
|
2020-10-14 11:56:34 +08:00
|
|
|
$user = User::factory()->create();
|
2020-03-28 22:36:27 +08:00
|
|
|
$url = URL::temporarySignedRoute(
|
|
|
|
|
'auth.reset',
|
2020-06-02 10:49:06 +08:00
|
|
|
Carbon::now()->addHour(),
|
2020-03-28 22:36:27 +08:00
|
|
|
['uid' => $user->uid],
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// invalid signature
|
|
|
|
|
$this->postJson(route('auth.reset', ['uid' => $user->uid]))
|
|
|
|
|
->assertForbidden();
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if `password` is empty
|
2019-04-05 08:58:00 +08:00
|
|
|
$this->postJson($url)->assertJsonValidationErrors('password');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if `password` is too short
|
2019-12-24 17:09:30 +08:00
|
|
|
$this->postJson($url, ['password' => '123'])
|
|
|
|
|
->assertJsonValidationErrors('password');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
|
|
|
|
// Should return a warning if `password` is too long
|
2019-12-24 17:09:30 +08:00
|
|
|
$this->postJson($url, ['password' => Str::random(33)])
|
|
|
|
|
->assertJsonValidationErrors('password');
|
2017-10-30 12:40:34 +08:00
|
|
|
|
2017-11-05 10:25:20 +08:00
|
|
|
// Success
|
2019-12-24 17:09:30 +08:00
|
|
|
$this->postJson($url, ['password' => '12345678'])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
|
'message' => trans('auth.reset.success'),
|
2017-10-30 12:40:34 +08:00
|
|
|
]);
|
2019-12-24 17:09:30 +08:00
|
|
|
$user->refresh();
|
2017-10-30 12:40:34 +08:00
|
|
|
$this->assertTrue($user->verifyPassword('12345678'));
|
2019-12-24 17:09:30 +08:00
|
|
|
Event::assertDispatched('auth.reset.before', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->uid, $payload[0]->uid);
|
|
|
|
|
$this->assertEquals('12345678', $payload[1]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
Event::assertDispatched('auth.reset.after', function ($event, $payload) use ($user) {
|
|
|
|
|
$this->assertEquals($user->uid, $payload[0]->uid);
|
|
|
|
|
$this->assertEquals('12345678', $payload[1]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2017-10-30 12:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
2019-09-07 10:18:24 +08:00
|
|
|
public function testCaptcha()
|
|
|
|
|
{
|
|
|
|
|
$this->mock(\Gregwar\Captcha\CaptchaBuilder::class, function ($mock) {
|
|
|
|
|
$mock->shouldReceive('build')->with(100, 34)->once();
|
|
|
|
|
$mock->shouldReceive('getPhrase')->once()->andReturn('くみこ');
|
|
|
|
|
$mock->shouldReceive('output')->once()->andReturn('');
|
|
|
|
|
});
|
|
|
|
|
$this->get('/auth/captcha')
|
|
|
|
|
->assertSuccessful()
|
|
|
|
|
->assertHeader('Content-Type', 'image/jpeg')
|
|
|
|
|
->assertHeader('Cache-Control', 'no-store, private')
|
|
|
|
|
->assertSessionHas('captcha', 'くみこ');
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 13:01:39 +08:00
|
|
|
public function testFillEmail()
|
|
|
|
|
{
|
2020-10-14 11:56:34 +08:00
|
|
|
$user = User::factory()->create(['email' => '']);
|
|
|
|
|
$other = User::factory()->create();
|
2019-04-25 13:01:39 +08:00
|
|
|
$this->actingAs($user)->post('/auth/bind')->assertRedirect('/');
|
|
|
|
|
$this->actingAs($user)->post('/auth/bind', ['email' => 'a'])->assertRedirect('/');
|
|
|
|
|
$this->actingAs($user)->post('/auth/bind', ['email' => $other->email])->assertRedirect('/');
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->post('/auth/bind', ['email' => 'a@b.c'])->assertRedirect('/user');
|
|
|
|
|
$user->refresh();
|
|
|
|
|
$this->assertEquals('a@b.c', $user->email);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 12:32:44 +08:00
|
|
|
public function testVerify()
|
|
|
|
|
{
|
2020-08-31 19:48:31 +08:00
|
|
|
$url = URL::signedRoute('auth.verify', ['user' => 1], null, false);
|
2018-08-17 12:32:44 +08:00
|
|
|
|
2020-08-31 19:48:31 +08:00
|
|
|
// should be forbidden if account verification is disabled
|
2018-08-17 12:32:44 +08:00
|
|
|
option(['require_verification' => false]);
|
|
|
|
|
$this->get($url)->assertSee(trans('user.verification.disabled'));
|
|
|
|
|
option(['require_verification' => true]);
|
|
|
|
|
|
2020-08-31 19:48:31 +08:00
|
|
|
// invalid link
|
|
|
|
|
$this->get(route('auth.verify', ['user' => 1]))->assertForbidden();
|
2018-08-17 12:32:44 +08:00
|
|
|
|
2020-10-14 11:56:34 +08:00
|
|
|
$user = User::factory()->create(['verified' => false]);
|
2020-08-31 19:48:31 +08:00
|
|
|
$url = URL::signedRoute('auth.verify', ['user' => $user], null, false);
|
2018-08-17 12:32:44 +08:00
|
|
|
$this->get($url)->assertViewIs('auth.verify');
|
2020-08-31 19:48:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHandleVerify()
|
|
|
|
|
{
|
2020-10-14 11:56:34 +08:00
|
|
|
$user = User::factory()->create(['verified' => false]);
|
2020-08-31 19:48:31 +08:00
|
|
|
$url = URL::signedRoute('auth.verify', ['user' => $user], null, false);
|
|
|
|
|
|
|
|
|
|
// empty email
|
|
|
|
|
$this->post($url, [], ['Referer' => $url])->assertRedirect($url);
|
|
|
|
|
|
|
|
|
|
// not an email
|
|
|
|
|
$this->post($url, ['email' => 't'], ['Referer' => $url])->assertRedirect($url);
|
|
|
|
|
|
|
|
|
|
// invalid email
|
|
|
|
|
$this->post($url, ['email' => 'a@b.c'], ['Referer' => $url])
|
|
|
|
|
->assertRedirect($url);
|
|
|
|
|
|
|
|
|
|
// success
|
|
|
|
|
$this->post($url, ['email' => $user->email], ['Referer' => $url])
|
|
|
|
|
->assertRedirect(route('user.home'));
|
2018-08-17 12:32:44 +08:00
|
|
|
}
|
2017-10-30 12:40:34 +08:00
|
|
|
}
|