From 2ae28ebac1644b4aef70caab455c82397fad357f Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Fri, 17 May 2024 08:23:39 -0400 Subject: [PATCH 1/3] feat(auth): add `create oauth app` link to enterprise route --- src/routes/LoginEnterprise.tsx | 27 ++++++++++++++++++- .../LoginEnterprise.test.tsx.snap | 27 +++++++++++++++++++ src/utils/auth.test.ts | 19 +++++++++++++ src/utils/auth.ts | 22 +++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/routes/LoginEnterprise.tsx b/src/routes/LoginEnterprise.tsx index c025ba327..6532247f5 100644 --- a/src/routes/LoginEnterprise.tsx +++ b/src/routes/LoginEnterprise.tsx @@ -9,6 +9,8 @@ import { useNavigate } from 'react-router-dom'; import { FieldInput } from '../components/fields/FieldInput'; import { AppContext } from '../context/App'; import type { AuthOptions } from '../types'; +import { getNewOAuthAppURL } from '../utils/auth'; +import { openExternalLink } from '../utils/comms'; interface IValues { hostname?: string; @@ -65,8 +67,15 @@ export const LoginEnterpriseRoute: FC = () => { } }, [enterpriseAccounts]); + const openLink = useCallback((url: string) => { + openExternalLink(url); + }, []); + const renderForm = (formProps: FormRenderProps) => { - const { handleSubmit, submitting, pristine } = formProps; + const { handleSubmit, submitting, pristine, values } = formProps; + + const buttonClasses = + 'rounded bg-gray-300 font-semibold rounded text-sm text-center hover:bg-gray-500 hover:text-white dark:text-black focus:outline-none cursor-pointer'; return (
@@ -74,6 +83,22 @@ export const LoginEnterpriseRoute: FC = () => { name="hostname" label="Hostname" placeholder="github.company.com" + helpText={ +
+
+ {' '} + then{' '} + Generate a new client secret. +
+
+ } /> diff --git a/src/routes/__snapshots__/LoginEnterprise.test.tsx.snap b/src/routes/__snapshots__/LoginEnterprise.test.tsx.snap index 0efd39df5..59ea2c310 100644 --- a/src/routes/__snapshots__/LoginEnterprise.test.tsx.snap +++ b/src/routes/__snapshots__/LoginEnterprise.test.tsx.snap @@ -69,6 +69,33 @@ exports[`routes/LoginEnterprise.tsx renders correctly 1`] = ` type="text" value="" /> +
+
+
+ + + then + + + Generate a new client secret + + . +
+
+
{ describe('authGitHub', () => { @@ -130,4 +131,22 @@ describe('utils/auth.tsx', () => { }); }); }); + + describe('getNewOAuthAppURL', () => { + it('should generate new oauth app url - github cloud', () => { + expect( + getNewOAuthAppURL('github.com').startsWith( + 'https://github.com/settings/applications/new', + ), + ).toBeTruthy(); + }); + + it('should generate new oauth app url - github server', () => { + expect( + getNewOAuthAppURL('github.gitify.io').startsWith( + 'https://github.gitify.io/settings/applications/new', + ), + ).toBeTruthy(); + }); + }); }); diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 222e1900d..4d27fea27 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -1,5 +1,6 @@ import { BrowserWindow } from '@electron/remote'; +import { format } from 'date-fns'; import type { AuthResponse, AuthState, @@ -136,3 +137,24 @@ export const addAccount = ( ], }; }; + +export function getNewOAuthAppURL(hostname: string): string { + const date = format(new Date(), 'PP p'); + const newOAuthAppURL = new URL( + `https://${hostname}/settings/applications/new`, + ); + newOAuthAppURL.searchParams.append( + 'oauth_application[name]', + `Gitify (Created on ${date})`, + ); + newOAuthAppURL.searchParams.append( + 'oauth_application[url]', + 'https://www.gitify.io', + ); + newOAuthAppURL.searchParams.append( + 'oauth_application[callback_url]', + 'https://www.gitify.io/callback', + ); + + return newOAuthAppURL.toString(); +} From cb6581d5b88db5a2905f92a61e9b81106d5d5ff1 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Fri, 17 May 2024 08:26:05 -0400 Subject: [PATCH 2/3] feat(auth): add `create oauth app` link to enterprise route --- src/routes/LoginEnterprise.tsx | 2 +- src/routes/__snapshots__/LoginEnterprise.test.tsx.snap | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/LoginEnterprise.tsx b/src/routes/LoginEnterprise.tsx index 6532247f5..b38d77f22 100644 --- a/src/routes/LoginEnterprise.tsx +++ b/src/routes/LoginEnterprise.tsx @@ -95,7 +95,7 @@ export const LoginEnterpriseRoute: FC = () => { Create new OAuth App {' '} then{' '} - Generate a new client secret. + generate a new client secret.
} diff --git a/src/routes/__snapshots__/LoginEnterprise.test.tsx.snap b/src/routes/__snapshots__/LoginEnterprise.test.tsx.snap index 59ea2c310..50381c371 100644 --- a/src/routes/__snapshots__/LoginEnterprise.test.tsx.snap +++ b/src/routes/__snapshots__/LoginEnterprise.test.tsx.snap @@ -90,7 +90,7 @@ exports[`routes/LoginEnterprise.tsx renders correctly 1`] = ` - Generate a new client secret + generate a new client secret . From c1e7fd4e41e18b0c28cf51a9272922021a21c0c8 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Fri, 17 May 2024 08:42:18 -0400 Subject: [PATCH 3/3] feat(auth): add `create oauth app` link to enterprise route --- src/routes/LoginEnterprise.test.tsx | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/routes/LoginEnterprise.test.tsx b/src/routes/LoginEnterprise.test.tsx index fbacb1dda..ebbc91c8c 100644 --- a/src/routes/LoginEnterprise.test.tsx +++ b/src/routes/LoginEnterprise.test.tsx @@ -2,6 +2,7 @@ import { fireEvent, render, screen } from '@testing-library/react'; import { MemoryRouter } from 'react-router-dom'; import * as TestRenderer from 'react-test-renderer'; const { ipcRenderer } = require('electron'); +import { shell } from 'electron'; import { mockedEnterpriseAccounts } from '../__mocks__/mockedData'; import { AppContext } from '../context/App'; import type { AuthState } from '../types'; @@ -14,6 +15,8 @@ jest.mock('react-router-dom', () => ({ })); describe('routes/LoginEnterprise.tsx', () => { + const openExternalMock = jest.spyOn(shell, 'openExternal'); + const mockAccounts: AuthState = { enterpriseAccounts: [], user: null, @@ -75,6 +78,40 @@ describe('routes/LoginEnterprise.tsx', () => { expect(validate(values).clientSecret).toBe('Invalid client secret.'); }); + describe("'Create new OAuth App' button", () => { + it('should be disabled if no hostname configured', async () => { + render( + + + + + , + ); + + fireEvent.click(screen.getByText('Create new OAuth App')); + + expect(openExternalMock).toHaveBeenCalledTimes(0); + }); + + it('should open if hostname configured', async () => { + render( + + + + + , + ); + + fireEvent.change(screen.getByLabelText('Hostname'), { + target: { value: 'company.github.com' }, + }); + + fireEvent.click(screen.getByText('Create new OAuth App')); + + expect(openExternalMock).toHaveBeenCalledTimes(1); + }); + }); + it('should receive a logged-in enterprise account', () => { const { rerender } = render(