diff --git a/app/Http/Controllers/api/AdminController.php b/app/Http/Controllers/api/AdminController.php index ce02f943..b2da727b 100644 --- a/app/Http/Controllers/api/AdminController.php +++ b/app/Http/Controllers/api/AdminController.php @@ -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) @@ -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) @@ -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) @@ -78,17 +97,23 @@ 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) { @@ -96,8 +121,10 @@ public function update(Request $request, $id) } $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) @@ -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 + ); } } diff --git a/app/Http/Requests/LoginRequest.php b/app/Http/Requests/LoginRequest.php index a9279871..56d29705 100644 --- a/app/Http/Requests/LoginRequest.php +++ b/app/Http/Requests/LoginRequest.php @@ -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 { diff --git a/tests/Feature/LoginControllerTest.php b/tests/Feature/LoginControllerTest.php index aa151802..1f29ac0a 100644 --- a/tests/Feature/LoginControllerTest.php +++ b/tests/Feature/LoginControllerTest.php @@ -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(); @@ -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) @@ -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', ]); @@ -80,7 +76,6 @@ public function test_login_failure_bad_data() $response->assertJson([ 'message' => __('Dni-Nie o contrasenya incorrecte'), ]); - } public function test_can_logout() @@ -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'); diff --git a/tests/Feature/Student/StudentDeleteTest.php b/tests/Feature/Student/StudentDeleteTest.php index 56b655e3..ae2e4ee1 100644 --- a/tests/Feature/Student/StudentDeleteTest.php +++ b/tests/Feature/Student/StudentDeleteTest.php @@ -101,7 +101,5 @@ public function a_student_cannot_get_unregistered_by_another_student(): void $response->status(401); - - } } diff --git a/tests/Feature/Student/StudentListTest.php b/tests/Feature/Student/StudentListTest.php index 80837b34..30476bef 100644 --- a/tests/Feature/Student/StudentListTest.php +++ b/tests/Feature/Student/StudentListTest.php @@ -14,6 +14,7 @@ public function setUp(): void $this->artisan('passport:install'); } + public function verifyOrCreateRole() { if (!Role::where('name', 'student')->exists()) { @@ -47,6 +48,4 @@ public function a_list_of_registered_students_can_be_retrieved(): void $response->assertHeader('Content-Type', 'application/json'); } - - } diff --git a/tests/Feature/Student/StudentUpdateTest.php b/tests/Feature/Student/StudentUpdateTest.php index f7cb9697..adc9864f 100644 --- a/tests/Feature/Student/StudentUpdateTest.php +++ b/tests/Feature/Student/StudentUpdateTest.php @@ -17,7 +17,6 @@ public function setUp(): void $this->artisan('passport:install'); } - public function verifyOrCreateRolesAndPermissions() { if (!Role::where('name', 'student')->exists()) { @@ -57,8 +56,6 @@ public function student_data_can_be_updated_by_himself(): void $this->assertAuthenticatedAs($user); - - $data = [ 'name' => 'Johnny', 'surname' => 'Doe', @@ -109,7 +106,6 @@ public function student_data_cannot_be_updated_by_another_student(): void 'password' => $password, ]); - $this->actingAs($user, 'api'); $data = [