|
| 1 | +import { scenario } from '@testduet/given-when-then'; |
| 2 | +import type RemoveBearerInAuthorizationTokenType from './removeBearerInAuthorizationToken'; |
| 3 | + |
| 4 | +let removeBearerInAuthorizationToken: typeof RemoveBearerInAuthorizationTokenType; |
| 5 | +let consoleWarn: jest.SpyInstance; |
| 6 | + |
| 7 | +beforeAll(() => { |
| 8 | + // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 9 | + consoleWarn = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 10 | + |
| 11 | + jest.mock('@msinternal/botframework-webchat-base/utils', () => ({ |
| 12 | + ...jest.requireActual('@msinternal/botframework-webchat-base/utils'), |
| 13 | + warnOnce: |
| 14 | + (...args) => |
| 15 | + () => |
| 16 | + console.warn(...args) |
| 17 | + })); |
| 18 | + |
| 19 | + removeBearerInAuthorizationToken = require('./removeBearerInAuthorizationToken').default; |
| 20 | +}); |
| 21 | + |
| 22 | +afterEach(() => consoleWarn.mockClear()); |
| 23 | + |
| 24 | +scenario('Authorization token prefixed with "Bearer" in ', bdd => { |
| 25 | + bdd |
| 26 | + .given('resolved value', () => ({ |
| 27 | + authorizationToken: 'Bearer XYZ', |
| 28 | + region: 'westus' |
| 29 | + })) |
| 30 | + .when('removeBearerInAuthorizationToken', precondition => removeBearerInAuthorizationToken(precondition)) |
| 31 | + .then('should match snapshot', (_, result) => |
| 32 | + expect(result).toEqual({ |
| 33 | + authorizationToken: 'XYZ', |
| 34 | + region: 'westus' |
| 35 | + }) |
| 36 | + ) |
| 37 | + .and('should have warned', () => expect(consoleWarn).toHaveBeenCalledTimes(1)); |
| 38 | + |
| 39 | + bdd |
| 40 | + .given('Promise value', () => |
| 41 | + Promise.resolve({ |
| 42 | + authorizationToken: 'Bearer XYZ', |
| 43 | + region: 'westus' |
| 44 | + }) |
| 45 | + ) |
| 46 | + .when('removeBearerInAuthorizationToken', precondition => removeBearerInAuthorizationToken(precondition)) |
| 47 | + .then('should match snapshot', (_, result) => |
| 48 | + expect(result).toEqual({ |
| 49 | + authorizationToken: 'XYZ', |
| 50 | + region: 'westus' |
| 51 | + }) |
| 52 | + ) |
| 53 | + .and('should have warned', () => expect(consoleWarn).toHaveBeenCalledTimes(1)); |
| 54 | + |
| 55 | + bdd |
| 56 | + .given('callback of a resolved value', () => () => ({ |
| 57 | + authorizationToken: 'Bearer XYZ', |
| 58 | + region: 'westus' |
| 59 | + })) |
| 60 | + .when('removeBearerInAuthorizationToken', precondition => (removeBearerInAuthorizationToken(precondition) as any)()) |
| 61 | + .then('should match snapshot', (_, result) => |
| 62 | + expect(result).toEqual({ |
| 63 | + authorizationToken: 'XYZ', |
| 64 | + region: 'westus' |
| 65 | + }) |
| 66 | + ) |
| 67 | + .and('should have warned', () => expect(consoleWarn).toHaveBeenCalledTimes(1)); |
| 68 | + |
| 69 | + bdd |
| 70 | + .given( |
| 71 | + 'callback of a Promise', |
| 72 | + () => () => |
| 73 | + Promise.resolve({ |
| 74 | + authorizationToken: 'Bearer XYZ', |
| 75 | + region: 'westus' |
| 76 | + }) |
| 77 | + ) |
| 78 | + .when('removeBearerInAuthorizationToken', precondition => (removeBearerInAuthorizationToken(precondition) as any)()) |
| 79 | + .then('should match snapshot', async (_, result) => |
| 80 | + expect(await result).toEqual({ |
| 81 | + authorizationToken: 'XYZ', |
| 82 | + region: 'westus' |
| 83 | + }) |
| 84 | + ) |
| 85 | + .and('should have warned', () => expect(consoleWarn).toHaveBeenCalledTimes(1)); |
| 86 | +}); |
| 87 | + |
| 88 | +scenario('Authorization token with no prefix in ', bdd => { |
| 89 | + bdd |
| 90 | + .given('resolved value', () => ({ |
| 91 | + authorizationToken: 'XYZ', |
| 92 | + region: 'westus' |
| 93 | + })) |
| 94 | + .when('removeBearerInAuthorizationToken', precondition => removeBearerInAuthorizationToken(precondition)) |
| 95 | + .then('should left untouched', (precondition, result) => expect(result).toBe(precondition)) |
| 96 | + .and('should not have warned', () => expect(consoleWarn).not.toHaveBeenCalled()); |
| 97 | + |
| 98 | + bdd |
| 99 | + .given('Promise value', () => |
| 100 | + Promise.resolve({ |
| 101 | + authorizationToken: 'XYZ', |
| 102 | + region: 'westus' |
| 103 | + }) |
| 104 | + ) |
| 105 | + .when('removeBearerInAuthorizationToken', precondition => removeBearerInAuthorizationToken(precondition)) |
| 106 | + .then('should match snapshot', (_, result) => |
| 107 | + expect(result).toEqual({ |
| 108 | + authorizationToken: 'XYZ', |
| 109 | + region: 'westus' |
| 110 | + }) |
| 111 | + ) |
| 112 | + .and('should not have warned', () => expect(consoleWarn).not.toHaveBeenCalled()); |
| 113 | + |
| 114 | + bdd |
| 115 | + .given('callback of a resolved value', () => () => ({ |
| 116 | + authorizationToken: 'XYZ', |
| 117 | + region: 'westus' |
| 118 | + })) |
| 119 | + .when('removeBearerInAuthorizationToken', precondition => (removeBearerInAuthorizationToken(precondition) as any)()) |
| 120 | + .then('should match snapshot', (_, result) => |
| 121 | + expect(result).toEqual({ |
| 122 | + authorizationToken: 'XYZ', |
| 123 | + region: 'westus' |
| 124 | + }) |
| 125 | + ) |
| 126 | + .and('should not have warned', () => expect(consoleWarn).not.toHaveBeenCalled()); |
| 127 | + |
| 128 | + bdd |
| 129 | + .given( |
| 130 | + 'callback of a Promise', |
| 131 | + () => () => |
| 132 | + Promise.resolve({ |
| 133 | + authorizationToken: 'XYZ', |
| 134 | + region: 'westus' |
| 135 | + }) |
| 136 | + ) |
| 137 | + .when('removeBearerInAuthorizationToken', precondition => (removeBearerInAuthorizationToken(precondition) as any)()) |
| 138 | + .then('should match snapshot', async (_, result) => |
| 139 | + expect(await result).toEqual({ |
| 140 | + authorizationToken: 'XYZ', |
| 141 | + region: 'westus' |
| 142 | + }) |
| 143 | + ) |
| 144 | + .and('should not have warned', () => expect(consoleWarn).not.toHaveBeenCalled()); |
| 145 | +}); |
0 commit comments