-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate.test.ts
More file actions
73 lines (57 loc) · 2.51 KB
/
create.test.ts
File metadata and controls
73 lines (57 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { main } from '../src/functions/create/handler';
import { createEvent, mockContext } from './helper';
import * as bcrypt from 'bcryptjs';
jest.mock('bcryptjs');
//mock the db route
jest.mock('../src/util', () => ({
// eslint-disable-next-line @typescript-eslint/naming-convention
MongoDB: {
getInstance: jest.fn().mockReturnValue({
connect: jest.fn(),
disconnect: jest.fn(),
getCollection: jest.fn().mockReturnValue({
findOne: jest.fn().mockReturnValueOnce({ email: 'test@test.org', password: 'test' }).mockReturnValue(null),
insertOne: jest.fn(),
}),
}),
},
}));
jest.mock('../src/config.ts', () => ({
registrationStart: '06/02/2024',
registrationEnd: '12/31/2099',
}));
describe('Create endpoint', () => {
it('Duplicate User', async () => {
const mockEvent = createEvent({ email: 'testEmail@gmail.com', password: 'testPassword123' }, '/create', 'POST');
const mockCallback = jest.fn();
(bcrypt.hash as jest.Mock).mockResolvedValue('hashedPassword');
const res = await main(mockEvent, mockContext, mockCallback);
expect(res.statusCode).toBe(400);
expect(JSON.parse(res.body).message).toBe('Duplicate user!');
});
it('Create a new user', async () => {
const mockEvent = createEvent({ email: 'testEmail@gmail.com', password: 'testPassword123' }, '/create', 'POST');
const mockCallback = jest.fn();
(bcrypt.hash as jest.Mock).mockResolvedValue('hashedPassword');
const res = await main(mockEvent, mockContext, mockCallback);
expect(res.statusCode).toBe(200);
expect(JSON.parse(res.body).message).toBe('User created!');
});
it('Email entry is invalid', async () => {
const mockEvent = createEvent({ email: 'notValidFormat', password: 'testPassword123' }, '/create', 'POST');
const mockCallback = jest.fn();
(bcrypt.hash as jest.Mock).mockResolvedValue('hashedPassword');
const res = await main(mockEvent, mockContext, mockCallback);
expect(res.statusCode).toBe(403);
expect(JSON.parse(res.body).message).toBe('Improper Email format');
});
it('Registration time has passed', async () => {
jest.useFakeTimers();
jest.setSystemTime(new Date('01/01/2100'));
const mockEvent = createEvent({ email: 'testEmail@gmail.com', password: 'testPassword123' }, '/create', 'POST');
const mockCallback = jest.fn();
const res = await main(mockEvent, mockContext, mockCallback);
expect(res.statusCode).toBe(400);
expect(JSON.parse(res.body).message).toBe('Registration is closed!');
});
});