-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathhasActiveUserSession.test.ts
More file actions
132 lines (110 loc) · 4.11 KB
/
Copy pathhasActiveUserSession.test.ts
File metadata and controls
132 lines (110 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { getCurrentUser } from 'aws-amplify/auth/server';
import { NextRequest } from 'next/server';
import { AuthUser } from 'aws-amplify/auth';
import { NextApiRequest } from 'next';
import { AmplifyContext } from '@aws-amplify/core';
import {
hasActiveUserSessionWithAppRouter,
hasActiveUserSessionWithPagesRouter,
} from '../../../src/auth/utils/hasActiveUserSession';
import { NextServer } from '../../../src/types';
import { createMockNextApiResponse } from '../testUtils';
jest.mock('aws-amplify/auth/server');
const mockRunWithAmplifyServerContext =
jest.fn() as jest.MockedFunction<NextServer.RunOperationWithContext>;
const mockGetCurrentUser = jest.mocked(getCurrentUser);
describe('hasUserSignedIn', () => {
const mockContextSpec: AmplifyContext = {
resourcesConfig: {},
libraryOptions: {},
fetchAuthSession: jest.fn(),
clearCredentials: jest.fn(),
getTokens: jest.fn(),
};
const mockCurrentUserResult: AuthUser = {
userId: 'mockUserId',
username: 'mockUsername',
};
beforeAll(() => {
mockRunWithAmplifyServerContext.mockImplementation(
async ({ nextServerContext: _, operation }) => {
return operation(mockContextSpec);
},
);
mockGetCurrentUser.mockResolvedValue(mockCurrentUserResult);
});
afterEach(() => {
mockRunWithAmplifyServerContext.mockClear();
mockGetCurrentUser.mockClear();
});
describe('hasUserSignedInWithAppRouter', () => {
const mockRequest = new NextRequest('https://example.com/api/auth/sign-in');
it('invokes server getCurrentUser() with expected parameter within the injected runWithAmplifyServerContext function', async () => {
await hasActiveUserSessionWithAppRouter({
request: mockRequest,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
});
expect(mockRunWithAmplifyServerContext).toHaveBeenCalledWith({
nextServerContext: {
request: mockRequest,
response: expect.any(Response),
},
operation: expect.any(Function),
});
expect(mockGetCurrentUser).toHaveBeenCalledWith(mockContextSpec);
});
it('returns true when getCurrentUser() resolves (returned auth tokens)', async () => {
const result = await hasActiveUserSessionWithAppRouter({
request: mockRequest,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
});
expect(result).toBe(true);
});
it('returns false when getCurrentUser() rejects (no auth tokens)', async () => {
mockGetCurrentUser.mockRejectedValueOnce(new Error('No current user'));
const result = await hasActiveUserSessionWithAppRouter({
request: mockRequest,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
});
expect(result).toBe(false);
});
});
describe('hasUserSignedInWithPagesRouter', () => {
const mockRequest = {
url: 'https://example.com/api/auth/sign-in',
} as unknown as NextApiRequest;
const { mockResponse } = createMockNextApiResponse();
it('invokes server getCurrentUser() with expected parameter within the injected runWithAmplifyServerContext function', async () => {
await hasActiveUserSessionWithPagesRouter({
request: mockRequest,
response: mockResponse,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
});
expect(mockRunWithAmplifyServerContext).toHaveBeenCalledWith({
nextServerContext: {
request: mockRequest,
response: mockResponse,
},
operation: expect.any(Function),
});
expect(mockGetCurrentUser).toHaveBeenCalledWith(mockContextSpec);
});
it('returns true when getCurrentUser() resolves (returned auth tokens)', async () => {
const result = await hasActiveUserSessionWithPagesRouter({
request: mockRequest,
response: mockResponse,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
});
expect(result).toBe(true);
});
it('returns false when getCurrentUser() rejects (no auth tokens)', async () => {
mockGetCurrentUser.mockRejectedValueOnce(new Error('No current user'));
const result = await hasActiveUserSessionWithPagesRouter({
request: mockRequest,
response: mockResponse,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
});
expect(result).toBe(false);
});
});
});