Skip to content

Commit 3c93635

Browse files
authored
chore(graphql-server): Fix errors in useRequireAuth test (#422)
1 parent 75e24a0 commit 3c93635

File tree

1 file changed

+15
-52
lines changed

1 file changed

+15
-52
lines changed

packages/graphql-server/src/functions/__tests__/useRequireAuth.test.ts

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ export const mockedAuthenticationEvent = ({
5959
}
6060
}
6161

62-
const handler = async (
63-
_event: APIGatewayEvent,
64-
_context?: Context,
65-
): Promise<any> => {
62+
const handler = async () => {
6663
// @MARK
6764
// Don't use globalContext until beforeAll runs
6865
const globalContext = (await import('@cedarjs/context')).context
@@ -77,10 +74,7 @@ const handler = async (
7774
}
7875
}
7976

80-
const handlerWithAuthChecks = async (
81-
_event: APIGatewayEvent,
82-
_context: Context,
83-
): Promise<unknown> => {
77+
const handlerWithAuthChecks = async () => {
8478
// TODO: Add requireAuth('role') here
8579
// or isAuthenticated()
8680

@@ -111,10 +105,7 @@ const handlerWithAuthChecks = async (
111105
}
112106
}
113107

114-
const handlerWithError = async (
115-
_event: APIGatewayEvent,
116-
_context: Context,
117-
): Promise<any> => {
108+
const handlerWithError = async () => {
118109
// @MARK
119110
// Don't use globalContext until beforeAll runs
120111
const globalContext = (await import('@cedarjs/context')).context
@@ -131,20 +122,20 @@ const handlerWithError = async (
131122
body: JSON.stringify(currentUser),
132123
}
133124
} catch (error) {
125+
const errorMsg =
126+
error instanceof AuthenticationError ? error.message : 'Unknown error'
127+
134128
return {
135129
statusCode: 500,
136130
headers: {
137131
'Content-Type': 'application/json',
138132
},
139-
body: JSON.stringify({ error: error.message }),
133+
body: JSON.stringify({ error: errorMsg }),
140134
}
141135
}
142136
}
143137

144-
const getCurrentUserWithError = async (
145-
_decoded: unknown,
146-
{ token: _token }: { token: string },
147-
): Promise<RedwoodUser> => {
138+
const getCurrentUserWithError = async (): Promise<RedwoodUser> => {
148139
throw Error('Something went wrong getting the user info')
149140
}
150141

@@ -160,11 +151,7 @@ describe('useRequireAuth', () => {
160151
const handlerEnrichedWithAuthentication = useRequireAuth({
161152
handlerFn: handler,
162153
getCurrentUser,
163-
authDecoder: async (
164-
token: string,
165-
_type: string,
166-
_req: { event: APIGatewayEvent; context?: Context },
167-
) => {
154+
authDecoder: async (token) => {
168155
return { token }
169156
},
170157
})
@@ -194,11 +181,7 @@ describe('useRequireAuth', () => {
194181
const handlerEnrichedWithAuthentication = useRequireAuth({
195182
handlerFn: handler,
196183
getCurrentUser,
197-
authDecoder: async (
198-
token: string,
199-
_type: string,
200-
_req: { event: APIGatewayEvent; context?: Context },
201-
) => {
184+
authDecoder: async (token) => {
202185
return jwt.decode(token) as Record<string, any>
203186
},
204187
})
@@ -261,11 +244,7 @@ describe('useRequireAuth', () => {
261244
const handlerEnrichedWithAuthentication = useRequireAuth({
262245
handlerFn: handler,
263246
getCurrentUser,
264-
authDecoder: async (
265-
_token: string,
266-
_type: string,
267-
_req: { event: APIGatewayEvent; context: Context },
268-
) => {
247+
authDecoder: async () => {
269248
return null
270249
},
271250
})
@@ -290,11 +269,7 @@ describe('useRequireAuth', () => {
290269
const handlerEnrichedWithAuthentication = useRequireAuth({
291270
handlerFn: handler,
292271
getCurrentUser,
293-
authDecoder: async (
294-
token: string,
295-
_type: string,
296-
_req: { event: APIGatewayEvent; context: Context },
297-
) => {
272+
authDecoder: async (token) => {
298273
return { token }
299274
},
300275
})
@@ -323,11 +298,7 @@ describe('useRequireAuth', () => {
323298
const handlerEnrichedWithAuthentication = useRequireAuth({
324299
handlerFn: handler,
325300
getCurrentUser,
326-
authDecoder: async (
327-
token: string,
328-
_type: string,
329-
_req: { event: APIGatewayEvent; context: Context },
330-
) => {
301+
authDecoder: async (token) => {
331302
return jwt.decode(token) as Record<string, any>
332303
},
333304
})
@@ -482,11 +453,7 @@ describe('useRequireAuth', () => {
482453
const handlerEnrichedWithAuthentication = useRequireAuth({
483454
handlerFn: handlerWithAuthChecks,
484455
getCurrentUser,
485-
authDecoder: async (
486-
token: string,
487-
_type: string,
488-
_req: { event: APIGatewayEvent; context: Context },
489-
) => {
456+
authDecoder: async (token) => {
490457
return jwt.decode(token) as Record<string, any>
491458
},
492459
})
@@ -522,11 +489,7 @@ describe('useRequireAuth', () => {
522489
const handlerEnrichedWithAuthentication = useRequireAuth({
523490
handlerFn: handlerWithAuthChecks,
524491
getCurrentUser,
525-
authDecoder: async (
526-
token: string,
527-
_type: string,
528-
_req: { event: APIGatewayEvent; context: Context },
529-
) => {
492+
authDecoder: async (token) => {
530493
return jwt.decode(token) as Record<string, any>
531494
},
532495
})

0 commit comments

Comments
 (0)