[extension] Redesign: the popup in layout C with the custos room, Custos's theme, a new password on sign-up and change-password forms, Save or Update
This commit is contained in:
@@ -15,7 +15,7 @@ const code = fs.readFileSync(path.join(root, 'extension', 'background.js'), 'utf
|
||||
const ID = 'custos-test-id';
|
||||
|
||||
/** A fresh background with a fake API. `host(req)` answers the native host's requests. */
|
||||
function background(host) {
|
||||
function background(host, console = globalThis.console) {
|
||||
const sent = [];
|
||||
let listener = null;
|
||||
const port = {
|
||||
@@ -28,7 +28,7 @@ function background(host) {
|
||||
};
|
||||
const tabs = [{ id: 1, url: 'https://github.com/login', active: true }];
|
||||
const api = {
|
||||
runtime: { id: ID, connectNative: () => port, onMessage: { addListener: (f) => (listener = f) } },
|
||||
runtime: { id: ID, getURL: (p) => `chrome-extension://${ID}/${p}`, connectNative: () => port, onMessage: { addListener: (f) => (listener = f) } },
|
||||
tabs: { query: async () => tabs, onRemoved: { addListener: () => {} } },
|
||||
action: { setBadgeText: async () => {}, setBadgeBackgroundColor: async () => {} },
|
||||
};
|
||||
@@ -104,9 +104,9 @@ test('a login offered for saving never comes back out, and goes only to Custos',
|
||||
const b = background((m) => (m.op === 'browser_save' ? { ok: true, saved: 'new' } : { ok: true, code: '123456' }));
|
||||
const at = top('https://github.com/login');
|
||||
const offered = await b.call({ kind: 'offer-save', username: 'mark', password: 'hunter2-secret' }, at);
|
||||
assert.deepEqual(offered, { ok: true, offer: { host: 'github.com', username: 'mark' } });
|
||||
assert.deepEqual(offered, { ok: true, offer: { host: 'github.com', username: 'mark', update: false } });
|
||||
// Another page of the site in the same tab sees the offer, without the password; a frame or another site does not.
|
||||
assert.deepEqual(await b.call({ kind: 'pending-save' }, top('https://github.com/home')), { ok: true, offer: { host: 'github.com', username: 'mark' } });
|
||||
assert.deepEqual(await b.call({ kind: 'pending-save' }, top('https://github.com/home')), { ok: true, offer: { host: 'github.com', username: 'mark', update: false } });
|
||||
assert.deepEqual(await b.call({ kind: 'pending-save' }, frame('https://github.com/x', 'https://github.com/')), { ok: true, offer: null });
|
||||
assert.deepEqual(await b.call({ kind: 'pending-save' }, top('https://evil.net/')), { ok: true, offer: null });
|
||||
const popup = await b.call({ kind: 'match-tab' }, { url: `chrome-extension://${ID}/popup.html` });
|
||||
@@ -127,6 +127,59 @@ test('an offer needs a password, from a web page', async () => {
|
||||
assert.equal((await b.call({ kind: 'offer-save', username: 'm', password: 'x'.repeat(4097) }, top('https://a.cz/'))).ok, false);
|
||||
});
|
||||
|
||||
test('a new password is asked for by origin only, and goes back only to the frame that asked', async () => {
|
||||
const b = background((m) => (m.op === 'browser_generate' ? { ok: true, password: 'Gen-3rated!pw', bits: 128 } : { ok: true }));
|
||||
const r = await b.call({ kind: 'generate', length: 4, origin: 'https://bank.cz' }, frame('https://accounts.example/signup', 'https://example.com/'));
|
||||
assert.deepEqual(r, { ok: true, password: 'Gen-3rated!pw', bits: 128, seq: 1 });
|
||||
assert.deepEqual(b.sent.map((m) => [m.op, m.origin, m.top, m.length]), [['browser_generate', 'https://accounts.example', 'https://example.com', undefined]]);
|
||||
for (const s of [{ ...top('https://a.cz/'), origin: 'null' }, top('about:blank'), top('file:///x.html')]) {
|
||||
assert.deepEqual(await b.call({ kind: 'generate' }, s), { ok: false, error: 'no_match' });
|
||||
}
|
||||
assert.equal(b.sent.length, 1);
|
||||
});
|
||||
|
||||
test('the popup’s requests come only from the extension’s own pages, never from a content script', async () => {
|
||||
const b = background((m) => (m.op === 'browser_generate' ? { ok: true, password: 'Gen-3rated!pw', bits: 128 } : { ok: true, totp: '123456', logins: [] }));
|
||||
const popup = { url: `chrome-extension://${ID}/popup.html` };
|
||||
for (const kind of ['match-tab', 'totp-tab', 'generate-tab', 'save-tab', 'dismiss-tab']) {
|
||||
assert.deepEqual(await b.call({ kind, id: 'a' }, frame('https://evil.net/ad', 'https://github.com/')), { ok: false, error: 'bad_request' }, kind);
|
||||
assert.deepEqual(await b.call({ kind, id: 'a' }, top('https://github.com/')), { ok: false, error: 'bad_request' }, kind);
|
||||
}
|
||||
assert.equal(b.sent.length, 0);
|
||||
const g = await b.call({ kind: 'generate-tab' }, popup);
|
||||
assert.deepEqual([g.password, g.origin], ['Gen-3rated!pw', 'https://github.com']);
|
||||
assert.deepEqual(b.sent.map((m) => [m.op, m.origin, m.top]), [['browser_generate', 'https://github.com', 'https://github.com']]);
|
||||
});
|
||||
|
||||
test('an offer says Update when Custos has that login, and a change-password form finds its one login', async () => {
|
||||
const logins = [{ id: 'a', title: 'GitHub', username: 'mark', totp: false }];
|
||||
const b = background((m) => (m.op === 'browser_match' ? { ok: true, logins } : { ok: true, saved: 'updated' }));
|
||||
const at = top('https://github.com/settings/password');
|
||||
assert.deepEqual(await b.call({ kind: 'offer-save', username: 'mark', password: 'new-pw-1' }, at), { ok: true, offer: { host: 'github.com', username: 'mark', update: true } });
|
||||
// No username on the form, and one login for the site: that login is the one that changes.
|
||||
assert.deepEqual(await b.call({ kind: 'offer-save', username: '', password: 'new-pw-2' }, at), { ok: true, offer: { host: 'github.com', username: 'mark', update: true } });
|
||||
assert.deepEqual(await b.call({ kind: 'offer-save', username: 'petra', password: 'new-pw-3' }, at), { ok: true, offer: { host: 'github.com', username: 'petra', update: false } });
|
||||
// A locked Custos cannot say: it is offered as new, and Custos's card decides.
|
||||
const locked = background((m) => (m.op === 'browser_match' ? { ok: false, error: 'locked' } : { ok: true }));
|
||||
assert.deepEqual(await locked.call({ kind: 'offer-save', username: '', password: 'p' }, at), { ok: true, offer: { host: 'github.com', username: '', update: false } });
|
||||
});
|
||||
|
||||
test('nothing is ever written to the console, and no script of the extension can', async () => {
|
||||
const said = [];
|
||||
const quiet = new Proxy({}, { get: () => (...a) => said.push(a) });
|
||||
const b = background((m) => (m.op === 'browser_generate' ? { ok: true, password: 'Gen-3rated!pw' } : { ok: true, logins: [], saved: 'new' }), quiet);
|
||||
const at = top('https://github.com/login');
|
||||
await b.call({ kind: 'offer-save', username: 'mark', password: 'hunter2-secret' }, at);
|
||||
await b.call({ kind: 'save' }, at);
|
||||
await b.call({ kind: 'generate' }, at);
|
||||
await b.call({ kind: 'fill', id: 'a' }, at);
|
||||
await b.call({ kind: 'nonsense' }, at);
|
||||
assert.deepEqual(said, []);
|
||||
for (const f of fs.readdirSync(path.join(root, 'extension')).filter((f) => f.endsWith('.js'))) {
|
||||
assert.ok(!/\bconsole\s*\.|\bdebugger\b/.test(fs.readFileSync(path.join(root, 'extension', f), 'utf8')), f);
|
||||
}
|
||||
});
|
||||
|
||||
let failed = 0;
|
||||
for (const [name, f] of tests) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user