Skip to content

Commit

Permalink
fix: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandepass committed Feb 21, 2024
1 parent 39ff039 commit 8203c7a
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 1 deletion.
26 changes: 25 additions & 1 deletion test/app/components/plugin/env-switcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
import fetchMock from 'fetch-mock/esm/client.js';
import sinon from 'sinon';
import { expect, waitUntil } from '@open-wc/testing';
import { recursiveQuery } from '../../../test-utils.js';
import { recursiveQuery, recursiveQueryAll } from '../../../test-utils.js';
import chromeMock from '../../../mocks/chrome.js';
import { AEMSidekick } from '../../../../src/extension/app/aem-sidekick.js';
import { mockFetchEnglishMessagesSuccess } from '../../../mocks/i18n.js';
import { defaultSidekickConfig } from '../../../fixtures/sidekick-config.js';
import {
mockFetchConfigJSONNotFound,
mockFetchStatusSuccess,
mockFetchStatusWithProfileUnauthorized,
} from '../../../mocks/helix-admin.js';
import '../../../../src/extension/index.js';
import { appStore } from '../../../../src/extension/app/store/app.js';
Expand Down Expand Up @@ -76,5 +77,28 @@ describe('Environment Switcher', () => {

switchEnvStub.restore();
});

it('not authorized - logged into a different account', async () => {
mockFetchStatusWithProfileUnauthorized();
mockFetchConfigJSONNotFound();
mockHelixEnvironment(document, 'preview');

const switchEnvStub = sinon.stub(appStore, 'switchEnv').resolves();

sidekick = new AEMSidekick(defaultSidekickConfig);
document.body.appendChild(sidekick);

await waitUntil(() => recursiveQuery(sidekick, 'action-bar-picker'));

const actionBar = recursiveQuery(sidekick, 'action-bar');
const envPlugin = recursiveQuery(actionBar, 'env-switcher');
const picker = recursiveQuery(envPlugin, 'action-bar-picker');
const menuItems = [...recursiveQueryAll(picker, 'sp-menu-item')];
menuItems.forEach((item) => {
expect(item.disabled).to.eq(true);
expect(item.querySelector('span').textContent).to.eq('Not authorized');
});
switchEnvStub.restore();
});
});
});
114 changes: 114 additions & 0 deletions test/app/store/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,4 +747,118 @@ describe('Test App Store', () => {
expect(resp.error).to.eq('Some error');
});
});

describe('login', () => {
let instance;
let clock;
let checkProfileStatusStub;
let sandbox;

beforeEach(() => {
instance = appStore;
sandbox = sinon.createSandbox();
clock = sandbox.useFakeTimers();
window.hlx = {};
window.hlx.sidekickConfig = {};

// window.chrome = { runtime: { id: 'test-extension-id' } };
// window.navigator.vendor = 'Non-Apple Vendor';
sandbox.stub(appStore, 'openPage').returns({ closed: true });
checkProfileStatusStub = sandbox.stub(appStore, 'checkProfileStatus').resolves(false);
});

afterEach(() => {
clock.restore();
sandbox.restore();
});

it('should attempt to check login status up to 5 times after login window is closed', async () => {
const modalSpy = sinon.spy();
EventBus.instance.addEventListener(EVENTS.OPEN_MODAL, modalSpy);

instance.login(false);

// Fast-forward time to simulate the retries
for (let i = 0; i < 5; i += 1) {
// eslint-disable-next-line no-await-in-loop
await clock.tickAsync(1000); // Fast-forward 1 second for each attempt
}

expect(checkProfileStatusStub.callCount).to.equal(5);

await waitUntil(() => modalSpy.called, 'Modal never opened');

expect(modalSpy.callCount).to.equal(2);
expect(modalSpy.args[1][0].detail.type).to.equal(MODALS.ERROR);
}).timeout(20000);

it('handles successful login correctly', async () => {
instance.sidekick = document.createElement('div');
checkProfileStatusStub.onCall(0).resolves(false);
checkProfileStatusStub.onCall(4).resolves(true); // Simulate success on the 5th attempt

const loginEventSpy = sinon.spy();
instance.sidekick.addEventListener('loggedin', loginEventSpy);

// Mock other methods called upon successful login
const initStoreStub = sandbox.stub(instance.siteStore, 'initStore').resolves();
const setupCorePluginsStub = sandbox.stub(instance, 'setupCorePlugins');
const fetchStatusStub = sandbox.stub(instance, 'fetchStatus');
const hideWaitStub = sandbox.stub(instance, 'hideWait');

instance.login(false); // Call without selectAccount

await clock.tickAsync(5000); // Fast-forward time

expect(initStoreStub.called).to.be.true;
expect(setupCorePluginsStub.called).to.be.true;
expect(fetchStatusStub.called).to.be.true;
expect(hideWaitStub.calledOnce).to.be.true;
}).timeout(20000);
});

