Skip to content
Open
Show file tree
Hide file tree
Changes from 14 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
2 changes: 2 additions & 0 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 Down
1 change: 1 addition & 0 deletions e2e/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const config: JestConfigWithTsJest = {
transform: {
'^.+\\.ts$': 'ts-jest',
},
testTimeout: 15000,
};

export default config;
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 account details', function () {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should not use word account as a term on UTXO wallet.

it('should show nick name for account', 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 account', 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 account', 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 account', 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 account', 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
Copy Markdown
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();
});
});
127 changes: 127 additions & 0 deletions e2e/tests/add-whitelist-site.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
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');
});

it('should get networkChange that the url is not in whitelist', async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

seems this test case should be moved to "change-network.test.ts"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems a duplicate test, I will remove it.

await page.evaluate(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the purpose of this patch watching current nexus network? maybe use ckb_getBlockchainInfo instead

// @ts-ignore

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do not need it anymore.

window.ckbNetworkName = '';
window.ckb.on('networkChanged', (networkName: string) => {
// @ts-ignore
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(() => {
// @ts-ignore
return window.ckbNetworkName;
});
expect(ckbNetworkName).toBe('ckb');
await extensionIdPage.getByText('Testnet').click();
ckbNetworkName = await page.evaluate(() => {
// @ts-ignore
return window.ckbNetworkName;
});
expect(ckbNetworkName).toBe('ckb_testnet');
});

/**
* 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 work that add url is http', async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should wallet_enable work that add url prefix is http

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 work that add url 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] : '';
}
75 changes: 75 additions & 0 deletions e2e/tests/change-network.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { DefaultTestEnv } from '../helpers';

DefaultTestEnv.setupTest({ initWalletWithDefaults: true });

describe('Change network', function () {
it('should get networkChanged when network change', async () => {
await page.evaluate(() => {
// @ts-ignore
window.ckbNetworkName = '';
window.ckb.on('networkChanged', (networkName: string) => {
// @ts-ignore
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(() => {
// @ts-ignore
return window.ckbNetworkName;
});
expect(ckbNetworkName).toBe('ckb');
await extensionIdPage.getByText('Testnet').click();
ckbNetworkName = await page.evaluate(() => {
// @ts-ignore
return window.ckbNetworkName;
});
expect(ckbNetworkName).toBe('ckb_testnet');
});
it('should get ckb name that change network is user added', async () => {

Copy link
Copy Markdown
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(() => {
// @ts-ignore
window.ckbNetworkName = '';
window.ckb.on('networkChanged', (networkName: string) => {
// @ts-ignore
window.ckbNetworkName = networkName;
});
});
await extensionIdPage.getByText(randName).click();
let ckbNetworkName = await page.evaluate(() => {
// @ts-ignore
return window.ckbNetworkName;
});
expect(ckbNetworkName).toBe(randName);

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

ckbNetworkName = await page.evaluate(() => {
// @ts-ignore
return window.ckbNetworkName;
});

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