|
1 | 1 | import { getAuthUrl } from '@utils/get-auth-url'; |
2 | 2 |
|
3 | 3 | describe('getAuthUrl', () => { |
4 | | - const originalEnv = process.env; |
5 | | - |
6 | | - beforeEach(() => { |
7 | | - jest.resetModules(); |
8 | | - process.env = { ...originalEnv }; |
9 | | - }); |
| 4 | + const originalWindow = { ...global.window }; |
| 5 | + const originalEnv = { ...process.env }; |
10 | 6 |
|
11 | 7 | afterAll(() => { |
12 | | - process.env = originalEnv; |
| 8 | + Object.defineProperty(process, 'env', { |
| 9 | + value: originalEnv, |
| 10 | + configurable: true, |
| 11 | + }); |
| 12 | + |
| 13 | + Object.defineProperty(global, 'window', { |
| 14 | + value: originalWindow, |
| 15 | + configurable: true, |
| 16 | + }); |
13 | 17 | }); |
14 | 18 |
|
15 | | - describe('in production', () => { |
| 19 | + describe('client side (when window is available)', () => { |
16 | 20 | beforeEach(() => { |
17 | | - Object.defineProperty(process.env, 'NODE_ENV', { |
18 | | - value: 'production', |
| 21 | + Object.defineProperty(global, 'window', { |
| 22 | + value: { |
| 23 | + location: { |
| 24 | + protocol: 'https:', |
| 25 | + host: 'nhsnotify.national.nhs.uk', |
| 26 | + }, |
| 27 | + }, |
19 | 28 | configurable: true, |
20 | 29 | }); |
21 | | - process.env.NOTIFY_DOMAIN_NAME = 'nhsnotify.nhs.uk'; |
22 | 30 | }); |
23 | 31 |
|
24 | | - it('should use https protocol', () => { |
25 | | - const result = getAuthUrl('/auth'); |
26 | | - expect(result).toBe('https://nhsnotify.nhs.uk/auth'); |
| 32 | + afterAll(() => { |
| 33 | + Object.defineProperty(global, 'window', { |
| 34 | + value: originalWindow, |
| 35 | + configurable: true, |
| 36 | + }); |
27 | 37 | }); |
28 | 38 |
|
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'); |
| 39 | + it('should construct URL', () => { |
| 40 | + const result = getAuthUrl('/auth'); |
| 41 | + expect(result).toBe('https://nhsnotify.national.nhs.uk/auth'); |
32 | 42 | }); |
33 | 43 |
|
34 | 44 | it('should handle query parameters', () => { |
35 | 45 | const result = getAuthUrl('/auth?redirect=%2Ftemplates%2Fcreate'); |
| 46 | + |
36 | 47 | expect(result).toBe( |
37 | | - 'https://nhsnotify.nhs.uk/auth?redirect=%2Ftemplates%2Fcreate' |
| 48 | + 'https://nhsnotify.national.nhs.uk/auth?redirect=%2Ftemplates%2Fcreate' |
38 | 49 | ); |
39 | 50 | }); |
40 | 51 |
|
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'); |
| 52 | + describe('in development env', () => { |
| 53 | + beforeEach(() => { |
| 54 | + Object.defineProperty(process.env, 'NODE_ENV', { |
| 55 | + value: 'development', |
| 56 | + configurable: true, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + afterAll(() => { |
| 61 | + Object.defineProperty(process.env, 'NODE_ENV', { |
| 62 | + value: originalEnv, |
| 63 | + configurable: true, |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should include the base path set', () => { |
| 68 | + process.env.NEXT_PUBLIC_BASE_PATH = '/base-path'; |
| 69 | + |
| 70 | + const result = getAuthUrl('/auth'); |
| 71 | + expect(result).toBe('https://nhsnotify.national.nhs.uk/base-path/auth'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should fallback to templates when no base path environment variable provided', () => { |
| 75 | + delete process.env.NEXT_PUBLIC_BASE_PATH; |
| 76 | + |
| 77 | + const result = getAuthUrl('/auth'); |
| 78 | + expect(result).toBe('https://nhsnotify.national.nhs.uk/templates/auth'); |
| 79 | + }); |
45 | 80 | }); |
46 | 81 | }); |
47 | 82 |
|
48 | | - describe('in development', () => { |
| 83 | + describe('when window is not available', () => { |
49 | 84 | beforeEach(() => { |
50 | | - Object.defineProperty(process.env, 'NODE_ENV', { |
51 | | - value: 'development', |
52 | | - configurable: true, |
53 | | - }); |
| 85 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 86 | + delete (global as any).window; |
54 | 87 | }); |
55 | 88 |
|
56 | | - it('should use http protocol', () => { |
57 | | - const result = getAuthUrl('/auth'); |
58 | | - expect(result).toBe('http://localhost:3000/templates/auth'); |
| 89 | + afterAll(() => { |
| 90 | + global.window = originalWindow; |
59 | 91 | }); |
60 | 92 |
|
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 | | - }); |
| 93 | + describe('when gateway URL environment variable is available', () => { |
| 94 | + beforeEach(() => { |
| 95 | + process.env.NEXT_PUBLIC_GATEWAY_URL = |
| 96 | + 'https://dev.web-gateway.nhsnotify.national.nhs.uk'; |
| 97 | + }); |
66 | 98 |
|
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'); |
| 99 | + it('should use NEXT_PUBLIC_GATEWAY_URL', () => { |
| 100 | + const result = getAuthUrl('/auth'); |
| 101 | + expect(result).toBe( |
| 102 | + 'https://dev.web-gateway.nhsnotify.national.nhs.uk/auth' |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should handle query parameters', () => { |
| 107 | + const result = getAuthUrl('/auth?redirect=%2Ftemplates%2Fcreate'); |
| 108 | + expect(result).toBe( |
| 109 | + 'https://dev.web-gateway.nhsnotify.national.nhs.uk/auth?redirect=%2Ftemplates%2Fcreate' |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('in development env', () => { |
| 114 | + beforeEach(() => { |
| 115 | + Object.defineProperty(process.env, 'NODE_ENV', { |
| 116 | + value: 'development', |
| 117 | + configurable: true, |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + afterAll(() => { |
| 122 | + Object.defineProperty(process.env, 'NODE_ENV', { |
| 123 | + value: originalEnv, |
| 124 | + configurable: true, |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should include the base path set', () => { |
| 129 | + process.env.NEXT_PUBLIC_BASE_PATH = '/base-path'; |
| 130 | + |
| 131 | + const result = getAuthUrl('/auth'); |
| 132 | + expect(result).toBe( |
| 133 | + 'https://dev.web-gateway.nhsnotify.national.nhs.uk/base-path/auth' |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should fallback to templates when no base path environment variable provided', () => { |
| 138 | + delete process.env.NEXT_PUBLIC_BASE_PATH; |
| 139 | + |
| 140 | + const result = getAuthUrl('/auth'); |
| 141 | + expect(result).toBe( |
| 142 | + 'https://dev.web-gateway.nhsnotify.national.nhs.uk/templates/auth' |
| 143 | + ); |
| 144 | + }); |
| 145 | + }); |
71 | 146 | }); |
72 | 147 |
|
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 | | - ); |
| 148 | + describe('when no gateway URL environment variable is available', () => { |
| 149 | + beforeEach(() => { |
| 150 | + delete process.env.NEXT_PUBLIC_GATEWAY_URL; |
| 151 | + }); |
| 152 | + |
| 153 | + it('should fallback to localhost:3000', () => { |
| 154 | + const result = getAuthUrl('/auth'); |
| 155 | + expect(result).toBe('http://localhost:3000/auth'); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('in development env', () => { |
| 159 | + beforeEach(() => { |
| 160 | + Object.defineProperty(process.env, 'NODE_ENV', { |
| 161 | + value: 'development', |
| 162 | + configurable: true, |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + afterAll(() => { |
| 167 | + Object.defineProperty(process.env, 'NODE_ENV', { |
| 168 | + value: originalEnv, |
| 169 | + configurable: true, |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should include the base path set', () => { |
| 174 | + process.env.NEXT_PUBLIC_BASE_PATH = '/base-path'; |
| 175 | + |
| 176 | + const result = getAuthUrl('/auth'); |
| 177 | + expect(result).toBe('http://localhost:3000/base-path/auth'); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should fallback to templates when no base path environment variable provided', () => { |
| 181 | + delete process.env.NEXT_PUBLIC_BASE_PATH; |
| 182 | + |
| 183 | + const result = getAuthUrl('/auth'); |
| 184 | + expect(result).toBe('http://localhost:3000/templates/auth'); |
| 185 | + }); |
| 186 | + }); |
78 | 187 | }); |
79 | 188 | }); |
80 | 189 | }); |
0 commit comments