|
| 1 | +import { ResourcesConfig } from 'aws-amplify'; |
| 2 | +import { |
| 3 | + assertOAuthConfig, |
| 4 | + assertTokenProviderConfig, |
| 5 | +} from 'aws-amplify/adapter-core/internals'; |
| 6 | + |
| 7 | +import { createAuthRouteHandlersFactory } from '../../src/auth/createAuthRouteHandlersFactory'; |
| 8 | +import { handleAuthApiRouteRequestForAppRouter } from '../../src/auth/handleAuthApiRouteRequestForAppRouter'; |
| 9 | +import { handleAuthApiRouteRequestForPagesRouter } from '../../src/auth/handleAuthApiRouteRequestForPagesRouter'; |
| 10 | +import { NextServer } from '../../src'; |
| 11 | +import { |
| 12 | + AuthRouteHandlers, |
| 13 | + CreateAuthRouteHandlersFactoryInput, |
| 14 | + CreateAuthRoutesHandlersInput, |
| 15 | +} from '../../src/auth/types'; |
| 16 | +import { |
| 17 | + isAuthRoutesHandlersContext, |
| 18 | + isNextApiRequest, |
| 19 | + isNextApiResponse, |
| 20 | + isNextRequest, |
| 21 | + isValidOrigin, |
| 22 | +} from '../../src/auth/utils'; |
| 23 | +import { globalSettings } from '../../src/utils'; |
| 24 | + |
| 25 | +jest.mock('aws-amplify/adapter-core/internals', () => ({ |
| 26 | + ...jest.requireActual('aws-amplify/adapter-core/internals'), |
| 27 | + assertOAuthConfig: jest.fn(), |
| 28 | + assertTokenProviderConfig: jest.fn(), |
| 29 | +})); |
| 30 | +jest.mock('../../src/auth/handleAuthApiRouteRequestForAppRouter'); |
| 31 | +jest.mock('../../src/auth/handleAuthApiRouteRequestForPagesRouter'); |
| 32 | +jest.mock('../../src/auth/utils'); |
| 33 | +jest.mock('../../src/utils', () => ({ |
| 34 | + globalSettings: { |
| 35 | + isServerSideAuthEnabled: jest.fn(() => true), |
| 36 | + enableServerSideAuth: jest.fn(), |
| 37 | + setRuntimeOptions: jest.fn(), |
| 38 | + getRuntimeOptions: jest.fn(() => ({ |
| 39 | + cookies: { |
| 40 | + sameSite: 'strict', |
| 41 | + }, |
| 42 | + })), |
| 43 | + isSSLOrigin: jest.fn(() => true), |
| 44 | + setIsSSLOrigin: jest.fn(), |
| 45 | + }, |
| 46 | +})); |
| 47 | + |
| 48 | +const mockAmplifyConfig: ResourcesConfig = { |
| 49 | + Auth: { |
| 50 | + Cognito: { |
| 51 | + identityPoolId: '123', |
| 52 | + userPoolId: 'abc', |
| 53 | + userPoolClientId: 'def', |
| 54 | + loginWith: { |
| 55 | + oauth: { |
| 56 | + domain: 'example.com', |
| 57 | + responseType: 'code', |
| 58 | + redirectSignIn: ['https://example.com/signin'], |
| 59 | + redirectSignOut: ['https://example.com/signout'], |
| 60 | + scopes: ['openid', 'email'], |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | +}; |
| 66 | + |
| 67 | +const mockAssertTokenProviderConfig = jest.mocked(assertTokenProviderConfig); |
| 68 | +const mockAssertOAuthConfig = jest.mocked(assertOAuthConfig); |
| 69 | +const mockHandleAuthApiRouteRequestForAppRouter = jest.mocked( |
| 70 | + handleAuthApiRouteRequestForAppRouter, |
| 71 | +); |
| 72 | +const mockHandleAuthApiRouteRequestForPagesRouter = jest.mocked( |
| 73 | + handleAuthApiRouteRequestForPagesRouter, |
| 74 | +); |
| 75 | +const mockIsNextApiRequest = jest.mocked(isNextApiRequest); |
| 76 | +const mockIsNextApiResponse = jest.mocked(isNextApiResponse); |
| 77 | +const mockIsNextRequest = jest.mocked(isNextRequest); |
| 78 | +const mockIsAuthRoutesHandlersContext = jest.mocked( |
| 79 | + isAuthRoutesHandlersContext, |
| 80 | +); |
| 81 | +const mockIsValidOrigin = jest.mocked(isValidOrigin); |
| 82 | +const mockRunWithAmplifyServerContext = |
| 83 | + jest.fn() as jest.MockedFunction<NextServer.RunOperationWithContext>; |
| 84 | + |
| 85 | +describe('createAuthRoutesHandlersFactory', () => { |
| 86 | + const AMPLIFY_APP_ORIGIN = 'https://example.com'; |
| 87 | + |
| 88 | + beforeAll(() => { |
| 89 | + mockIsValidOrigin.mockReturnValue(true); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('the created createAuthRouteHandlers function', () => { |
| 93 | + it('throws an error if the AMPLIFY_APP_ORIGIN environment variable is not defined', () => { |
| 94 | + const throwingFunc = createAuthRouteHandlersFactory({ |
| 95 | + config: mockAmplifyConfig, |
| 96 | + amplifyAppOrigin: undefined, |
| 97 | + runWithAmplifyServerContext: mockRunWithAmplifyServerContext, |
| 98 | + globalSettings, |
| 99 | + }); |
| 100 | + expect(() => throwingFunc()).toThrow( |
| 101 | + 'Could not find the AMPLIFY_APP_ORIGIN environment variable.', |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it('throws an error if the AMPLIFY_APP_ORIGIN environment variable is invalid', () => { |
| 106 | + mockIsValidOrigin.mockReturnValueOnce(false); |
| 107 | + const throwingFunc = createAuthRouteHandlersFactory({ |
| 108 | + config: mockAmplifyConfig, |
| 109 | + amplifyAppOrigin: 'domain-without-protocol.com', |
| 110 | + runWithAmplifyServerContext: mockRunWithAmplifyServerContext, |
| 111 | + globalSettings, |
| 112 | + }); |
| 113 | + expect(() => throwingFunc()).toThrow( |
| 114 | + 'AMPLIFY_APP_ORIGIN environment variable contains an invalid origin string.', |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('calls config assertion functions to validate the Auth configuration', () => { |
| 119 | + const func = createAuthRouteHandlersFactory({ |
| 120 | + config: mockAmplifyConfig, |
| 121 | + amplifyAppOrigin: AMPLIFY_APP_ORIGIN, |
| 122 | + runWithAmplifyServerContext: mockRunWithAmplifyServerContext, |
| 123 | + globalSettings, |
| 124 | + }); |
| 125 | + |
| 126 | + func(); |
| 127 | + |
| 128 | + expect(mockAssertTokenProviderConfig).toHaveBeenCalledWith( |
| 129 | + mockAmplifyConfig.Auth?.Cognito, |
| 130 | + ); |
| 131 | + expect(mockAssertOAuthConfig).toHaveBeenCalledWith( |
| 132 | + mockAmplifyConfig.Auth!.Cognito, |
| 133 | + ); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('the created route handler function', () => { |
| 138 | + const testCreateAuthRoutesHandlersFactoryInput: CreateAuthRouteHandlersFactoryInput = |
| 139 | + { |
| 140 | + config: mockAmplifyConfig, |
| 141 | + amplifyAppOrigin: AMPLIFY_APP_ORIGIN, |
| 142 | + runWithAmplifyServerContext: mockRunWithAmplifyServerContext, |
| 143 | + globalSettings, |
| 144 | + }; |
| 145 | + const testCreateAuthRoutesHandlersInput: CreateAuthRoutesHandlersInput = { |
| 146 | + customState: 'random-state', |
| 147 | + redirectOnSignInComplete: '/home', |
| 148 | + redirectOnSignOutComplete: '/login', |
| 149 | + }; |
| 150 | + let handler: AuthRouteHandlers; |
| 151 | + |
| 152 | + beforeAll(() => { |
| 153 | + const createAuthRoutesHandlers = createAuthRouteHandlersFactory( |
| 154 | + testCreateAuthRoutesHandlersFactoryInput, |
| 155 | + ); |
| 156 | + handler = createAuthRoutesHandlers(testCreateAuthRoutesHandlersInput); |
| 157 | + }); |
| 158 | + |
| 159 | + afterEach(() => { |
| 160 | + mockIsAuthRoutesHandlersContext.mockReset(); |
| 161 | + mockIsNextApiRequest.mockReset(); |
| 162 | + mockIsNextApiResponse.mockReset(); |
| 163 | + mockIsNextRequest.mockReset(); |
| 164 | + }); |
| 165 | + |
| 166 | + it('calls handleAuthApiRouteRequestForPagesRouter when 1st param is a NextApiRequest and 2nd param is a NextApiResponse', async () => { |
| 167 | + const param1 = {} as any; |
| 168 | + const param2 = {} as any; |
| 169 | + mockIsNextApiRequest.mockReturnValueOnce(true); |
| 170 | + mockIsNextApiResponse.mockReturnValueOnce(true); |
| 171 | + mockIsNextRequest.mockReturnValueOnce(false); |
| 172 | + mockIsAuthRoutesHandlersContext.mockReturnValueOnce(false); |
| 173 | + |
| 174 | + await handler(param1, param2); |
| 175 | + |
| 176 | + expect(mockHandleAuthApiRouteRequestForPagesRouter).toHaveBeenCalledWith({ |
| 177 | + request: param1, |
| 178 | + response: param2, |
| 179 | + handlerInput: testCreateAuthRoutesHandlersInput, |
| 180 | + oAuthConfig: mockAmplifyConfig.Auth!.Cognito!.loginWith!.oauth, |
| 181 | + setCookieOptions: { |
| 182 | + sameSite: 'strict', |
| 183 | + }, |
| 184 | + origin: 'https://example.com', |
| 185 | + userPoolClientId: 'def', |
| 186 | + runWithAmplifyServerContext: mockRunWithAmplifyServerContext, |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + it('calls handleAuthApiRouteRequestForAppRouter when 1st param is a NextRequest and the 2nd param is a AuthRoutesHandlersContext', async () => { |
| 191 | + const request = {} as any; |
| 192 | + const context = {} as any; |
| 193 | + mockIsNextApiRequest.mockReturnValueOnce(false); |
| 194 | + mockIsNextApiResponse.mockReturnValueOnce(false); |
| 195 | + mockIsNextRequest.mockReturnValueOnce(true); |
| 196 | + mockIsAuthRoutesHandlersContext.mockReturnValueOnce(true); |
| 197 | + |
| 198 | + await handler(request, context); |
| 199 | + |
| 200 | + expect(mockHandleAuthApiRouteRequestForAppRouter).toHaveBeenCalledWith({ |
| 201 | + request, |
| 202 | + handlerContext: context, |
| 203 | + handlerInput: testCreateAuthRoutesHandlersInput, |
| 204 | + oAuthConfig: mockAmplifyConfig.Auth!.Cognito!.loginWith!.oauth, |
| 205 | + setCookieOptions: { |
| 206 | + sameSite: 'strict', |
| 207 | + }, |
| 208 | + origin: 'https://example.com', |
| 209 | + userPoolClientId: 'def', |
| 210 | + runWithAmplifyServerContext: mockRunWithAmplifyServerContext, |
| 211 | + }); |
| 212 | + }); |
| 213 | + |
| 214 | + it('throws an error when the request and context/response combination is invalid', () => { |
| 215 | + const request = {} as any; |
| 216 | + const context = {} as any; |
| 217 | + mockIsNextApiRequest.mockReturnValueOnce(false); |
| 218 | + mockIsNextApiResponse.mockReturnValueOnce(false); |
| 219 | + mockIsNextRequest.mockReturnValueOnce(false); |
| 220 | + mockIsAuthRoutesHandlersContext.mockReturnValueOnce(false); |
| 221 | + |
| 222 | + expect(handler(request, context)).rejects.toThrow( |
| 223 | + 'Invalid request and context/response combination. The request cannot be handled.', |
| 224 | + ); |
| 225 | + }); |
| 226 | + |
| 227 | + it('uses default values for parameters that have values as undefined', async () => { |
| 228 | + (globalSettings.getRuntimeOptions as jest.Mock).mockReturnValueOnce({}); |
| 229 | + const createAuthRoutesHandlers = createAuthRouteHandlersFactory({ |
| 230 | + config: mockAmplifyConfig, |
| 231 | + amplifyAppOrigin: AMPLIFY_APP_ORIGIN, |
| 232 | + runWithAmplifyServerContext: mockRunWithAmplifyServerContext, |
| 233 | + globalSettings, |
| 234 | + }); |
| 235 | + const handlerWithDefaultParamValues = |
| 236 | + createAuthRoutesHandlers(/* undefined */); |
| 237 | + |
| 238 | + const request = {} as any; |
| 239 | + const response = {} as any; |
| 240 | + |
| 241 | + mockIsNextApiRequest.mockReturnValueOnce(true); |
| 242 | + mockIsNextApiResponse.mockReturnValueOnce(true); |
| 243 | + mockIsNextRequest.mockReturnValueOnce(false); |
| 244 | + mockIsAuthRoutesHandlersContext.mockReturnValueOnce(false); |
| 245 | + |
| 246 | + await handlerWithDefaultParamValues(request, response); |
| 247 | + |
| 248 | + expect(handleAuthApiRouteRequestForPagesRouter).toHaveBeenCalledWith({ |
| 249 | + request, |
| 250 | + response, |
| 251 | + handlerInput: {}, |
| 252 | + oAuthConfig: mockAmplifyConfig.Auth!.Cognito!.loginWith!.oauth, |
| 253 | + setCookieOptions: {}, |
| 254 | + origin: 'https://example.com', |
| 255 | + userPoolClientId: 'def', |
| 256 | + runWithAmplifyServerContext: mockRunWithAmplifyServerContext, |
| 257 | + }); |
| 258 | + }); |
| 259 | + }); |
| 260 | +}); |
0 commit comments