|
| 1 | +/** |
| 2 | + * We don't need the DOM for this specific test suite. |
| 3 | + * |
| 4 | + * @jest-environment node |
| 5 | + */ |
| 6 | + |
| 7 | +import { Auth0Client, Auth0ClientOptions } from '../../src'; |
| 8 | +import { |
| 9 | + TEST_CLIENT_ID, |
| 10 | + TEST_DOMAIN, |
| 11 | + TEST_DPOP_NONCE, |
| 12 | + TEST_DPOP_PROOF |
| 13 | +} from '../constants'; |
| 14 | + |
| 15 | +import { beforeEach, describe, expect } from '@jest/globals'; |
| 16 | +import { Dpop } from '../../src/dpop/dpop'; |
| 17 | + |
| 18 | +function newTestAuth0Client( |
| 19 | + extraOpts?: Partial<Auth0ClientOptions> |
| 20 | +): Auth0Client { |
| 21 | + return new Auth0Client({ |
| 22 | + ...extraOpts, |
| 23 | + domain: TEST_DOMAIN, |
| 24 | + clientId: TEST_CLIENT_ID |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +describe('Auth0Client', () => { |
| 29 | + beforeEach(() => { |
| 30 | + jest.resetAllMocks(); |
| 31 | + jest.restoreAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('_assertDpop()', () => { |
| 35 | + const auth0 = newTestAuth0Client(); |
| 36 | + |
| 37 | + describe('DPoP disabled', () => { |
| 38 | + it('throws an error', () => |
| 39 | + expect(() => auth0['_assertDpop'](undefined)).toThrow( |
| 40 | + '`useDpop` option must be enabled before using DPoP.' |
| 41 | + )); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('DPoP enabled', () => { |
| 45 | + const dpop = new Dpop(TEST_CLIENT_ID); |
| 46 | + |
| 47 | + it('does not throw', () => |
| 48 | + expect(() => auth0['_assertDpop'](dpop)).not.toThrow()); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('getDpopNonce()', () => { |
| 53 | + const id = 'my_custom_api'; |
| 54 | + const auth0 = newTestAuth0Client({ useDpop: true }); |
| 55 | + const dpop = auth0['dpop']!; |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + auth0['_assertDpop'] = jest.fn(); |
| 59 | + jest.spyOn(dpop, 'getNonce').mockResolvedValue(TEST_DPOP_NONCE); |
| 60 | + }); |
| 61 | + |
| 62 | + let output: unknown; |
| 63 | + |
| 64 | + beforeEach(async () => { |
| 65 | + output = await auth0.getDpopNonce(id); |
| 66 | + }); |
| 67 | + |
| 68 | + it('asserts DPoP is enabled', () => |
| 69 | + expect(auth0['_assertDpop']).toHaveBeenCalled()); |
| 70 | + |
| 71 | + it('delegates into Dpop.getNonce()', () => { |
| 72 | + expect(dpop.getNonce).toHaveBeenCalledWith(id); |
| 73 | + expect(output).toBe(TEST_DPOP_NONCE); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('setDpopNonce()', () => { |
| 78 | + const id = 'my_custom_api'; |
| 79 | + const auth0 = newTestAuth0Client({ useDpop: true }); |
| 80 | + const dpop = auth0['dpop']!; |
| 81 | + |
| 82 | + beforeEach(() => { |
| 83 | + auth0['_assertDpop'] = jest.fn(); |
| 84 | + jest.spyOn(dpop, 'setNonce').mockResolvedValue(); |
| 85 | + }); |
| 86 | + |
| 87 | + beforeEach(() => auth0.setDpopNonce(TEST_DPOP_NONCE, id)); |
| 88 | + |
| 89 | + it('asserts DPoP is enabled', () => |
| 90 | + expect(auth0['_assertDpop']).toHaveBeenCalled()); |
| 91 | + |
| 92 | + it('delegates into Dpop.setNonce()', () => |
| 93 | + expect(dpop.setNonce).toHaveBeenCalledWith(TEST_DPOP_NONCE, id)); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('generateDpopProof()', () => { |
| 97 | + const auth0 = newTestAuth0Client({ useDpop: true }); |
| 98 | + const dpop = auth0['dpop']!; |
| 99 | + |
| 100 | + const params: Parameters<Auth0Client['generateDpopProof']>[0] = { |
| 101 | + accessToken: 'test-access-token', |
| 102 | + method: 'test-method', |
| 103 | + url: 'test-url', |
| 104 | + nonce: 'test-nonce' |
| 105 | + }; |
| 106 | + |
| 107 | + beforeEach(() => { |
| 108 | + auth0['_assertDpop'] = jest.fn(); |
| 109 | + jest.spyOn(dpop, 'generateProof').mockResolvedValue(TEST_DPOP_PROOF); |
| 110 | + }); |
| 111 | + |
| 112 | + let output: string; |
| 113 | + |
| 114 | + beforeEach(async () => { |
| 115 | + output = await auth0.generateDpopProof(params); |
| 116 | + }); |
| 117 | + |
| 118 | + it('asserts DPoP is enabled', () => |
| 119 | + expect(auth0['_assertDpop']).toHaveBeenCalled()); |
| 120 | + |
| 121 | + it('delegates into Dpop.generateProof()', () => |
| 122 | + expect(dpop.generateProof).toHaveBeenCalledWith(params)); |
| 123 | + |
| 124 | + it('returns the proof', () => expect(output).toBe(TEST_DPOP_PROOF)); |
| 125 | + }); |
| 126 | +}); |
0 commit comments