Skip to content

feat(accounts): enhance auth account data structure #1139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
2362a2b
refactor: introduce new account types
setchy May 23, 2024
1c9d0b4
refactor: introduce new account types
setchy May 23, 2024
d1cc0a9
refactor: introduce new account types
setchy May 23, 2024
02e799d
refactor: introduce new account types
setchy May 23, 2024
fa41850
refactor: introduce new account types
setchy May 23, 2024
d5fa156
refactor: introduce new account types
setchy May 23, 2024
2ed3a3f
refactor: introduce new account types
setchy May 23, 2024
22e9364
refactor: introduce new account types
setchy May 23, 2024
01d9135
refactor: introduce new account types
setchy May 23, 2024
a8ce79b
refactor: introduce new account types
setchy May 23, 2024
9956999
refactor: move migration logic into own file
setchy May 24, 2024
da89b0e
refactor: introduce new account types
setchy May 24, 2024
fc7d563
refactor: change migration logic
setchy May 24, 2024
ec3d392
refactor: change migration logic
setchy May 24, 2024
56328f1
refactor: introduce new account types
setchy May 24, 2024
9cb2e6a
refactor: introduce new account types
setchy May 25, 2024
af13bd8
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 26, 2024
1049b10
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 26, 2024
129a24b
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 28, 2024
e886ddc
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 28, 2024
b9ee052
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 29, 2024
b9a47e4
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 29, 2024
f9e314e
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 30, 2024
a2104ae
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 30, 2024
094ef58
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 30, 2024
77cfe05
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 31, 2024
4f9ce9a
Merge branch 'refactor/new-account-type' of https://github.com/gitify…
setchy May 31, 2024
c16edd2
Merge branch 'refactor/new-account-type' of https://github.com/gitify…
setchy May 31, 2024
5d97d1c
Merge branch 'refactor/new-account-type' of https://github.com/gitify…
setchy May 31, 2024
a03544e
Merge branch 'refactor/new-account-type' of https://github.com/gitify…
setchy May 31, 2024
4083ad5
Merge branch 'refactor/new-account-type' of https://github.com/gitify…
setchy May 31, 2024
9e89686
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 31, 2024
0855c53
Merge branch 'refactor/new-account-type' of https://github.com/gitify…
setchy May 31, 2024
e4b15dc
Merge branch 'refactor/new-account-type' of https://github.com/gitify…
setchy May 31, 2024
af6de50
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 31, 2024
66676b5
feat: refresh user on migration
setchy May 31, 2024
9b51869
Merge remote-tracking branch 'origin/main' into refactor/new-account-…
setchy May 31, 2024
f784058
test: add coverage
setchy May 31, 2024
ee117b1
Merge branch 'main' into refactor/new-account-type
afonsojramos Jun 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/__mocks__/notifications-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ import type { AccountNotifications } from '../types';
import {
mockEnterpriseNotifications,
mockGitHubNotifications,
mockSingleNotification,
} from '../utils/api/__mocks__/response-mocks';
import {
mockGitHubCloudAccount,
mockGitHubEnterpriseServerAccount,
} from './state-mocks';

export const mockAccountNotifications: AccountNotifications[] = [
{
hostname: 'github.com',
account: mockGitHubCloudAccount,
notifications: mockGitHubNotifications,
},
{
hostname: 'github.gitify.io',
account: mockGitHubEnterpriseServerAccount,
notifications: mockEnterpriseNotifications,
},
];

export const mockSingleAccountNotifications: AccountNotifications[] = [
{
hostname: 'github.com',
notifications: [mockSingleNotification],
account: mockGitHubCloudAccount,
notifications: [mockGitHubNotifications[0]],
},
];
9 changes: 8 additions & 1 deletion src/__mocks__/partial-mocks.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import type { Notification, Subject, User } from '../typesGitHub';
import Constants from '../utils/constants';
import { mockGitifyUser, mockToken } from './state-mocks';

export function partialMockNotification(
subject: Partial<Subject>,
): Notification {
const mockNotification: Partial<Notification> = {
hostname: Constants.GITHUB_API_BASE_URL,
account: {
method: 'Personal Access Token',
platform: 'GitHub Cloud',
hostname: Constants.GITHUB_API_BASE_URL,
token: mockToken,
user: mockGitifyUser,
},
subject: subject as Subject,
};

Expand Down
42 changes: 38 additions & 4 deletions src/__mocks__/state-mocks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
type Account,
type AuthState,
type GitifyUser,
type SettingsState,
Theme,
} from '../types';
import type { EnterpriseAccount } from '../utils/auth/types';
import Constants from '../utils/constants';

