Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 3c594d7

Browse files
committed
add checkAuth tests
1 parent 0aa9b91 commit 3c594d7

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

express-handlers.jest-test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,22 @@ const mockResponse = () => {
99
return res;
1010
};
1111

12-
test.todo('should pass')
12+
const { checkAuth } = require('./express-handlers');
13+
14+
// describe('login', () => {});
15+
// describe('logout', () => {});
16+
describe('checkAuth', () => {
17+
test('should 401 if session data is not set', async () => {
18+
const req = mockRequest();
19+
const res = mockResponse();
20+
await checkAuth(req, res);
21+
expect(res.status).toHaveBeenCalledWith(401);
22+
});
23+
test('should 200 with username from session if session data is set', async () => {
24+
const req = mockRequest({ username: 'hugo' });
25+
const res = mockResponse();
26+
await checkAuth(req, res);
27+
expect(res.status).toHaveBeenCalledWith(200);
28+
expect(res.json).toHaveBeenCalledWith({ username: 'hugo' });
29+
});
30+
});

express-handlers.sinon-test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,19 @@ const mockResponse = () => {
1212
return res;
1313
};
1414

15-
test.todo('should succeed')
15+
const { checkAuth } = require('./express-handlers');
16+
17+
test('checkAuth > should 401 if session data is not set', async (t) => {
18+
const req = mockRequest();
19+
const res = mockResponse();
20+
await checkAuth(req, res);
21+
t.true(res.status.calledWith(401));
22+
});
23+
24+
test('checkAuth > should 200 with username from session if data is set', async (t) => {
25+
const req = mockRequest({ username: 'hugo' });
26+
const res = mockResponse();
27+
await checkAuth(req, res);
28+
t.true(res.status.calledWith(200));
29+
t.true(res.json.calledWith({ username: 'hugo' }));
30+
})

0 commit comments

Comments
 (0)