signal-desktop/ts/test-mock/pnp/username_test.ts

388 lines
12 KiB
TypeScript
Raw Normal View History

// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { Proto, StorageState } from '@signalapp/mock-server';
import type { PrimaryDevice } from '@signalapp/mock-server';
2023-07-20 03:14:08 +00:00
import { usernames } from '@signalapp/libsignal-client';
import createDebug from 'debug';
import * as durations from '../../util/durations';
import { uuidToBytes } from '../../util/uuidToBytes';
import { MY_STORY_ID } from '../../types/Stories';
import { Bootstrap } from '../bootstrap';
import type { App } from '../bootstrap';
2024-04-03 17:17:39 +00:00
import { bufferToUuid, typeIntoInput } from '../helpers';
2023-11-02 19:42:31 +00:00
import { contactByEncryptedUsernameRoute } from '../../util/signalRoutes';
export const debug = createDebug('mock:test:username');
const IdentifierType = Proto.ManifestRecord.Identifier.Type;
const USERNAME = 'signalapp.55';
2023-02-08 17:14:59 +00:00
const NICKNAME = 'signalapp';
const CARL_USERNAME = 'carl.84';
describe('pnp/username', function (this: Mocha.Suite) {
this.timeout(durations.MINUTE);
let bootstrap: Bootstrap;
let app: App;
let usernameContact: PrimaryDevice;
beforeEach(async () => {
bootstrap = new Bootstrap({ contactCount: 0 });
await bootstrap.init();
const { server, phone } = bootstrap;
usernameContact = await server.createPrimaryDevice({
profileName: 'ACI Contact',
});
let state = StorageState.getEmpty();
state = state.updateAccount({
profileKey: phone.profileKey.serialize(),
e164: phone.device.number,
});
state = state.addContact(usernameContact, {
username: USERNAME,
serviceE164: undefined,
});
// Put contact into left pane
state = state.pin(usernameContact);
// Add my story
state = state.addRecord({
type: IdentifierType.STORY_DISTRIBUTION_LIST,
record: {
storyDistributionList: {
allowsReplies: true,
identifier: uuidToBytes(MY_STORY_ID),
isBlockList: true,
name: MY_STORY_ID,
2023-08-16 20:54:39 +00:00
recipientServiceIds: [],
},
},
});
await phone.setStorageState(state);
app = await bootstrap.link();
});
afterEach(async function (this: Mocha.Context) {
await bootstrap.maybeSaveLogs(this.currentTest, app);
await app.close();
await bootstrap.teardown();
});
2023-02-13 22:40:11 +00:00
for (const type of ['profile', 'system']) {
// eslint-disable-next-line no-loop-func
it(`drops username when contact's ${type} name becomes known`, async () => {
const { phone } = bootstrap;
const window = await app.getWindow();
const leftPane = window.locator('#LeftPane');
2023-02-13 22:40:11 +00:00
debug('find username in the left pane');
await leftPane
.locator(
2023-08-16 20:54:39 +00:00
`[data-testid="${usernameContact.device.aci}"] >> "${USERNAME}"`
2023-02-13 22:40:11 +00:00
)
2024-03-06 23:59:51 +00:00
.click();
debug('Send message to username');
{
const compositionInput = await app.waitForEnabledComposer();
2024-04-03 17:17:39 +00:00
await typeIntoInput(compositionInput, 'Hello username');
2024-03-06 23:59:51 +00:00
await compositionInput.press('Enter');
}
2023-02-13 22:40:11 +00:00
let state = await phone.expectStorageState('consistency check');
if (type === 'profile') {
debug('adding profile key for username contact');
state = state.updateContact(usernameContact, {
profileKey: usernameContact.profileKey.serialize(),
});
} else {
debug('adding nickname for username contact');
state = state.updateContact(usernameContact, {
systemNickname: usernameContact.profileName,
});
}
await phone.setStorageState(state);
await phone.sendFetchStorage({
timestamp: bootstrap.getTimestamp(),
});
2023-02-13 22:40:11 +00:00
debug('find profile name in the left pane');
await leftPane
.locator(
2023-08-16 20:54:39 +00:00
`[data-testid="${usernameContact.device.aci}"] >> ` +
2023-02-13 22:40:11 +00:00
`"${usernameContact.profileName}"`
)
.waitFor();
debug('verify that storage service state is updated');
{
const newState = await phone.waitForStorageState({
after: state,
});
const { added, removed } = newState.diff(state);
assert.strictEqual(added.length, 1, 'only one record must be added');
assert.strictEqual(
removed.length,
1,
'only one record must be removed'
);
2023-08-16 20:54:39 +00:00
assert.strictEqual(added[0].contact?.aci, usernameContact.device.aci);
2023-02-13 22:40:11 +00:00
assert.strictEqual(added[0].contact?.username, '');
2023-08-16 20:54:39 +00:00
assert.strictEqual(removed[0].contact?.aci, usernameContact.device.aci);
2023-02-13 22:40:11 +00:00
assert.strictEqual(removed[0].contact?.username, USERNAME);
}
2024-03-06 23:59:51 +00:00
if (type === 'system') {
// No notifications
const notifications = window.locator('.SystemMessage');
assert.strictEqual(
await notifications.count(),
0,
'notification count'
);
} else {
// One notification - the username transition
const notifications = window.locator('.SystemMessage');
await notifications.waitFor();
assert.strictEqual(
await notifications.count(),
1,
'notification count'
);
const first = await notifications.first();
assert.strictEqual(
await first.innerText(),
`You started this chat with ${USERNAME}`
);
}
2023-02-13 22:40:11 +00:00
});
}
2023-02-08 17:14:59 +00:00
it('reserves/confirms/deletes username', async () => {
const { phone, server } = bootstrap;
const window = await app.getWindow();
debug('opening avatar context menu');
2023-08-09 00:53:06 +00:00
await window.getByRole('button', { name: 'Profile' }).click();
2023-02-08 17:14:59 +00:00
debug('opening username editor');
const profileEditor = window.locator('.ProfileEditor');
await profileEditor.locator('.ProfileEditor__row >> "Username"').click();
debug('entering new username');
const usernameField = profileEditor.locator('.Input__input');
2024-04-03 17:17:39 +00:00
await typeIntoInput(usernameField, NICKNAME);
2023-02-08 17:14:59 +00:00
debug('waiting for generated discriminator');
const discriminator = profileEditor.locator(
'.EditUsernameModalBody__discriminator__input[value]'
2023-02-08 17:14:59 +00:00
);
await discriminator.waitFor();
const discriminatorValue = await discriminator.inputValue();
assert.match(discriminatorValue, /^\d+$/);
2023-02-08 17:14:59 +00:00
const username = `${NICKNAME}.${discriminatorValue}`;
2023-02-08 17:14:59 +00:00
debug('saving username');
let state = await phone.expectStorageState('consistency check');
await profileEditor.locator('.module-Button >> "Save"').click();
debug('checking the username is saved');
{
await profileEditor
.locator(`.ProfileEditor__row >> "${username}"`)
.waitFor();
const uuid = await server.lookupByUsername(username);
2023-08-16 20:54:39 +00:00
assert.strictEqual(uuid, phone.device.aci);
2023-02-08 17:14:59 +00:00
const newState = await phone.waitForStorageState({
after: state,
});
const { added, removed } = newState.diff(state);
assert.strictEqual(added.length, 1, 'only one record must be added');
assert.strictEqual(removed.length, 1, 'only one record must be removed');
assert.strictEqual(added[0]?.account?.username, username);
2023-07-20 03:14:08 +00:00
const usernameLink = added[0]?.account?.usernameLink;
if (!usernameLink) {
throw new Error('No username link in AccountRecord');
}
if (!usernameLink.entropy) {
throw new Error('No username link entropy in AccountRecord');
}
if (!usernameLink.serverId) {
throw new Error('No username link serverId in AccountRecord');
}
const linkUuid = bufferToUuid(Buffer.from(usernameLink.serverId));
2023-07-20 23:19:32 +00:00
const encryptedLink = await server.lookupByUsernameLink(linkUuid);
if (!encryptedLink) {
2023-07-20 03:14:08 +00:00
throw new Error('Could not find link on the sever');
}
2023-07-20 23:19:32 +00:00
const linkUsername = usernames.decryptUsernameLink({
entropy: Buffer.from(usernameLink.entropy),
encryptedUsername: encryptedLink,
});
2023-07-20 03:14:08 +00:00
assert.strictEqual(linkUsername, username);
2023-02-08 17:14:59 +00:00
state = newState;
}
debug('deleting username');
await profileEditor
.locator('button[aria-label="Copy or delete username"]')
.click();
await profileEditor.locator('button[aria-label="Delete"]').click();
await window
.locator('.module-Modal .module-Modal__button-footer button >> "Delete"')
.click();
await profileEditor.locator('.ProfileEditor__row >> "Username"').waitFor();
debug('confirming username deletion');
{
const uuid = await server.lookupByUsername(username);
assert.strictEqual(uuid, undefined);
const newState = await phone.waitForStorageState({
after: state,
});
const { added, removed } = newState.diff(state);
assert.strictEqual(added.length, 1, 'only one record must be added');
assert.strictEqual(removed.length, 1, 'only one record must be removed');
2023-07-20 03:14:08 +00:00
assert.strictEqual(added[0]?.account?.username, '', 'clears username');
assert.strictEqual(
added[0]?.account?.usernameLink?.entropy?.length ?? 0,
0,
'clears usernameLink.entropy'
);
assert.strictEqual(
added[0]?.account?.usernameLink?.serverId?.length ?? 0,
0,
'clears usernameLink.serverId'
);
2023-02-08 17:14:59 +00:00
state = newState;
}
});
it('looks up contacts by username', async () => {
const { desktop, server } = bootstrap;
debug('creating a contact with username');
const carl = await server.createPrimaryDevice({
profileName: 'Carl',
});
2023-08-16 20:54:39 +00:00
await server.setUsername(carl.device.aci, CARL_USERNAME);
2023-02-08 17:14:59 +00:00
const window = await app.getWindow();
debug('entering username into search field');
2023-08-09 00:53:06 +00:00
await window.getByRole('button', { name: 'New chat' }).click();
2023-02-08 17:14:59 +00:00
const searchInput = window.locator('.module-SearchInput__container input');
2024-04-03 17:17:39 +00:00
await typeIntoInput(searchInput, CARL_USERNAME);
2023-02-08 17:14:59 +00:00
debug('starting lookup');
2024-04-03 17:17:39 +00:00
await window
.locator(`div.ListTile >> "${CARL_USERNAME}"`)
.click({ timeout: 2000 });
2023-02-08 17:14:59 +00:00
debug('sending a message');
{
const compositionInput = await app.waitForEnabledComposer();
2023-02-08 17:14:59 +00:00
2024-04-03 17:17:39 +00:00
await typeIntoInput(compositionInput, 'Hello Carl');
2023-02-08 17:14:59 +00:00
await compositionInput.press('Enter');
const { body, source } = await carl.waitForMessage();
assert.strictEqual(body, 'Hello Carl');
assert.strictEqual(source, desktop);
}
});
2023-07-20 03:14:08 +00:00
it('looks up contacts by username link', async () => {
const { desktop, phone, server } = bootstrap;
debug('creating a contact with username link');
const carl = await server.createPrimaryDevice({
profileName: 'Devin',
});
2023-08-16 20:54:39 +00:00
await server.setUsername(carl.device.aci, CARL_USERNAME);
2023-07-20 03:14:08 +00:00
const { entropy, serverId } = await server.setUsernameLink(
2023-08-16 20:54:39 +00:00
carl.device.aci,
2023-07-20 03:14:08 +00:00
CARL_USERNAME
);
2023-11-02 19:42:31 +00:00
const linkUrl = contactByEncryptedUsernameRoute
.toWebUrl({
encryptedUsername: Buffer.concat([
entropy,
uuidToBytes(serverId),
]).toString('base64url'),
2023-11-02 19:42:31 +00:00
})
.toString();
2023-07-20 03:14:08 +00:00
debug('sending link to Note to Self');
await phone.sendText(desktop, linkUrl, {
withProfileKey: true,
});
const window = await app.getWindow();
debug('opening note to self');
const leftPane = window.locator('#LeftPane');
2023-08-16 20:54:39 +00:00
await leftPane.locator(`[data-testid="${desktop.aci}"]`).click();
2023-07-20 03:14:08 +00:00
debug('clicking link');
await window.locator('.module-message__text a').click({
noWaitAfter: true,
});
debug('waiting for conversation to open');
await window
.locator(`.module-conversation-hero >> "${CARL_USERNAME}"`)
.waitFor();
debug('sending a message');
{
const compositionInput = await app.waitForEnabledComposer();
2024-04-03 17:17:39 +00:00
await typeIntoInput(compositionInput, 'Hello Carl');
2023-07-20 03:14:08 +00:00
await compositionInput.press('Enter');
const { body, source } = await carl.waitForMessage();
assert.strictEqual(body, 'Hello Carl');
assert.strictEqual(source, desktop);
}
});
});