Skip to content

Commit

Permalink
test: add tests for checkAuth.ts (#149)
Browse files Browse the repository at this point in the history
* test: add tests for checkAuth.ts

* call check user auth once
  • Loading branch information
diivi committed May 29, 2023
1 parent 77c7f27 commit 3a79fac
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 15 deletions.
31 changes: 20 additions & 11 deletions src/utils/checkAuthentication.ts
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
@@ -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
@@ -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();
});
})

0 comments on commit 3a79fac

Please sign in to comment.