Skip to content
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

test: add tests for checkAuth.ts #149

Merged
merged 3 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 20 additions & 11 deletions src/utils/checkAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,42 @@ import {
OPEN_SAUCED_INSIGHTS_DOMAIN,
SUPABASE_LOGIN_URL,
} from "../constants";
import { checkTokenValidity } from "./fetchOpenSaucedApiData";
import setAccessTokenInChromeStorage from "../utils/setAccessToken";

export const checkAuthentication = async () => {
export const checkAuthentication = async (
hasOptedLogOut: () => Promise<boolean>,
getCookie: (
details: chrome.cookies.Details,
callback: (cookie: chrome.cookies.Cookie | null) => void
) => void,
checkTokenValidity: (authCookie: any) => Promise<boolean>,
setAccessTokenInChromeStorage: (authCookie: any) => void,
removeAuthTokenFromStorage: () => void,
logError: (error: string) => void,
) => {
if (await hasOptedLogOut()) {
return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY);
return removeAuthTokenFromStorage();
}

chrome.cookies.get(
getCookie(
{
name: SUPABASE_AUTH_COOKIE_NAME,
url: `https://${OPEN_SAUCED_INSIGHTS_DOMAIN}`,
},
async cookie => {
if (!cookie) {
return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY);
return removeAuthTokenFromStorage();
}
try {
const authCookie = JSON.parse(decodeURIComponent(cookie.value))[0];
const isValidToken = await checkTokenValidity(authCookie);

if (!isValidToken) {
return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY);
return removeAuthTokenFromStorage();
}
void setAccessTokenInChromeStorage(authCookie);
setAccessTokenInChromeStorage(authCookie);
} catch (error) {
void chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY);
console.error("Error processing cookie:", error);
removeAuthTokenFromStorage();
logError(error as string);
}
},
);
Expand All @@ -55,5 +63,6 @@ export const optLogIn = () => {
window.open(SUPABASE_LOGIN_URL, "_blank");
};

const hasOptedLogOut = async (): Promise<boolean> => (await chrome.storage.local.get(OPEN_SAUCED_OPTED_LOG_OUT_KEY))[OPEN_SAUCED_OPTED_LOG_OUT_KEY] === true;
export const hasOptedLogOut = async (): Promise<boolean> => (await chrome.storage.local.get(OPEN_SAUCED_OPTED_LOG_OUT_KEY))[OPEN_SAUCED_OPTED_LOG_OUT_KEY] === true;

export const removeAuthTokenFromStorage = async (): Promise<void> => chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY);
19 changes: 15 additions & 4 deletions src/worker/background.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { checkAuthentication } from "../utils/checkAuthentication";
import { checkAuthentication, hasOptedLogOut, removeAuthTokenFromStorage } from "../utils/checkAuthentication";
import { SUPABASE_AUTH_COOKIE_NAME, OPEN_SAUCED_INSIGHTS_DOMAIN } from "../constants";
import { setDefaultDescriptionConfig } from "../utils/aiprdescription/descriptionconfig";
import { checkTokenValidity } from "../utils/fetchOpenSaucedApiData";
import setAccessTokenInChromeStorage from "../utils/setAccessToken";


const checkUserAuthentication = () => {
void checkAuthentication(hasOptedLogOut, chrome.cookies.get, checkTokenValidity, setAccessTokenInChromeStorage, removeAuthTokenFromStorage, console.error);
};

chrome.cookies.onChanged.addListener(changeInfo => {
if (
changeInfo.cookie.name === SUPABASE_AUTH_COOKIE_NAME ||
changeInfo.cookie.domain === OPEN_SAUCED_INSIGHTS_DOMAIN
) {
void checkAuthentication();
checkUserAuthentication();
}
});

chrome.runtime.onInstalled.addListener(checkAuthentication);
chrome.runtime.onInstalled.addListener(() => {
checkUserAuthentication();
});
chrome.runtime.onInstalled.addListener(setDefaultDescriptionConfig);
chrome.runtime.onStartup.addListener(checkAuthentication);
chrome.runtime.onStartup.addListener(() => {
checkUserAuthentication();
});
43 changes: 43 additions & 0 deletions test/utils/checkAuthentication.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { checkAuthentication } from '../../src/utils/checkAuthentication';


const hasOptedLogOut = vi.fn().mockResolvedValue(false);
const getCookie = vi.fn();
const checkTokenValidity = vi.fn().mockResolvedValue(true);
const setAccessTokenInChromeStorage = vi.fn();
const removeAuthTokenFromStorage = vi.fn();
const logError = vi.fn();

describe('checkAuthentication', () => {
beforeEach(() => {
vi.resetAllMocks();
});

it('should remove auth token from storage if user has opted out', async () => {
hasOptedLogOut.mockResolvedValue(true);
await checkAuthentication(
hasOptedLogOut,
getCookie,
checkTokenValidity,
setAccessTokenInChromeStorage,
removeAuthTokenFromStorage,
logError,
);
expect(removeAuthTokenFromStorage).toHaveBeenCalled();
});

it('should remove auth token from storage if cookie is not found', async () => {
getCookie.mockImplementation((details, callback) => callback(null));
await checkAuthentication(
hasOptedLogOut,
getCookie,
checkTokenValidity,
setAccessTokenInChromeStorage,
removeAuthTokenFromStorage,
logError,
);
expect(removeAuthTokenFromStorage).toHaveBeenCalled();
});
})