|
| 1 | +import { getAuthUrl } from '@utils/get-auth-url'; |
| 2 | + |
| 3 | +describe('getAuthUrl', () => { |
| 4 | + const originalEnv = process.env; |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + jest.resetModules(); |
| 8 | + process.env = { ...originalEnv }; |
| 9 | + }); |
| 10 | + |
| 11 | + afterAll(() => { |
| 12 | + process.env = originalEnv; |
| 13 | + }); |
| 14 | + |
| 15 | + describe('in production', () => { |
| 16 | + beforeEach(() => { |
| 17 | + Object.defineProperty(process.env, 'NODE_ENV', { |
| 18 | + value: 'production', |
| 19 | + configurable: true, |
| 20 | + }); |
| 21 | + process.env.NOTIFY_DOMAIN_NAME = 'nhsnotify.nhs.uk'; |
| 22 | + }); |
| 23 | + |
| 24 | + it('should use https protocol', () => { |
| 25 | + const result = getAuthUrl('/auth'); |
| 26 | + expect(result).toBe('https://nhsnotify.nhs.uk/auth'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should not include basePath for auth app URLs', () => { |
| 30 | + const result = getAuthUrl('/auth/signout'); |
| 31 | + expect(result).toBe('https://nhsnotify.nhs.uk/auth/signout'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should handle query parameters', () => { |
| 35 | + const result = getAuthUrl('/auth?redirect=%2Ftemplates%2Fcreate'); |
| 36 | + expect(result).toBe( |
| 37 | + 'https://nhsnotify.nhs.uk/auth?redirect=%2Ftemplates%2Fcreate' |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should fallback to localhost when NOTIFY_DOMAIN_NAME is not set', () => { |
| 42 | + delete process.env.NOTIFY_DOMAIN_NAME; |
| 43 | + const result = getAuthUrl('/auth'); |
| 44 | + expect(result).toBe('https://localhost:3000/auth'); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('in development', () => { |
| 49 | + beforeEach(() => { |
| 50 | + Object.defineProperty(process.env, 'NODE_ENV', { |
| 51 | + value: 'development', |
| 52 | + configurable: true, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should use http protocol', () => { |
| 57 | + const result = getAuthUrl('/auth'); |
| 58 | + expect(result).toBe('http://localhost:3000/templates/auth'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should include basePath to hit local auth pages', () => { |
| 62 | + process.env.NEXT_PUBLIC_BASE_PATH = '/templates'; |
| 63 | + const result = getAuthUrl('/auth'); |
| 64 | + expect(result).toBe('http://localhost:3000/templates/auth'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should fallback to /templates basePath when NEXT_PUBLIC_BASE_PATH is undefined', () => { |
| 68 | + delete process.env.NEXT_PUBLIC_BASE_PATH; |
| 69 | + const result = getAuthUrl('/auth'); |
| 70 | + expect(result).toBe('http://localhost:3000/templates/auth'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should handle query parameters with basePath', () => { |
| 74 | + const result = getAuthUrl('/auth?redirect=%2Ftemplates%2Fcreate'); |
| 75 | + expect(result).toBe( |
| 76 | + 'http://localhost:3000/templates/auth?redirect=%2Ftemplates%2Fcreate' |
| 77 | + ); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments