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

feat: allow tools.aem.live and labs.aem.live to list configured sites #468

Merged
merged 2 commits into from
Feb 23, 2025
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
25 changes: 22 additions & 3 deletions src/extension/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export async function showSidekickNotification(tabId, data, callback) {
* @param {string} origin
* @returns {boolean} true - trusted / false - untrusted
*/
function isGetAuthInfoTrustedOrigin(origin) {
function isTrustedOrigin(origin) {
const TRUSTED_ORIGINS = [
ADMIN_ORIGIN,
'https://labs.aem.live',
Expand Down Expand Up @@ -138,10 +138,10 @@ function isGetAuthInfoTrustedOrigin(origin) {
* Returns the organizations the user is currently authenticated for.
* @returns {Promise<string[]>} The organizations
*/
async function getAuthInfo(message, sender) {
async function getAuthInfo(_, sender) {
const { origin } = new URL(sender.url);

if (!isGetAuthInfoTrustedOrigin(origin)) {
if (!isTrustedOrigin(origin)) {
return []; // don't give out any information
}

Expand All @@ -151,6 +151,24 @@ async function getAuthInfo(message, sender) {
.map(({ owner }) => owner);
}

/**
* Returns the configured sites.
* @returns {Promise<Object[]>} The sites
*/
async function getSites(_, sender) {
const { origin } = new URL(sender.url);

if (!isTrustedOrigin(origin)) {
return []; // don't give out any information
}

return (await getConfig('sync', 'projects') || [])
.map((handle) => {
const [org, site] = handle.split('/');
return { org, site };
});
}

/**
* Adds or removes a project based on the tab's URL
* @param {chrome.tabs.Tab} tab The tab
Expand Down Expand Up @@ -306,4 +324,5 @@ export const internalActions = {
export const externalActions = {
updateAuthToken,
getAuthInfo,
getSites,
};
49 changes: 49 additions & 0 deletions test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,55 @@ describe('Test actions', () => {
expect(resp).to.deep.equal([]);
});

it('external: getSites', async () => {
const getStub = sandbox.stub(chrome.storage.sync, 'get');
const projects = [
'foo/bar',
'foo1/baz',
'foo2/baz',
];
const expectedOutput = [
{ org: 'foo', site: 'bar' },
{ org: 'foo1', site: 'baz' },
{ org: 'foo2', site: 'baz' },
];

let resp;

// without projects
getStub.resolves({});
resp = await externalActions.getSites({}, mockTab('https://tools.aem.live'));
expect(resp).to.deep.equal([]);

// with auth info
getStub.resolves({
projects,
});

// trusted actors
resp = await externalActions.getSites({}, mockTab('https://tools.aem.live/foo'));
expect(resp).to.deep.equal(expectedOutput);

resp = await externalActions.getSites({}, mockTab('https://labs.aem.live/foo'));
expect(resp).to.deep.equal(expectedOutput);

resp = await externalActions.getSites({}, mockTab('https://feature--helix-labs-website--adobe.aem.page/feature'));
expect(resp).to.deep.equal(expectedOutput);

// untrusted actors
resp = await externalActions.getSites({}, mockTab('https://evil.live'));
expect(resp).to.deep.equal([]);

resp = await externalActions.getSites({}, mockTab('https://main--site--owner.aem.live'));
expect(resp).to.deep.equal([]);

resp = await externalActions.getSites({}, mockTab('https://tools.aem.live.evil.com'));
expect(resp).to.deep.equal([]);

resp = await externalActions.getSites({}, mockTab('https://main--helix-tools-website--adobe-evl.aem.live'));
expect(resp).to.deep.equal([]);
});

it('internal: addRemoveProject', async () => {
const set = sandbox.spy(chrome.storage.sync, 'set');
const remove = sandbox.spy(chrome.storage.sync, 'remove');
Expand Down