Skip to content

Commit

Permalink
Test: add new auth router tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderleegs committed Aug 19, 2022
1 parent 86cdc31 commit eec8788
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/routes/v2/__tests__/Auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const request = require("supertest")
const { attachReadRouteHandlerWrapper } = require("@middleware/routeHandler")

const { generateRouter } = require("@fixtures/app")
const { mockUserSessionData } = require("@fixtures/sessionData")
const { mockUserSessionData, mockEmail } = require("@fixtures/sessionData")

const { CSRF_COOKIE_NAME, COOKIE_NAME, AuthRouter } = require("../auth")

Expand All @@ -17,6 +17,8 @@ describe("Unlinked Pages Router", () => {
getAuthRedirectDetails: jest.fn(),
getGithubAuthToken: jest.fn(),
getUserInfo: jest.fn(),
sendOtp: jest.fn(),
verifyOtp: jest.fn(),
}

const router = new AuthRouter({
Expand All @@ -31,6 +33,8 @@ describe("Unlinked Pages Router", () => {
attachReadRouteHandlerWrapper(router.authRedirect)
)
subrouter.get("/", attachReadRouteHandlerWrapper(router.githubAuth))
subrouter.post("/login", attachReadRouteHandlerWrapper(router.login))
subrouter.post("/verify", attachReadRouteHandlerWrapper(router.verify))
subrouter.delete("/logout", attachReadRouteHandlerWrapper(router.logout))
subrouter.get("/whoami", attachReadRouteHandlerWrapper(router.whoami))
const app = generateRouter(subrouter)
Expand Down Expand Up @@ -83,6 +87,25 @@ describe("Unlinked Pages Router", () => {
)
})
})
describe("login", () => {
it("calls the service to send otp", async () => {
await request(app).post(`/login`).send({ email: mockEmail }).expect(200)
expect(mockAuthService.sendOtp).toHaveBeenCalledWith(
mockEmail.toLowerCase()
)
})
})
describe("verify", () => {
const mockOtp = "123456"
it("adds the cookie on login", async () => {
mockAuthService.getAuthRedirectDetails.mockResolvedValueOnce(cookieToken)
await request(app)
.post(`/verify`)
.send({ email: mockEmail, otp: mockOtp })
.set("Cookie", `${COOKIE_NAME}=${cookieToken}`)
.expect(200)
})
})
describe("logout", () => {
it("removes cookies on logout", async () => {
const resp = await request(app)
Expand Down

0 comments on commit eec8788

Please sign in to comment.