Skip to content
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

[BE] Small details on LoginControllerTest and AdminController #41

Merged
merged 6 commits into from
Dec 25, 2023
Merged
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
69 changes: 51 additions & 18 deletions app/Http/Controllers/api/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ public function index()
$admins = Admin::all();

if ($admins->isEmpty()) {
throw new HttpResponseException(response()->json(['message' => __('No hi ha administradors a la base de dades')], 404));
throw new HttpResponseException(response()->json(
['message' => __('No hi ha administradors a la base de dades')],
404
));
}

return response()->json(['data' => AdminIndexResource::collection($admins)], 200);
return response()->json(
['data' => AdminIndexResource::collection($admins)],
200
);
}

public function store(UserRequest $request)
Expand All @@ -44,10 +50,16 @@ public function store(UserRequest $request)
return $user;
});
if (!$transaction) {
throw new HttpResponseException(response()->json(['message' => __('Registre no efectuat. Si-us-plau, torna-ho a provar.')], 404));
throw new HttpResponseException(response()->json(
['message' => __('Registre no efectuat. Si-us-plau, torna-ho a provar.')],
404
));
}

return response()->json(['message' => __('Registre realitzat amb èxit.')], 201);
return response()->json(
['message' => __('Registre realitzat amb èxit.')],
201
);
}

public function show($id)
Expand All @@ -56,20 +68,27 @@ public function show($id)
$admin = $this->findAdmin($id);

if (!$admin) {
throw new HttpResponseException(response()->json(['message' => __('No hi ha administradors a la base de dades')], 404));
throw new HttpResponseException(response()->json(
['message' => __('No hi ha administradors a la base de dades')],
404
));
}
if ($admin->id !== $loggeuser->admin->id) {
return response()->json(['message' => __('No tens permisos per veure aquest usuari.')], 401);
return response()->json(
['message' => __('No tens permisos per veure aquest usuari.')],
401
);
}

return response()->json(['data' => new AdminShowResource($admin)], 200);
return response()->json(
['data' => new AdminShowResource($admin)],
200
);
}

private function findAdmin($id)
{
$admin = Admin::findOrFail($id);

return $admin;
return Admin::findOrFail($id);
}

public function update(Request $request, $id)
Expand All @@ -78,26 +97,34 @@ public function update(Request $request, $id)

$admin = $this->findAdmin($id);
if ($admin->id !== $loggeuser->admin->id) {
throw new HttpResponseException(response()->json(['message' => __('No tens permís per modificar aquest usuari')], 401));
throw new HttpResponseException(response()->json(
['message' => __('No tens permís per modificar aquest usuari')],
401
));
}

if (!$admin) {
throw new HttpResponseException(response()->json(['message' => __('No hi ha administradors a la base de dades')], 404));
throw new HttpResponseException(response()->json(
['message' => __('No hi ha administradors a la base de dades')],
404
));
}

if ($request->surname) {
$admin->user->surname = $request->surname;

}if ($request->name) {
}
if ($request->name) {
$admin->user->name = $request->name;
}
if ($request->email) {
$admin->user->email = $request->email;
}
$admin->save();

return response()->json(['message' => 'Usuari actualitzat amb èxit', 'data' => new AdminshowResource($admin)], 200);

return response()->json(
['message' => 'Usuari actualitzat amb èxit', 'data' => new AdminshowResource($admin)],
200
);
}

public function destroy($id)
Expand All @@ -107,10 +134,16 @@ public function destroy($id)
$loggeuser = Auth::user();
$id = $this->findAdmin($id);
if ($admin->id !== $loggeuser->admin->id) {
throw new HttpResponseException(response()->json(['message' => __('No tens permís per eliminar aquest usuari')], 401));
throw new HttpResponseException(response()->json(
['message' => __('No tens permís per eliminar aquest usuari')],
401
));
}
$admin->user->delete();

return response()->json(['message' => __('La seva compte ha estat eliminada amb èxit')], 200);
return response()->json(
['message' => __('La seva compte ha estat eliminada amb èxit')],
200
);
}
}
2 changes: 1 addition & 1 deletion app/Http/Requests/LoginRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace App\Http\Requests;

use App\Rules\DniRule;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use App\Rules\DniRule;

class LoginRequest extends FormRequest
{
Expand Down
33 changes: 14 additions & 19 deletions tests/Feature/LoginControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Tests\Feature;

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

class LoginControllerTest extends TestCase
{
use DatabaseTransactions;

public function setUp(): void
{
parent::setUp();
Expand All @@ -16,46 +19,40 @@ public function setUp(): void

public function test_Login_Success()
{
$user = User::factory()->create([
User::factory()->create([
'dni' => '28386914S',
'password' => bcrypt('password123'),
]);

$requestData = [
$credentials = [
'dni' => '28386914S',
'password' => 'password123',
];

$response = $this->json('POST', '/api/v1/login', $requestData);
$response = $this->post(route('login'), $credentials);

$response->assertStatus(200)->assertJsonStructure(['token']);

}

public function test_a_user_can_login_with_short_password()
public function test_a_user_cannot_login_with_short_password()
{
$user = User::factory()->create([
User::factory()->create([
'dni' => 'Z0038540C',
'password' => '12345678',
]);
$credentials = [
'dni' => '28386914S',
'dni' => 'Z0038540C',
'password' => '123456',
];

$response = $this->json('POST', '/api/v1/login', $credentials);
$response = $this->post(route('login'), $credentials);

$response->assertStatus(422);

}

public function test_a_user_can_login_with_not_dni()
public function test_a_user_cannot_login_without_dni_neither_passport()
{
$user = User::factory()->create();
$credentials = [
'dni',
'password' => '12345678',
];
User::factory()->create();

$response = $this->json('POST', '/api/v1/login');
$response->assertStatus(422)
Expand All @@ -66,12 +63,11 @@ public function test_a_user_can_login_with_not_dni()
'password' => [__('El camp contrasenya és obligatori.')],
],
]);

}

public function test_login_failure_bad_data()
{
$response = $this->post('/api/v1/login', [
$response = $this->post(route('login'), [
'dni' => 'Z0038540C',
'password' => 'wrongpassword',
]);
Expand All @@ -80,7 +76,6 @@ public function test_login_failure_bad_data()
$response->assertJson([
'message' => __('Dni-Nie o contrasenya incorrecte'),
]);

}

public function test_can_logout()
Expand All @@ -100,7 +95,7 @@ public function test_can_logout()
public function test_logout_no_auth()
{

$user = User::factory()->create();
User::factory()->create();

$response = $this->postJson('/api/v1/logout');

Expand Down
2 changes: 0 additions & 2 deletions tests/Feature/Student/StudentDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,5 @@ public function a_student_cannot_get_unregistered_by_another_student(): void

$response->status(401);



}
}
3 changes: 1 addition & 2 deletions tests/Feature/Student/StudentListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function setUp(): void

$this->artisan('passport:install');
}

public function verifyOrCreateRole()
{
if (!Role::where('name', 'student')->exists()) {
Expand Down Expand Up @@ -47,6 +48,4 @@ public function a_list_of_registered_students_can_be_retrieved(): void
$response->assertHeader('Content-Type', 'application/json');

}


}
4 changes: 0 additions & 4 deletions tests/Feature/Student/StudentUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function setUp(): void
$this->artisan('passport:install');
}


public function verifyOrCreateRolesAndPermissions()
{
if (!Role::where('name', 'student')->exists()) {
Expand Down Expand Up @@ -57,8 +56,6 @@ public function student_data_can_be_updated_by_himself(): void

$this->assertAuthenticatedAs($user);



$data = [
'name' => 'Johnny',
'surname' => 'Doe',
Expand Down Expand Up @@ -109,7 +106,6 @@ public function student_data_cannot_be_updated_by_another_student(): void
'password' => $password,
]);


$this->actingAs($user, 'api');

$data = [
Expand Down