|
1 | 1 | // vim: ft=javascript tabstop=2 softtabstop=2 expandtab shiftwidth=2
|
2 | 2 | const test = require('ava'),
|
3 | 3 | nock = require('nock'),
|
4 |
| - authenticator = require('../server/api-authenticator'); |
| 4 | + authenticator = require('../server/api-authenticator'), |
| 5 | + fakeRequest = {headers: {host: 'http://example.com'}}; |
5 | 6 |
|
6 | 7 | test('valid key passes', t => {
|
7 | 8 | const key = 'some-secret-key',
|
8 | 9 | authServerUri = 'http://example.com',
|
9 | 10 | response = {sub: 'userinfo', iss: 'iss'};
|
10 |
| - nock(`${authServerUri}`).get(`/key/${key}`).reply(200, response); |
| 11 | + nock(authServerUri).get(`/key/${key}`).reply(200, response); |
11 | 12 | const authenticate = authenticator.getAuthenticator(authServerUri);
|
12 |
| - return authenticate(key).then(clientInfo => { |
13 |
| - t.deepEqual(clientInfo, response); |
| 13 | + return authenticate(key, fakeRequest).then(clientInfo => { |
| 14 | + t.deepEqual(clientInfo, {authenticated: true, token: response}); |
14 | 15 | });
|
15 | 16 | });
|
16 | 17 |
|
| 18 | +test('auth is skipped for selected hostnames even when the key is invalid', t => { |
| 19 | + const key = 'some-secret-key', |
| 20 | + authServerUri = 'http://example.com', |
| 21 | + authenticate = authenticator.getAuthenticator(authServerUri, 'a.example.com,b.example.com'), |
| 22 | + request = {headers: {host: 'b.example.com'}}, |
| 23 | + promises = []; |
| 24 | + |
| 25 | + promises.push( |
| 26 | + authenticate(key, request).then(clientInfo => { |
| 27 | + t.is(clientInfo.authenticated, false); |
| 28 | + }) |
| 29 | + ); |
| 30 | + // nock(authServerUri).get(`/key/${key}`).reply(422); |
| 31 | + // promises.push( |
| 32 | + // authenticate(key, fakeRequest).catch((err) => { |
| 33 | + // t.assert(err instanceof authenticator.InvalidKey) |
| 34 | + // }) |
| 35 | + // ); |
| 36 | + return Promise.all(promises); |
| 37 | +}); |
| 38 | + |
17 | 39 | test('fail on invalid key', t => {
|
18 | 40 | const key = 'some-secret-key',
|
19 | 41 | authServerUri = 'http://example.com';
|
20 | 42 | nock(`${authServerUri}`).get(`/key/${key}`).reply(422);
|
21 |
| - const authenticate = authenticator.getAuthenticator(authServerUri); |
22 |
| - return authenticate(key).catch((err) => { |
| 43 | + const authenticate = authenticator.getAuthenticator(authServerUri, 'a.example.com,b.example.com'); |
| 44 | + return authenticate(key, fakeRequest).catch((err) => { |
23 | 45 | t.assert(err instanceof authenticator.InvalidKey)
|
24 | 46 | });
|
25 | 47 | });
|
26 | 48 |
|
27 | 49 |
|
| 50 | + |
28 | 51 | test('authentication rejected on server error', t => {
|
29 | 52 | const key = 'some-secret-key',
|
30 | 53 | authServerUri = 'http://example.com';
|
31 | 54 | nock(`${authServerUri}`).get(`/key/${key}`).reply(500);
|
32 | 55 | const authenticate = authenticator.getAuthenticator(authServerUri);
|
33 |
| - return authenticate(key).catch((err) => { |
| 56 | + return authenticate(key, fakeRequest).catch((err) => { |
34 | 57 | t.assert(!(err instanceof authenticator.InvalidKey))
|
35 | 58 | });
|
36 | 59 | });
|
0 commit comments