first commit
This commit is contained in:
227
tests/Feature/AuthTest.php
Normal file
227
tests/Feature/AuthTest.php
Normal file
@@ -0,0 +1,227 @@
|
||||
<?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.'],
|
||||
]);
|
||||
});
|
||||
10
tests/Feature/ProjectTest.php
Normal file
10
tests/Feature/ProjectTest.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
test('an user can add project', function () {
|
||||
$user = \Laravel\Sanctum\Sanctum::actingAs(\App\Models\User::factory()->create());
|
||||
|
||||
$response = $this->post('api/projects', [
|
||||
'name' => 'Test',
|
||||
'description' => 'Test description',
|
||||
]);
|
||||
})->todo();
|
||||
16
tests/Feature/TimeTrackerTest.php
Normal file
16
tests/Feature/TimeTrackerTest.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
test('an user can start a time tracker', function () {
|
||||
Sanctum::actingAs($user = User::factory()->create());
|
||||
|
||||
$this->postJson('/api/time-tracker', [])
|
||||
->assertCreated()
|
||||
->assertJson([
|
||||
'id' => $user->timeTrackers()->value('id'),
|
||||
'user_id' => $user->id,
|
||||
'start_at' => now()->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
});
|
||||
108
tests/Feature/ToDoTest.php
Normal file
108
tests/Feature/ToDoTest.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
use App\Models\ToDo;
|
||||
use App\Models\User;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->user = User::factory()->create();
|
||||
});
|
||||
|
||||
test('an user can add todo', function () {
|
||||
Sanctum::actingAs($this->user);
|
||||
|
||||
$response = $this->post('api/todos', [
|
||||
'name' => 'Test',
|
||||
]);
|
||||
|
||||
$response->assertStatus(201)
|
||||
->assertJson([
|
||||
'user_id' => $this->user->id,
|
||||
'name' => 'Test',
|
||||
'checked' => false,
|
||||
]);
|
||||
|
||||
expect($this->user->toDos)
|
||||
->toHaveCount(1)
|
||||
->first()->name->toBe('Test')
|
||||
->first()->checked->toBeNull();
|
||||
});
|
||||
|
||||
test('an user can retrieve his to dos', function () {
|
||||
Sanctum::actingAs($this->user);
|
||||
ToDo::factory()->count(10)->create([
|
||||
'user_id' => $this->user->id,
|
||||
'checked' => false,
|
||||
]);
|
||||
|
||||
$response = $this->get('api/todos');
|
||||
|
||||
$toDos = $this->user->toDos->map(fn ($toDo) => [
|
||||
'id' => $toDo->id,
|
||||
'user_id' => $toDo->user_id,
|
||||
'name' => $toDo->name,
|
||||
'checked' => false,
|
||||
])->toArray();
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson($toDos);
|
||||
});
|
||||
|
||||
test('an user can retrieve a to do', function () {
|
||||
Sanctum::actingAs($this->user);
|
||||
$toDos = ToDo::factory()->count(10)->create([
|
||||
'user_id' => $this->user->id,
|
||||
'checked' => false,
|
||||
]);
|
||||
|
||||
$toDo = $toDos[rand(0, 9)];
|
||||
|
||||
$response = $this->get('api/todos/'.$toDo->id);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
'id' => $toDo->id,
|
||||
'user_id' => $toDo->user_id,
|
||||
'name' => $toDo->name,
|
||||
'checked' => false,
|
||||
]);
|
||||
});
|
||||
|
||||
test('an user can update a to do', function () {
|
||||
Sanctum::actingAs($this->user);
|
||||
$toDos = ToDo::factory()->count(10)->create([
|
||||
'user_id' => $this->user->id,
|
||||
'checked' => false,
|
||||
]);
|
||||
|
||||
$toDo = $toDos[rand(0, 9)];
|
||||
|
||||
$response = $this->put('api/todos/'.$toDo->id, ['name' => 'update test']);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
'id' => $toDo->id,
|
||||
'user_id' => $toDo->user_id,
|
||||
'name' => 'update test',
|
||||
'checked' => false,
|
||||
]);
|
||||
|
||||
expect(ToDo::find($toDo->id))
|
||||
->name->toBe('update test');
|
||||
});
|
||||
|
||||
test('an user can delete a to do', function () {
|
||||
Sanctum::actingAs($this->user);
|
||||
$toDos = ToDo::factory()->count(10)->create([
|
||||
'user_id' => $this->user->id,
|
||||
'checked' => false,
|
||||
]);
|
||||
|
||||
$toDo = $toDos[rand(0, 9)];
|
||||
|
||||
$response = $this->delete('api/todos/'.$toDo->id);
|
||||
|
||||
$response->assertNoContent();
|
||||
|
||||
expect(ToDo::all())->toHaveCount(9);
|
||||
});
|
||||
Reference in New Issue
Block a user