-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathflows.test.ts
More file actions
241 lines (200 loc) · 8.29 KB
/
flows.test.ts
File metadata and controls
241 lines (200 loc) · 8.29 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// @vitest-environment happy-dom
// Use a hoist-safe mock factory for '@octokit/oauth-methods'
vi.mock('@octokit/oauth-methods', async () => {
const actual = await vi.importActual<typeof import('@octokit/oauth-methods')>(
'@octokit/oauth-methods',
);
return {
...actual,
createDeviceCode: vi.fn(),
exchangeDeviceCode: vi.fn(),
exchangeWebFlowCode: vi.fn(),
};
});
import {
createDeviceCode,
exchangeDeviceCode,
exchangeWebFlowCode,
} from '@octokit/oauth-methods';
import { RequestError } from '@octokit/request-error';
import type { MockedFunction } from 'vitest';
import { Constants } from '../../constants';
import type { AuthCode, ClientID, ClientSecret, Hostname } from '../../types';
import type { DeviceFlowSession, LoginOAuthWebOptions } from './types';
import * as comms from '../../utils/system/comms';
import * as logger from '../core/logger';
import * as authUtils from './flows';
import { getRecommendedScopeNames } from './scopes';
const createDeviceCodeMock = createDeviceCode as unknown as MockedFunction<
typeof createDeviceCode
>;
const exchangeDeviceCodeMock = exchangeDeviceCode as unknown as MockedFunction<
typeof exchangeDeviceCode
>;
const exchangeWebFlowCodeMock =
exchangeWebFlowCode as unknown as MockedFunction<typeof exchangeWebFlowCode>;
describe('renderer/utils/auth/flows.ts', () => {
vi.spyOn(logger, 'rendererLogInfo').mockImplementation(vi.fn());
const openExternalLinkSpy = vi
.spyOn(comms, 'openExternalLink')
.mockImplementation(vi.fn());
beforeEach(() => {
// Mock OAUTH_DEVICE_FLOW_CLIENT_ID value
Constants.OAUTH_DEVICE_FLOW_CLIENT_ID = 'FAKE_CLIENT_ID_123' as ClientID;
});
describe('Gitify GitHub OAuth - Device Code Flow', () => {
describe('startGitHubDeviceFlow', () => {
it('should request a device code and return a session', async () => {
createDeviceCodeMock.mockResolvedValueOnce({
data: {
device_code: 'device-code-xyz',
user_code: 'user-code-xyz',
verification_uri: 'https://github.com/login/device',
expires_in: 600,
interval: 5,
},
} as unknown as Awaited<ReturnType<typeof createDeviceCode>>);
const session = await authUtils.startGitHubDeviceFlow();
expect(createDeviceCodeMock).toHaveBeenCalledWith({
clientType: 'oauth-app',
clientId: 'FAKE_CLIENT_ID_123',
scopes: getRecommendedScopeNames(),
request: expect.any(Function),
});
expect(session.deviceCode).toBe('device-code-xyz');
expect(session.userCode).toBe('user-code-xyz');
expect(session.verificationUri).toBe('https://github.com/login/device');
expect(session.intervalSeconds).toBe(5);
expect(session.expiresAt).toBeGreaterThan(Date.now());
});
});
describe('pollGitHubDeviceFlow', () => {
const baseSession = {
hostname: 'github.com',
clientId: 'FAKE_CLIENT_ID_123',
deviceCode: 'device-code',
userCode: 'user-code',
verificationUri: 'https://github.com/login/device',
intervalSeconds: 5,
expiresAt: Date.now() + 10000,
} as const;
it('returns token on successful exchange', async () => {
exchangeDeviceCodeMock.mockResolvedValueOnce({
authentication: { token: 'device-token-xyz' },
} as unknown as Awaited<ReturnType<typeof exchangeDeviceCode>>);
const token = await authUtils.pollGitHubDeviceFlow(
baseSession as DeviceFlowSession,
);
expect(exchangeDeviceCodeMock).toHaveBeenCalledWith({
clientType: 'oauth-app',
clientId: 'FAKE_CLIENT_ID_123',
code: 'device-code',
request: expect.any(Function),
});
expect(token).toBe('device-token-xyz');
});
it('returns null when authorization is pending or slow_down', async () => {
const pendingErr = Object.create(RequestError.prototype);
pendingErr.response = { data: { error: 'authorization_pending' } };
exchangeDeviceCodeMock.mockRejectedValueOnce(pendingErr);
const token = await authUtils.pollGitHubDeviceFlow(
baseSession as DeviceFlowSession,
);
expect(token).toBeNull();
});
it('throws on other errors', async () => {
const otherErr = new Error('boom');
exchangeDeviceCodeMock.mockRejectedValueOnce(otherErr);
await expect(
async () =>
await authUtils.pollGitHubDeviceFlow(
baseSession as DeviceFlowSession,
),
).rejects.toThrow('boom');
});
});
});
describe('performGitHubWebOAuth', () => {
const webAuthOptions: LoginOAuthWebOptions = {
hostname: 'github.com' as Hostname,
clientId: 'FAKE_CLIENT_ID_123' as ClientID,
clientSecret: 'FAKE_CLIENT_SECRET_123' as ClientSecret,
};
it('should call performGitHubWebOAuth using custom oauth app - success oauth flow', async () => {
window.gitify.onAuthCallback = vi.fn().mockImplementation((callback) => {
callback('gitify://oauth?code=123-456');
});
const res = await authUtils.performGitHubWebOAuth({
clientId: 'BYO_CLIENT_ID' as ClientID,
clientSecret: 'BYO_CLIENT_SECRET' as ClientSecret,
hostname: 'my.git.com' as Hostname,
});
expect(openExternalLinkSpy).toHaveBeenCalledTimes(1);
expect(openExternalLinkSpy).toHaveBeenCalledWith(
expect.stringContaining(
'https://my.git.com/login/oauth/authorize?allow_signup=false&client_id=BYO_CLIENT_ID&scope=notifications%2Cread%3Auser%2Crepo',
),
);
expect(window.gitify.onAuthCallback).toHaveBeenCalledTimes(1);
expect(window.gitify.onAuthCallback).toHaveBeenCalledWith(
expect.any(Function),
);
expect(res.authMethod).toBe('OAuth App');
expect(res.authCode).toBe('123-456');
});
it('should call performGitHubWebOAuth - failure', async () => {
window.gitify.onAuthCallback = vi.fn().mockImplementation((callback) => {
callback(
'gitify://auth?error=invalid_request&error_description=The+redirect_uri+is+missing+or+invalid.&error_uri=https://docs.github.com/en/developers/apps/troubleshooting-oauth-errors',
);
});
await expect(
async () => await authUtils.performGitHubWebOAuth(webAuthOptions),
).rejects.toEqual(
new Error(
"Oops! Something went wrong and we couldn't log you in using GitHub. Please try again. Reason: The redirect_uri is missing or invalid. Docs: https://docs.github.com/en/developers/apps/troubleshooting-oauth-errors",
),
);
expect(openExternalLinkSpy).toHaveBeenCalledTimes(1);
expect(openExternalLinkSpy).toHaveBeenCalledWith(
expect.stringContaining(
'https://github.com/login/oauth/authorize?allow_signup=false&client_id=FAKE_CLIENT_ID_123&scope=notifications%2Cread%3Auser%2Crepo',
),
);
expect(window.gitify.onAuthCallback).toHaveBeenCalledTimes(1);
expect(window.gitify.onAuthCallback).toHaveBeenCalledWith(
expect.any(Function),
);
});
describe('exchangeAuthCodeForAccessToken', () => {
const authCode = '123-456' as AuthCode;
it('should exchange auth code for access token', async () => {
exchangeWebFlowCodeMock.mockResolvedValueOnce({
authentication: {
token: 'this-is-a-token',
},
} as unknown as Awaited<ReturnType<typeof exchangeWebFlowCode>>);
const res = await authUtils.exchangeAuthCodeForAccessToken(authCode, {
...webAuthOptions,
});
expect(exchangeWebFlowCodeMock).toHaveBeenCalledWith({
clientType: 'oauth-app',
clientId: 'FAKE_CLIENT_ID_123',
clientSecret: 'FAKE_CLIENT_SECRET_123',
code: '123-456',
request: expect.any(Function),
});
expect(res).toBe('this-is-a-token');
});
it('should throw when client secret is missing', async () => {
await expect(
async () =>
await authUtils.exchangeAuthCodeForAccessToken(authCode, {
...webAuthOptions,
clientSecret: undefined as unknown as ClientSecret,
}),
).rejects.toThrow('clientSecret is required to exchange an auth code');
});
});
});
});