11import { mockGitHubCloudAccount } from '../../__mocks__/account-mocks' ;
22import { mockAuth } from '../../__mocks__/state-mocks' ;
3+ import { mockRawUser } from '../api/__mocks__/response-mocks' ;
34
45import { Constants } from '../../constants' ;
56
67import type {
78 Account ,
89 AuthCode ,
10+ AuthState ,
911 ClientID ,
1012 ClientSecret ,
1113 Hostname ,
14+ Link ,
1215 Token ,
1316} from '../../types' ;
1417import type { AuthMethod } from './types' ;
1518
1619import * as comms from '../../utils/comms' ;
17- // import * as apiClient from '../api/client';
20+ import * as apiClient from '../api/client' ;
1821import * as logger from '../logger' ;
1922import * as authUtils from './utils' ;
2023import {
@@ -30,6 +33,8 @@ jest.mock('@octokit/oauth-methods', () => ({
3033
3134import { exchangeWebFlowCode } from '@octokit/oauth-methods' ;
3235
36+ import type { GetAuthenticatedUserResponse } from '../api/types' ;
37+
3338const exchangeWebFlowCodeMock = exchangeWebFlowCode as jest . MockedFunction <
3439 typeof exchangeWebFlowCode
3540> ;
@@ -156,130 +161,153 @@ describe('renderer/utils/auth/utils.ts', () => {
156161 } ) ;
157162 } ) ;
158163
159- // describe('addAccount', () => {
160- // let mockAuthState: AuthState;
161- // const fetchAuthenticatedUserDetailsSpy = jest.spyOn(
162- // apiClient,
163- // 'fetchAuthenticatedUserDetails',
164- // );
165-
166- // beforeEach(() => {
167- // mockAuthState = {
168- // accounts: [],
169- // };
170- // });
171-
172- // describe('should add GitHub Cloud account', () => {
173- // beforeEach(() => {
174- // fetchAuthenticatedUserDetailsSpy.mockResolvedValue({
175- // data: {
176- // viewer: mockGitifyUser,
177- // },
178- // headers: {
179- // 'x-oauth-scopes': Constants.OAUTH_SCOPES.RECOMMENDED.join(', '),
180- // },
181- // });
182- // });
183-
184- // it('should add personal access token account', async () => {
185- // const result = await authUtils.addAccount(
186- // mockAuthState,
187- // 'Personal Access Token',
188- // '123-456' as Token,
189- // 'github.com' as Hostname,
190- // );
191-
192- // expect(result.accounts).toEqual([
193- // {
194- // hasRequiredScopes: true,
195- // hostname: 'github.com' as Hostname,
196- // method: 'Personal Access Token',
197- // platform: 'GitHub Cloud',
198- // token: 'encrypted' as Token,
199- // user: mockGitifyUser,
200- // version: 'latest',
201- // },
202- // ]);
203- // });
204-
205- // it('should add oauth app account', async () => {
206- // const result = await authUtils.addAccount(
207- // mockAuthState,
208- // 'OAuth App',
209- // '123-456' as Token,
210- // 'github.com' as Hostname,
211- // );
212-
213- // expect(result.accounts).toEqual([
214- // {
215- // hasRequiredScopes: true,
216- // hostname: 'github.com' as Hostname,
217- // method: 'OAuth App',
218- // platform: 'GitHub Cloud',
219- // token: 'encrypted' as Token,
220- // user: mockGitifyUser,
221- // version: 'latest',
222- // },
223- // ]);
224- // });
225- // });
226-
227- // describe('should add GitHub Enterprise Server account', () => {
228- // beforeEach(() => {
229- // fetchAuthenticatedUserDetailsSpy.mockResolvedValue({
230- // data: {
231- // ...mockGitifyUser,
232- // },
233- // headers: {
234- // 'x-github-enterprise-version': '3.0.0',
235- // 'x-oauth-scopes': Constants.OAUTH_SCOPES.RECOMMENDED.join(', '),
236- // },
237- // });
238- // });
239-
240- // it('should add personal access token account', async () => {
241- // const result = await authUtils.addAccount(
242- // mockAuthState,
243- // 'Personal Access Token',
244- // '123-456' as Token,
245- // 'github.gitify.io' as Hostname,
246- // );
247-
248- // expect(result.accounts).toEqual([
249- // {
250- // hasRequiredScopes: true,
251- // hostname: 'github.gitify.io' as Hostname,
252- // method: 'Personal Access Token',
253- // platform: 'GitHub Enterprise Server',
254- // token: 'encrypted' as Token,
255- // user: mockGitifyUser,
256- // version: '3.0.0',
257- // },
258- // ]);
259- // });
260-
261- // it('should add oauth app account', async () => {
262- // const result = await authUtils.addAccount(
263- // mockAuthState,
264- // 'OAuth App',
265- // '123-456' as Token,
266- // 'github.gitify.io' as Hostname,
267- // );
268-
269- // expect(result.accounts).toEqual([
270- // {
271- // hasRequiredScopes: true,
272- // hostname: 'github.gitify.io' as Hostname,
273- // method: 'OAuth App',
274- // platform: 'GitHub Enterprise Server',
275- // token: 'encrypted' as Token,
276- // user: mockGitifyUser,
277- // version: '3.0.0',
278- // },
279- // ]);
280- // });
281- // });
282- // });
164+ describe ( 'addAccount' , ( ) => {
165+ let mockAuthState : AuthState ;
166+
167+ const mockAuthenticatedResponse = mockRawUser ( 'authenticated-user' ) ;
168+
169+ const fetchAuthenticatedUserDetailsSpy = jest . spyOn (
170+ apiClient ,
171+ 'fetchAuthenticatedUserDetails' ,
172+ ) ;
173+
174+ beforeEach ( ( ) => {
175+ mockAuthState = {
176+ accounts : [ ] ,
177+ } ;
178+ } ) ;
179+
180+ describe ( 'should add GitHub Cloud account' , ( ) => {
181+ beforeEach ( ( ) => {
182+ fetchAuthenticatedUserDetailsSpy . mockResolvedValue ( {
183+ status : 200 ,
184+ url : 'https://api.github.com/user' ,
185+ data : mockAuthenticatedResponse as GetAuthenticatedUserResponse ,
186+ headers : {
187+ 'x-oauth-scopes' : Constants . OAUTH_SCOPES . RECOMMENDED . join ( ', ' ) ,
188+ } ,
189+ } ) ;
190+ } ) ;
191+
192+ it ( 'should add personal access token account' , async ( ) => {
193+ const result = await authUtils . addAccount (
194+ mockAuthState ,
195+ 'Personal Access Token' ,
196+ '123-456' as Token ,
197+ 'github.com' as Hostname ,
198+ ) ;
199+
200+ expect ( result . accounts ) . toEqual ( [
201+ {
202+ hasRequiredScopes : true ,
203+ hostname : 'github.com' as Hostname ,
204+ method : 'Personal Access Token' ,
205+ platform : 'GitHub Cloud' ,
206+ token : 'encrypted' as Token ,
207+ user : {
208+ id : String ( mockAuthenticatedResponse . id ) ,
209+ name : mockAuthenticatedResponse . name ,
210+ login : mockAuthenticatedResponse . login ,
211+ avatar : mockAuthenticatedResponse . avatar_url as Link ,
212+ } ,
213+ version : 'latest' ,
214+ } satisfies Account ,
215+ ] ) ;
216+ } ) ;
217+
218+ it ( 'should add oauth app account' , async ( ) => {
219+ const result = await authUtils . addAccount (
220+ mockAuthState ,
221+ 'OAuth App' ,
222+ '123-456' as Token ,
223+ 'github.com' as Hostname ,
224+ ) ;
225+
226+ expect ( result . accounts ) . toEqual ( [
227+ {
228+ hasRequiredScopes : true ,
229+ hostname : 'github.com' as Hostname ,
230+ method : 'OAuth App' ,
231+ platform : 'GitHub Cloud' ,
232+ token : 'encrypted' as Token ,
233+ user : {
234+ id : String ( mockAuthenticatedResponse . id ) ,
235+ name : mockAuthenticatedResponse . name ,
236+ login : mockAuthenticatedResponse . login ,
237+ avatar : mockAuthenticatedResponse . avatar_url as Link ,
238+ } ,
239+ version : 'latest' ,
240+ } satisfies Account ,
241+ ] ) ;
242+ } ) ;
243+ } ) ;
244+
245+ describe ( 'should add GitHub Enterprise Server account' , ( ) => {
246+ beforeEach ( ( ) => {
247+ fetchAuthenticatedUserDetailsSpy . mockResolvedValue ( {
248+ status : 200 ,
249+ url : 'https://github.gitify.io/api/v3/user' ,
250+ data : mockAuthenticatedResponse as GetAuthenticatedUserResponse ,
251+ headers : {
252+ 'x-github-enterprise-version' : '3.0.0' ,
253+ 'x-oauth-scopes' : Constants . OAUTH_SCOPES . RECOMMENDED . join ( ', ' ) ,
254+ } ,
255+ } ) ;
256+ } ) ;
257+
258+ it ( 'should add personal access token account' , async ( ) => {
259+ const result = await authUtils . addAccount (
260+ mockAuthState ,
261+ 'Personal Access Token' ,
262+ '123-456' as Token ,
263+ 'github.gitify.io' as Hostname ,
264+ ) ;
265+
266+ expect ( result . accounts ) . toEqual ( [
267+ {
268+ hasRequiredScopes : true ,
269+ hostname : 'github.gitify.io' as Hostname ,
270+ method : 'Personal Access Token' ,
271+ platform : 'GitHub Enterprise Server' ,
272+ token : 'encrypted' as Token ,
273+ user : {
274+ id : String ( mockAuthenticatedResponse . id ) ,
275+ name : mockAuthenticatedResponse . name ,
276+ login : mockAuthenticatedResponse . login ,
277+ avatar : mockAuthenticatedResponse . avatar_url as Link ,
278+ } ,
279+ version : '3.0.0' ,
280+ } satisfies Account ,
281+ ] ) ;
282+ } ) ;
283+
284+ it ( 'should add oauth app account' , async ( ) => {
285+ const result = await authUtils . addAccount (
286+ mockAuthState ,
287+ 'OAuth App' ,
288+ '123-456' as Token ,
289+ 'github.gitify.io' as Hostname ,
290+ ) ;
291+
292+ expect ( result . accounts ) . toEqual ( [
293+ {
294+ hasRequiredScopes : true ,
295+ hostname : 'github.gitify.io' as Hostname ,
296+ method : 'OAuth App' ,
297+ platform : 'GitHub Enterprise Server' ,
298+ token : 'encrypted' as Token ,
299+ user : {
300+ id : String ( mockAuthenticatedResponse . id ) ,
301+ name : mockAuthenticatedResponse . name ,
302+ login : mockAuthenticatedResponse . login ,
303+ avatar : mockAuthenticatedResponse . avatar_url as Link ,
304+ } ,
305+ version : '3.0.0' ,
306+ } satisfies Account ,
307+ ] ) ;
308+ } ) ;
309+ } ) ;
310+ } ) ;
283311
284312 describe ( 'removeAccount' , ( ) => {
285313 it ( 'should remove account with matching token' , async ( ) => {
0 commit comments