@@ -6,6 +6,23 @@ import { OAuthConfig } from '@aws-amplify/core';
6
6
7
7
import { handleAuthApiRouteRequestForAppRouter } from '../../src/auth/handleAuthApiRouteRequestForAppRouter' ;
8
8
import { CreateAuthRoutesHandlersInput } from '../../src/auth/types' ;
9
+ import {
10
+ handleSignInCallbackRequest ,
11
+ handleSignInSignUpRequest ,
12
+ handleSignOutCallbackRequest ,
13
+ handleSignOutRequest ,
14
+ } from '../../src/auth/handlers' ;
15
+
16
+ jest . mock ( '../../src/auth/handlers' ) ;
17
+
18
+ const mockHandleSignInSignUpRequest = jest . mocked ( handleSignInSignUpRequest ) ;
19
+ const mockHandleSignOutRequest = jest . mocked ( handleSignOutRequest ) ;
20
+ const mockHandleSignInCallbackRequest = jest . mocked (
21
+ handleSignInCallbackRequest ,
22
+ ) ;
23
+ const mockHandleSignOutCallbackRequest = jest . mocked (
24
+ handleSignOutCallbackRequest ,
25
+ ) ;
9
26
10
27
describe ( 'handleAuthApiRouteRequestForAppRouter' , ( ) => {
11
28
const testOrigin = 'https://example.com' ;
@@ -23,17 +40,18 @@ describe('handleAuthApiRouteRequestForAppRouter', () => {
23
40
} ;
24
41
const _ = handleAuthApiRouteRequestForAppRouter ;
25
42
26
- it ( 'returns a 405 response when input.request has an unsupported method' , ( ) => {
43
+ it ( 'returns a 405 response when input.request has an unsupported method' , async ( ) => {
27
44
const request = new NextRequest (
28
45
new URL ( 'https://example.com/api/auth/sign-in' ) ,
29
46
{
30
47
method : 'POST' ,
31
48
} ,
32
49
) ;
33
- const response = handleAuthApiRouteRequestForAppRouter ( {
50
+ const response = await handleAuthApiRouteRequestForAppRouter ( {
34
51
request,
35
52
handlerContext : testHandlerContext ,
36
53
handlerInput : testHandlerInput ,
54
+ userPoolClientId : 'userPoolClientId' ,
37
55
oAuthConfig : testOAuthConfig ,
38
56
setCookieOptions : { } ,
39
57
origin : testOrigin ,
@@ -42,17 +60,18 @@ describe('handleAuthApiRouteRequestForAppRouter', () => {
42
60
expect ( response . status ) . toBe ( 405 ) ;
43
61
} ) ;
44
62
45
- it ( 'returns a 400 response when handlerContext.params.slug is undefined' , ( ) => {
63
+ it ( 'returns a 400 response when handlerContext.params.slug is undefined' , async ( ) => {
46
64
const request = new NextRequest (
47
65
new URL ( 'https://example.com/api/auth/sign-in' ) ,
48
66
{
49
67
method : 'GET' ,
50
68
} ,
51
69
) ;
52
- const response = handleAuthApiRouteRequestForAppRouter ( {
70
+ const response = await handleAuthApiRouteRequestForAppRouter ( {
53
71
request,
54
72
handlerContext : { params : { slug : undefined } } ,
55
73
handlerInput : testHandlerInput ,
74
+ userPoolClientId : 'userPoolClientId' ,
56
75
oAuthConfig : testOAuthConfig ,
57
76
setCookieOptions : { } ,
58
77
origin : testOrigin ,
@@ -61,17 +80,18 @@ describe('handleAuthApiRouteRequestForAppRouter', () => {
61
80
expect ( response . status ) . toBe ( 400 ) ;
62
81
} ) ;
63
82
64
- it ( 'returns a 404 response when handlerContext.params.slug is not a supported path' , ( ) => {
83
+ it ( 'returns a 404 response when handlerContext.params.slug is not a supported path' , async ( ) => {
65
84
const request = new NextRequest (
66
85
new URL ( 'https://example.com/api/auth/exchange-token' ) ,
67
86
{
68
87
method : 'GET' ,
69
88
} ,
70
89
) ;
71
- const response = handleAuthApiRouteRequestForAppRouter ( {
90
+ const response = await handleAuthApiRouteRequestForAppRouter ( {
72
91
request,
73
92
handlerContext : { params : { slug : 'exchange-token' } } ,
74
93
handlerInput : testHandlerInput ,
94
+ userPoolClientId : 'userPoolClientId' ,
75
95
oAuthConfig : testOAuthConfig ,
76
96
setCookieOptions : { } ,
77
97
origin : testOrigin ,
@@ -80,23 +100,135 @@ describe('handleAuthApiRouteRequestForAppRouter', () => {
80
100
expect ( response . status ) . toBe ( 404 ) ;
81
101
} ) ;
82
102
83
- // TODO(HuiSF): add use cases tests for each supported path when implemented
84
- it ( 'returns a 501 response when handlerContext.params.slug is a supported path' , ( ) => {
85
- const request = new NextRequest (
86
- new URL ( 'https://example.com/api/auth/sign-in' ) ,
103
+ test . each ( [
104
+ [ 'sign-in' , 'signIn' ] ,
105
+ [ 'sign-up' , 'signUp' ] ,
106
+ ] ) (
107
+ `calls handleSignInSignUpRequest with correct params when handlerContext.params.slug is %s` ,
108
+ async ( slug , expectedType ) => {
109
+ const mockRequest = new NextRequest (
110
+ new URL ( 'https://example.com/api/auth/sign-in' ) ,
111
+ {
112
+ method : 'GET' ,
113
+ } ,
114
+ ) ;
115
+ const mockResponse = new Response ( null , { status : 302 } ) ;
116
+
117
+ mockHandleSignInSignUpRequest . mockReturnValueOnce ( mockResponse ) ;
118
+
119
+ const response = await handleAuthApiRouteRequestForAppRouter ( {
120
+ request : mockRequest ,
121
+ handlerContext : { params : { slug } } ,
122
+ handlerInput : testHandlerInput ,
123
+ userPoolClientId : 'userPoolClientId' ,
124
+ oAuthConfig : testOAuthConfig ,
125
+ setCookieOptions : { } ,
126
+ origin : testOrigin ,
127
+ } ) ;
128
+
129
+ expect ( response ) . toBe ( mockResponse ) ;
130
+ expect ( mockHandleSignInSignUpRequest ) . toHaveBeenCalledWith ( {
131
+ request : mockRequest ,
132
+ userPoolClientId : 'userPoolClientId' ,
133
+ oAuthConfig : testOAuthConfig ,
134
+ customState : testHandlerInput . customState ,
135
+ origin : testOrigin ,
136
+ setCookieOptions : { } ,
137
+ type : expectedType ,
138
+ } ) ;
139
+ } ,
140
+ ) ;
141
+
142
+ it ( 'calls handleSignOutRequest with correct params when handlerContext.params.slug is sign-out' , async ( ) => {
143
+ const mockRequest = new NextRequest (
144
+ new URL ( 'https://example.com/api/auth/sign-out' ) ,
87
145
{
88
146
method : 'GET' ,
89
147
} ,
90
148
) ;
91
- const response = handleAuthApiRouteRequestForAppRouter ( {
92
- request,
93
- handlerContext : { params : { slug : 'sign-in' } } ,
149
+ const mockResponse = new Response ( null , { status : 302 } ) ;
150
+
151
+ mockHandleSignOutRequest . mockReturnValueOnce ( mockResponse ) ;
152
+
153
+ const response = await handleAuthApiRouteRequestForAppRouter ( {
154
+ request : mockRequest ,
155
+ handlerContext : { params : { slug : 'sign-out' } } ,
156
+ handlerInput : testHandlerInput ,
157
+ userPoolClientId : 'userPoolClientId' ,
158
+ oAuthConfig : testOAuthConfig ,
159
+ setCookieOptions : { } ,
160
+ origin : testOrigin ,
161
+ } ) ;
162
+
163
+ expect ( response ) . toBe ( mockResponse ) ;
164
+ expect ( mockHandleSignOutRequest ) . toHaveBeenCalledWith ( {
165
+ userPoolClientId : 'userPoolClientId' ,
166
+ oAuthConfig : testOAuthConfig ,
167
+ origin : testOrigin ,
168
+ setCookieOptions : { } ,
169
+ } ) ;
170
+ } ) ;
171
+
172
+ it ( 'calls handleSignInCallbackRequest with correct params when handlerContext.params.slug is sign-in-callback' , async ( ) => {
173
+ const mockRequest = new NextRequest (
174
+ new URL ( 'https://example.com/api/auth/sign-in-callback' ) ,
175
+ {
176
+ method : 'GET' ,
177
+ } ,
178
+ ) ;
179
+ const mockResponse = new Response ( null , { status : 302 } ) ;
180
+
181
+ mockHandleSignInCallbackRequest . mockResolvedValueOnce ( mockResponse ) ;
182
+
183
+ const response = await handleAuthApiRouteRequestForAppRouter ( {
184
+ request : mockRequest ,
185
+ handlerContext : { params : { slug : 'sign-in-callback' } } ,
186
+ handlerInput : testHandlerInput ,
187
+ userPoolClientId : 'userPoolClientId' ,
188
+ oAuthConfig : testOAuthConfig ,
189
+ setCookieOptions : { } ,
190
+ origin : testOrigin ,
191
+ } ) ;
192
+
193
+ expect ( response ) . toBe ( mockResponse ) ;
194
+ expect ( mockHandleSignInCallbackRequest ) . toHaveBeenCalledWith ( {
195
+ request : mockRequest ,
196
+ handlerInput : testHandlerInput ,
197
+ oAuthConfig : testOAuthConfig ,
198
+ origin : testOrigin ,
199
+ setCookieOptions : { } ,
200
+ userPoolClientId : 'userPoolClientId' ,
201
+ } ) ;
202
+ } ) ;
203
+
204
+ it ( 'calls handleSignOutCallbackRequest with correct params when handlerContext.params.slug is sign-out-callback' , async ( ) => {
205
+ const mockRequest = new NextRequest (
206
+ new URL ( 'https://example.com/api/auth/sign-out-callback' ) ,
207
+ {
208
+ method : 'GET' ,
209
+ } ,
210
+ ) ;
211
+ const mockResponse = new Response ( null , { status : 302 } ) ;
212
+
213
+ mockHandleSignOutCallbackRequest . mockResolvedValueOnce ( mockResponse ) ;
214
+
215
+ const response = await handleAuthApiRouteRequestForAppRouter ( {
216
+ request : mockRequest ,
217
+ handlerContext : { params : { slug : 'sign-out-callback' } } ,
94
218
handlerInput : testHandlerInput ,
219
+ userPoolClientId : 'userPoolClientId' ,
95
220
oAuthConfig : testOAuthConfig ,
96
221
setCookieOptions : { } ,
97
222
origin : testOrigin ,
98
223
} ) ;
99
224
100
- expect ( response . status ) . toBe ( 501 ) ;
225
+ expect ( response ) . toBe ( mockResponse ) ;
226
+ expect ( mockHandleSignOutCallbackRequest ) . toHaveBeenCalledWith ( {
227
+ request : mockRequest ,
228
+ handlerInput : testHandlerInput ,
229
+ oAuthConfig : testOAuthConfig ,
230
+ setCookieOptions : { } ,
231
+ userPoolClientId : 'userPoolClientId' ,
232
+ } ) ;
101
233
} ) ;
102
234
} ) ;
0 commit comments