Files
ticcat/tests/Feature/AuthTest.php
2024-02-10 14:59:46 +01:00

228 lines
6.4 KiB
PHP

<?php
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
test('an user can login', function () {
User::factory()->create([
'name' => 'test',
'email' => 'test@mail.fr',
'password' => Hash::make('password-test'),
]);
$this->post('/api/login', [
'email' => 'test@mail.fr',
'password' => 'password-test',
])
->assertOk();
});
test('invalid credential return an error', function () {
$this->post('/api/login', [
'email' => 'test@mail.fr',
'password' => 'password-test',
])
->assertStatus(422)
->assertJson([
'message' => 'Invalid credentials.',
'errors' => ['form_info' => 'Invalid credentials.'],
]);
});
test('an user can logout', function () {
User::factory()->create([
'name' => 'test',
'email' => 'test@mail.fr',
'password' => Hash::make('password-test'),
]);
$this->post('/api/login', [
'email' => 'test@mail.fr',
'password' => 'password-test',
])
->assertOk();
$this->delete('/api/logout', [
'email' => 'test@mail.fr',
'password' => 'password-test',
])
->assertOk()
->assertJson(['message' => 'logout']);
});
test('a connected user can retrieve is data', function () {
$user = User::factory()->create([
'name' => 'test',
'email' => 'test@mail.fr',
'password' => Hash::make('password-test'),
]);
$this->post('/api/login', [
'email' => 'test@mail.fr',
'password' => 'password-test',
])
->assertOk();
$this->get('/api/user')
->assertOk()
->assertJson([
'id' => $user->id,
'name' => 'test',
'email' => 'test@mail.fr',
]);
});
test('a new user can register', function () {
$this->post('/api/register', [
'name' => 'test',
'email' => 'test@mail.fr',
'password' => 'password-test',
])
->assertOk()
->assertJson([
'id' => User::first()->id,
'name' => 'test',
'email' => 'test@mail.fr',
]);
expect(User::get())->toHaveCount(1)
->first()->name->toBe('test')
->first()->email->toBe('test@mail.fr');
});
test('user registration has required fields', function () {
$this->postJson('/api/register', [
])
->assertStatus(422)
->assertJson([
'message' => 'The name field is required. (and 2 more errors)',
'errors' => [
'name' => ['The name field is required.'],
'email' => ['The email field is required.'],
'password' => ['The password field is required.'],
],
]);
});
test('user registration has minimum values', function () {
$this->postJson('/api/register', [
'name' => 'te',
'email' => 'test@mail.fr',
'password' => 'password-test',
])
->assertStatus(422)
->assertJson([
'message' => 'The name field must be at least 3 characters.',
'errors' => ['name' => ['The name field must be at least 3 characters.']],
]);
$this->postJson('/api/register', [
'name' => 'test',
'email' => 'test@mail.fr',
'password' => 'passwor',
])
->assertStatus(422)
->assertJson([
'message' => 'The password field must be at least 8 characters.',
'errors' => ['password' => ['The password field must be at least 8 characters.']],
]);
User::factory()->create([
'name' => 'test',
'email' => 'test@mail.fr',
'password' => Hash::make('password-test'),
]);
$this->postJson('/api/register', [
'name' => 'test',
'email' => 'test@mail.fr',
'password' => 'password-test',
])
->assertStatus(422)
->assertJson([
'message' => 'The email has already been taken.',
'errors' => ['email' => ['The email has already been taken.']],
]);
});
test('an user can reset his password', function () {
$user = User::factory()->create([
'name' => 'test',
'email' => 'test@mail.fr',
'password' => Hash::make('password-test'),
]);
$this->postJson('/api/forgot', [
'email' => $user->email,
])
->assertOk()
->assertJson(['message' => 'Check your email']);
expect(DB::table('password_reset_tokens')->get())
->toHaveCount(1)
->first()->email->toBe($user->email);
$this->postJson('/api/forgot', [
'email' => 'not-email@existing',
])
->assertOk()
->assertJson(['message' => 'Check your email']);
$token = DB::table('password_reset_tokens')->where('email', $user->email)->first();
$this->postJson('/api/reset', [
'email' => $user->email,
'token' => $token->token,
'password' => 'Password-test12',
'confirm_password' => 'Password-test12',
])
->assertOk()
->assertJson(['message' => 'New password success']);
expect(DB::table('password_reset_tokens')->get())
->toHaveCount(0);
});
test('reset action need specific credentials', function () {
$user = User::factory()->create([
'name' => 'test',
'email' => 'test@mail.fr',
'password' => Hash::make('password-test'),
]);
$this->postJson('/api/forgot', ['email' => $user->email]);
$this->postJson('/api/reset', [
'email' => $user->email,
'token' => 'xoxo',
'password' => 'Password-test12',
'confirm_password' => 'Password-test12',
])
->assertStatus(422)
->assertJson([
'message' => 'The selected token is invalid.',
'errors' => ['token' => ['The selected token is invalid.']],
]);
DB::table('password_reset_tokens')
->where('email', $user->email)
->update(['created_at' => now()->subWeek()]);
$token = DB::table('password_reset_tokens')
->where('email', $user->email)
->first();
$this->postJson('/api/reset', [
'email' => $user->email,
'token' => $token->token,
'password' => 'Password-test12',
'confirm_password' => 'Password-test12',
])
->assertStatus(422)
->assertJson([
'message' => 'Invalid credentials.',
'errors' => ['form_info' => 'Invalid credentials.'],
]);
});