-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathLoginWithDeviceFlow.test.tsx
More file actions
162 lines (129 loc) · 5.28 KB
/
LoginWithDeviceFlow.test.tsx
File metadata and controls
162 lines (129 loc) · 5.28 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
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
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { navigateMock, renderWithAppContext } from '../__helpers__/test-utils';
import * as comms from '../utils/system/comms';
import { LoginWithDeviceFlowRoute } from './LoginWithDeviceFlow';
describe('renderer/routes/LoginWithDeviceFlow.tsx', () => {
const copyToClipboardSpy = vi
.spyOn(comms, 'copyToClipboard')
.mockResolvedValue();
const openExternalLinkSpy = vi
.spyOn(comms, 'openExternalLink')
.mockImplementation(vi.fn());
beforeEach(() => {
vi.clearAllMocks();
});
it('should render scope choice buttons', async () => {
const loginWithDeviceFlowStartMock = vi.fn().mockResolvedValueOnce({
hostname: 'github.com',
clientId: 'test-id',
deviceCode: 'device-code',
userCode: 'USER-1234',
verificationUri: 'https://github.com/login/device',
intervalSeconds: 5,
expiresAt: Date.now() + 900000,
});
renderWithAppContext(<LoginWithDeviceFlowRoute />, {
loginWithDeviceFlowStart: loginWithDeviceFlowStartMock,
});
expect(screen.getByText('Receive notifications for:')).toBeInTheDocument();
expect(screen.getByTestId('device-scope-public')).toBeInTheDocument();
expect(screen.getByTestId('device-scope-full')).toBeInTheDocument();
// Device flow should not start until user makes a choice
expect(loginWithDeviceFlowStartMock).not.toHaveBeenCalled();
});
it('should start device flow with public scope when clicking Public', async () => {
const loginWithDeviceFlowStartMock = vi.fn().mockResolvedValueOnce({
hostname: 'github.com',
clientId: 'test-id',
deviceCode: 'device-code',
userCode: 'USER-1234',
verificationUri: 'https://github.com/login/device',
intervalSeconds: 5,
expiresAt: Date.now() + 900000,
});
renderWithAppContext(<LoginWithDeviceFlowRoute />, {
loginWithDeviceFlowStart: loginWithDeviceFlowStartMock,
});
await userEvent.click(screen.getByTestId('device-scope-public'));
expect(loginWithDeviceFlowStartMock).toHaveBeenCalledWith(undefined, [
'notifications',
'read:user',
'public_repo',
]);
await screen.findByTestId('device-user-code');
expect(screen.getByTestId('device-verification-link')).toBeInTheDocument();
// Verify auto-copy and auto-open were called
expect(copyToClipboardSpy).toHaveBeenCalledWith('USER-1234');
expect(openExternalLinkSpy).toHaveBeenCalledWith(
'https://github.com/login/device',
);
});
it('should start device flow with full scope when clicking Public and Private', async () => {
const loginWithDeviceFlowStartMock = vi.fn().mockResolvedValueOnce({
hostname: 'github.com',
clientId: 'test-id',
deviceCode: 'device-code',
userCode: 'USER-1234',
verificationUri: 'https://github.com/login/device',
intervalSeconds: 5,
expiresAt: Date.now() + 900000,
});
renderWithAppContext(<LoginWithDeviceFlowRoute />, {
loginWithDeviceFlowStart: loginWithDeviceFlowStartMock,
});
await userEvent.click(screen.getByTestId('device-scope-full'));
expect(loginWithDeviceFlowStartMock).toHaveBeenCalledWith(undefined, [
'notifications',
'read:user',
'repo',
]);
await screen.findByTestId('device-user-code');
});
it('should copy user code to clipboard when clicking copy button', async () => {
const loginWithDeviceFlowStartMock = vi.fn().mockResolvedValueOnce({
hostname: 'github.com',
clientId: 'test-id',
deviceCode: 'device-code',
userCode: 'USER-1234',
verificationUri: 'https://github.com/login/device',
intervalSeconds: 5,
expiresAt: Date.now() + 900000,
});
renderWithAppContext(<LoginWithDeviceFlowRoute />, {
loginWithDeviceFlowStart: loginWithDeviceFlowStartMock,
});
await userEvent.click(screen.getByTestId('device-scope-public'));
await screen.findByTestId('device-user-code');
// Clear the auto-copy call from initialization
copyToClipboardSpy.mockClear();
await userEvent.click(screen.getByTestId('copy-device-code'));
expect(copyToClipboardSpy).toHaveBeenCalledWith('USER-1234');
});
it('should handle device flow errors during initialization', async () => {
const loginWithDeviceFlowStartMock = vi
.fn()
.mockRejectedValueOnce(new Error('Network error'));
renderWithAppContext(<LoginWithDeviceFlowRoute />, {
loginWithDeviceFlowStart: loginWithDeviceFlowStartMock,
});
await userEvent.click(screen.getByTestId('device-scope-full'));
await screen.findByText(/Failed to start authentication/);
});
it('should navigate back on cancel from scope choice', async () => {
const loginWithDeviceFlowStartMock = vi.fn().mockResolvedValueOnce({
hostname: 'github.com',
clientId: 'test-id',
deviceCode: 'device-code',
userCode: 'USER-1234',
verificationUri: 'https://github.com/login/device',
intervalSeconds: 5,
expiresAt: Date.now() + 900000,
});
renderWithAppContext(<LoginWithDeviceFlowRoute />, {
loginWithDeviceFlowStart: loginWithDeviceFlowStartMock,
});
await userEvent.click(screen.getByTestId('cancel-button'));
expect(navigateMock).toHaveBeenCalledWith(-1);
});
});