import { faker } from '@faker-js/faker'; import { resetOwnE2EKey } from './resetOwnE2EKey'; import { setupE2EEPassword } from './setupE2EEPassword'; import { ADMIN_CREDENTIALS, BASE_URL } from '../config/constants'; import { Users } from '../fixtures/userStates'; import { EncryptedRoomPage } from '../page-objects/encrypted-room'; import { Navbar } from '../page-objects/fragments'; import { CreateE2EEChannel } from '../page-objects/fragments/e2ee'; import { FileUploadModal } from '../page-objects/fragments/modals'; import { LoginPage } from '../page-objects/login'; import { createTargetGroupAndReturnFullRoom, deletePrivateRoomsByName, deleteRoom } from '../utils'; import { preserveSettings } from '../utils/preserveSettings'; import { sendMessageFromUser } from '../utils/sendMessage'; import { test, expect } from '../utils/test'; const settingsList = ['E2E_Enable', 'E2E_Allow_Unencrypted_Messages']; preserveSettings(settingsList); test.describe('E2EE Encryption and Decryption - Basic Features', () => { const createdChannels: string[] = []; let loginPage: LoginPage; let navbar: Navbar; let encryptedRoomPage: EncryptedRoomPage; let createE2EEChannel: CreateE2EEChannel; test.use({ storageState: Users.admin.state }); test.beforeAll(async ({ api }) => { await api.post('/settings/E2E_Enable', { value: true }); await api.post('/settings/E2E_Allow_Unencrypted_Messages', { value: true }); }); test.beforeEach(async ({ page }) => { loginPage = new LoginPage(page); navbar = new Navbar(page); encryptedRoomPage = new EncryptedRoomPage(page); createE2EEChannel = new CreateE2EEChannel(page); await expect(await resetOwnE2EKey(ADMIN_CREDENTIALS)).toBeOK(); await loginPage.goto(); await loginPage.loginByUserState(Users.admin); }); test.afterAll(async () => { await deletePrivateRoomsByName(ADMIN_CREDENTIALS, createdChannels); }); test('expect placeholder text in place of encrypted message', async ({ page }) => { const channelName = faker.string.uuid(); const messageText = 'This is an encrypted message.'; await setupE2EEPassword(page); await createE2EEChannel.createAndStore(channelName, createdChannels); await expect(page).toHaveURL(`/group/${channelName}`); await encryptedRoomPage.waitForChannel(); await expect(encryptedRoomPage.encryptedTitle).toBeVisible(); await expect(encryptedRoomPage.encryptionNotReadyIndicator).not.toBeVisible(); await encryptedRoomPage.sendMessage(messageText); await expect(encryptedRoomPage.lastMessage.encryptedIcon).toBeVisible(); await expect(encryptedRoomPage.lastMessage.body).toHaveText(messageText); // Log out await navbar.logout(); // Login again await loginPage.loginByUserState(Users.admin); // Navigate to the encrypted channel WITHOUT entering the password await navbar.openChat(channelName); await expect(encryptedRoomPage.encryptedTitle).toBeVisible(); await expect(encryptedRoomPage.encryptionNotReadyIndicator).toBeVisible(); await expect(encryptedRoomPage.lastMessage.encryptedIcon).toBeVisible(); await expect(encryptedRoomPage.lastMessage.body).toHaveText( 'This message is end-to-end encrypted. To view it, you must enter your encryption key in your account settings.', ); }); test('expect placeholder text in place of encrypted file upload description', async ({ page }) => { const fileUploadModal = new FileUploadModal(page); const channelName = faker.string.uuid(); const fileName = faker.system.commonFileName('txt'); const fileDescription = faker.lorem.sentence(); await setupE2EEPassword(page); // Create an encrypted channel await createE2EEChannel.createAndStore(channelName, createdChannels); await expect(page).toHaveURL(`/group/${channelName}`); await expect(encryptedRoomPage.encryptedTitle).toBeVisible(); await expect(encryptedRoomPage.encryptionNotReadyIndicator).not.toBeVisible(); await test.step('upload the file with encryption', async () => { // Upload a file await encryptedRoomPage.dragAndDropTxtFile(); // Update file name and send await encryptedRoomPage.composer.getFileByName('any_file.txt').click(); await fileUploadModal.setName(fileName); await fileUploadModal.update(); await expect(encryptedRoomPage.composer.getFileByName(fileName)).toBeVisible(); await encryptedRoomPage.composer.inputMessage.fill(fileDescription); await encryptedRoomPage.composer.btnSend.click(); await expect(encryptedRoomPage.lastMessage.encryptedIcon).toBeVisible(); await expect(encryptedRoomPage.lastMessage.getFileUploadByName(fileName)).toContainText(fileName); await expect(encryptedRoomPage.lastMessage.body).toHaveText(fileDescription); }); await test.step('disable encryption in the room', async () => { await encryptedRoomPage.disableEncryption(); await expect(encryptedRoomPage.encryptedTitle).not.toBeVisible(); }); await test.step('upload the file without encryption', async () => { await encryptedRoomPage.dragAndDropTxtFile(); // Update file name and send await encryptedRoomPage.composer.getFileByName('any_file.txt').click(); await fileUploadModal.setName(fileName); await fileUploadModal.update(); await expect(encryptedRoomPage.composer.getFileByName(fileName)).toBeVisible(); await encryptedRoomPage.composer.inputMessage.fill(fileDescription); await encryptedRoomPage.composer.btnSend.click(); await expect(encryptedRoomPage.lastMessage.encryptedIcon).not.toBeVisible(); await expect(encryptedRoomPage.lastMessage.getFileUploadByName(fileName)).toContainText(fileName); await expect(encryptedRoomPage.lastMessage.body).toHaveText(fileDescription); }); await test.step('enable encryption in the room', async () => { await encryptedRoomPage.enableEncryption(); await expect(encryptedRoomPage.encryptedTitle).toBeVisible(); }); // Log out await navbar.logout(); // Login again await loginPage.loginByUserState(Users.admin); await expect(navbar.btnCreateNew).toBeVisible(); await navbar.openChat(channelName); await expect(encryptedRoomPage.encryptedTitle).toBeVisible(); await expect(encryptedRoomPage.lastNthMessage(1).body).toHaveText( 'This message is end-to-end encrypted. To view it, you must enter your encryption key in your account settings.', ); await expect(encryptedRoomPage.lastNthMessage(1).encryptedIcon).toBeVisible(); await expect(encryptedRoomPage.lastMessage.encryptedIcon).not.toBeVisible(); await expect(encryptedRoomPage.lastMessage.getFileUploadByName(fileName)).toContainText(fileName); await expect(encryptedRoomPage.lastMessage.body).toHaveText(fileDescription); }); test.describe('E2EE Quotes', () => { let targetRoomId: string; let targetChannelName: string; test.afterAll(async ({ api }) => { await deleteRoom(api, targetRoomId); }); test('expect to not crash and not show quote message for a message_link which is not accessible to the user', async ({ page, request, api, }) => { const encryptedRoomPage = new EncryptedRoomPage(page); targetChannelName = faker.string.uuid(); await createE2EEChannel.createAndStore(targetChannelName, createdChannels); await expect(page).toHaveURL(`/group/${targetChannelName}`); await expect(encryptedRoomPage.encryptedTitle).toBeVisible(); await expect(encryptedRoomPage.encryptionNotReadyIndicator).not.toBeVisible(); await encryptedRoomPage.sendMessage('First encrypted message.'); await expect(encryptedRoomPage.lastMessage.encryptedIcon).toBeVisible(); await expect(encryptedRoomPage.lastMessage.body).toHaveText('First encrypted message.'); // create a private group for user2 const { group: user1Channel } = await createTargetGroupAndReturnFullRoom(api, { excludeSelf: true, members: [Users.user2.data._id], }); targetRoomId = user1Channel._id; // send a message to the private group, which is not accessible to the main user const sentMessage = (await sendMessageFromUser(request, Users.user2, targetRoomId, 'This is a test message.')).message; const messageLink = `${BASE_URL}/group/${user1Channel.name}?msg=${sentMessage._id}`; await encryptedRoomPage.sendMessage(`This is a message with message link - ${messageLink}`); await expect(encryptedRoomPage.lastMessage.encryptedIcon).toBeVisible(); await expect(encryptedRoomPage.lastMessage.body).toContainText(`This is a message with message link - ${messageLink}`); await expect(encryptedRoomPage.lastNthMessage(1).body).toContainText('First encrypted message.'); await page.reload(); await expect(encryptedRoomPage.lastMessage.encryptedIcon).toBeVisible(); await expect(encryptedRoomPage.lastMessage.body).toContainText(`This is a message with message link - ${messageLink}`); await expect(encryptedRoomPage.lastNthMessage(1).body).toContainText('First encrypted message.'); }); }); });