|
1 | 1 | import { Request } from 'express'; |
2 | 2 | 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'; |
5 | 4 |
|
6 | 5 | import { AuthService } from '../../../src/auth/AuthService'; |
7 | | -import { env } from '../../../src/env'; |
8 | 6 | import { LogMock } from '../lib/LogMock'; |
| 7 | +import { RepositoryMock } from '../lib/RepositoryMock'; |
9 | 8 |
|
10 | 9 | describe('AuthService', () => { |
11 | 10 |
|
12 | 11 | let authService: AuthService; |
| 12 | + let userRepository: RepositoryMock<User>; |
13 | 13 | let log: LogMock; |
14 | 14 | beforeEach(() => { |
15 | 15 | log = new LogMock(); |
16 | | - authService = new AuthService(log); |
| 16 | + userRepository = new RepositoryMock<User>(); |
| 17 | + authService = new AuthService(log, userRepository as any); |
17 | 18 | }); |
18 | 19 |
|
19 | 20 | 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'); |
21 | 23 | const req: Request = new MockExpressRequest({ |
22 | 24 | headers: { |
23 | | - Authorization: 'Bearer 1234', |
| 25 | + Authorization: `Basic ${base64}`, |
24 | 26 | }, |
25 | 27 | }); |
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'); |
28 | 31 | }); |
29 | 32 |
|
30 | | - test('Should return undefined if there is no Bearer', () => { |
| 33 | + test('Should return undefined if there is no basic authorization header', () => { |
31 | 34 | const req: Request = new MockExpressRequest({ |
32 | | - headers: { |
33 | | - Authorization: 'Basic 1234', |
34 | | - }, |
| 35 | + headers: {}, |
35 | 36 | }); |
36 | | - const token = authService.parseTokenFromRequest(req); |
| 37 | + const token = authService.parseBasicAuthFromRequest(req); |
37 | 38 | expect(token).toBeUndefined(); |
38 | 39 | expect(log.infoMock).toBeCalledWith('No Token provided by the client', []); |
39 | 40 | }); |
40 | 41 |
|
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); |
44 | 49 | expect(token).toBeUndefined(); |
45 | 50 | expect(log.infoMock).toBeCalledWith('No Token provided by the client', []); |
46 | 51 | }); |
47 | | - }); |
48 | 52 |
|
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 | | - }); |
74 | 53 | }); |
75 | 54 |
|
76 | 55 | }); |
0 commit comments