Skip to content

Feature/refactoring changes #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions ANSWER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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`)

1. Instala 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`

5 changes: 5 additions & 0 deletions config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
'driver' => 'session',
'provider' => 'users',
],

'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],

/*
Expand Down
181 changes: 181 additions & 0 deletions tests/Feature/UserControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php
namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UserControllerTest extends TestCase
{
use RefreshDatabase;

protected function authenticatedUser()
{
$user = User::factory()->create();
Passport::actingAs($user);
return $user;
}

public function it_can_create_a_user()
{
$this->authenticatedUser();
$userData = User::factory()->make()->toArray();
$userData['password'] = \Hash::make("123456a", ['rounds' => 12]);

$response = $this->postJson('/api/users', $userData);

$response->assertStatus(201)
->assertJson([
'name' => $userData['name'],
'email' => $userData['email'],
]);

$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()
{
$this->authenticatedUser();
$user = User::factory()->create();

$updateData = [
'name' => 'Test name',
'email' => '[email protected]',
];

$response = $this->putJson("/api/users/{$user->id}", $updateData);

$response->assertStatus(200)
->assertJson([
'name' => 'Test name',
'email' => '[email protected]',
]);

$this->assertDatabaseHas('users', [
'id' => $user->id,
'name' => 'Test name',
'email' => '[email protected]',
]);
}

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' => '[email protected]',
];

$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' => '[email protected]',
];

$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);
}


}