Skip to content

Commit 65273f9

Browse files
author
hirsch88
committed
✅ Adjust tests for new AuthService
1 parent b46e605 commit 65273f9

File tree

3 files changed

+25
-46
lines changed

3 files changed

+25
-46
lines changed

test/e2e/api/users.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { User } from '../../../src/api/models/User';
55
import { CreateBruce } from '../../../src/database/seeds/CreateBruce';
66
import { runSeed } from '../../../src/lib/seed';
77
import { closeDatabase } from '../../utils/database';
8-
import { fakeAuthenticationForUser } from '../utils/auth';
98
import { BootstrapSettings } from '../utils/bootstrap';
109
import { prepareServer } from '../utils/server';
1110

@@ -21,7 +20,6 @@ describe('/api/users', () => {
2120
beforeAll(async () => {
2221
settings = await prepareServer({ migrate: true });
2322
bruce = await runSeed<User>(CreateBruce);
24-
fakeAuthenticationForUser(bruce, true);
2523
});
2624

2725
// -------------------------------------------------------------------------
@@ -40,7 +38,7 @@ describe('/api/users', () => {
4038
test('GET: / should return a list of users', async (done) => {
4139
const response = await request(settings.app)
4240
.get('/api/users')
43-
.set('Authorization', `Bearer 1234`)
41+
.set('Authorization', `Basic ${bruce.toBase64()}`)
4442
.expect('Content-Type', /json/)
4543
.expect(200);
4644

@@ -51,7 +49,7 @@ describe('/api/users', () => {
5149
test('GET: /:id should return bruce', async (done) => {
5250
const response = await request(settings.app)
5351
.get(`/api/users/${bruce.id}`)
54-
.set('Authorization', `Bearer 1234`)
52+
.set('Authorization', `Basic ${bruce.toBase64()}`)
5553
.expect('Content-Type', /json/)
5654
.expect(200);
5755

test/unit/auth/AuthService.test.ts

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,55 @@
11
import { Request } from 'express';
22
import * as MockExpressRequest from 'mock-express-request';
3-
import * as nock from 'nock';
4-
import * as request from 'request';
3+
import { User } from 'src/api/models/User';
54

65
import { AuthService } from '../../../src/auth/AuthService';
7-
import { env } from '../../../src/env';
86
import { LogMock } from '../lib/LogMock';
7+
import { RepositoryMock } from '../lib/RepositoryMock';
98

109
describe('AuthService', () => {
1110

1211
let authService: AuthService;
12+
let userRepository: RepositoryMock<User>;
1313
let log: LogMock;
1414
beforeEach(() => {
1515
log = new LogMock();
16-
authService = new AuthService(log);
16+
userRepository = new RepositoryMock<User>();
17+
authService = new AuthService(log, userRepository as any);
1718
});
1819

1920
describe('parseTokenFromRequest', () => {
20-
test('Should return the token without Bearer', () => {
21+
test('Should return the credentials of the basic authorization header', () => {
22+
const base64 = Buffer.from(`bruce:1234`).toString('base64');
2123
const req: Request = new MockExpressRequest({
2224
headers: {
23-
Authorization: 'Bearer 1234',
25+
Authorization: `Basic ${base64}`,
2426
},
2527
});
26-
const token = authService.parseTokenFromRequest(req);
27-
expect(token).toBe('1234');
28+
const credentials = authService.parseBasicAuthFromRequest(req);
29+
expect(credentials.username).toBe('bruce');
30+
expect(credentials.password).toBe('1234');
2831
});
2932

30-
test('Should return undefined if there is no Bearer', () => {
33+
test('Should return undefined if there is no basic authorization header', () => {
3134
const req: Request = new MockExpressRequest({
32-
headers: {
33-
Authorization: 'Basic 1234',
34-
},
35+
headers: {},
3536
});
36-
const token = authService.parseTokenFromRequest(req);
37+
const token = authService.parseBasicAuthFromRequest(req);
3738
expect(token).toBeUndefined();
3839
expect(log.infoMock).toBeCalledWith('No Token provided by the client', []);
3940
});
4041

41-
test('Should return undefined if there is no "Authorization" header', () => {
42-
const req: Request = new MockExpressRequest();
43-
const token = authService.parseTokenFromRequest(req);
42+
test('Should return undefined if there is a invalid basic authorization header', () => {
43+
const req: Request = new MockExpressRequest({
44+
headers: {
45+
Authorization: 'Basic 1234',
46+
},
47+
});
48+
const token = authService.parseBasicAuthFromRequest(req);
4449
expect(token).toBeUndefined();
4550
expect(log.infoMock).toBeCalledWith('No Token provided by the client', []);
4651
});
47-
});
4852

49-
describe('getTokenInfo', () => {
50-
test('Should get the tokeninfo', async (done) => {
51-
nock(env.auth.route)
52-
.post('')
53-
.reply(200, {
54-
user_id: 'auth0|test@test.com',
55-
});
56-
57-
const tokeninfo = await authService.getTokenInfo('1234');
58-
expect(tokeninfo.user_id).toBe('auth0|test@test.com');
59-
done();
60-
});
61-
62-
test('Should fail due to invalid token', async (done) => {
63-
nock(env.auth.route)
64-
.post('')
65-
.reply(401, 'Invalid token');
66-
67-
try {
68-
await authService.getTokenInfo('1234');
69-
} catch (error) {
70-
expect(error).toBe('Invalid token');
71-
}
72-
done();
73-
});
7453
});
7554

7655
});

test/unit/validations/UserValidations.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ describe('UserValidations', () => {
3636
user.firstName = 'TestName';
3737
user.lastName = 'TestName';
3838
user.email = 'test@test.com';
39+
user.username = 'test';
40+
user.password = '1234';
3941
const errors = await validate(user);
4042
expect(errors.length).toEqual(0);
4143
done();

0 commit comments

Comments
 (0)