-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.spec.ts
188 lines (159 loc) · 5.32 KB
/
server.spec.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
jest.mock('@coveord/platform-client');
jest.mock('./middlewares/searchToken', () => {
const originalMiddleware = jest.requireActual('./middlewares/searchToken');
const oldMiddleware = originalMiddleware.ensureTokenGenerated;
originalMiddleware.ensureTokenGenerated = function (
req: Request,
res: Response,
next: NextFunction
) {
mockedMiddleware(req, res, next);
oldMiddleware(req, res, next);
};
return originalMiddleware;
});
import {mocked} from 'ts-jest/utils';
import {agent} from 'supertest';
import app from './app';
import {Request, Response, NextFunction} from 'express';
import PlatformClient, {RestUserIdType} from '@coveord/platform-client';
const mockedPlatformClient = mocked(PlatformClient);
const mockedCreateToken = jest.fn();
const mockedMiddleware = jest.fn();
describe('server', () => {
const OLD_ENV = process.env;
const setInvalidEnvironmentVariables = () => {
process.env.ORGANIZATION_ID = undefined;
process.env.API_KEY = undefined;
process.env.USER_EMAIL = undefined;
};
const setValidEnvironmentVariables = () => {
process.env.PLATFORM_URL = 'https://platform.cloud.coveo.com';
process.env.ORGANIZATION_ID = 'my-org';
process.env.API_KEY = 'xxx';
process.env.USER_EMAIL = 'bob@coveo.com';
};
const triggerAMiddlewareError = () => {
mockedMiddleware.mockImplementationOnce(
(_req: Request, _res: Response, next: NextFunction) => {
const err: Error = new Error();
next(err);
}
);
};
const resetAppLocals = () => {
mockedMiddleware.mockImplementationOnce(
(req: Request, _res: Response, _next: NextFunction) => {
delete req.app.locals.platform;
}
);
};
const doMockPlatformClient = () => {
mockedPlatformClient.mockImplementation(
() =>
({
search: {
createToken: mockedCreateToken,
},
} as unknown as PlatformClient)
);
};
beforeEach(() => {
jest.resetModules();
process.env = {...OLD_ENV};
doMockPlatformClient();
});
afterAll(() => {
process.env = OLD_ENV;
});
describe('when an unknown error happens', () => {
beforeEach(() => {
setValidEnvironmentVariables();
triggerAMiddlewareError();
});
it('should return a generic error message', async () => {
const res = await agent(app).get('/token');
expect(res.body.message).toEqual('Something broke!');
});
});
describe('when required environment variables are missing', () => {
beforeEach(() => {
setInvalidEnvironmentVariables();
});
it('should return a JSON response', async () => {
await agent(app).get('/token').expect('Content-Type', /json/);
});
it('should return an error with a message', async () => {
const res = await agent(app).get('/token');
expect(res.serverError).toBeTruthy();
expect(res.body.message).toEqual(
'Make sure to configure the environment variables in the ".env" file. Refer to the README to set up the server.'
);
});
});
describe('when the API key does not have the right privileges', () => {
beforeEach(() => {
setValidEnvironmentVariables();
mockedCreateToken.mockRejectedValueOnce({
statusCode: 403,
message: 'Forbidden',
type: 'AccessDeniedException',
});
});
it('it should return an error', async () => {
const res = await agent(app).get('/token');
expect(res.status).toBe(403);
expect(res.body).not.toHaveProperty('token');
});
});
describe('when the environment variables are correctly set', () => {
beforeEach(() => {
setValidEnvironmentVariables();
resetAppLocals();
mockedCreateToken.mockResolvedValueOnce({
token: 'this.is.a.search.token',
});
});
it('should correctly initialize the #platformClient based on environment variables', async () => {
await agent(app).get('/token');
});
it('should correctly initialize the #platformClient based on environment variables', async () => {
await agent(app).get('/token');
expect(mockedPlatformClient).toHaveBeenLastCalledWith(
expect.objectContaining({
accessToken: 'xxx',
host: 'https://platform.cloud.coveo.com',
organizationId: 'my-org',
})
);
});
it('should return JSON response', async () => {
await agent(app).get('/token').expect('Content-Type', /json/);
});
it('should not create multiple instances of #platformClient', async () => {
await agent(app).get('/token');
await agent(app).get('/token');
await agent(app).get('/token');
expect(mockedPlatformClient).toHaveBeenCalledTimes(1);
});
it('should correctly call #createToken with the appropriate arguments', async () => {
await agent(app).get('/token');
expect(mockedCreateToken).toHaveBeenLastCalledWith(
expect.objectContaining({
userIds: [
{
name: 'bob@coveo.com',
provider: 'Email Security Provider',
type: RestUserIdType.User,
},
],
})
);
});
it('should return a search token', async () => {
const res = await agent(app).get('/token');
expect(res.status).toBe(200);
expect(res.body).toEqual({token: 'this.is.a.search.token'});
});
});
});