|
1 | 1 | import { main } from '../src/functions/user-exists/handler'; |
2 | | - |
3 | 2 | import { createEvent, mockContext } from './helper'; |
4 | 3 | import * as util from '../src/util'; |
5 | 4 |
|
6 | 5 | jest.mock('../src/util', () => ({ |
7 | | - // eslint-disable-next-line @typescript-eslint/naming-convention |
8 | 6 | MongoDB: { |
9 | 7 | getInstance: jest.fn().mockReturnValue({ |
10 | 8 | connect: jest.fn(), |
11 | 9 | disconnect: jest.fn(), |
12 | 10 | getCollection: jest.fn().mockReturnValue({ |
13 | 11 | findOne: jest.fn(), |
14 | | - find: jest.fn().mockReturnValue({ |
15 | | - toArray: jest.fn(), |
16 | | - }), |
17 | 12 | }), |
18 | 13 | }), |
19 | 14 | }, |
20 | | - validateToken: jest.fn().mockReturnValueOnce(false).mockReturnValue(true), |
| 15 | + validateToken: jest.fn(), |
| 16 | + userExistsLogic: jest.fn(), |
21 | 17 | })); |
22 | 18 |
|
23 | 19 | describe('/user-exists endpoint', () => { |
24 | 20 | beforeEach(() => { |
25 | 21 | jest.clearAllMocks(); |
26 | 22 | }); |
| 23 | + |
27 | 24 | const path = '/user-exists'; |
28 | 25 | const httpMethod = 'POST'; |
29 | 26 |
|
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 | + }); |
32 | 34 |
|
33 | | - it('invalid token', async () => { |
34 | 35 | const userData = { |
35 | 36 | auth_email: 'hacker@hackru.org', |
36 | | - auth_token: 'mockToken', |
| 37 | + auth_token: 'invalidToken', |
37 | 38 | email: 'hacker@hackru.org', |
38 | 39 | }; |
39 | 40 | const mockEvent = createEvent(userData, path, httpMethod); |
40 | 41 |
|
41 | | - const res = await main(mockEvent, mockContext, mockCallback); |
| 42 | + const res = await main(mockEvent, mockContext, null); |
| 43 | + |
42 | 44 | expect(res.statusCode).toBe(401); |
43 | 45 | expect(JSON.parse(res.body).message).toBe('Unauthorized'); |
44 | 46 | }); |
45 | 47 |
|
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 | + |
48 | 54 | const userData = { |
49 | 55 | auth_email: 'non-existent-user@hackru.org', |
50 | | - auth_token: 'mockToken', |
| 56 | + auth_token: 'validToken', |
51 | 57 | email: 'hacker@hackru.org', |
52 | 58 | }; |
53 | 59 | const mockEvent = createEvent(userData, path, httpMethod); |
54 | 60 |
|
55 | | - const res = await main(mockEvent, mockContext, mockCallback); |
| 61 | + const res = await main(mockEvent, mockContext, null); |
| 62 | + |
56 | 63 | expect(res.statusCode).toBe(404); |
57 | 64 | expect(JSON.parse(res.body).message).toBe('Auth user not found.'); |
58 | 65 | }); |
59 | 66 |
|
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 | + |
66 | 73 | const userData = { |
67 | 74 | auth_email: 'hackerCheck@hackru.org', |
68 | | - auth_token: 'mockToken', |
| 75 | + auth_token: 'validToken', |
69 | 76 | email: 'non-existent-user@hackru.org', |
70 | 77 | }; |
71 | 78 | const mockEvent = createEvent(userData, path, httpMethod); |
72 | 79 |
|
73 | | - const res = await main(mockEvent, mockContext, mockCallback); |
| 80 | + const res = await main(mockEvent, mockContext, null); |
74 | 81 |
|
75 | 82 | expect(res.statusCode).toBe(404); |
76 | 83 | expect(JSON.parse(res.body).message).toBe('Look-up user was not found'); |
77 | 84 | }); |
78 | 85 |
|
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 | + |
86 | 92 | const userData = { |
87 | 93 | auth_email: 'hackerCheck@hackru.org', |
88 | | - auth_token: 'mockToken', |
| 94 | + auth_token: 'validToken', |
89 | 95 | email: 'hacker@hackru.org', |
90 | 96 | }; |
91 | 97 | const mockEvent = createEvent(userData, path, httpMethod); |
92 | 98 |
|
93 | | - const res = await main(mockEvent, mockContext, mockCallback); |
| 99 | + const res = await main(mockEvent, mockContext, null); |
94 | 100 |
|
95 | 101 | expect(res.statusCode).toBe(200); |
96 | | - expect(res.body).toBeDefined(); |
| 102 | + expect(JSON.parse(res.body)).toBe('User exists'); |
97 | 103 | }); |
98 | 104 |
|
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 | + |
105 | 111 | const userData = { |
106 | 112 | auth_email: 'hackerCheck@hackru.org', |
107 | | - auth_token: 'mockToken', |
| 113 | + auth_token: 'validToken', |
108 | 114 | email: 'anyemail@hackru.org', |
109 | 115 | }; |
110 | 116 | 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 | + |
113 | 120 | expect(res.statusCode).toBe(200); |
114 | | - expect(JSON.parse(res.body)).toEqual('User exists'); |
| 121 | + expect(JSON.parse(res.body)).toBe('User exists'); |
115 | 122 | }); |
116 | 123 | }); |
0 commit comments