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

test: add test #237

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions e2e/helpers/DefaultTestEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ export class DefaultTestEnv implements TestEnv {
`--disable-extensions-except=${this.options.extensionDirPath}`,
`--load-extension=${this.options.extensionDirPath}`,
],
permissions: ['clipboard-read'],
});
this._context.setDefaultTimeout(5000);

let [background] = this.context.serviceWorkers();
if (!background) background = await this.context.waitForEvent('serviceworker');
Expand All @@ -138,8 +140,7 @@ export class DefaultTestEnv implements TestEnv {
await asyncSleep(200);

await background.evaluate(async (data) => {
// @ts-ignore
await chrome.storage.local.set(data);
await chrome.storage.local.set(data as Record<string, any>);
}, getDefaultStorageData());
}
}
Expand Down Expand Up @@ -184,8 +185,7 @@ export class DefaultTestEnv implements TestEnv {
request: async (payload) => {
const res = thePage.evaluate(async (payload) => {
await new Promise((resolve) => setTimeout(resolve, 10));
// @ts-ignore
return window.ckb.request(payload);
return window.ckb.request(payload as never);
}, payload);

if (this.options.autoApproveEnable && payload.method === 'wallet_enable') {
Expand Down
18 changes: 18 additions & 0 deletions e2e/mock-page-with-disallowed-iframe/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>Mock E2E Phishing Page</title>
</head>
<body>
<label for="url">link:</label>
<input type="text" id="url" placeholder="https://demo-nexus.vercel.app/" />
<button onclick="loadPage()">show</button>
<br /><br />
<iframe id="page" style="width: 100%; height: 80vh"></iframe>
<script>
function loadPage() {
document.getElementById('page').src = document.getElementById('url').value;
}
</script>
</body>
</html>
10 changes: 10 additions & 0 deletions e2e/mock-page-with-iframe/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Mock E2E Phishing Page</title>
</head>
<body>
<div>Hello</div>
<iframe src="https://demo-nexus.vercel.app/" width="900" height="900"></iframe>
</body>
</html>
43 changes: 43 additions & 0 deletions e2e/tests/account-detail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { DefaultTestEnv } from '../helpers';

DefaultTestEnv.setupTest({ initWalletWithDefaults: true });

describe('Show wallet details', function () {
it('should show nick name for wallet', async () => {
const page = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await page.getByText(testEnv.defaultE2eData.nickname).waitFor();
});

it('should show disconnected for the wallet', async () => {
const page = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await page.getByText('Disconnected').waitFor();
});
it('should show whitelist sites for the wallet', async () => {
const page = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await page.getByRole('button', { name: 'Whitelist Sites' }).click();
await page.getByText('No whitelist sites found.').waitFor();
});
it('should show networks list for the wallet', async () => {
const page = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await page.getByRole('button', { name: 'Network' }).click();
await page.getByText('Mainnet').waitFor();
await page.getByText('Testnet').waitFor();
});

it('should show feedback for the wallet', async () => {
const page = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await page.getByRole('link', { name: 'Feedback' }).click();
await page.waitForTimeout(1000);
expect(testEnv.context.pages().some((page) => page.url().includes('ckb-js/nexus/issues'))).toBe(true);
});
});
62 changes: 62 additions & 0 deletions e2e/tests/add-network.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { DefaultTestEnv } from '../helpers';

DefaultTestEnv.setupTest({ initWalletWithDefaults: true });

describe('add network', function () {
it('should work when the add name too long', async () => {
const page = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await page.getByRole('button', { name: 'Network' }).click();
await page.getByRole('button', { name: 'Add Network' }).click();

const tooLongName =
'too too too too too long long long long long long long long long long long long long long long long long long long long long long long long';
await page.getByLabel('Name').fill(tooLongName);
await page.getByLabel('URL').fill('https://testnet.ckbapp.dev/');
await page.getByRole('button', { name: 'Add' }).click();
await page.getByText(tooLongName).waitFor();
});

it('should work when the add name exist', async () => {
const page = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await page.getByRole('button', { name: 'Network' }).click();
await page.getByRole('button', { name: 'Add Network' }).click();

const existName = 'Testnet';
await page.getByLabel('Name').fill(existName);
await page.getByLabel('URL').fill('https://testnet.ckbapp.dev/');
await page.getByRole('button', { name: 'Add' }).click();
await page.getByText(existName).allInnerTexts();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should get inner tests and asset it to contain two Testnet

});

it('should work when the add url is not ckb rpc', async () => {
const page = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await page.getByRole('button', { name: 'Network' }).click();
await page.getByRole('button', { name: 'Add Network' }).click();

const randName = 'rand test name';
await page.getByLabel('Name').fill(randName);
await page.getByLabel('URL').fill('https://www.baidu.com');
await page.getByRole('button', { name: 'Add' }).click();
await page.getByText(randName).allInnerTexts();
});

it('should work when the add url is ckb rpc', async () => {
const page = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await page.goto(`chrome-extension://${extensionId}/popup.html`);
await page.getByRole('button', { name: 'Network' }).click();
await page.getByRole('button', { name: 'Add Network' }).click();

const randName = 'rand name';
await page.getByLabel('Name').fill(randName);
await page.getByLabel('URL').fill('https://testnet.ckbapp.dev/');
await page.getByRole('button', { name: 'Add' }).click();
await page.getByText(randName).allInnerTexts();
});
});
99 changes: 99 additions & 0 deletions e2e/tests/add-whitelist-site.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { DefaultTestEnv } from '../helpers';
import { asyncSleep } from '@nexus-wallet/utils';

DefaultTestEnv.setupTest({ initWalletWithDefaults: true });
describe('add whitelist site', function () {
it('should request failed before add whitelist', async () => {
try {
await ckb.request({ method: 'wallet_fullOwnership_getOffChainLocks' });
} catch (error) {
expect(`${error}`).toMatch(/not in the whitelist/);
return;
}
expect('').toBe('failed');
});

/**
* TODO: impl localhost can be visited
*/
it.todo('should work that add localhost url');
/**
* TODO: impl 192.168 can be visited
*/
it.todo('should work that add 192.168.. url');

it('Should wallet_enable work that add url protocol is HTTP', async () => {
const httpUrl = 'http://info.cern.ch';
await page.goto(httpUrl);
const enableTask = ckb.request({ method: 'wallet_enable' });

const notificationPage = await testEnv.getNotificationPage();
await notificationPage.getByRole('button', { name: 'Connect' }).click();

const res = await enableTask;
expect(res.nickname).toBe(testEnv.defaultE2eData.nickname);
});

it('Should wallet_enable work that add url protocol is HTTPS', async () => {
const httpsUrl = 'https://github.com';
await page.goto(httpsUrl);
const enableTask = ckb.request({ method: 'wallet_enable' });

const notificationPage = await testEnv.getNotificationPage();
await notificationPage.getByRole('button', { name: 'Connect' }).click();

const res = await enableTask;
expect(res.nickname).toBe(testEnv.defaultE2eData.nickname);
});

/**
* skip reason :TODO: check url is in the box
*/
it.skip('should work that add url is too long', async () => {
const tooLongUrl = 'https://mememmememememmemememmememememmemememmememememme.bit.cc/';
await page.goto(tooLongUrl);
const enableTask = ckb.request({ method: 'wallet_enable' });

const notificationPage = await testEnv.getNotificationPage();
await notificationPage.getByRole('button', { name: 'Connect' }).click();

const res = await enableTask;
expect(res.nickname).toBe(testEnv.defaultE2eData.nickname);
});

it('should request work after add whitelist ', async () => {
const enableTask = ckb.request({ method: 'wallet_enable' });

await asyncSleep(1000);
const notificationPage = await testEnv.getNotificationPage();
await notificationPage.getByRole('button', { name: 'Connect' }).click();

const res = await enableTask;
expect(res.nickname).toBe(testEnv.defaultE2eData.nickname);

await ckb.request({ method: 'wallet_fullOwnership_getOffChainLocks', params: {} });
});

it('should find url in whitelist after add whitelist', async () => {
const enableTask = ckb.request({ method: 'wallet_enable' });

const notificationPage = await testEnv.getNotificationPage();
await notificationPage.getByRole('button', { name: 'Connect' }).click();

const res = await enableTask;
expect(res.nickname).toBe(testEnv.defaultE2eData.nickname);

const page = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await page.goto(`chrome-extension://${extensionId}/popup.html`);

await page.getByRole('button', { name: 'Whitelist Sites' }).click();
// await page.getByRole('').fill(urlTransferDomainName(testEnv.defaultE2eData.localServerUrl))
await page.getByText(urlTransferDomainName(testEnv.defaultE2eData.localServerUrl));
});
});

export function urlTransferDomainName(url: string): string {
const match = url.match(/^https?:\/\/([^/]+)/);
return match ? match[1] : '';
}
67 changes: 67 additions & 0 deletions e2e/tests/change-network.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { DefaultTestEnv } from '../helpers';

DefaultTestEnv.setupTest({ initWalletWithDefaults: true });

describe('Change network', function () {
it('should get networkChanged when network change', async () => {
await page.evaluate(() => {
window.ckbNetworkName = '';
window.ckb.on('networkChanged', (networkName: string) => {
window.ckbNetworkName = networkName;
});
});
const extensionIdPage = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await extensionIdPage.goto(`chrome-extension://${extensionId}/popup.html`);
await extensionIdPage.getByRole('button', { name: 'Network' }).click();
await extensionIdPage.getByText('Mainnet').click();
let ckbNetworkName = await page.evaluate(() => {
return window.ckbNetworkName;
});
expect(ckbNetworkName).toBe('ckb');
await extensionIdPage.getByText('Testnet').click();
ckbNetworkName = await page.evaluate(() => {
return window.ckbNetworkName;
});
expect(ckbNetworkName).toBe('ckb_testnet');
});
it('should get ckb name that change network is user added', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a composed test case for "add network" and "change network", is it necessary?

const extensionIdPage = await testEnv.context.newPage();
const extensionId = testEnv.extensionId;
await extensionIdPage.goto(`chrome-extension://${extensionId}/popup.html`);
await extensionIdPage.getByRole('button', { name: 'Network' }).click();
await extensionIdPage.getByRole('button', { name: 'Add Network' }).click();

const randName = 'user test net name';
await extensionIdPage.getByLabel('Name').fill(randName);
await extensionIdPage.getByLabel('URL').fill('https://testnet.ckbapp.dev/');
await extensionIdPage.getByRole('button', { name: 'Add' }).click();
await extensionIdPage.getByText(randName).allInnerTexts();

await extensionIdPage.getByRole('button', { name: 'Add Network' }).click();
const notCkbName = 'user add not ckb test net name';
await extensionIdPage.getByLabel('Name').fill(notCkbName);
await extensionIdPage.getByLabel('URL').fill('https://github.com');
await extensionIdPage.getByRole('button', { name: 'Add' }).click();
await extensionIdPage.getByText(notCkbName).allInnerTexts();
await page.evaluate(() => {
window.ckbNetworkName = '';
window.ckb.on('networkChanged', (networkName: string) => {
window.ckbNetworkName = networkName;
});
});
await extensionIdPage.getByText(randName).click();
let ckbNetworkName = await page.evaluate(() => {
return window.ckbNetworkName;
});
expect(ckbNetworkName).toBe(randName);

await extensionIdPage.getByText(notCkbName).click();

ckbNetworkName = await page.evaluate(() => {
return window.ckbNetworkName;
});

expect(ckbNetworkName).toBe(notCkbName);
});
});
Loading