-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathIIIFAuthentication.test.jsx
More file actions
115 lines (113 loc) · 4.86 KB
/
IIIFAuthentication.test.jsx
File metadata and controls
115 lines (113 loc) · 4.86 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
import { screen, render, waitFor } from '@tests/utils/test-utils';
import userEvent from '@testing-library/user-event';
import { IIIFAuthentication } from '../../../src/components/IIIFAuthentication';
/**
* Helper function to create IIIFAuthentication
*/
function createWrapper(props) {
return render(
<IIIFAuthentication
accessTokenServiceId="http://example.com/token"
authServiceId="http://example.com/auth"
failureDescription="... and this is why."
failureHeader="Login failed"
handleAuthInteraction={() => {}}
isInteractive
logoutServiceId="http://example.com/logout"
resetAuthenticationState={() => {}}
resolveAccessTokenRequest={() => {}}
resolveAuthenticationRequest={() => {}}
windowId="w"
{...props}
/>,
);
}
describe('IIIFAuthentication', () => {
let user;
beforeEach(() => {
user = userEvent.setup();
});
describe('without an auth service', () => {
it('renders nothing', () => {
createWrapper({ authServiceId: null });
expect(screen.queryByRole('button', { name: 'Log in' })).not.toBeInTheDocument();
expect(screen.queryByRole('button')).not.toBeInTheDocument();
});
});
describe('with an available auth service', () => {
it('renders a login bar', async () => {
const handleAuthInteraction = vi.fn();
createWrapper({ handleAuthInteraction });
await user.click(screen.getByRole('button', { name: 'Log in' }));
expect(handleAuthInteraction).toHaveBeenCalledWith('w', 'http://example.com/auth');
});
it('renders nothing for a non-interactive login', () => {
createWrapper({ isInteractive: false });
expect(screen.queryByText('Log in')).not.toBeInTheDocument();
});
});
describe('with a failed authentication', () => {
it('renders with an error message', async () => {
const handleAuthInteraction = vi.fn();
createWrapper({ handleAuthInteraction, status: 'failed' });
await user.click(screen.getByRole('button', { name: 'Continue' }));
const confirmBtn = screen.getByRole('button', { name: /Retry/ });
expect(screen.getByText('Login failed')).toBeInTheDocument();
expect(screen.getByText('Cancel')).toBeInTheDocument();
expect(screen.getByText('... and this is why.')).toBeInTheDocument();
await user.click(confirmBtn);
expect(handleAuthInteraction).toHaveBeenCalledWith('w', 'http://example.com/auth');
});
});
describe('with a failed external authentication', () => {
it('renders with an error message', async () => {
const handleAuthInteraction = vi.fn();
createWrapper({
accessTokenServiceId: null, handleAuthInteraction, isInteractive: false, status: 'failed',
});
expect(screen.getByText('Login failed')).toBeInTheDocument();
expect(screen.getByText('... and this is why.')).toBeInTheDocument();
});
});
describe('in the middle of authenticating', () => {
it('does the IIIF access cookie behavior', async () => {
const mockWindow = { close: vi.fn(), closed: false };
const mockWindowOpen = vi.fn(() => (mockWindow));
window.open = mockWindowOpen;
const resolveCookieMock = vi.fn();
createWrapper({ resolveAuthenticationRequest: resolveCookieMock, status: 'cookie' });
expect(screen.getByRole('button', { name: 'Log in' })).toBeInTheDocument();
expect(mockWindowOpen).toHaveBeenCalledWith(`http://example.com/auth?origin=${window.origin}`, 'IiifLoginSender', 'centerscreen');
mockWindow.closed = true;
await waitFor(() => expect(resolveCookieMock).toHaveBeenCalledTimes(1));
});
it('does the IIIF access token behavior', async () => {
const resolveTokenMock = vi.fn();
createWrapper({ resolveAccessTokenRequest: resolveTokenMock, status: 'token' });
expect(screen.getByRole('button', { name: 'Log in' })).toBeInTheDocument();
window.dispatchEvent(new MessageEvent('message', {
data: { messageId: 'http://example.com/token' },
}));
await waitFor(() => expect(resolveTokenMock).toHaveBeenCalledWith('http://example.com/auth', 'http://example.com/token', { messageId: 'http://example.com/token' }));
});
});
describe('when logged in', () => {
it('renders a logout button', async () => {
const mockWindow = { open: vi.fn() };
const mockWindowOpen = vi.fn(() => (mockWindow));
window.open = mockWindowOpen;
const resetAuthenticationState = vi.fn();
createWrapper({
logoutConfirm: 'exit',
openWindow: mockWindowOpen,
resetAuthenticationState,
status: 'ok',
});
const confirmBtn = await screen.findByRole('button', { name: 'exit' });
await user.click(confirmBtn);
await waitFor(() => expect(resetAuthenticationState).toHaveBeenCalledWith({
authServiceId: 'http://example.com/auth', tokenServiceId: 'http://example.com/token',
}));
});
});
});