describe('logout', () => {
let instance;
let clock;
let checkProfileStatusStub;
let sandbox;

beforeEach(() => {
instance = appStore;
sandbox = sinon.createSandbox();
clock = sandbox.useFakeTimers();
window.hlx = {};
window.hlx.sidekickConfig = {};

// window.chrome = { runtime: { id: 'test-extension-id' } };
// window.navigator.vendor = 'Non-Apple Vendor';
sandbox.stub(appStore, 'openPage').returns({ closed: true });
checkProfileStatusStub = sandbox.stub(appStore, 'checkProfileStatus').resolves(false);
});

afterEach(() => {
clock.restore();
sandbox.restore();
});

it('should attempt to check logout status up to 5 times after login window is closed', async () => {
const modalSpy = sinon.spy();
EventBus.instance.addEventListener(EVENTS.OPEN_MODAL, modalSpy);

instance.logout();

// Fast-forward time to simulate the retries
for (let i = 0; i < 5; i += 1) {
// eslint-disable-next-line no-await-in-loop
await clock.tickAsync(1000); // Fast-forward 1 second for each attempt
}

expect(checkProfileStatusStub.callCount).to.equal(5);

await waitUntil(() => modalSpy.called, 'Modal never opened');

expect(modalSpy.callCount).to.equal(2);
expect(modalSpy.args[1][0].detail.type).to.equal(MODALS.ERROR);
}).timeout(20000);
});
});
45 changes: 45 additions & 0 deletions test/fixtures/helix-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,51 @@ export const defaultStatusResponseWithProfile = {
},
};

export const defaultStatusLoggedInNotAuthorizedResponse = {
webPath: '/',
resourcePath: '/index.md',
live: {
status: 403,
url: 'https://main--aem-boilerplate--adobe.hlx.live/',
error: 'forbidden',
},
preview: {
status: 403,
url: 'https://main--aem-boilerplate--adobe.hlx.page/',
error: 'forbidden',
},
edit: {
status: 403,
},
code: {
status: 403,
},
links: {
status: 'https://admin.hlx.page/status/adobe/aem-boilerplate/main/',
preview: 'https://admin.hlx.page/preview/adobe/aem-boilerplate/main/',
live: 'https://admin.hlx.page/live/adobe/aem-boilerplate/main/',
code: 'https://admin.hlx.page/code/adobe/aem-boilerplate/main/',
logout: 'https://admin.hlx.page/logout/adobe/aem-boilerplate/main',
},
profile: {
iss: 'https://accounts.google.com',
aud: 'user-id.apps.googleusercontent.com',
sub: '1234',
hd: 'example.com',
email: 'foo@example.com',
email_verified: true,
name: 'Peter Parker',
picture: 'https://lh3.googleusercontent.com/a/user-id',
given_name: 'Peter',
family_name: 'Parker',
locale: 'en',
iat: 111,
exp: 222,
ttl: 333,
hlx_hash: 'J9y9hUNu5vJS9_38ck_POjo2FgQ',
},
};

export const defaultGdriveProfileResponse = {
status: 200,
profile: {
Expand Down
6 changes: 6 additions & 0 deletions test/mocks/helix-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
defaultGdriveProfileResponse,
defaultSharepointProfileResponse,
defaultStatusResponseWithProfile,
defaultStatusLoggedInNotAuthorizedResponse,
} from '../fixtures/helix-admin.js';
import {
getDefaultEditorEnviromentLocations,
Expand Down Expand Up @@ -69,6 +70,11 @@ export const mockFetchStatusWithProfileSuccess = () => fetchMock.get(defaultStat
body: defaultStatusResponseWithProfile,
}, { overwriteRoutes: true });

export const mockFetchStatusWithProfileUnauthorized = () => fetchMock.get(defaultStatusUrl, {
status: 200,
body: defaultStatusLoggedInNotAuthorizedResponse,
}, { overwriteRoutes: true });

export const defaultProfileUrl = 'https://admin.hlx.page/profile/adobe/aem-boilerplate/main';
export const mockFetchProfileSuccess = (overrides = {}, contentSource = 'sharepoint') => fetchMock.get(defaultProfileUrl, {
status: 200,
Expand Down

0 comments on commit 8203c7a

Please sign in to comment.