export const mockEnterpriseAccounts: EnterpriseAccount[] = [
{
Expand All @@ -13,18 +15,50 @@ export const mockEnterpriseAccounts: EnterpriseAccount[] = [
},
];

export const mockUser: GitifyUser = {
export const mockGitifyUser: GitifyUser = {
login: 'octocat',
name: 'Mona Lisa Octocat',
id: 123456789,
};

export const mockAuth: AuthState = {
export const mockPersonalAccessTokenAccount: Account = {
platform: 'GitHub Cloud',
method: 'Personal Access Token',
token: 'token-123-456',
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
user: mockGitifyUser,
};

export const mockOAuthAccount: Account = {
platform: 'GitHub Enterprise Server',
method: 'OAuth App',
token: '1234568790',
hostname: 'github.gitify.io',
user: mockGitifyUser,
};

export const mockGitHubCloudAccount: Account = {
platform: 'GitHub Cloud',
method: 'Personal Access Token',
token: 'token-123-456',
enterpriseAccounts: mockEnterpriseAccounts,
user: mockUser,
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
user: mockGitifyUser,
};

export const mockGitHubEnterpriseServerAccount: Account = {
platform: 'GitHub Enterprise Server',
method: 'Personal Access Token',
token: '1234568790',
hostname: 'github.gitify.io',
user: mockGitifyUser,
};

export const mockAuth: AuthState = {
accounts: [mockGitHubCloudAccount, mockGitHubEnterpriseServerAccount],
};

export const mockToken = 'token-123-456';

export const mockSettings: SettingsState = {
participating: false,
playSound: true,
Expand Down
4 changes: 2 additions & 2 deletions src/components/NotificationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ interface IProps {

export const NotificationRow: FC<IProps> = ({ notification, hostname }) => {
const {
settings,
auth,
settings,
removeNotificationFromState,
markNotificationRead,
markNotificationDone,
Expand All @@ -46,7 +46,7 @@ export const NotificationRow: FC<IProps> = ({ notification, hostname }) => {
} = useContext(AppContext);

const openNotification = useCallback(() => {
openInBrowser(notification, auth);
openInBrowser(notification);

if (settings.markAsDoneOnOpen) {
markNotificationDone(notification.id, hostname);
Expand Down
15 changes: 8 additions & 7 deletions src/context/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { act, fireEvent, render, waitFor } from '@testing-library/react';
import { useContext } from 'react';

import { mockAuth, mockSettings } from '../__mocks__/state-mocks';
import { useNotifications } from '../hooks/useNotifications';
import type { AuthState, SettingsState } from '../types';
Expand Down Expand Up @@ -133,6 +132,7 @@ describe('context/App.tsx', () => {
expect(markNotificationReadMock).toHaveBeenCalledTimes(1);
expect(markNotificationReadMock).toHaveBeenCalledWith(
{
accounts: [],
enterpriseAccounts: [],
token: null,
user: null,
Expand Down Expand Up @@ -165,7 +165,7 @@ describe('context/App.tsx', () => {

expect(markNotificationDoneMock).toHaveBeenCalledTimes(1);
expect(markNotificationDoneMock).toHaveBeenCalledWith(
{ enterpriseAccounts: [], token: null, user: null },
{ accounts: [], enterpriseAccounts: [], token: null, user: null },
mockSettings,
'123-456',
'github.com',
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('context/App.tsx', () => {

expect(unsubscribeNotificationMock).toHaveBeenCalledTimes(1);
expect(unsubscribeNotificationMock).toHaveBeenCalledWith(
{ enterpriseAccounts: [], token: null, user: null },
{ accounts: [], enterpriseAccounts: [], token: null, user: null },
mockSettings,
'123-456',
'github.com',
Expand Down Expand Up @@ -228,7 +228,7 @@ describe('context/App.tsx', () => {

expect(markRepoNotificationsMock).toHaveBeenCalledTimes(1);
expect(markRepoNotificationsMock).toHaveBeenCalledWith(
{ enterpriseAccounts: [], token: null, user: null },
{ accounts: [], enterpriseAccounts: [], token: null, user: null },
mockSettings,
'gitify-app/notifications-test',
'github.com',
Expand Down Expand Up @@ -262,13 +262,14 @@ describe('context/App.tsx', () => {

expect(markRepoNotificationsDoneMock).toHaveBeenCalledTimes(1);
expect(markRepoNotificationsDoneMock).toHaveBeenCalledWith(
{ enterpriseAccounts: [], token: null, user: null },
{ accounts: [], enterpriseAccounts: [], token: null, user: null },
mockSettings,
'gitify-app/notifications-test',
'github.com',
);
});
});

describe('authentication methods', () => {
const apiRequestAuthMock = jest.spyOn(apiRequests, 'apiRequestAuth');
const fetchNotificationsMock = jest.fn();
Expand Down Expand Up @@ -370,7 +371,7 @@ describe('context/App.tsx', () => {
});

expect(saveStateMock).toHaveBeenCalledWith({
auth: { enterpriseAccounts: [], token: null, user: null },
auth: { accounts: [], enterpriseAccounts: [], token: null, user: null },
settings: {
participating: true,
playSound: true,
Expand Down Expand Up @@ -415,7 +416,7 @@ describe('context/App.tsx', () => {
expect(setAutoLaunchMock).toHaveBeenCalledWith(true);

expect(saveStateMock).toHaveBeenCalledWith({
auth: { enterpriseAccounts: [], token: null, user: null },
auth: { accounts: [], enterpriseAccounts: [], token: null, user: null },
settings: {
participating: false,
playSound: true,
Expand Down
23 changes: 16 additions & 7 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Theme,
} from '../types';
import { headNotifications } from '../utils/api/client';
import { migrateAuthenticatedAccounts } from '../utils/auth/migration';
import type {
LoginOAuthAppOptions,
LoginPersonalAccessTokenOptions,
Expand All @@ -34,6 +35,7 @@ import { clearState, loadState, saveState } from '../utils/storage';
import { setTheme } from '../utils/theme';

const defaultAuth: AuthState = {
accounts: [],
token: null,
enterpriseAccounts: [],
user: null,
Expand Down Expand Up @@ -116,8 +118,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
settings.participating,
settings.showBots,
settings.detailedNotifications,
auth.token,
auth.enterpriseAccounts.length,
auth.accounts.length,
]);

useInterval(() => {
Expand Down Expand Up @@ -149,15 +150,15 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
);

const isLoggedIn = useMemo(() => {
return !!auth.token || auth.enterpriseAccounts.length > 0;
return auth.accounts.length > 0;
}, [auth]);

const loginWithGitHubApp = useCallback(async () => {
const { authCode } = await authGitHub();
const { token } = await getToken(authCode);
const hostname = Constants.DEFAULT_AUTH_OPTIONS.hostname;
const user = await getUserData(token, hostname);
const updatedAuth = addAccount(auth, token, hostname, user);
const updatedAuth = addAccount(auth, 'GitHub App', token, hostname, user);
setAuth(updatedAuth);
saveState({ auth: updatedAuth, settings });
}, [auth, settings]);
Expand All @@ -166,7 +167,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
async (data: LoginOAuthAppOptions) => {
const { authOptions, authCode } = await authGitHub(data);
const { token, hostname } = await getToken(authCode, authOptions);
const updatedAuth = addAccount(auth, token, hostname);
const updatedAuth = addAccount(auth, 'OAuth App', token, hostname);
setAuth(updatedAuth);
saveState({ auth: updatedAuth, settings });
},
Expand All @@ -178,7 +179,13 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
await headNotifications(hostname, token);

const user = await getUserData(token, hostname);
const updatedAuth = addAccount(auth, token, hostname, user);
const updatedAuth = addAccount(
auth,
'Personal Access Token',
token,
hostname,
user,
);
setAuth(updatedAuth);
saveState({ auth: updatedAuth, settings });
},
Expand All @@ -190,7 +197,9 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
clearState();
}, []);

const restoreSettings = useCallback(() => {
const restoreSettings = useCallback(async () => {
await migrateAuthenticatedAccounts();

const existing = loadState();

if (existing.auth) {
Expand Down
Loading