Skip to content

Commit

Permalink
feat: Refactor Get Institution Presenter
Browse files Browse the repository at this point in the history
Refactor the Get Institution Presenter to improve code readability and remove unnecessary imports and conditions.
  • Loading branch information
FelipeCarillo committed May 9, 2024
1 parent 821f23f commit 83bb41c
Showing 1 changed file with 25 additions and 44 deletions.
69 changes: 25 additions & 44 deletions test/modules/get_institution/app/get_instition.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});

0 comments on commit 83bb41c

Please sign in to comment.