From 83bb41c877351e5767840b4565bd3c7b75ea97be Mon Sep 17 00:00:00 2001 From: FelipeCarillo Date: Thu, 9 May 2024 15:29:27 -0300 Subject: [PATCH] feat: Refactor Get Institution Presenter Refactor the Get Institution Presenter to improve code readability and remove unnecessary imports and conditions. --- .../get_institution/app/get_instition.test.ts | 69 +++++++------------ 1 file changed, 25 insertions(+), 44 deletions(-) diff --git a/test/modules/get_institution/app/get_instition.test.ts b/test/modules/get_institution/app/get_instition.test.ts index 35febbc..406d419 100644 --- a/test/modules/get_institution/app/get_instition.test.ts +++ b/test/modules/get_institution/app/get_instition.test.ts @@ -1,61 +1,42 @@ import { it, describe, expect } from 'vitest'; -import { GetInstitutionController } from '../../../../src/modules/get_institution/app/get_institution_controler'; -import { GetInstitutionUsecase } from '../../../../src/modules/get_institution/app/get_institution_usecase'; -import { Repository } from '../../../../src/core/repositories/Repository'; -import { HttpRequest } from '../../../../src/core/helpers/http/http_codes'; -import { InstitutionRepoMock } from '../../../../src/core/repositories/mocks/InstitutionRepoMock'; -import { UserRepoMock } from '../../../../src/core/repositories/mocks/UserRepoMock'; import { UserMock } from '../../../../src/core/structure/mocks/UserMock'; - import {TokenAuth} from '../../../../src/core/helpers/functions/token_auth'; +import { InstitutionMock } from '../../../../src/core/structure/mocks/InstitutionMock'; +import { handler } from '../../../../src/modules/get_institution/app/get_institution_presenter'; describe("Testing Get Institution Presenter", () => { - const mockUserRepo = new UserRepoMock(); - const mockInstitutionRepo = new InstitutionRepoMock(); - - - const usecase = new GetInstitutionUsecase( - mockInstitutionRepo, mockUserRepo - ); - const controller = new GetInstitutionController(usecase); - - const mockAdmin = new UserMock().users[0]; + const user_admin = new UserMock().users[0]; + const mockInstitution = new InstitutionMock(); it("should return institution data", async () => { - var token = (await new TokenAuth().generate_token(mockAdmin.id)).toString(); - const mockEvent = { + var token = (await new TokenAuth().generate_token(user_admin.id)).toString(); + + var response = await handler({ headers: { - Authorization: - token, + Authorization: token }, body: JSON.stringify({ - id: mockAdmin.id, - name: mockAdmin.name, - email: mockAdmin.email, - user_type: mockAdmin.user_type, - course: mockAdmin.course, - semester_course: mockAdmin.semester_course, - created_at: mockAdmin.created_at, - updated_at: mockAdmin.updated_at - }), - queryStringParameters: { - id: 1, - }, - }; + institution_id: mockInstitution.institutions[0].id + }) + }, null); - const request = new HttpRequest(mockEvent); + expect(response.statusCode).toBe(200); + expect(JSON.parse(response.body).message).toBe("Institution found"); + }); - // Execute the controller - const response = await controller.execute(request); + it("should not return institution with invalid token", async () => { + var response = await handler({ + headers: { + Authorization: "invalid_token" + }, + body: JSON.stringify({ + institution_id: mockInstitution.institutions[0].id + }) + }, null); - // Assert the response - expect(response.statusCode).toBe(200); - expect(response.body).toEqual({ - id: 1, - name: "Sample Institution", - // Add other expected properties - }); + expect(response.statusCode).toBe(401); + expect(JSON.parse(response.body).message).toBe("Invalid or expired token"); }); }); \ No newline at end of file