Skip to content

Commit 861a690

Browse files
committed
test: Update test cases
1 parent eee361f commit 861a690

File tree

6 files changed

+111
-4
lines changed

6 files changed

+111
-4
lines changed

src/api/controllers/healthController.test.ts renamed to __tests__/api/controllers/healthController.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import request from 'supertest';
22
import express from 'express';
3-
import healthRoutes from '../routes/healthRoutes'; // Adjust the path as necessary
3+
import healthRoutes from '../../../src/api/routes/healthRoutes'; // Adjust the path as necessary
44

55
// Create a new express app for testing
66
const app = express();
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import express from 'express';
2+
import request from 'supertest';
3+
4+
jest.mock('../../../src/api/controllers/authController', () => ({
5+
handleCallback: jest.fn((req, res) => res.status(200).json({ route: 'callback' })),
6+
handleLoginProcess: jest.fn((req, res) => res.status(200).json({ route: 'loginProcess' })),
7+
handleLogout: jest.fn((req, res) => res.status(200).json({ route: 'logout' })),
8+
handleGitHubLogin: jest.fn((req, res) => res.status(200).json({ route: 'githubLogin' })),
9+
}));
10+
11+
import authRoutes from '../../../src/api/routes/authRoutes';
12+
import {
13+
handleCallback,
14+
handleGitHubLogin,
15+
handleLoginProcess,
16+
handleLogout,
17+
} from '../../../src/api/controllers/authController';
18+
19+
const mockedHandleCallback = handleCallback as jest.MockedFunction<typeof handleCallback>;
20+
const mockedHandleLoginProcess = handleLoginProcess as jest.MockedFunction<typeof handleLoginProcess>;
21+
const mockedHandleLogout = handleLogout as jest.MockedFunction<typeof handleLogout>;
22+
const mockedHandleGitHubLogin = handleGitHubLogin as jest.MockedFunction<typeof handleGitHubLogin>;
23+
24+
describe('auth routes', () => {
25+
const API_PREFIX = `/api/${process.env.API_VERSION || 'v1'}`;
26+
27+
const app = express();
28+
app.use(`${API_PREFIX}/auth`, authRoutes);
29+
30+
beforeEach(() => {
31+
jest.clearAllMocks();
32+
});
33+
34+
it('routes GET /callback to handleCallback', async () => {
35+
const response = await request(app).get(`${API_PREFIX}/auth/callback`);
36+
37+
expect(response.status).toBe(200);
38+
expect(response.body).toEqual({ route: 'callback' });
39+
expect(mockedHandleCallback).toHaveBeenCalledTimes(1);
40+
});
41+
42+
it('routes GET /login/process to handleLoginProcess', async () => {
43+
const response = await request(app).get(`${API_PREFIX}/auth/login/process`);
44+
45+
expect(response.status).toBe(200);
46+
expect(response.body).toEqual({ route: 'loginProcess' });
47+
expect(mockedHandleLoginProcess).toHaveBeenCalledTimes(1);
48+
});
49+
50+
it('routes GET /logout to handleLogout', async () => {
51+
const response = await request(app).get(`${API_PREFIX}/auth/logout`);
52+
53+
expect(response.status).toBe(200);
54+
expect(response.body).toEqual({ route: 'logout' });
55+
expect(mockedHandleLogout).toHaveBeenCalledTimes(1);
56+
});
57+
58+
it('routes GET /github/login to handleGitHubLogin', async () => {
59+
const response = await request(app).get(`${API_PREFIX}/auth/github/login`);
60+
61+
expect(response.status).toBe(200);
62+
expect(response.body).toEqual({ route: 'githubLogin' });
63+
expect(mockedHandleGitHubLogin).toHaveBeenCalledTimes(1);
64+
});
65+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { checkIsAdminUser } from '../../../src/api/services/authorization';
2+
import { UserData } from '../../../src/api/interfaces/UserData';
3+
4+
describe('checkIsAdminUser', () => {
5+
const baseUser: UserData = {
6+
id: 'user-1',
7+
role: 'viewer',
8+
acceptedAuthorAgreement: true,
9+
name: 'Test User',
10+
email: 'test@example.com',
11+
handle: 'test-user',
12+
avatarUrl: 'https://example.com/avatar.png',
13+
status: 'active',
14+
location: 'Earth',
15+
profileUrl: 'https://example.com/profile',
16+
provider: 'github',
17+
providerUserId: '123',
18+
};
19+
20+
it('returns true for users with the ADMIN role regardless of case', () => {
21+
expect(checkIsAdminUser({ ...baseUser, role: 'admin' })).toBe(true);
22+
expect(checkIsAdminUser({ ...baseUser, role: 'ADMIN' })).toBe(true);
23+
});
24+
25+
it('returns false for non-admin roles', () => {
26+
expect(checkIsAdminUser(baseUser)).toBe(false);
27+
});
28+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getFileExtension } from '../../../src/api/utils/fileUtils';
2+
3+
describe('getFileExtension', () => {
4+
it('returns the lower-cased extension for filenames with a single dot', () => {
5+
expect(getFileExtension('image.PNG')).toBe('png');
6+
});
7+
8+
it('returns the extension after the last dot for filenames with multiple dots', () => {
9+
expect(getFileExtension('archive.tar.gz')).toBe('gz');
10+
});
11+
12+
it('returns an empty string when there is no extension', () => {
13+
expect(getFileExtension('README')).toBe('');
14+
});
15+
});

jest.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
4-
roots: ['<rootDir>/src'],
5-
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
4+
roots: ['<rootDir>/__tests__'],
65
transform: {
76
'^.+\\.(ts|tsx)$': 'ts-jest',
87
},

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"include": [
2222
"src/**/*",
2323
"types"
24-
],
24+
, "__tests__/api/controllers/healthController.test.ts" ],
2525
"exclude": [
2626
"node_modules"
2727
],

0 commit comments

Comments
 (0)