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

WIP: feat: Enhanced Client Side Auth Injection #397

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
feat: Enhanced Client Side Auth Injection
andreituicu committed Dec 9, 2024
commit 255d71f4aa161a9b6c42abf50f896259db680ded
4 changes: 0 additions & 4 deletions src/extension/auth.js
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@
import { log } from './log.js';
import { getConfig, setConfig } from './config.js';
import { ADMIN_ORIGIN } from './utils/admin.js';
// import { checkTab, getCurrentTab } from './tab.js';

const { host: adminHost } = new URL(ADMIN_ORIGIN);

@@ -38,7 +37,7 @@
});

if (!projectFilter || projectFilter.length === 0) {
return;

Check warning on line 40 in src/extension/auth.js

Codecov / codecov/patch

src/extension/auth.js#L40

Added line #L40 was not covered by tests
}

const sessionProjects = await getConfig('session', 'projects') || [];
@@ -60,7 +59,7 @@
}
});
} else {
result = sessionProjects;

Check warning on line 62 in src/extension/auth.js

Codecov / codecov/patch

src/extension/auth.js#L62

Added line #L62 was not covered by tests
}

const addRulesPromises = result.map(async ({
@@ -234,7 +233,4 @@
}
await setConfig('session', { projects });
}

// const currentTab = await getCurrentTab();
// await checkTab(currentTab.id);
}
68 changes: 47 additions & 21 deletions test/auth.test.js
Original file line number Diff line number Diff line change
@@ -17,9 +17,9 @@ import { expect } from '@open-wc/testing';
import { setUserAgent } from '@web/test-runner-commands';
import sinon from 'sinon';

import { configureAuthAndCorsHeaders, setAuthToken } from '../src/extension/auth.js';
import { setAuthToken } from '../src/extension/auth.js';
import chromeMock from './mocks/chrome.js';
import { error } from './test-utils.js';
import { checkTab } from '../src/extension/tab.js';

// @ts-ignore
window.chrome = chromeMock;
@@ -35,21 +35,6 @@ describe('Test auth', () => {
sandbox.restore();
});

it('configureAuthAndCorsHeaders', async () => {
const getSessionRules = sandbox.stub(chrome.declarativeNetRequest, 'getSessionRules')
// @ts-ignore
.resolves([{ id: 1 }]);
const updateSessionRules = sandbox.spy(chrome.declarativeNetRequest, 'updateSessionRules');
await configureAuthAndCorsHeaders();
expect(getSessionRules.called).to.be.true;
expect(updateSessionRules.called).to.be.true;
// error handling
updateSessionRules.restore();
sandbox.stub(chrome.declarativeNetRequest, 'updateSessionRules')
.throws(error);
await configureAuthAndCorsHeaders();
});

it('setAuthToken', async () => {
const updateSessionRules = sandbox.spy(chrome.declarativeNetRequest, 'updateSessionRules');
const setConfig = sandbox.spy(chrome.storage.session, 'set');
@@ -69,21 +54,57 @@ describe('Test auth', () => {

// set auth token
await setAuthToken(owner, repo, authToken, authTokenExpiry);
expect(setConfig.callCount).to.be.equal(1);
expect(setConfig.calledWith({
projects: [
{
id: owner,
owner,
repo,
authToken,
authTokenExpiry: authTokenExpiry * 1000,
picture: undefined,
},
],
})).to.be.true;

await checkTab(mockTab.id);

// update auth token without expiry
await setAuthToken(owner, repo, authToken);
expect(setConfig.callCount).to.be.equal(2);
expect(setConfig.calledWith({
projects: [
{
id: owner,
owner,
repo,
authToken,
authTokenExpiry: 0,
picture: undefined,
},
],
})).to.be.true;
await checkTab(mockTab.id);

// remove auth token
await setAuthToken(owner, repo, '');
expect(setConfig.callCount).to.equal(3);
expect(setConfig.calledWith({
projects: [],
})).to.be.true;
await checkTab(mockTab.id);

// remove auth token again
await setAuthToken(owner, repo, '');
expect(setConfig.callCount).to.equal(4);
expect(setConfig.calledWith({
projects: [],
})).to.be.true;
await checkTab(mockTab.id);

// testing else paths
setConfig.resetHistory();
// @ts-ignore
await setAuthToken();
expect(setConfig.notCalled).to.be.true;
await checkTab(mockTab.id);

expect(updateSessionRules.calledWith({
addRules: [
@@ -178,6 +199,7 @@ describe('Test auth', () => {

await setAuthToken(owner, repo, authToken);
expect(setConfig.callCount).to.equal(1);
await checkTab(mockTab.id);

expect(updateSessionRules.calledWith({
addRules: [
@@ -342,6 +364,7 @@ describe('Test auth', () => {

await setAuthToken(owner, repo, authToken, expiry, siteToken, expiry);
expect(setConfig.callCount).to.equal(1);
await checkTab(mockTab.id);

expect(updateSessionRules.calledWith({
addRules: [
@@ -437,11 +460,14 @@ describe('Test auth', () => {
// update existing auth and site tokens
expiry = Date.now() / 1000 + 120;
await setAuthToken(owner, repo, authToken, expiry, siteToken, expiry);
await checkTab(mockTab.id);
expect(setConfig.callCount).to.equal(2);
expect(updateSessionRules.callCount).to.equal(4);

// remove existing auth and site tokens
await setAuthToken(owner, repo, '', undefined, '', undefined);
await checkTab(mockTab.id);

expect(setConfig.callCount).to.equal(3);
expect(updateSessionRules.callCount).to.equal(5);
});

Unchanged files with check annotations Beta

if (!isGetAuthInfoTrustedOrigin(new URL(url).origin)) {
await configureAuthAndCorsHeaders(matches);
} else {
await configureAuthAndCorsHeaders('all');

Check warning on line 75 in src/extension/tab.js

Codecov / codecov/patch

src/extension/tab.js#L75

Added line #L75 was not covered by tests
}
updateUI({