Skip to content

Commit 3ed6d2b

Browse files
author
hirsch88
committed
fix tests
1 parent 1f3689b commit 3ed6d2b

File tree

7 files changed

+55
-47
lines changed

7 files changed

+55
-47
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@
124124
"js",
125125
"json"
126126
],
127-
"testEnvironment": "node"
127+
"testEnvironment": "node",
128+
"setupTestFrameworkScriptFile": "./test/unit/lib/setup.ts"
128129
},
129130
"license": "MIT"
130131
}

test/setup/.gitkeep

Whitespace-only changes.
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
import { authenticate as Authenticate } from '../../../../src/api/middlewares/authenticate';
1+
import { AuthenticateMiddleware } from '../../../../src/api/middlewares/AuthenticateMiddleware';
2+
import { LogMock } from '../../lib/LogMock';
23

4+
describe('AuthenticateMiddleware', () => {
35

4-
describe('authenticate', () => {
5-
6-
let authenticate, request, log, res, req, next;
6+
let authenticate, request, res, req, next;
77
beforeEach(() => {
88
process.env.AUTH0_HOST = 'test';
9-
log = {
10-
debug: jest.fn(),
11-
info: jest.fn(),
12-
warn: jest.fn()
13-
};
149
request = jest.fn();
15-
authenticate = Authenticate(request, log);
10+
authenticate = new AuthenticateMiddleware(LogMock, request);
1611
res = {
1712
failed: jest.fn()
1813
};
@@ -24,11 +19,11 @@ describe('authenticate', () => {
2419

2520
test('Should fail because no token was given', () => {
2621
req.headers.authorization = undefined;
27-
authenticate(req, res, next);
22+
authenticate.use(req, res, next);
2823
expect(res.failed).toHaveBeenCalledWith(403, 'You are not allowed to request this resource!');
2924

3025
req.headers.authorization = '';
31-
authenticate(req, res, next);
26+
authenticate.use(req, res, next);
3227
expect(res.failed).toHaveBeenCalledWith(403, 'You are not allowed to request this resource!');
3328

3429
});
@@ -39,31 +34,27 @@ describe('authenticate', () => {
3934
expect(options.url).toBe('test/tokeninfo');
4035
expect(options.form.id_token).toBe('1234');
4136
};
42-
const auth = Authenticate(request, log);
43-
auth(req, res, next);
44-
expect(log.debug).toHaveBeenCalledWith('Token is provided');
45-
37+
const auth = new AuthenticateMiddleware(LogMock, request);
38+
auth.use(req, res, next);
4639
});
4740

4841
test('Should pass and add the token info to the request object', () => {
4942
request = (options, done) => {
5043
done(null, { statusCode: 200 }, '{ "user_id": 77, "email": "test@jest.org" }');
5144
expect(req.tokeninfo.user_id).toBe(77);
52-
expect(log.info).toHaveBeenCalled();
5345
expect(next).toHaveBeenCalled();
5446
};
55-
const auth = Authenticate(request, log);
56-
auth(req, res, next);
47+
const auth = new AuthenticateMiddleware(LogMock, request);
48+
auth.use(req, res, next);
5749
});
5850

5951
test('Should fail and respond with a 401 error', () => {
6052
request = (options, done) => {
6153
done(null, { statusCode: 401 }, 'Bad message :-)');
62-
expect(log.warn).toHaveBeenCalled();
6354
expect(res.failed).toHaveBeenCalledWith(401, 'Bad message :-)');
6455
};
65-
const auth = Authenticate(request, log);
66-
auth(req, res, next);
56+
const auth = new AuthenticateMiddleware(LogMock, request);
57+
auth.use(req, res, next);
6758
});
6859

6960
});
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import { populateUser as PopulateUser } from '../../../../src/api/middlewares/populateUser';
1+
import { PopulateUserMiddleware } from '../../../../src/api/middlewares/PopulateUserMiddleware';
2+
import { LogMock } from '../../lib/LogMock';
23

34

4-
describe('populateUser', () => {
5+
describe('PopulateUserMiddleware', () => {
56

6-
let populateUser, userService, log, res, req, next;
7+
let populateUser, userService, res, req, next;
78
beforeEach(() => {
89
process.env.AUTH0_HOST = 'test';
9-
log = {
10-
debug: jest.fn(),
11-
warn: jest.fn()
12-
};
13-
populateUser = PopulateUser(() => userService, log);
10+
populateUser = new PopulateUserMiddleware(LogMock, userService);
1411
res = {
1512
failed: jest.fn()
1613
};
@@ -22,10 +19,10 @@ describe('populateUser', () => {
2219

2320
test('Should fail because no tokeninfo or user_id is given', () => {
2421
req.tokeninfo.user_id = undefined;
25-
populateUser(req, res, next);
22+
populateUser.use(req, res, next);
2623
expect(res.failed).toBeCalledWith(400, 'Missing token information!');
2724
req.tokeninfo = undefined;
28-
populateUser(req, res, next);
25+
populateUser.use(req, res, next);
2926
expect(res.failed).toBeCalledWith(400, 'Missing token information!');
3027
});
3128

@@ -39,13 +36,12 @@ describe('populateUser', () => {
3936
})
4037
});
4138
expect(req.user.id).toBe(88);
42-
expect(log.debug).toHaveBeenCalledWith(`populated user with the id=88 to the request object`);
4339
expect(next).toBeCalled();
4440
});
4541
})
4642
};
47-
const pop = PopulateUser(() => userService, log);
48-
pop(req, res, next);
43+
const pop = new PopulateUserMiddleware(LogMock, userService);
44+
pop.use(req, res, next);
4945
});
5046

5147
test('Should behave...', () => {
@@ -54,13 +50,12 @@ describe('populateUser', () => {
5450
return new Promise((resolve, reject) => {
5551
reject(new Error('test'));
5652
expect(req.user).toBeUndefined();
57-
expect(log.warn).toHaveBeenCalledWith(`could not populate user to the request object`);
5853
expect(next).toBeCalled();
5954
});
6055
})
6156
};
62-
const pop = PopulateUser(() => userService, log);
63-
pop(req, res, next);
57+
const pop = new PopulateUserMiddleware(LogMock, userService);
58+
pop.use(req, res, next);
6459
});
6560

6661
});

test/unit/core/Bootstrap.test.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ import { Bootstrap } from '../../../src/core/Bootstrap';
22

33

44
describe('Bootstrap', () => {
5-
test('getApp should return a new app with port and host', () => {
6-
process.env.APP_HOST = 'host';
7-
process.env.PORT = '3000';
8-
const app = Bootstrap.getApp();
9-
expect(app.get('host')).toBe(process.env.APP_HOST);
10-
expect(app.get('port')).toBe(Bootstrap.normalizePort(process.env.PORT));
11-
});
12-
135
test('normalizePort should return a valid port', () => {
146
expect(Bootstrap.normalizePort('0')).toBe(0);
157
expect(Bootstrap.normalizePort('-1')).toBe(false);

test/unit/lib/LogMock.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Log } from '../../../src/core/log';
2+
3+
4+
export class LogMock extends Log {
5+
6+
public debugMock = jest.fn();
7+
public infoMock = jest.fn();
8+
public warnMock = jest.fn();
9+
public errorMock = jest.fn();
10+
11+
public debug(message: string, ...args: any[]): void {
12+
this.debugMock('debug', message, args);
13+
}
14+
15+
public info(message: string, ...args: any[]): void {
16+
this.infoMock('info', message, args);
17+
}
18+
19+
public warn(message: string, ...args: any[]): void {
20+
this.warnMock('warn', message, args);
21+
}
22+
23+
public error(message: string, ...args: any[]): void {
24+
this.errorMock('error', message, args);
25+
}
26+
27+
}

test/unit/lib/setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Helps to add metadata to classes with annotations
2+
import 'reflect-metadata';

0 commit comments

Comments
 (0)