Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for checkAuth.ts (#149)
* test: add tests for checkAuth.ts * call check user auth once
- Loading branch information
Showing
3 changed files
with
78 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}) | ||
|