-
Notifications
You must be signed in to change notification settings - Fork 250
/
with-authentication-required.test.tsx
225 lines (213 loc) · 7.75 KB
/
with-authentication-required.test.tsx
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import '@testing-library/jest-dom/extend-expect';
import React from 'react';
import withAuthenticationRequired from '../src/with-authentication-required';
import { render, screen, waitFor, act } from '@testing-library/react';
import { Auth0Client} from '@auth0/auth0-spa-js';
import Auth0Provider from '../src/auth0-provider';
import { Auth0ContextInterface, initialContext } from '../src/auth0-context';
const mockClient = jest.mocked(new Auth0Client({ clientId: '', domain: '' }));
describe('withAuthenticationRequired', () => {
it('should block access to a private component when not authenticated', async () => {
mockClient.getUser.mockResolvedValue(undefined);
const MyComponent = (): JSX.Element => <>Private</>;
const WrappedComponent = withAuthenticationRequired(MyComponent);
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
);
await waitFor(() =>
expect(mockClient.loginWithRedirect).toHaveBeenCalled()
);
expect(screen.queryByText('Private')).not.toBeInTheDocument();
});
it('should allow access to a private component when authenticated', async () => {
mockClient.getUser.mockResolvedValue({ name: '__test_user__' });
const MyComponent = (): JSX.Element => <>Private</>;
const WrappedComponent = withAuthenticationRequired(MyComponent);
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
);
await waitFor(() =>
expect(mockClient.loginWithRedirect).not.toHaveBeenCalled()
);
await waitFor(() =>
expect(screen.getByText('Private')).toBeInTheDocument()
);
});
it('should show a custom redirecting message', async () => {
mockClient.getUser.mockResolvedValue(
Promise.resolve({ name: '__test_user__' })
);
const MyComponent = (): JSX.Element => <>Private</>;
const OnRedirecting = (): JSX.Element => <>Redirecting</>;
const WrappedComponent = withAuthenticationRequired(MyComponent, {
onRedirecting: OnRedirecting,
});
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
);
await waitFor(() =>
expect(screen.getByText('Redirecting')).toBeInTheDocument()
);
await waitFor(() =>
expect(mockClient.loginWithRedirect).not.toHaveBeenCalled()
);
await waitFor(() =>
expect(screen.queryByText('Redirecting')).not.toBeInTheDocument()
);
});
it('should pass additional options on to loginWithRedirect', async () => {
mockClient.getUser.mockResolvedValue(undefined);
const MyComponent = (): JSX.Element => <>Private</>;
const WrappedComponent = withAuthenticationRequired(MyComponent, {
loginOptions: {
fragment: 'foo',
},
});
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
);
await waitFor(() =>
expect(mockClient.loginWithRedirect).toHaveBeenCalledWith(
expect.objectContaining({
fragment: 'foo',
})
)
);
});
it('should merge additional appState with the returnTo', async () => {
mockClient.getUser.mockResolvedValue(undefined);
const MyComponent = (): JSX.Element => <>Private</>;
const WrappedComponent = withAuthenticationRequired(MyComponent, {
loginOptions: {
appState: {
foo: 'bar',
},
},
returnTo: '/baz',
});
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
);
await waitFor(() =>
expect(mockClient.loginWithRedirect).toHaveBeenCalledWith(
expect.objectContaining({
appState: {
foo: 'bar',
returnTo: '/baz',
},
})
)
);
});
it('should accept a returnTo function', async () => {
mockClient.getUser.mockResolvedValue(undefined);
const MyComponent = (): JSX.Element => <>Private</>;
const WrappedComponent = withAuthenticationRequired(MyComponent, {
returnTo: () => '/foo',
});
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
);
await waitFor(() =>
expect(mockClient.loginWithRedirect).toHaveBeenCalledWith(
expect.objectContaining({
appState: {
returnTo: '/foo',
},
})
)
);
});
it('should call loginWithRedirect only once even if parent state changes', async () => {
mockClient.getUser.mockResolvedValue(undefined);
const MyComponent = (): JSX.Element => <>Private</>;
const WrappedComponent = withAuthenticationRequired(MyComponent);
const App = ({ foo }: { foo: number }): JSX.Element => (
<div>
{foo}
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
</div>
);
const { rerender } = render(<App foo={1} />);
await waitFor(() =>
expect(mockClient.loginWithRedirect).toHaveBeenCalled()
);
mockClient.loginWithRedirect.mockClear();
rerender(<App foo={2} />);
await waitFor(() =>
expect(mockClient.loginWithRedirect).not.toHaveBeenCalled()
);
});
it('should provide access when the provider associated with the context is authenticated', async () => {
// Calls happen up the tree, i.e the nested Auth0Provider will get a return value and the top level will get undefined
mockClient.getUser.mockResolvedValueOnce({ name: '__test_user__' });
mockClient.getUser.mockResolvedValueOnce(undefined);
const context = React.createContext<Auth0ContextInterface>(initialContext);
const MyComponent = (): JSX.Element => <>Private</>;
const WrappedComponent = withAuthenticationRequired(MyComponent, {
context,
});
await act(() => {
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<Auth0Provider
clientId="__test_client_id__"
domain="__test_domain__"
context={context}
>
<WrappedComponent />
</Auth0Provider>
</Auth0Provider>
);
});
await waitFor(() =>
expect(mockClient.loginWithRedirect).not.toHaveBeenCalled()
);
// There should be one call per provider
expect(mockClient.getUser).toHaveBeenCalledTimes(2);
expect(screen.queryByText('Private')).toBeInTheDocument();
});
it('should block access when the provider associated with the context is not authenticated', async () => {
// Calls happen up the tree, i.e the nested Auth0Provider will get undefined and the top level will get a return value
mockClient.getUser.mockResolvedValueOnce(undefined);
mockClient.getUser.mockResolvedValueOnce({ name: '__test_user__' });
const context = React.createContext<Auth0ContextInterface>(initialContext);
const MyComponent = (): JSX.Element => <>Private</>;
const WrappedComponent = withAuthenticationRequired(MyComponent, {
context,
});
await act(() => {
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<Auth0Provider
clientId="__test_client_id__"
domain="__test_domain__"
context={context}
>
<WrappedComponent />
</Auth0Provider>
</Auth0Provider>
);
});
await waitFor(() =>
expect(mockClient.loginWithRedirect).toHaveBeenCalled()
);
// There should be one call per provider
expect(mockClient.getUser).toHaveBeenCalledTimes(2);
expect(screen.queryByText('Private')).not.toBeInTheDocument();
});
});