From 74cb970eed3493924f54601f39273f624681445a Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Sat, 27 May 2023 22:19:07 +0530 Subject: [PATCH 1/2] test: add tests for checkAuth.ts --- .gitignore | 1 + src/utils/checkAuthentication.ts | 31 ++++++++++++------- src/worker/background.ts | 14 ++++++--- test/utils/checkAuthentication.test.ts | 43 ++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 test/utils/checkAuthentication.test.ts diff --git a/.gitignore b/.gitignore index a547bf36..4b73ddd2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +coverage # Logs logs *.log diff --git a/src/utils/checkAuthentication.ts b/src/utils/checkAuthentication.ts index cdf07697..4fd3b94a 100644 --- a/src/utils/checkAuthentication.ts +++ b/src/utils/checkAuthentication.ts @@ -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, + getCookie: ( + details: chrome.cookies.Details, + callback: (cookie: chrome.cookies.Cookie | null) => void + ) => void, + checkTokenValidity: (authCookie: any) => Promise, + 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); } }, ); @@ -55,5 +63,6 @@ export const optLogIn = () => { window.open(SUPABASE_LOGIN_URL, "_blank"); }; -const hasOptedLogOut = async (): Promise => (await chrome.storage.local.get(OPEN_SAUCED_OPTED_LOG_OUT_KEY))[OPEN_SAUCED_OPTED_LOG_OUT_KEY] === true; +export const hasOptedLogOut = async (): Promise => (await chrome.storage.local.get(OPEN_SAUCED_OPTED_LOG_OUT_KEY))[OPEN_SAUCED_OPTED_LOG_OUT_KEY] === true; +export const removeAuthTokenFromStorage = async (): Promise => chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); diff --git a/src/worker/background.ts b/src/worker/background.ts index 62cd846f..68f766ff 100644 --- a/src/worker/background.ts +++ b/src/worker/background.ts @@ -1,16 +1,22 @@ -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"; chrome.cookies.onChanged.addListener(changeInfo => { if ( changeInfo.cookie.name === SUPABASE_AUTH_COOKIE_NAME || changeInfo.cookie.domain === OPEN_SAUCED_INSIGHTS_DOMAIN ) { - void checkAuthentication(); + void checkAuthentication(hasOptedLogOut, chrome.cookies.get, checkTokenValidity, setAccessTokenInChromeStorage, removeAuthTokenFromStorage, console.error); } }); -chrome.runtime.onInstalled.addListener(checkAuthentication); +chrome.runtime.onInstalled.addListener(() => { + void checkAuthentication(hasOptedLogOut, chrome.cookies.get, checkTokenValidity, setAccessTokenInChromeStorage, removeAuthTokenFromStorage, console.error); +}); chrome.runtime.onInstalled.addListener(setDefaultDescriptionConfig); -chrome.runtime.onStartup.addListener(checkAuthentication); +chrome.runtime.onStartup.addListener(() => { + void checkAuthentication(hasOptedLogOut, chrome.cookies.get, checkTokenValidity, setAccessTokenInChromeStorage, removeAuthTokenFromStorage, console.error); +}); diff --git a/test/utils/checkAuthentication.test.ts b/test/utils/checkAuthentication.test.ts new file mode 100644 index 00000000..3cb92a3e --- /dev/null +++ b/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(); + }); +}) + \ No newline at end of file From 39eac97ec1fe24ab07612b6b37fd8073c7fba806 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Sun, 28 May 2023 12:55:58 +0530 Subject: [PATCH 2/2] call check user auth once --- src/worker/background.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/worker/background.ts b/src/worker/background.ts index 68f766ff..3bd0b63d 100644 --- a/src/worker/background.ts +++ b/src/worker/background.ts @@ -4,19 +4,24 @@ import { setDefaultDescriptionConfig } from "../utils/aiprdescription/descriptio 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(hasOptedLogOut, chrome.cookies.get, checkTokenValidity, setAccessTokenInChromeStorage, removeAuthTokenFromStorage, console.error); + checkUserAuthentication(); } }); chrome.runtime.onInstalled.addListener(() => { - void checkAuthentication(hasOptedLogOut, chrome.cookies.get, checkTokenValidity, setAccessTokenInChromeStorage, removeAuthTokenFromStorage, console.error); + checkUserAuthentication(); }); chrome.runtime.onInstalled.addListener(setDefaultDescriptionConfig); chrome.runtime.onStartup.addListener(() => { - void checkAuthentication(hasOptedLogOut, chrome.cookies.get, checkTokenValidity, setAccessTokenInChromeStorage, removeAuthTokenFromStorage, console.error); + checkUserAuthentication(); });