1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2024-10-30 15:42:41 +01:00
BookStack/tests/Auth/UserInviteTest.php

141 lines
5.2 KiB
PHP
Raw Normal View History

2021-06-26 17:23:15 +02:00
<?php
2021-06-26 17:23:15 +02:00
namespace Tests\Auth;
use BookStack\Auth\Access\UserInviteService;
use BookStack\Auth\User;
use BookStack\Notifications\UserInvite;
use Carbon\Carbon;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Notification;
2021-09-26 16:48:22 +02:00
use Illuminate\Support\Str;
use Tests\TestCase;
class UserInviteTest extends TestCase
{
public function test_user_creation_creates_invite()
{
Notification::fake();
$admin = $this->getAdmin();
$email = Str::random(16) . '@example.com';
$resp = $this->actingAs($admin)->post('/settings/users/create', [
2022-02-01 12:49:30 +01:00
'name' => 'Barry',
'email' => $email,
'send_invite' => 'true',
]);
$resp->assertRedirect('/settings/users');
$newUser = User::query()->where('email', '=', $email)->orderBy('id', 'desc')->first();
Notification::assertSentTo($newUser, UserInvite::class);
$this->assertDatabaseHas('user_invites', [
2021-06-26 17:23:15 +02:00
'user_id' => $newUser->id,
]);
}
public function test_user_invite_sent_in_selected_language()
{
Notification::fake();
$admin = $this->getAdmin();
$email = Str::random(16) . '@example.com';
$resp = $this->actingAs($admin)->post('/settings/users/create', [
2022-02-01 12:49:30 +01:00
'name' => 'Barry',
'email' => $email,
'send_invite' => 'true',
'language' => 'de',
]);
$resp->assertRedirect('/settings/users');
$newUser = User::query()->where('email', '=', $email)->orderBy('id', 'desc')->first();
Notification::assertSentTo($newUser, UserInvite::class, function ($notification, $channels, $notifiable) {
/** @var MailMessage $mail */
$mail = $notification->toMail($notifiable);
2022-02-01 12:49:30 +01:00
return 'Du wurdest eingeladen BookStack beizutreten!' === $mail->subject &&
'Ein Konto wurde für Sie auf BookStack erstellt.' === $mail->greeting;
});
}
public function test_invite_set_password()
{
Notification::fake();
$user = $this->getViewer();
$inviteService = app(UserInviteService::class);
$inviteService->sendInvitation($user);
$token = DB::table('user_invites')->where('user_id', '=', $user->id)->first()->token;
$setPasswordPageResp = $this->get('/register/invite/' . $token);
$setPasswordPageResp->assertSuccessful();
$setPasswordPageResp->assertSee('Welcome to BookStack!');
$setPasswordPageResp->assertSee('Password');
$setPasswordPageResp->assertSee('Confirm Password');
$setPasswordResp = $this->followingRedirects()->post('/register/invite/' . $token, [
'password' => 'my test password',
]);
$setPasswordResp->assertSee('Password set, you should now be able to login using your set password to access BookStack!');
$newPasswordValid = auth()->validate([
2022-02-01 12:49:30 +01:00
'email' => $user->email,
2021-06-26 17:23:15 +02:00
'password' => 'my test password',
]);
$this->assertTrue($newPasswordValid);
$this->assertDatabaseMissing('user_invites', [
2021-06-26 17:23:15 +02:00
'user_id' => $user->id,
]);
}
public function test_invite_set_has_password_validation()
{
Notification::fake();
$user = $this->getViewer();
$inviteService = app(UserInviteService::class);
$inviteService->sendInvitation($user);
$token = DB::table('user_invites')->where('user_id', '=', $user->id)->first()->token;
2019-09-14 00:58:40 +02:00
$this->get('/register/invite/' . $token);
$shortPassword = $this->followingRedirects()->post('/register/invite/' . $token, [
2019-09-14 00:58:40 +02:00
'password' => 'mypassw',
]);
2019-09-14 00:58:40 +02:00
$shortPassword->assertSee('The password must be at least 8 characters.');
2019-09-14 00:58:40 +02:00
$this->get('/register/invite/' . $token);
$noPassword = $this->followingRedirects()->post('/register/invite/' . $token, [
'password' => '',
]);
$noPassword->assertSee('The password field is required.');
$this->assertDatabaseHas('user_invites', [
2021-06-26 17:23:15 +02:00
'user_id' => $user->id,
]);
}
public function test_non_existent_invite_token_redirects_to_home()
{
2019-09-14 00:58:40 +02:00
$setPasswordPageResp = $this->get('/register/invite/' . Str::random(12));
$setPasswordPageResp->assertRedirect('/');
2019-09-14 00:58:40 +02:00
$setPasswordResp = $this->post('/register/invite/' . Str::random(12), ['password' => 'Password Test']);
$setPasswordResp->assertRedirect('/');
}
public function test_token_expires_after_two_weeks()
{
Notification::fake();
$user = $this->getViewer();
$inviteService = app(UserInviteService::class);
$inviteService->sendInvitation($user);
$tokenEntry = DB::table('user_invites')->where('user_id', '=', $user->id)->first();
DB::table('user_invites')->update(['created_at' => Carbon::now()->subDays(14)->subHour(1)]);
$setPasswordPageResp = $this->get('/register/invite/' . $tokenEntry->token);
$setPasswordPageResp->assertRedirect('/password/email');
$setPasswordPageResp->assertSessionHas('error', 'This invitation link has expired. You can instead try to reset your account password.');
}
2021-06-26 17:23:15 +02:00
}