|
| 1 | +const jwt = require('jsonwebtoken'); |
| 2 | +const { v4: uuidv4 } = require('uuid'); |
| 3 | +const { EmulatorValidation, AuthenticationConstants } = require('../..'); |
| 4 | +const { strictEqual } = require('assert'); |
| 5 | + |
| 6 | +function generateMockBearerToken(issuer) { |
| 7 | + const secretKey = 'ThisIsASuperMockSecretKey123456789'; |
| 8 | + const tenantId = uuidv4(); |
| 9 | + const completeIssuer = issuer ? issuer.replace('{0}', tenantId) : null; |
| 10 | + |
| 11 | + const payload = { |
| 12 | + name: 'John Doe', |
| 13 | + role: 'Admin', |
| 14 | + tid: tenantId, |
| 15 | + iss: completeIssuer, |
| 16 | + aud: 'https://api.example.com', |
| 17 | + }; |
| 18 | + |
| 19 | + const options = { |
| 20 | + expiresIn: '1h', |
| 21 | + }; |
| 22 | + |
| 23 | + return `Bearer ${jwt.sign(payload, secretKey, options)}`; |
| 24 | +} |
| 25 | + |
| 26 | +describe('EmulatorValidation', function () { |
| 27 | + it('should return false on token BadFormat', function () { |
| 28 | + // Token with bad format is not processed |
| 29 | + let authHeader = ''; |
| 30 | + strictEqual(EmulatorValidation.isTokenFromEmulator(authHeader), false); |
| 31 | + |
| 32 | + // If the token doesn't contain an issuer value, it returns false |
| 33 | + authHeader = generateMockBearerToken(null); |
| 34 | + strictEqual(EmulatorValidation.isTokenFromEmulator(authHeader), false); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return false on invalid token issuer', function () { |
| 38 | + const authHeader = generateMockBearerToken('https://mockIssuer.com'); |
| 39 | + strictEqual(EmulatorValidation.isTokenFromEmulator(authHeader), false); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return true on valid token issuer', function () { |
| 43 | + // Validate issuer with V1 Token |
| 44 | + let authHeader = generateMockBearerToken(`${AuthenticationConstants.ValidTokenIssuerUrlTemplateV1}{0}/`); |
| 45 | + strictEqual(EmulatorValidation.isTokenFromEmulator(authHeader), true); |
| 46 | + |
| 47 | + // Validate issuer with V2 Token |
| 48 | + authHeader = generateMockBearerToken(`${AuthenticationConstants.ValidTokenIssuerUrlTemplateV2}{0}/v2.0`); |
| 49 | + strictEqual(EmulatorValidation.isTokenFromEmulator(authHeader), true); |
| 50 | + |
| 51 | + // Validate Government issuer with V1 Token |
| 52 | + authHeader = generateMockBearerToken(`${AuthenticationConstants.ValidGovernmentTokenIssuerUrlTemplateV1}{0}/`); |
| 53 | + strictEqual(EmulatorValidation.isTokenFromEmulator(authHeader), true); |
| 54 | + |
| 55 | + // Validate Government issuer with V2 Token |
| 56 | + authHeader = generateMockBearerToken( |
| 57 | + `${AuthenticationConstants.ValidGovernmentTokenIssuerUrlTemplateV2}{0}/v2.0`, |
| 58 | + ); |
| 59 | + strictEqual(EmulatorValidation.isTokenFromEmulator(authHeader), true); |
| 60 | + }); |
| 61 | +}); |
0 commit comments