Skip to content

Commit d2c6410

Browse files
author
hirsch88
committed
add tests for our middlewares
1 parent fafd680 commit d2c6410

File tree

3 files changed

+137
-2
lines changed

3 files changed

+137
-2
lines changed

src/api/middlewares/authenticate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ export const authenticate = (request: RequestAPI<Request, Options, RequiredUriUr
2424
log.debug('Token is provided');
2525

2626
// Request user info at auth0 with the provided token
27-
request.post({
27+
request({
28+
method: 'POST',
2829
url: `${process.env.AUTH0_HOST}/tokeninfo`,
2930
form: {
3031
id_token: token
3132
}
3233
}, (error: any, response: RequestResponse, body: any) => {
33-
3434
// Verify if the requests was successful and append user
3535
// information to our extended express request object
3636
if (!error && response.statusCode === 200) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { authenticate as Authenticate } from '../../../../src/api/middlewares/authenticate';
2+
3+
4+
describe('authenticate', () => {
5+
6+
let authenticate, request, log, res, req, next;
7+
beforeEach(() => {
8+
process.env.AUTH0_HOST = 'test';
9+
log = {
10+
debug: jest.fn(),
11+
info: jest.fn(),
12+
warn: jest.fn()
13+
};
14+
request = jest.fn();
15+
authenticate = Authenticate(request, log);
16+
res = {
17+
failed: jest.fn()
18+
};
19+
req = {
20+
headers: { authorization: 'Bearer 1234' }
21+
};
22+
next = jest.fn();
23+
});
24+
25+
test('Should fail because no token was given', () => {
26+
req.headers.authorization = undefined;
27+
authenticate(req, res, next);
28+
expect(res.failed).toHaveBeenCalledWith(403, 'You are not allowed to request this resource!');
29+
30+
req.headers.authorization = '';
31+
authenticate(req, res, next);
32+
expect(res.failed).toHaveBeenCalledWith(403, 'You are not allowed to request this resource!');
33+
34+
});
35+
36+
test('Should set the correct request options', () => {
37+
request = (options, done) => {
38+
expect(options.method).toBe('POST');
39+
expect(options.url).toBe('test/tokeninfo');
40+
expect(options.form.id_token).toBe('1234');
41+
};
42+
const auth = Authenticate(request, log);
43+
auth(req, res, next);
44+
expect(log.debug).toHaveBeenCalledWith('Token is provided');
45+
46+
});
47+
48+
test('Should pass and add the token info to the request object', () => {
49+
request = (options, done) => {
50+
done(null, { statusCode: 200 }, '{ "user_id": 77, "email": "test@jest.org" }');
51+
expect(req.tokeninfo.user_id).toBe(77);
52+
expect(log.info).toHaveBeenCalled();
53+
expect(next).toHaveBeenCalled();
54+
};
55+
const auth = Authenticate(request, log);
56+
auth(req, res, next);
57+
});
58+
59+
test('Should fail and respond with a 401 error', () => {
60+
request = (options, done) => {
61+
done(null, { statusCode: 401 }, 'Bad message :-)');
62+
expect(log.warn).toHaveBeenCalled();
63+
expect(res.failed).toHaveBeenCalledWith(401, 'Bad message :-)');
64+
};
65+
const auth = Authenticate(request, log);
66+
auth(req, res, next);
67+
});
68+
69+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { populateUser as PopulateUser } from '../../../../src/api/middlewares/populateUser';
2+
3+
4+
describe('populateUser', () => {
5+
6+
let populateUser, userService, log, res, req, next;
7+
beforeEach(() => {
8+
process.env.AUTH0_HOST = 'test';
9+
log = {
10+
debug: jest.fn(),
11+
warn: jest.fn()
12+
};
13+
populateUser = PopulateUser(() => userService, log);
14+
res = {
15+
failed: jest.fn()
16+
};
17+
req = {
18+
tokeninfo: { user_id: 77 }
19+
};
20+
next = jest.fn();
21+
});
22+
23+
test('Should fail because no tokeninfo or user_id is given', () => {
24+
req.tokeninfo.user_id = undefined;
25+
populateUser(req, res, next);
26+
expect(res.failed).toBeCalledWith(400, 'Missing token information!');
27+
req.tokeninfo = undefined;
28+
populateUser(req, res, next);
29+
expect(res.failed).toBeCalledWith(400, 'Missing token information!');
30+
});
31+
32+
test('Should pass the database query and attache the user to the request object', () => {
33+
userService = {
34+
findByUserId: jest.fn().mockImplementation(() => {
35+
return new Promise((resolve, reject) => {
36+
resolve({
37+
toJSON: () => ({
38+
id: 88
39+
})
40+
});
41+
expect(req.user.id).toBe(88);
42+
expect(log.debug).toHaveBeenCalledWith(`populated user with the id=88 to the request object`);
43+
expect(next).toBeCalled();
44+
});
45+
})
46+
};
47+
const pop = PopulateUser(() => userService, log);
48+
pop(req, res, next);
49+
});
50+
51+
test('Should behave...', () => {
52+
userService = {
53+
findByUserId: jest.fn().mockImplementation(() => {
54+
return new Promise((resolve, reject) => {
55+
reject(new Error('test'));
56+
expect(req.user).toBeUndefined();
57+
expect(log.warn).toHaveBeenCalledWith(`could not populate user to the request object`);
58+
expect(next).toBeCalled();
59+
});
60+
})
61+
};
62+
const pop = PopulateUser(() => userService, log);
63+
pop(req, res, next);
64+
});
65+
66+
});

0 commit comments

Comments
 (0)