Skip to content

Commit e020539

Browse files
Add an endpoint for authentication verification
1 parent 0e866ce commit e020539

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/api/v1/auth/auth.router.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { RequestHandler, Router } from 'express';
88
import { AuthResponse } from '../../../types';
99
import logger from '../../../lib/logger';
1010
import passport from '../../../lib/passport';
11+
import { authValidator } from '../../../middlewares/auth-validator';
1112

1213
export const authRouter = Router();
1314

@@ -37,4 +38,8 @@ authRouter.post('/signin', async (req, res, next) => {
3738
)(req, res, next);
3839
});
3940

41+
authRouter.get('/verify', authValidator, (req, res) => {
42+
res.json(true);
43+
});
44+
4045
export default authRouter;

src/tests/api/v1/auth.int.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { it, expect, describe, afterAll, beforeAll } from 'vitest';
1+
import { it, expect, describe, afterAll, beforeAll, vi } from 'vitest';
22
import { SALT } from '../../../lib/config';
33
import {
44
AppErrorResponse,
@@ -16,6 +16,7 @@ import { User } from '../../../../prisma/generated/client';
1616
describe('Authentication endpoint', () => {
1717
const BASE_URL = '/api/v1/auth';
1818
const SIGNIN_URL = `${BASE_URL}/signin`;
19+
const VERIFY_URL = `${BASE_URL}/verify`;
1920

2021
const userData: NewDefaultUser = {
2122
fullname: 'Clark Kent/Kal-El',
@@ -94,6 +95,43 @@ describe('Authentication endpoint', () => {
9495
expect(resJwtPayload.id).toBeTypeOf('string');
9596
expect(resJwtPayload.username).toBe(userData.username);
9697
expect(resJwtPayload.fullname).toBe(userData.fullname);
98+
expect(resJwtPayload.password).toBeUndefined();
99+
expect(resJwtPayload.isAdmin).toBeUndefined();
100+
});
101+
});
102+
103+
describe(`GET ${VERIFY_URL}`, () => {
104+
it('should verify a valid, fresh token and respond with `true`', async () => {
105+
const signinResBody = (await api.post(SIGNIN_URL).send(signInData))
106+
.body as AuthResponse;
107+
const res = await api
108+
.get(VERIFY_URL)
109+
.set('Authorization', signinResBody.token);
110+
expect(res.type).toMatch(/json/);
111+
expect(res.statusCode).toBe(200);
112+
expect(res.body).toBe(true);
113+
});
114+
115+
it('should not verify an invalid token and respond 401', async () => {
116+
const signinResBody = (await api.post(SIGNIN_URL).send(signInData))
117+
.body as AuthResponse;
118+
const res = await api
119+
.get(VERIFY_URL)
120+
.set('Authorization', signinResBody.token.replace(/\../, '.x'));
121+
expect(res.statusCode).toBe(401);
122+
});
123+
124+
it('should not verify an expired token and respond 401', async () => {
125+
const signinResBody = (await api.post(SIGNIN_URL).send(signInData))
126+
.body as AuthResponse;
127+
vi.useFakeTimers();
128+
const now = new Date();
129+
const future = new Date(now.setFullYear(now.getFullYear() + 3));
130+
vi.setSystemTime(future);
131+
const res = await api
132+
.get(VERIFY_URL)
133+
.set('Authorization', signinResBody.token);
134+
expect(res.statusCode).toBe(401);
97135
});
98136
});
99137
});

0 commit comments

Comments
 (0)