-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor Get Institution Presenter
Refactor the Get Institution Presenter to improve code readability and remove unnecessary imports and conditions.
- Loading branch information
1 parent
821f23f
commit 83bb41c
Showing
1 changed file
with
25 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |