2017-11-18 13:25:08 +08:00
|
|
|
<?php
|
|
|
|
|
2018-08-17 15:25:08 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
2019-05-01 10:38:50 +08:00
|
|
|
use Event;
|
2018-08-17 15:25:08 +08:00
|
|
|
use Parsedown;
|
2017-11-18 13:25:08 +08:00
|
|
|
use App\Events;
|
|
|
|
use App\Models\User;
|
2019-02-27 23:44:50 +08:00
|
|
|
use Illuminate\Support\Str;
|
2018-08-17 12:32:44 +08:00
|
|
|
use App\Mail\EmailVerification;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
2017-11-18 13:25:08 +08:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
|
|
|
class UserControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
2019-04-27 23:10:21 +08:00
|
|
|
public function testUser()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
$this->actingAs($user, 'jwt')
|
|
|
|
->get('/api/user')
|
|
|
|
->assertJson([
|
|
|
|
'code' => 0,
|
|
|
|
'data' => [
|
|
|
|
'uid' => $user->uid,
|
|
|
|
'email' => $user->email,
|
|
|
|
'nickname' => $user->nickname,
|
|
|
|
'score' => $user->score,
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
public function testIndex()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
factory(\App\Models\Player::class)->create(['uid' => $user->uid]);
|
|
|
|
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2018-07-13 16:24:19 +08:00
|
|
|
->get('/user')
|
2017-11-18 13:25:08 +08:00
|
|
|
->assertViewHas('statistics')
|
2018-07-22 16:32:34 +08:00
|
|
|
->assertSee((new Parsedown())->text(option_localized('announcement')))
|
2018-07-13 16:24:19 +08:00
|
|
|
->assertSee((string) $user->score);
|
2018-08-17 12:32:44 +08:00
|
|
|
|
|
|
|
$unverified = factory(User::class)->create(['verified' => false]);
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($unverified)
|
2018-08-17 12:32:44 +08:00
|
|
|
->get('/user')
|
|
|
|
->assertDontSee(trans('user.verification.notice.title'));
|
2017-11-18 13:25:08 +08:00
|
|
|
}
|
|
|
|
|
2018-08-08 09:50:35 +08:00
|
|
|
public function testScoreInfo()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
factory(\App\Models\Player::class)->create(['uid' => $user->uid]);
|
|
|
|
|
|
|
|
$this->actingAs($user)
|
|
|
|
->get('/user/score-info')
|
2019-04-23 19:14:41 +08:00
|
|
|
->assertJson(['data' => [
|
2018-08-08 09:50:35 +08:00
|
|
|
'user' => [
|
|
|
|
'score' => $user->score,
|
2019-03-02 22:58:37 +08:00
|
|
|
'lastSignAt' => $user->last_sign_at,
|
2018-08-08 09:50:35 +08:00
|
|
|
],
|
|
|
|
'stats' => [
|
|
|
|
'players' => [
|
|
|
|
'used' => 1,
|
|
|
|
'total' => 11,
|
2019-03-02 22:58:37 +08:00
|
|
|
'percentage' => 1 / 11 * 100,
|
2018-08-08 09:50:35 +08:00
|
|
|
],
|
|
|
|
'storage' => [
|
|
|
|
'used' => 0,
|
|
|
|
'total' => $user->score,
|
2019-03-02 22:58:37 +08:00
|
|
|
'percentage' => 0,
|
|
|
|
],
|
2018-08-08 09:50:35 +08:00
|
|
|
],
|
|
|
|
'signAfterZero' => option('sign_after_zero'),
|
2019-03-02 22:58:37 +08:00
|
|
|
'signGapTime' => option('sign_gap_time'),
|
2019-04-23 19:14:41 +08:00
|
|
|
]]);
|
2018-08-08 09:50:35 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
public function testSign()
|
|
|
|
{
|
|
|
|
option(['sign_score' => '50,50']);
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
|
|
|
// Success
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2018-07-13 16:24:19 +08:00
|
|
|
->postJson('/user/sign')
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('user.sign-success', ['score' => 50]),
|
2019-04-23 19:14:41 +08:00
|
|
|
'data' => [
|
|
|
|
'score' => option('user_initial_score') + 50,
|
|
|
|
'storage' => [
|
|
|
|
'percentage' => 0,
|
|
|
|
'total' => option('user_initial_score') + 50,
|
|
|
|
'used' => 0,
|
|
|
|
],
|
|
|
|
'remaining_time' => (int) option('sign_gap_time'),
|
|
|
|
]
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Remaining time is greater than 0
|
2019-02-17 20:12:42 +08:00
|
|
|
$user = factory(User::class)->create(['last_sign_at' => get_datetime_string()]);
|
2019-02-17 21:23:07 +08:00
|
|
|
option(['sign_gap_time' => 2]);
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2019-02-17 20:12:42 +08:00
|
|
|
->postJson('/user/sign')
|
2018-07-13 16:24:19 +08:00
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans(
|
2017-11-18 13:25:08 +08:00
|
|
|
'user.cant-sign-until',
|
|
|
|
[
|
2019-02-17 21:23:07 +08:00
|
|
|
'time' => 2,
|
2019-03-02 22:58:37 +08:00
|
|
|
'unit' => trans('user.time-unit-hour'),
|
2017-11-18 13:25:08 +08:00
|
|
|
]
|
2019-03-02 22:58:37 +08:00
|
|
|
),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Can sign after 0 o'clock
|
|
|
|
option(['sign_after_zero' => true]);
|
2019-02-17 20:12:42 +08:00
|
|
|
$user = factory(User::class)->create(['last_sign_at' => get_datetime_string()]);
|
2017-11-18 13:25:08 +08:00
|
|
|
$diff = \Carbon\Carbon::now()->diffInSeconds(\Carbon\Carbon::tomorrow());
|
|
|
|
if ($diff / 3600 >= 1) {
|
|
|
|
$diff = round($diff / 3600);
|
|
|
|
$unit = 'hour';
|
|
|
|
} else {
|
|
|
|
$diff = round($diff / 60);
|
|
|
|
$unit = 'min';
|
|
|
|
}
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2019-02-17 20:12:42 +08:00
|
|
|
->postJson('/user/sign')
|
2018-07-13 16:24:19 +08:00
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans(
|
2017-11-18 13:25:08 +08:00
|
|
|
'user.cant-sign-until',
|
|
|
|
[
|
|
|
|
'time' => $diff,
|
2019-03-02 22:58:37 +08:00
|
|
|
'unit' => trans("user.time-unit-$unit"),
|
2017-11-18 13:25:08 +08:00
|
|
|
]
|
2019-03-02 22:58:37 +08:00
|
|
|
),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
|
2018-07-14 08:41:25 +08:00
|
|
|
$user = factory(User::class)->create([
|
2019-03-02 22:58:37 +08:00
|
|
|
'last_sign_at' => \Carbon\Carbon::today()->toDateTimeString(),
|
2018-07-14 08:41:25 +08:00
|
|
|
]);
|
2019-04-23 19:14:41 +08:00
|
|
|
$this->actingAs($user)->postJson('/user/sign')->assertJson(['code' => 0]);
|
2017-11-18 13:25:08 +08:00
|
|
|
}
|
|
|
|
|
2018-08-17 12:32:44 +08:00
|
|
|
public function testSendVerificationEmail()
|
|
|
|
{
|
|
|
|
Mail::fake();
|
|
|
|
|
|
|
|
$unverified = factory(User::class)->create(['verified' => false]);
|
|
|
|
$verified = factory(User::class)->create();
|
|
|
|
|
|
|
|
// Should be forbidden if account verification is disabled
|
|
|
|
option(['require_verification' => false]);
|
|
|
|
$this->actingAs($unverified)
|
|
|
|
->postJson('/user/email-verification')
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('user.verification.disabled'),
|
2018-08-17 12:32:44 +08:00
|
|
|
]);
|
|
|
|
option(['require_verification' => true]);
|
|
|
|
|
|
|
|
// Too frequent
|
|
|
|
$this->actingAs($unverified)
|
|
|
|
->withSession([
|
2019-03-02 22:58:37 +08:00
|
|
|
'last_mail_time' => time() - 10,
|
2018-08-17 12:32:44 +08:00
|
|
|
])
|
|
|
|
->postJson('/user/email-verification')
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('user.verification.frequent-mail'),
|
2018-08-17 12:32:44 +08:00
|
|
|
]);
|
|
|
|
$this->flushSession();
|
|
|
|
|
|
|
|
// Already verified
|
|
|
|
$this->actingAs($verified)
|
|
|
|
->postJson('/user/email-verification')
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('user.verification.verified'),
|
2018-08-17 12:32:44 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->actingAs($unverified)
|
|
|
|
->postJson('/user/email-verification')
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('user.verification.success'),
|
2018-08-17 12:32:44 +08:00
|
|
|
]);
|
|
|
|
Mail::assertSent(EmailVerification::class, function ($mail) use ($unverified) {
|
|
|
|
return $mail->hasTo($unverified->email);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Should handle exception when sending email
|
|
|
|
Mail::shouldReceive('to')
|
|
|
|
->once()
|
|
|
|
->andThrow(new \Mockery\Exception('A fake exception.'));
|
|
|
|
$this->flushSession();
|
|
|
|
$this->actingAs($unverified)
|
|
|
|
->postJson('/user/email-verification')
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 2,
|
|
|
|
'message' => trans('user.verification.failed', ['msg' => 'A fake exception.']),
|
2018-08-17 12:32:44 +08:00
|
|
|
]);
|
2018-08-17 14:44:22 +08:00
|
|
|
|
|
|
|
// Addition: Mailable test
|
|
|
|
$site_name = option_localized('site_name');
|
|
|
|
$mailable = new EmailVerification('url');
|
|
|
|
$mailable->build();
|
|
|
|
$this->assertTrue($mailable->hasFrom(config('mail.username'), $site_name));
|
|
|
|
$this->assertEquals(trans('user.verification.mail.title', ['sitename' => $site_name]), $mailable->subject);
|
|
|
|
$this->assertEquals('mails.email-verification', $mailable->view);
|
2018-08-17 12:32:44 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
public function testProfile()
|
|
|
|
{
|
2019-03-31 09:08:31 +08:00
|
|
|
$this->actAs('normal')->get('/user/profile')->assertViewIs('user.profile');
|
2017-11-18 13:25:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleProfile()
|
|
|
|
{
|
2019-05-01 10:38:50 +08:00
|
|
|
Event::fake();
|
2017-11-18 13:25:08 +08:00
|
|
|
$user = factory(User::class)->create();
|
2018-07-19 10:31:44 +08:00
|
|
|
$user->changePassword('12345678');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Invalid action
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2018-07-13 16:24:19 +08:00
|
|
|
->postJson('/user/profile')
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('general.illegal-parameters'),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Change nickname without `new_nickname` field
|
2019-03-14 00:30:53 +08:00
|
|
|
$this->postJson('/user/profile', ['action' => 'nickname'])
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('new_nickname');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Invalid nickname
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'nickname',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_nickname' => '\\',
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('new_nickname');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Too long nickname
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'nickname',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_nickname' => Str::random(256),
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('new_nickname');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
2019-03-22 21:40:12 +08:00
|
|
|
// Single player
|
|
|
|
option(['single_player' => true]);
|
|
|
|
factory(\App\Models\Player::class)->create(['uid' => $user->uid]);
|
|
|
|
$this->postJson('/user/profile', ['action' => 'nickname'])
|
2019-04-23 11:47:45 +08:00
|
|
|
->assertJson(['code' => 1, 'message' => trans('user.profile.nickname.single')]);
|
2019-03-22 21:40:12 +08:00
|
|
|
option(['single_player' => false]);
|
|
|
|
|
2017-11-18 13:25:08 +08:00
|
|
|
// Change nickname successfully
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'nickname',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_nickname' => 'nickname',
|
2018-07-13 16:24:19 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('user.profile.nickname.success', ['nickname' => 'nickname']),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
$this->assertEquals('nickname', User::find($user->uid)->nickname);
|
2019-05-01 10:38:50 +08:00
|
|
|
Event::assertDispatched(Events\UserProfileUpdated::class);
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Change password without `current_password` field
|
2019-03-14 00:30:53 +08:00
|
|
|
$this->postJson('/user/profile', ['action' => 'password'])
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('current_password');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Too short current password
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'password',
|
2018-06-18 21:50:32 +08:00
|
|
|
'current_password' => '1',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_password' => '12345678',
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('current_password');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Too long current password
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'password',
|
2019-02-27 23:44:50 +08:00
|
|
|
'current_password' => Str::random(33),
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_password' => '12345678',
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('current_password');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Too short new password
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'password',
|
|
|
|
'current_password' => '12345678',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_password' => '1',
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('new_password');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Too long new password
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'password',
|
|
|
|
'current_password' => '12345678',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_password' => Str::random(33),
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('new_password');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Wrong old password
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'password',
|
|
|
|
'current_password' => '1234567',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_password' => '87654321',
|
2018-07-13 16:24:19 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('user.profile.password.wrong-password'),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Change password successfully
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'password',
|
|
|
|
'current_password' => '12345678',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_password' => '87654321',
|
2018-07-13 16:24:19 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('user.profile.password.success'),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
2019-05-01 10:38:50 +08:00
|
|
|
Event::assertDispatched(Events\EncryptUserPassword::class);
|
2017-11-18 13:25:08 +08:00
|
|
|
$this->assertTrue(User::find($user->uid)->verifyPassword('87654321'));
|
|
|
|
// After changed password, user should re-login.
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->assertGuest();
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
$user = User::find($user->uid);
|
|
|
|
// Change email without `new_email` field
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2018-07-13 16:24:19 +08:00
|
|
|
->postJson(
|
2017-11-18 13:25:08 +08:00
|
|
|
'/user/profile',
|
2019-03-14 00:30:53 +08:00
|
|
|
['action' => 'email']
|
|
|
|
)
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('new_email');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Invalid email
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'email',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_email' => 'not_an_email',
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('new_email');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Too short current password
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'email',
|
|
|
|
'new_email' => 'a@b.c',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => '1',
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('password');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Too long current password
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'email',
|
|
|
|
'new_email' => 'a@b.c',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => Str::random(33),
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('password');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Use a duplicated email
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'email',
|
|
|
|
'new_email' => $user->email,
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => '87654321',
|
2018-07-13 16:24:19 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('user.profile.email.existed'),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Wrong password
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'email',
|
|
|
|
'new_email' => 'a@b.c',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => '7654321',
|
2018-07-13 16:24:19 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('user.profile.email.wrong-password'),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Change email successfully
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'email',
|
|
|
|
'new_email' => 'a@b.c',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => '87654321',
|
2018-07-13 16:24:19 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('user.profile.email.success'),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
$this->assertEquals('a@b.c', User::find($user->uid)->email);
|
2018-08-17 12:32:44 +08:00
|
|
|
$this->assertEquals(0, User::find($user->uid)->verified);
|
2017-11-18 13:25:08 +08:00
|
|
|
// After changed email, user should re-login.
|
2018-07-20 14:42:43 +08:00
|
|
|
$this->assertGuest();
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
$user = User::find($user->uid);
|
2018-08-17 12:32:44 +08:00
|
|
|
$user->verified = true;
|
|
|
|
$user->save();
|
2017-11-18 13:25:08 +08:00
|
|
|
// Delete account without `password` field
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2018-07-13 16:24:19 +08:00
|
|
|
->postJson(
|
2017-11-18 13:25:08 +08:00
|
|
|
'/user/profile',
|
2019-03-14 00:30:53 +08:00
|
|
|
['action' => 'delete']
|
|
|
|
)
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('password');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Too short current password
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'delete',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => '1',
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('password');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Too long current password
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'delete',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => Str::random(33),
|
2019-04-05 08:58:00 +08:00
|
|
|
])->assertJsonValidationErrors('password');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Wrong password
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'delete',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => '7654321',
|
2018-07-13 16:24:19 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('user.profile.delete.wrong-password'),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Delete account successfully
|
2018-07-13 16:24:19 +08:00
|
|
|
$this->postJson('/user/profile', [
|
2017-11-18 13:25:08 +08:00
|
|
|
'action' => 'delete',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => '87654321',
|
2018-07-13 16:24:19 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('user.profile.delete.success'),
|
2018-07-19 10:45:06 +08:00
|
|
|
]);
|
2017-11-18 13:25:08 +08:00
|
|
|
$this->assertNull(User::find($user->uid));
|
2018-08-02 10:21:25 +08:00
|
|
|
|
|
|
|
// Administrator cannot be deleted
|
|
|
|
$this->actAs('admin')
|
|
|
|
->postJson('/user/profile', [
|
|
|
|
'action' => 'delete',
|
2019-03-02 22:58:37 +08:00
|
|
|
'password' => '87654321',
|
2018-08-02 10:21:25 +08:00
|
|
|
])->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('user.profile.delete.admin'),
|
2018-08-02 10:21:25 +08:00
|
|
|
]);
|
2017-11-18 13:25:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetAvatar()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
$steve = factory(\App\Models\Texture::class)->create();
|
|
|
|
$cape = factory(\App\Models\Texture::class, 'cape')->create();
|
|
|
|
|
|
|
|
// Without `tid` field
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2019-03-14 00:30:53 +08:00
|
|
|
->postJson('/user/profile/avatar')
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('tid');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// TID is not a integer
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2019-03-14 00:30:53 +08:00
|
|
|
->postJson('/user/profile/avatar', ['tid' => 'string'])
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('tid');
|
2017-11-18 13:25:08 +08:00
|
|
|
|
|
|
|
// Texture cannot be found
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2018-07-13 16:24:19 +08:00
|
|
|
->postJson('/user/profile/avatar', [
|
2019-03-17 09:46:02 +08:00
|
|
|
'tid' => -1,
|
2017-11-18 13:25:08 +08:00
|
|
|
])
|
2018-07-13 16:24:19 +08:00
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('skinlib.non-existent'),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Use cape
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2018-07-13 16:24:19 +08:00
|
|
|
->postJson('/user/profile/avatar', [
|
2019-03-02 22:58:37 +08:00
|
|
|
'tid' => $cape->tid,
|
2017-11-18 13:25:08 +08:00
|
|
|
])
|
2018-07-13 16:24:19 +08:00
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('user.profile.avatar.wrong-type'),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Success
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2018-07-13 16:24:19 +08:00
|
|
|
->postJson('/user/profile/avatar', [
|
2019-03-02 22:58:37 +08:00
|
|
|
'tid' => $steve->tid,
|
2017-11-18 13:25:08 +08:00
|
|
|
])
|
2018-07-13 16:24:19 +08:00
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('user.profile.avatar.success'),
|
2017-11-18 13:25:08 +08:00
|
|
|
]);
|
|
|
|
$this->assertEquals($steve->tid, User::find($user->uid)->avatar);
|
2019-03-17 09:46:02 +08:00
|
|
|
|
|
|
|
// Reset avatar
|
|
|
|
$this->postJson('/user/profile/avatar', ['tid' => 0])
|
2019-04-23 11:47:45 +08:00
|
|
|
->assertJson(['code' => 0]);
|
2019-03-17 09:46:02 +08:00
|
|
|
$this->assertEquals(0, User::find($user->uid)->avatar);
|
2017-11-18 13:25:08 +08:00
|
|
|
}
|
|
|
|
}
|