X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/b90033a73032da8657f1bd3ec3687aa4426d8cc1..refs/pull/5280/head:/tests/User/UserManagementTest.php diff --git a/tests/User/UserManagementTest.php b/tests/User/UserManagementTest.php index a6d869b2f..37d9b3835 100644 --- a/tests/User/UserManagementTest.php +++ b/tests/User/UserManagementTest.php @@ -2,6 +2,7 @@ namespace Tests\User; +use BookStack\Access\UserInviteException; use BookStack\Access\UserInviteService; use BookStack\Activity\ActivityType; use BookStack\Uploads\Image; @@ -10,7 +11,6 @@ use BookStack\Users\Models\User; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; use Mockery\MockInterface; -use RuntimeException; use Tests\TestCase; class UserManagementTest extends TestCase @@ -85,6 +85,16 @@ class UserManagementTest extends TestCase $this->assertTrue(Hash::check('newpassword', $userPassword)); } + public function test_user_can_be_updated_with_single_char_name() + { + $user = $this->users->viewer(); + $this->asAdmin()->put("/settings/users/{$user->id}", [ + 'name' => 'b' + ])->assertRedirect('/settings/users'); + + $this->assertEquals('b', $user->refresh()->name); + } + public function test_user_cannot_be_deleted_if_last_admin() { $adminRole = Role::getRole('admin'); @@ -215,7 +225,7 @@ class UserManagementTest extends TestCase { $langs = ['en', 'fr', 'hr']; foreach ($langs as $lang) { - config()->set('app.locale', $lang); + config()->set('app.default_locale', $lang); $resp = $this->asAdmin()->get('/settings/users/create'); $this->withHtml($resp)->assertElementExists('select[name="language"] option[value="' . $lang . '"][selected]'); } @@ -229,7 +239,7 @@ class UserManagementTest extends TestCase // Simulate an invitation sending failure $this->mock(UserInviteService::class, function (MockInterface $mock) { - $mock->shouldReceive('sendInvitation')->once()->andThrow(RuntimeException::class); + $mock->shouldReceive('sendInvitation')->once()->andThrow(UserInviteException::class); }); $this->asAdmin()->post('/settings/users/create', [ @@ -247,22 +257,42 @@ class UserManagementTest extends TestCase { /** @var User $user */ $user = User::factory()->make(); - $adminRole = Role::getRole('admin'); $this->mock(UserInviteService::class, function (MockInterface $mock) { - $mock->shouldReceive('sendInvitation')->once()->andThrow(RuntimeException::class); + $mock->shouldReceive('sendInvitation')->once()->andThrow(UserInviteException::class); }); $this->asAdmin()->post('/settings/users/create', [ 'name' => $user->name, 'email' => $user->email, 'send_invite' => 'true', - 'roles[' . $adminRole->id . ']' => 'true', ]); $this->assertDatabaseMissing('activities', ['type' => 'USER_CREATE']); } + public function test_return_to_form_with_warning_if_the_invitation_sending_fails() + { + $logger = $this->withTestLogger(); + /** @var User $user */ + $user = User::factory()->make(); + + $this->mock(UserInviteService::class, function (MockInterface $mock) { + $mock->shouldReceive('sendInvitation')->once()->andThrow(UserInviteException::class); + }); + + $resp = $this->asAdmin()->post('/settings/users/create', [ + 'name' => $user->name, + 'email' => $user->email, + 'send_invite' => 'true', + ]); + + $resp->assertRedirect('/settings/users/create'); + $this->assertSessionError('Could not create user since invite email failed to send'); + $this->assertEquals($user->email, session()->getOldInput('email')); + $this->assertTrue($logger->hasErrorThatContains('Failed to send user invite with error:')); + } + public function test_user_create_update_fails_if_locale_is_invalid() { $user = $this->users->editor();