From 38b5a53b458844378afbcb2821693564ff916863 Mon Sep 17 00:00:00 2001 From: Reginaldo Date: Wed, 24 Jul 2024 10:03:50 -0300 Subject: [PATCH 1/8] refactor: change code for improved readability and maintainability --- tests/Feature/UserControllerTest.php | 181 +++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 tests/Feature/UserControllerTest.php diff --git a/tests/Feature/UserControllerTest.php b/tests/Feature/UserControllerTest.php new file mode 100644 index 0000000..593ebab --- /dev/null +++ b/tests/Feature/UserControllerTest.php @@ -0,0 +1,181 @@ +create(); + $this->actingAs($user, 'api'); + return $user; + } + + public function it_can_create_a_user() + { + $this->authenticatedUser(); + $userData = User::factory()->make()->toArray(); + + $response = $this->postJson('/api/users', $userData); + + $response->assertStatus(201) + ->assertJson([ + 'name' => $userData['name'], + 'email' => $userData['email'], + 'password' => $userData['password']; + ]); + + $this->assertDatabaseHas('users', ['email' => $userData['email']]); + } + + public function it_can_update_a_user() + { + $this->authenticatedUser(); + $user = User::factory()->create(); + + $updateData = [ + 'name' => 'Test name', + 'email' => 'test@hotmail.com', + 'password' => '123456a' + ]; + + $response = $this->putJson("/api/users/{$user->id}", $updateData); + + $response->assertStatus(200) + ->assertJson([ + 'name' => 'Test name', + 'email' => 'test@hotmail.com', + 'password' => '123456a' + ]); + + $this->assertDatabaseHas('users', [ + 'id' => $user->id, + 'name' => 'Test name', + 'email' => 'test@hotmail.com', + 'password' => '123456a' + ]); + } + + public function it_can_list_all_users() + { + $this->authenticatedUser(); + User::factory()->count(5)->create(); + + $response = $this->getJson('/api/users'); + + $response->assertStatus(200) + ->assertJsonCount(5); + } + + public function it_can_view_a_single_user() + { + $this->authenticatedUser(); + $user = User::factory()->create(); + + $response = $this->getJson("/api/users{$user->id}"); + + $response->assertStatus(200) + ->assertJson([ + 'id' => $user->id, + 'name' => $user->name, + 'email' => $user->email + ]); + } + + public function it_can_delete_a_user() + { + $this->authenticatedUser(); + $user = User::factory()->create(); + + $response = $this->deleteJson("/api/users/{$user->id}"); + + $response->assertStatus(200); + + $this->assertDatabaseMissing('users', ['id' => $user->id]); + } + + public function it_cannot_list_all_users_when_unauthenticated() + { + User::factory()->count(5)->create(); + + $response = $this->getJson('/api/users'); + + $response->assertStatus(401); + + } + + public function it_cannot_view_a_single_user_when_unauthenticated() + { + $user = User::factory()->create(); + + $response = $this->getJson("/api/users/{$user->id}"); + + $response->assertStatus(401); + } + + public function it_cannot_create_a_user_when_unauthenticated() + { + $userData = User::factory()->make()->toArray(); + + $response = $this->postJson('/api/users', $userData); + + $response->assertStatus(401); + } + + public function it_cannot_update_a_user_when_unauthenticated() + { + $user = User::factory()->create(); + + $updateData = [ + 'name' => 'Test Name', + 'email' => 'test@hotmail.com', + ]; + + $response = $this->putJson("/api/users/{$user->id}", $updateData); + + $response->assertStatus(401); + } + + public function it_cannot_delete_a_user_when_unauthenticated() + { + $user = User::factory()->create(); + + $response = $this->deleteJson("/api/users/{$user->id}"); + + $response->assertStatus(401); + } + + public function it_cannot_update_a_user_when_forbidden() + { + $user = User::factory()->create(); + $this->actingAs($user, 'api'); + $anotherUser = User::factory()->create(); + + $updateData = [ + 'name' => 'Test Name 2', + 'email' => 'test2@hotmail.com', + ]; + + $response = $this->putJson("/api/users/{$anotherUser->id}", $updateData); + + $response->assertStatus(403); + } + + public function it_cannot_delete_a_user_when_forbidden() + { + $user = User::factory()->create(); + $this->actingAs($user, 'api'); + $anotherUser = User::factory()->create(); + + $response = $this->deleteJson("/api/users/{$anotherUser->id}"); + + $response->assertStatus(403); + } + + +} \ No newline at end of file From c246b355353376b87ccb21ddd5c8b2a7c2037a03 Mon Sep 17 00:00:00 2001 From: Reginaldo Date: Wed, 24 Jul 2024 10:17:33 -0300 Subject: [PATCH 2/8] add file ANSWER.md --- ANSWER.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 ANSWER.md diff --git a/ANSWER.md b/ANSWER.md new file mode 100644 index 0000000..ce25135 --- /dev/null +++ b/ANSWER.md @@ -0,0 +1,11 @@ +# Eleven Soft Backend Refactoring Test + +# Implementação de Testes Unitários para a API com projeto base Laravel no endpoint de usuários + +A implementação de métodos de teste unitário para o controlador de usuários (`UserController`) em uma aplicação Laravel. Os testes cobrem tanto cenários de sucesso quanto de falha, verificando os status codes apropriados para cada situação. (`UserControllerTest`) + +`make install` + +`make up` + +`make api-test-feature` From 1086f3fd75c84aa7f630eff8431111ed38b03d02 Mon Sep 17 00:00:00 2001 From: Reginaldo Date: Wed, 24 Jul 2024 15:27:47 -0300 Subject: [PATCH 3/8] add config driver auth passport --- config/auth.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config/auth.php b/config/auth.php index 9548c15..f2db043 100644 --- a/config/auth.php +++ b/config/auth.php @@ -40,6 +40,11 @@ 'driver' => 'session', 'provider' => 'users', ], + + 'api' => [ + 'driver' => 'passport', + 'provider' => 'users', + ], ], /* From 118bb02b26acd9d87f6d086294c52cebb1c51c62 Mon Sep 17 00:00:00 2001 From: Reginaldo Date: Wed, 24 Jul 2024 15:28:51 -0300 Subject: [PATCH 4/8] refactor: change code for improved readability and maintainability --- tests/Feature/UserControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/UserControllerTest.php b/tests/Feature/UserControllerTest.php index 593ebab..a7e8b38 100644 --- a/tests/Feature/UserControllerTest.php +++ b/tests/Feature/UserControllerTest.php @@ -12,7 +12,7 @@ class UserControllerTest extends TestCase protected function authenticatedUser() { $user = User::factory()->create(); - $this->actingAs($user, 'api'); + Passport::actingAs($user); return $user; } From 39566d69c9c98988bc1b4a8899509c4103834955 Mon Sep 17 00:00:00 2001 From: Reginaldo Date: Wed, 24 Jul 2024 15:29:19 -0300 Subject: [PATCH 5/8] update file ANSWER.md --- ANSWER.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/ANSWER.md b/ANSWER.md index ce25135..7ff7175 100644 --- a/ANSWER.md +++ b/ANSWER.md @@ -4,8 +4,35 @@ A implementação de métodos de teste unitário para o controlador de usuários (`UserController`) em uma aplicação Laravel. Os testes cobrem tanto cenários de sucesso quanto de falha, verificando os status codes apropriados para cada situação. (`UserControllerTest`) +1. Instalae e configura o projeto + `make install` +2. Inicializa os containers + `make up` +3. Renomeia o arquivo .env.example para .env + +`make api-env` + +4. Gera a chave do Laravel e adiciona no arquivo .env + +`make api-key` + +5. Executa os migrations e seeds + +`make api-db` + +6. Executa para criar o cliente OAuth de autenticação de usuários via Passport + +`make api-passport-generate` + +7. Executa para gerar a documentação Swagger + +`make api-build-swagger` + +8. Executa a execução dos testes unitário de Feature + `make api-test-feature` + From d2452797648256adb2bb3ecfc0a3d92ddc43620e Mon Sep 17 00:00:00 2001 From: Reginaldo Date: Wed, 24 Jul 2024 15:31:31 -0300 Subject: [PATCH 6/8] update file ANSWER.md --- ANSWER.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ANSWER.md b/ANSWER.md index 7ff7175..7026013 100644 --- a/ANSWER.md +++ b/ANSWER.md @@ -4,7 +4,7 @@ A implementação de métodos de teste unitário para o controlador de usuários (`UserController`) em uma aplicação Laravel. Os testes cobrem tanto cenários de sucesso quanto de falha, verificando os status codes apropriados para cada situação. (`UserControllerTest`) -1. Instalae e configura o projeto +1. Instala e configura o projeto `make install` From 5aab74d88377c454ac61561794067c12c9fd4256 Mon Sep 17 00:00:00 2001 From: Reginaldo Date: Wed, 24 Jul 2024 15:38:28 -0300 Subject: [PATCH 7/8] refactor: change code for improved readability and maintainability --- tests/Feature/UserControllerTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Feature/UserControllerTest.php b/tests/Feature/UserControllerTest.php index a7e8b38..78824ff 100644 --- a/tests/Feature/UserControllerTest.php +++ b/tests/Feature/UserControllerTest.php @@ -20,6 +20,7 @@ public function it_can_create_a_user() { $this->authenticatedUser(); $userData = User::factory()->make()->toArray(); + $userData['password'] = '123456a'; $response = $this->postJson('/api/users', $userData); @@ -27,10 +28,12 @@ public function it_can_create_a_user() ->assertJson([ 'name' => $userData['name'], 'email' => $userData['email'], - 'password' => $userData['password']; ]); $this->assertDatabaseHas('users', ['email' => $userData['email']]); + + $user = User::where('email', $userData['email'])->first(); + $this->assertTrue(\Hash::check('123456a', $user->password)); } public function it_can_update_a_user() @@ -41,7 +44,6 @@ public function it_can_update_a_user() $updateData = [ 'name' => 'Test name', 'email' => 'test@hotmail.com', - 'password' => '123456a' ]; $response = $this->putJson("/api/users/{$user->id}", $updateData); @@ -50,14 +52,12 @@ public function it_can_update_a_user() ->assertJson([ 'name' => 'Test name', 'email' => 'test@hotmail.com', - 'password' => '123456a' ]); $this->assertDatabaseHas('users', [ 'id' => $user->id, 'name' => 'Test name', 'email' => 'test@hotmail.com', - 'password' => '123456a' ]); } @@ -83,7 +83,7 @@ public function it_can_view_a_single_user() ->assertJson([ 'id' => $user->id, 'name' => $user->name, - 'email' => $user->email + 'email' => $user->email, ]); } From 47d8735834f3fcc98f780579f5370b0bc750e8f8 Mon Sep 17 00:00:00 2001 From: Reginaldo Date: Thu, 25 Jul 2024 15:09:51 -0300 Subject: [PATCH 8/8] refactor: change code for improved readability and maintainability --- tests/Feature/UserControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/UserControllerTest.php b/tests/Feature/UserControllerTest.php index 78824ff..4f3d59b 100644 --- a/tests/Feature/UserControllerTest.php +++ b/tests/Feature/UserControllerTest.php @@ -20,7 +20,7 @@ public function it_can_create_a_user() { $this->authenticatedUser(); $userData = User::factory()->make()->toArray(); - $userData['password'] = '123456a'; + $userData['password'] = \Hash::make("123456a", ['rounds' => 12]); $response = $this->postJson('/api/users', $userData);