Skip to content

Commit c6f8f3f

Browse files
committed
fix unit tests & add scripts
1 parent e64ce09 commit c6f8f3f

3 files changed

Lines changed: 69 additions & 60 deletions

File tree

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"description": "Backend system for HackRU.",
55
"main": "serverless.ts",
66
"scripts": {
7-
"test": "jest --watch"
7+
"test": "jest --watch",
8+
"tests": "npx jest tests/ --config=tests/jest.config.ts",
9+
"eslint": "npx eslint --fix .",
10+
"prettier": "npx prettier --write ."
811
},
912
"engines": {
1013
"node": ">=14.15.0"
@@ -59,4 +62,4 @@
5962
},
6063
"author": "HackRU",
6164
"license": "MIT"
62-
}
65+
}

tests/teams-read.test.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ const mockTeamsCollection = {
1212
};
1313

1414
jest.mock('../src/util', () => ({
15-
// eslint-disable-next-line @typescript-eslint/naming-convention
1615
MongoDB: {
1716
getInstance: jest.fn().mockReturnValue({
1817
connect: jest.fn(),
1918
disconnect: jest.fn(),
2019
getCollection: jest.fn().mockImplementation((name: string) => {
2120
if (name === 'users') return mockUsersCollection;
2221
if (name === 'teams') return mockTeamsCollection;
23-
return mockUsersCollection;
22+
return null;
2423
}),
2524
}),
2625
},
@@ -63,7 +62,7 @@ describe('/teams/read endpoint', () => {
6362

6463
it('should reject when auth user not found', async () => {
6564
validateTokenMock.mockReturnValue(true);
66-
usersFindOneMock.mockReturnValueOnce(null);
65+
usersFindOneMock.mockResolvedValueOnce(null);
6766

6867
const mockEvent = createEvent(
6968
{
@@ -84,7 +83,7 @@ describe('/teams/read endpoint', () => {
8483
it("should reject unauthorized access to another user's team", async () => {
8584
validateTokenMock.mockReturnValue(true);
8685

87-
usersFindOneMock.mockReturnValueOnce({
86+
usersFindOneMock.mockResolvedValueOnce({
8887
email: 'user@test.com',
8988
role: { hacker: true },
9089
team_info: { team_id: 'user-team-id' },
@@ -109,13 +108,13 @@ describe('/teams/read endpoint', () => {
109108
it('should reject when team not found', async () => {
110109
validateTokenMock.mockReturnValue(true);
111110

112-
usersFindOneMock.mockReturnValueOnce({
111+
usersFindOneMock.mockResolvedValueOnce({
113112
email: 'user@test.com',
114113
role: { hacker: true },
115114
team_info: { team_id: 'test-team-id' },
116115
});
117116

118-
teamsFindOneMock.mockReturnValueOnce(null);
117+
teamsFindOneMock.mockResolvedValueOnce(null);
119118

120119
const mockEvent = createEvent(
121120
{
@@ -136,13 +135,13 @@ describe('/teams/read endpoint', () => {
136135
it('should reject when team is not active', async () => {
137136
validateTokenMock.mockReturnValue(true);
138137

139-
usersFindOneMock.mockReturnValueOnce({
138+
usersFindOneMock.mockResolvedValueOnce({
140139
email: 'user@test.com',
141140
role: { hacker: true },
142141
team_info: { team_id: 'test-team-id' },
143142
});
144143

145-
teamsFindOneMock.mockReturnValueOnce({
144+
teamsFindOneMock.mockResolvedValueOnce({
146145
team_id: 'test-team-id',
147146
status: 'Inactive',
148147
});
@@ -166,13 +165,13 @@ describe('/teams/read endpoint', () => {
166165
it('should return team details successfully', async () => {
167166
validateTokenMock.mockReturnValue(true);
168167

169-
usersFindOneMock.mockReturnValueOnce({
168+
usersFindOneMock.mockResolvedValueOnce({
170169
email: 'user@test.com',
171170
role: { hacker: true },
172171
team_info: { team_id: 'test-team-id' },
173172
});
174173

175-
teamsFindOneMock.mockReturnValueOnce({
174+
teamsFindOneMock.mockResolvedValueOnce({
176175
team_id: 'test-team-id',
177176
status: 'Active',
178177
members: ['user@test.com', 'member2@test.com'],
@@ -195,23 +194,23 @@ describe('/teams/read endpoint', () => {
195194
const body = JSON.parse(result.body);
196195
expect(body.message).toBe('Successfully read team');
197196
expect(body.team).toBeDefined();
198-
expect(JSON.parse(body.team).team_id).toBe('test-team-id');
197+
expect(body.team.team_id).toBe('test-team-id');
199198
});
200199

201200
it('should fetch team details using member_email', async () => {
202201
validateTokenMock.mockReturnValue(true);
203202

204203
usersFindOneMock
205-
.mockReturnValueOnce({
204+
.mockResolvedValueOnce({
206205
email: 'organizer@test.com',
207206
role: { organizer: true },
208207
})
209-
.mockReturnValueOnce({
208+
.mockResolvedValueOnce({
210209
email: 'member@test.com',
211210
team_info: { team_id: 'test-team-id' },
212211
});
213212

214-
teamsFindOneMock.mockReturnValueOnce({
213+
teamsFindOneMock.mockResolvedValueOnce({
215214
team_id: 'test-team-id',
216215
status: 'Active',
217216
members: ['member@test.com', 'member2@test.com'],
@@ -234,6 +233,6 @@ describe('/teams/read endpoint', () => {
234233
const body = JSON.parse(result.body);
235234
expect(body.message).toBe('Successfully read team');
236235
expect(body.team).toBeDefined();
237-
expect(JSON.parse(body.team).team_id).toBe('test-team-id');
236+
expect(body.team.team_id).toBe('test-team-id');
238237
});
239238
});

tests/user-exists.test.ts

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,123 @@
11
import { main } from '../src/functions/user-exists/handler';
2-
32
import { createEvent, mockContext } from './helper';
43
import * as util from '../src/util';
54

65
jest.mock('../src/util', () => ({
7-
// eslint-disable-next-line @typescript-eslint/naming-convention
86
MongoDB: {
97
getInstance: jest.fn().mockReturnValue({
108
connect: jest.fn(),
119
disconnect: jest.fn(),
1210
getCollection: jest.fn().mockReturnValue({
1311
findOne: jest.fn(),
14-
find: jest.fn().mockReturnValue({
15-
toArray: jest.fn(),
16-
}),
1712
}),
1813
}),
1914
},
20-
validateToken: jest.fn().mockReturnValueOnce(false).mockReturnValue(true),
15+
validateToken: jest.fn(),
16+
userExistsLogic: jest.fn(),
2117
}));
2218

2319
describe('/user-exists endpoint', () => {
2420
beforeEach(() => {
2521
jest.clearAllMocks();
2622
});
23+
2724
const path = '/user-exists';
2825
const httpMethod = 'POST';
2926

30-
const findOneMock = util.MongoDB.getInstance('uri').getCollection('users').findOne as jest.Mock;
31-
const mockCallback = jest.fn();
27+
const userExistsLogicMock = util.userExistsLogic as jest.Mock;
28+
29+
it('should reject invalid token', async () => {
30+
userExistsLogicMock.mockResolvedValueOnce({
31+
statusCode: 401,
32+
body: JSON.stringify({ message: 'Unauthorized' }),
33+
});
3234

33-
it('invalid token', async () => {
3435
const userData = {
3536
auth_email: 'hacker@hackru.org',
36-
auth_token: 'mockToken',
37+
auth_token: 'invalidToken',
3738
email: 'hacker@hackru.org',
3839
};
3940
const mockEvent = createEvent(userData, path, httpMethod);
4041

41-
const res = await main(mockEvent, mockContext, mockCallback);
42+
const res = await main(mockEvent, mockContext, null);
43+
4244
expect(res.statusCode).toBe(401);
4345
expect(JSON.parse(res.body).message).toBe('Unauthorized');
4446
});
4547

46-
it('auth user not found', async () => {
47-
findOneMock.mockReturnValueOnce(null);
48+
it('should reject when auth user is not found', async () => {
49+
userExistsLogicMock.mockResolvedValueOnce({
50+
statusCode: 404,
51+
body: JSON.stringify({ message: 'Auth user not found.' }),
52+
});
53+
4854
const userData = {
4955
auth_email: 'non-existent-user@hackru.org',
50-
auth_token: 'mockToken',
56+
auth_token: 'validToken',
5157
email: 'hacker@hackru.org',
5258
};
5359
const mockEvent = createEvent(userData, path, httpMethod);
5460

55-
const res = await main(mockEvent, mockContext, mockCallback);
61+
const res = await main(mockEvent, mockContext, null);
62+
5663
expect(res.statusCode).toBe(404);
5764
expect(JSON.parse(res.body).message).toBe('Auth user not found.');
5865
});
5966

60-
it('look-up user not found', async () => {
61-
findOneMock
62-
.mockReturnValueOnce({
63-
email: 'hackerCheck@hackru.org',
64-
})
65-
.mockReturnValueOnce(null);
67+
it('should reject when lookup user is not found', async () => {
68+
userExistsLogicMock.mockResolvedValueOnce({
69+
statusCode: 404,
70+
body: JSON.stringify({ message: 'Look-up user was not found' }),
71+
});
72+
6673
const userData = {
6774
auth_email: 'hackerCheck@hackru.org',
68-
auth_token: 'mockToken',
75+
auth_token: 'validToken',
6976
email: 'non-existent-user@hackru.org',
7077
};
7178
const mockEvent = createEvent(userData, path, httpMethod);
7279

73-
const res = await main(mockEvent, mockContext, mockCallback);
80+
const res = await main(mockEvent, mockContext, null);
7481

7582
expect(res.statusCode).toBe(404);
7683
expect(JSON.parse(res.body).message).toBe('Look-up user was not found');
7784
});
7885

79-
it('success case', async () => {
80-
//can check even if you don't have the admin role
81-
findOneMock
82-
.mockReturnValueOnce({
83-
email: 'hackerCheck@hackru.org',
84-
})
85-
.mockReturnValueOnce({});
86+
it('should return success when lookup user exists', async () => {
87+
userExistsLogicMock.mockResolvedValueOnce({
88+
statusCode: 200,
89+
body: JSON.stringify('User exists'),
90+
});
91+
8692
const userData = {
8793
auth_email: 'hackerCheck@hackru.org',
88-
auth_token: 'mockToken',
94+
auth_token: 'validToken',
8995
email: 'hacker@hackru.org',
9096
};
9197
const mockEvent = createEvent(userData, path, httpMethod);
9298

93-
const res = await main(mockEvent, mockContext, mockCallback);
99+
const res = await main(mockEvent, mockContext, null);
94100

95101
expect(res.statusCode).toBe(200);
96-
expect(res.body).toBeDefined();
102+
expect(JSON.parse(res.body)).toBe('User exists');
97103
});
98104

99-
it('success case for all lookup', async () => {
100-
findOneMock
101-
.mockReturnValueOnce({
102-
email: 'hackerCheck@hackru.org',
103-
})
104-
.mockReturnValueOnce({ email: 'targetHacker@hackru.org' });
105+
it('should return success for all lookup cases', async () => {
106+
userExistsLogicMock.mockResolvedValueOnce({
107+
statusCode: 200,
108+
body: JSON.stringify('User exists'),
109+
});
110+
105111
const userData = {
106112
auth_email: 'hackerCheck@hackru.org',
107-
auth_token: 'mockToken',
113+
auth_token: 'validToken',
108114
email: 'anyemail@hackru.org',
109115
};
110116
const mockEvent = createEvent(userData, path, httpMethod);
111-
const res = await main(mockEvent, mockContext, mockCallback);
112-
console.log(res.body);
117+
118+
const res = await main(mockEvent, mockContext, null);
119+
113120
expect(res.statusCode).toBe(200);
114-
expect(JSON.parse(res.body)).toEqual('User exists');
121+
expect(JSON.parse(res.body)).toBe('User exists');
115122
});
116123
});

0 commit comments

Comments
 (0)