Skip to content

Commit 59bce94

Browse files
committed
fix tests
Signed-off-by: Adam Setch <[email protected]>
1 parent 37a0b0b commit 59bce94

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

src/main/main.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { menubar } from 'menubar';
44

55
import { APPLICATION } from '../shared/constants';
66
import { namespacedEvent } from '../shared/events';
7+
import { logError } from '../shared/logger';
78
import { isMacOS, isWindows } from '../shared/platform';
89
import { onFirstRunMaybe } from './first-run';
910
import { TrayIcons } from './icons';
@@ -202,4 +203,15 @@ app.on('open-url', (event, url) => {
202203
if (code && (type === 'auth' || type === 'oauth')) {
203204
mb.window.webContents.send(namespacedEvent('auth-code'), type, code);
204205
}
206+
207+
const error = link.searchParams.get('error');
208+
const errorDescription = link.searchParams.get('error_description');
209+
210+
if (error) {
211+
logError(
212+
'main:open-url',
213+
`Error during OAuth 2.0 callback ${error}`,
214+
new Error(errorDescription),
215+
);
216+
}
205217
});

src/renderer/utils/auth/utils.test.ts

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import remote from '@electron/remote';
21
import axios from 'axios';
32
import type { AxiosPromise, AxiosResponse } from 'axios';
3+
import { ipcRenderer } from 'electron';
44
import nock from 'nock';
55
import {
66
mockAuth,
@@ -15,64 +15,46 @@ import type {
1515
Hostname,
1616
Token,
1717
} from '../../types';
18+
import * as comms from '../../utils/comms';
1819
import * as apiRequests from '../api/request';
1920
import type { AuthMethod } from './types';
2021
import * as auth from './utils';
2122
import { getNewOAuthAppURL, getNewTokenURL } from './utils';
2223

23-
const browserWindow = new remote.BrowserWindow();
24-
2524
describe('renderer/utils/auth/utils.ts', () => {
2625
describe('authGitHub', () => {
27-
const loadURLMock = jest.spyOn(browserWindow, 'loadURL');
26+
const openExternalLinkMock = jest
27+
.spyOn(comms, 'openExternalLink')
28+
.mockImplementation();
2829

2930
afterEach(() => {
3031
jest.clearAllMocks();
3132
});
3233

3334
it('should call authGitHub - success', async () => {
34-
// Casting to jest.Mock avoids Typescript errors, where the spy is expected to match all the original
35-
// function's typing. I might fix all that if the return type of this was actually used, or if I was
36-
// writing this test for a new feature. Since I'm just upgrading Jest, jest.Mock is a nice escape hatch
37-
(
38-
jest.spyOn(browserWindow.webContents, 'on') as jest.Mock
39-
).mockImplementation((event, callback): void => {
40-
if (event === 'will-redirect') {
41-
const event = new Event('will-redirect');
42-
callback(event, 'https://github.com/?code=123-456');
35+
const mockIpcRendererOn = (
36+
jest.spyOn(ipcRenderer, 'on') as jest.Mock
37+
).mockImplementation((event, callback) => {
38+
if (event === 'gitify:auth-code') {
39+
callback(null, 'auth', '123-456');
4340
}
4441
});
4542

4643
const res = await auth.authGitHub();
4744

48-
expect(res.authCode).toBe('123-456');
49-
50-
expect(
51-
browserWindow.webContents.session.clearStorageData,
52-
).toHaveBeenCalledTimes(1);
53-
54-
expect(loadURLMock).toHaveBeenCalledTimes(1);
55-
expect(loadURLMock).toHaveBeenCalledWith(
45+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
46+
expect(openExternalLinkMock).toHaveBeenCalledWith(
5647
'https://github.com/login/oauth/authorize?client_id=FAKE_CLIENT_ID_123&scope=read%3Auser%2Cnotifications%2Crepo',
5748
);
5849

59-
expect(browserWindow.destroy).toHaveBeenCalledTimes(1);
60-
});
61-
62-
it('should call authGitHub - failure', async () => {
63-
(
64-
jest.spyOn(browserWindow.webContents, 'on') as jest.Mock
65-
).mockImplementation((event, callback): void => {
66-
if (event === 'will-redirect') {
67-
const event = new Event('will-redirect');
68-
callback(event, 'https://www.github.com/?error=Oops');
69-
}
70-
});
71-
72-
await expect(async () => await auth.authGitHub()).rejects.toEqual(
73-
"Oops! Something went wrong and we couldn't log you in using GitHub. Please try again.",
50+
expect(mockIpcRendererOn).toHaveBeenCalledTimes(1);
51+
expect(mockIpcRendererOn).toHaveBeenCalledWith(
52+
'gitify:auth-code',
53+
expect.any(Function),
7454
);
75-
expect(loadURLMock).toHaveBeenCalledTimes(1);
55+
56+
expect(res.authType).toBe('GitHub App');
57+
expect(res.authCode).toBe('123-456');
7658
});
7759
});
7860

src/renderer/utils/auth/utils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ export function authGitHub(
2727
authOptions = Constants.DEFAULT_AUTH_OPTIONS,
2828
): Promise<AuthResponse> {
2929
return new Promise((resolve) => {
30-
// // Build the OAuth consent page URL
3130
const authUrl = new URL(`https://${authOptions.hostname}`);
3231
authUrl.pathname = '/login/oauth/authorize';
3332
authUrl.searchParams.append('client_id', authOptions.clientId);
3433
authUrl.searchParams.append('scope', Constants.AUTH_SCOPE.toString());
3534

3635
openExternalLink(authUrl.toString() as Link);
3736

37+
const handleCallback = (authType: AuthMethod, authCode: AuthCode) => {
38+
resolve({ authType, authCode, authOptions });
39+
};
40+
3841
ipcRenderer.on(
3942
namespacedEvent('auth-code'),
4043
(_, authType: 'auth' | 'oauth', authCode: AuthCode) => {
@@ -43,10 +46,6 @@ export function authGitHub(
4346
handleCallback(type, authCode);
4447
},
4548
);
46-
47-
const handleCallback = (authType: AuthMethod, authCode: AuthCode) => {
48-
resolve({ authType, authCode, authOptions });
49-
};
5049
});
5150
}
5251

0 commit comments

Comments
 (0)