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

325-refactor-model_binding-in-get-student_photo-endpoint #334

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion routes/api/v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
Route::get('student/{student}/resume/additionaltraining', StudentAdditionalTrainingListController::class)->name('student.additionaltraining');
Route::get('student/{student}/resume/collaborations', StudentCollaborationDetailController::class)->name('student.collaborations');
Route::put('student/{student}/resume/collaborations', UpdateStudentCollaborationsController::class)->name('student.updateCollaborations');
Route::get('student/{student}/resume/photo', GetStudentImageController::class)->middleware('auth:api', EnsureStudentOwner::class)->name('student.photo.get');
Route::put('student/{student}/resume/photo', UpdateStudentImageController::class)->name('student.updatePhoto');

Route::prefix('student/{studentId}/resume')->group(function () {
Route::put('languages', UpdateStudentLanguagesController::class)->name('student.languages.update');
Route::get('modality', StudentModalityController::class)->name('student.modality');
Route::get('photo', GetStudentImageController::class)->middleware('auth:api', EnsureStudentOwner::class)->name('student.photo.get');
Route::delete('languages/{languageId}', DeleteStudentResumeLanguageController::class)->name('student.language.delete');
});

Expand Down
68 changes: 33 additions & 35 deletions tests/Feature/Controller/Student/GetStudentImageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,89 +9,87 @@
};
use App\Service\Student\GetStudentImageService;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Mockery\MockInterface;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class GetStudentImageControllerTest extends TestCase
{
use DatabaseTransactions;
private const PHOTOS_PATH = 'public/photos/';
protected User $user;
protected Student $student;

protected function setUp(): void
{
parent::setUp();
}

private function createUser()
{
return User::factory()->create([
$this->user = User::factory()->create([
'dni'=>'27827083G', 'password'=>'Password%123'
]);

$this->student =Student::factory()->for($this->user)->create();

}

public function testCanInstantiateAnUser(): void
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the fixtures tests (only check the instance of what you are testing, if this has sense)

{
$this->assertInstanceOf(User::class, $this->user);
}

private function createStudent(User $user):Student
public function testCanInstantiateAStudent(): void
{
return Student::factory()->for($user)->create();
$this->assertInstanceOf(Student::class, $this->student);
}

private function getUserToken(User $user):string
{ //hay que pasar lo datos manualmente porque el password es encriptado
private function getUserToken():string
{
$singInUserData = ['dni'=>'27827083G', 'password'=>'Password%123'];
$url = route('signin');
$response = $this->json('POST', $url, $singInUserData);
$token = $response->json('token');
return $token;
}

public function test_get_student_image():void
public function testCanGetStudentImage():void
{
$user = $this->createUser();
$student = $this->createStudent($user);
$token = $this->getUserToken($user);
$studentId = $student->id;
$student = $this->student;
$token = $this->getUserToken();

$url = route('student.photo.get', $studentId);
$url = route('student.photo.get', $student->id);
$response = $this->withHeaders([
'Authorization' => 'Bearer ' . $token
])->get($url);

$response->assertStatus(200);
}
$response->assertJson(['photo' => Storage::url(self::PHOTOS_PATH . $student->photo)]);}

public function test_get_student_image_not_found():void
public function testCanGetAnEmptyArrayWhenTheStudentHasNoPhoto():void
{
$user = $this->createUser();
$student = $this->createStudent($user);
$student = $this->student;
$student->photo = null;
$student->save();
$token = $this->getUserToken($user);
$studentId = $student->id;
$token = $this->getUserToken();

$url = route('student.photo.get', $studentId);
$url = route('student.photo.get', $student->id);
$response = $this->withHeaders([
'Authorization' => 'Bearer ' . $token
])->get($url);

$response->assertStatus(200);
$response->assertJson([]);
$response->assertJson(['photo' => '']);
}

public function test_can_return_a_500_on_internal_server_error(): void
public function testCanReturns_404WithInvalidStudentUuid(): void
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beware the underscore

{
$this->mock(GetStudentImageService::class, function (MockInterface $mock) {
$mock->shouldReceive('execute')
->andThrow(new \Exception('Internal Server Error'));
});
$user = $this->createUser();
$student = $this->createStudent($user);
$token = $this->getUserToken($user);
$studentId = $student->id;

$url = route('student.photo.get', $studentId);
$invalidUuid = 'invalidUuid';
$token = $this->getUserToken();

$url = route('student.photo.get', $invalidUuid);
$response = $this->withHeaders([
'Authorization' => 'Bearer ' . $token
])->get($url);

$response->assertStatus(500);
$response->assertStatus(404);
}

}