Newer
Older

Douglas Lagemann
committed
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const { jwtDecode } = require('jwt-decode');
const { getHealth, getUser, getVersion } = require('./controller');
const { getDbVersion, healthCheck } = require('../data/dataLayer');
jest.mock('jwt-decode', () => ({
jwtDecode: jest.fn()
}));
jest.mock('../data/dataLayer', () => ({
healthCheck: jest.fn(),
getDbVersion: jest.fn()
}));
let mockRes;
beforeEach(() => {
mockRes = {
status: jest.fn().mockReturnThis(),
json: jest.fn()
};
console.log = jest.fn();
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('Controller', () => {
describe('getHealth', () => {
it('responds with 200 and message on success', async () => {
healthCheck.mockImplementation(async () => {
return {
healthy: true,
message: "test health check"
};
});
await getHealth(null, mockRes);
expect(mockRes.status).toHaveBeenCalledWith(200);
expect(mockRes.json).toHaveBeenCalledWith({
status: 200,
message: "test health check"
});
});
it('responds with 500 and message on failure', async () => {
healthCheck.mockImplementation(async () => {
return {
healthy: false,
message: "database issues"
};
});
await getHealth(null, mockRes);
expect(mockRes.status).toHaveBeenCalledWith(500);
expect(mockRes.json).toHaveBeenCalledWith({
status: 500,
message: "database issues"
});
});
it('responds with 500 and generic message on falsy result', async () => {
healthCheck.mockResolvedValue(undefined);
await getHealth(null, mockRes);
expect(mockRes.status).toHaveBeenCalledWith(500);
expect(mockRes.json).toHaveBeenCalledWith({
status: 500,
message: "Internal Server Error"
});
});
});
describe('getUser', () => {
it ('responds with 200 and decoded token from request header: Authorization', async () => {
jwtDecode.mockReturnValue({ token: "decoded-auth" });
const mockReq = { headersDistinct: { Authorization: [ "test-auth" ] } };
await getUser(mockReq, mockRes);
expect(mockRes.status).toHaveBeenCalledWith(200);
expect(mockRes.json).toHaveBeenCalledWith({ token: "decoded-auth" });
});
it ('responds with 200 and decoded token from request header: authorization', async () => {
jwtDecode.mockReturnValue({ token: "decoded-auth" });
const mockReq = { headersDistinct: { authorization: [ "test-auth" ] } };
await getUser(mockReq, mockRes);
expect(mockRes.status).toHaveBeenCalledWith(200);
expect(mockRes.json).toHaveBeenCalledWith({ token: "decoded-auth" });
});
it ('responds with 200 and generic message if no auth header found', async () => {
const mockReq = {};
await getUser(mockReq, mockRes);
expect(mockRes.status).toHaveBeenCalledWith(200);
expect(mockRes.json).toHaveBeenCalledWith({ result: "no authorization header found" });
});
it ('responds with 400 and error message if decode fails', async () => {
jwtDecode.mockImplementation(() => {
throw new Error("failed to decode");
});
const mockReq = { headersDistinct: { Authorization: [ "test-auth" ] } };
await getUser(mockReq, mockRes);
expect(mockRes.status).toHaveBeenCalledWith(400);
expect(mockRes.json.mock.calls[0][0].error).toContain("failed to decode");
});
});
describe('getVersion', () => {
it('responds with 200 and versions', async () => {
getDbVersion.mockResolvedValue("1.2.3");
await getVersion(null, mockRes);
expect(mockRes.status).toHaveBeenCalledWith(200);
expect(mockRes.json).toHaveBeenCalledWith({
apiVersion: "0.0.1",
dbVersion: "1.2.3"
});
});
});
});