diff --git a/packages/backend/src/nest/common/utils.ts b/packages/backend/src/nest/common/utils.ts index 801e35d614..3f92afc04c 100644 --- a/packages/backend/src/nest/common/utils.ts +++ b/packages/backend/src/nest/common/utils.ts @@ -255,21 +255,6 @@ export const tmpQuietDirPath = (name: string): string => { return path.join(name, TestConfig.QUIET_DIR) } -export function createFile(filePath: string, size: number) { - const stream = fs.createWriteStream(filePath) - const maxChunkSize = 1048576 // 1MB - - let remainingSize = size - - while (remainingSize > 0) { - const chunkSize = Math.min(maxChunkSize, remainingSize) - stream.write(crypto.randomBytes(chunkSize)) - remainingSize -= chunkSize - } - - stream.end() -} - export async function createPeerId(): Promise { const peerId = await createEd25519PeerId() return peerIdFromKeys(peerId.publicKey, peerId.privateKey) diff --git a/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts b/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts index 4cbb526d57..bb0a98d6d6 100644 --- a/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts +++ b/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts @@ -5,7 +5,7 @@ import { DownloadState, FileMetadata } from '@quiet/types' import { DirResult } from 'tmp' import waitForExpect from 'wait-for-expect' import { TestModule } from '../common/test.module' -import { createFile, createTmpDir, libp2pInstanceParams } from '../common/utils' +import { createTmpDir, libp2pInstanceParams } from '../common/utils' import { IpfsModule } from '../ipfs/ipfs.module' import { IpfsService } from '../ipfs/ipfs.service' import { Libp2pModule } from '../libp2p/libp2p.module' @@ -16,6 +16,7 @@ import { IpfsFileManagerModule } from './ipfs-file-manager.module' import { IpfsFileManagerService } from './ipfs-file-manager.service' import fs from 'fs' import { createLogger } from '../common/logger' +import { createArbitraryFile } from '@quiet/common' const logger = createLogger('bigFiles:test') const BIG_FILE_SIZE = 2147483000 @@ -33,7 +34,7 @@ describe('IpfsFileManagerService', () => { tmpDir = createTmpDir() filePath = new URL('./testUtils/large-file.txt', import.meta.url).pathname // Generate 2.1GB file - createFile(filePath, BIG_FILE_SIZE) + createArbitraryFile(filePath, BIG_FILE_SIZE) module = await Test.createTestingModule({ imports: [TestModule, IpfsFileManagerModule, IpfsModule, SocketModule, Libp2pModule], }).compile() diff --git a/packages/backend/src/nest/storage/storage.service.spec.ts b/packages/backend/src/nest/storage/storage.service.spec.ts index 6a40b55e7b..65c089790f 100644 --- a/packages/backend/src/nest/storage/storage.service.spec.ts +++ b/packages/backend/src/nest/storage/storage.service.spec.ts @@ -23,7 +23,7 @@ import path from 'path' import { type PeerId } from '@libp2p/interface' import waitForExpect from 'wait-for-expect' import { TestModule } from '../common/test.module' -import { createFile, libp2pInstanceParams } from '../common/utils' +import { libp2pInstanceParams } from '../common/utils' import { IpfsModule } from '../ipfs/ipfs.module' import { IpfsService } from '../ipfs/ipfs.service' import { Libp2pModule } from '../libp2p/libp2p.module' @@ -38,8 +38,8 @@ import { LocalDbModule } from '../local-db/local-db.module' import { LocalDbService } from '../local-db/local-db.service' import { ORBIT_DB_DIR } from '../const' import { createLogger } from '../common/logger' -import { sleep } from '../common/sleep' import { createUserCertificateTestHelper, createTestRootCA } from '@quiet/identity' +import { createArbitraryFile } from '@quiet/common' const logger = createLogger('storageService:test') @@ -343,7 +343,7 @@ describe('StorageService', () => { beforeEach(async () => { realFilePath = path.join(dirname, '/real-file.txt') - createFile(realFilePath, 2147483) + createArbitraryFile(realFilePath, 2147483) await storageService.init(peerId) const metadata: FileMetadata = { diff --git a/packages/common/src/tests.ts b/packages/common/src/tests.ts index 6eead0dfd8..c7ab3567a4 100644 --- a/packages/common/src/tests.ts +++ b/packages/common/src/tests.ts @@ -1,3 +1,6 @@ +import fs from 'fs' +import crypto from 'crypto' + import { InvitationData, InvitationDataV1, InvitationDataV2, InvitationDataVersion } from '@quiet/types' import { composeInvitationDeepUrl, composeInvitationShareUrl } from './invitationLink/invitationLink' import { QUIET_JOIN_PAGE } from './const' @@ -84,3 +87,18 @@ export function getValidInvitationUrlTestData(data: T) // data: data, // } // } + +export const createArbitraryFile = (filePath: string, sizeBytes: number) => { + const stream = fs.createWriteStream(filePath) + const maxChunkSize = 1048576 // 1MB + + let remainingSize = sizeBytes + + while (remainingSize > 0) { + const chunkSize = Math.min(maxChunkSize, remainingSize) + stream.write(crypto.randomBytes(chunkSize)) + remainingSize -= chunkSize + } + + stream.end() +} diff --git a/packages/e2e-tests/.gitignore b/packages/e2e-tests/.gitignore index 6ca1a6bae7..206215b55f 100644 --- a/packages/e2e-tests/.gitignore +++ b/packages/e2e-tests/.gitignore @@ -3,4 +3,5 @@ screenshots/ .DS_Store Quiet/Quiet** -.env \ No newline at end of file +.env +src/tests/resources/bigTestFile.bin \ No newline at end of file diff --git a/packages/e2e-tests/src/selectors.ts b/packages/e2e-tests/src/selectors.ts index 94c80a55a5..438e5c054a 100644 --- a/packages/e2e-tests/src/selectors.ts +++ b/packages/e2e-tests/src/selectors.ts @@ -457,7 +457,7 @@ export class Channel { } get uploadFileInput() { - return this.driver.wait(this.driver.findElement(By.xpath('//*[@data-testid="uploadFileInput"]'))) + return this.driver.wait(this.driver.findElement(By.xpath('//*[@data-testid="uploadFileInput"]')), 15_000) } async sendMessage(message: string, username: string): Promise { @@ -646,7 +646,8 @@ export class Channel { ): Promise { logger.info(`Waiting for file content for message with filename ${filename} and type ${fileType}`) const messageContentElements = await this.driver.wait( - messageElement.findElements(By.xpath(`//*[contains(@data-testid, "messagesGroupContent-")]`)) + messageElement.findElements(By.xpath(`//*[contains(@data-testid, "messagesGroupContent-")]`)), + 45_000 ) for (const element of messageContentElements) { logger.info(await element.getId()) @@ -655,12 +656,14 @@ export class Channel { switch (fileType) { case UploadedFileType.IMAGE: containerElements = await this.driver.wait( - element.findElements(By.xpath(`//*[@class='UploadedImagecontainer']`)) + element.findElements(By.xpath(`//*[@class='UploadedImagecontainer']`)), + 45_000 ) break case UploadedFileType.FILE: containerElements = await this.driver.wait( - element.findElements(By.xpath(`//*[contains(@data-testid, "-fileComponent")]`)) + element.findElements(By.xpath(`//*[contains(@data-testid, "-fileComponent")]`)), + 45_000 ) break } @@ -676,12 +679,14 @@ export class Channel { switch (fileType) { case UploadedFileType.IMAGE: contentElement = await this.driver.wait( - container.findElement(By.xpath(`//img[@class='UploadedImageimage']`)) + container.findElement(By.xpath(`//img[@class='UploadedImageimage']`)), + 45_000 ) break case UploadedFileType.FILE: contentElement = await this.driver.wait( - container.findElement(By.xpath(`//img[@class='FileComponentactionIcon']`)) + container.findElement(By.xpath(`//img[@class='FileComponentactionIcon']`)), + 45_000 ) break } diff --git a/packages/e2e-tests/src/tests/multipleClients.test.ts b/packages/e2e-tests/src/tests/multipleClients.test.ts index addbfeb7bf..88d273254f 100644 --- a/packages/e2e-tests/src/tests/multipleClients.test.ts +++ b/packages/e2e-tests/src/tests/multipleClients.test.ts @@ -15,6 +15,14 @@ import { MessageIds, UserTestData } from '../types' import { createLogger } from '../logger' import * as path from 'path' import { UploadedFileType } from '../enums' +import { + BIG_FILE_SIZE, + TEST_BIG_FILE_NAME, + TEST_FILE_NAME, + TEST_IMAGE_FILE_NAME, + UPLOAD_FILE_DIR, +} from '../uploadFile.const' +import { createArbitraryFile } from '@quiet/common' const logger = createLogger('multipleClients') @@ -473,38 +481,52 @@ describe('Multiple Clients', () => { let largeFileMessageIds: MessageIds | undefined = undefined it('Owner uploads an image', async () => { - const filename = 'testImage.gif' - const uploadFilePath = path.resolve('./src/tests/resources/', filename) - await generalChannelOwner.uploadFile(filename, uploadFilePath, UploadedFileType.IMAGE, users.owner.username) + const uploadFilePath = path.resolve(UPLOAD_FILE_DIR, TEST_IMAGE_FILE_NAME) + await generalChannelOwner.uploadFile( + TEST_IMAGE_FILE_NAME, + uploadFilePath, + UploadedFileType.IMAGE, + users.owner.username + ) }) it('Guest sees uploaded image', async () => { await sleep(10_000) - const filename = 'testImage.gif' - await generalChannelUser1.getMessageIdsByFile(filename, UploadedFileType.IMAGE, users.owner.username) + await generalChannelUser1.getMessageIdsByFile( + TEST_IMAGE_FILE_NAME, + UploadedFileType.IMAGE, + users.owner.username + ) }) it('Owner uploads a file', async () => { - const filename = 'testFile.pdf' - const uploadFilePath = path.resolve('./src/tests/resources/', filename) - await generalChannelOwner.uploadFile(filename, uploadFilePath, UploadedFileType.FILE, users.owner.username) + const uploadFilePath = path.resolve(UPLOAD_FILE_DIR, TEST_FILE_NAME) + await generalChannelOwner.uploadFile( + TEST_FILE_NAME, + uploadFilePath, + UploadedFileType.FILE, + users.owner.username + ) }) it('Guest sees uploaded file and it downloads', async () => { - const filename = 'testFile.pdf' - await generalChannelUser1.getMessageIdsByFile(filename, UploadedFileType.FILE, users.owner.username) + await generalChannelUser1.getMessageIdsByFile(TEST_FILE_NAME, UploadedFileType.FILE, users.owner.username) }) it('Owner uploads a large file', async () => { - const filename = 'largeTestFile.bin' - const uploadFilePath = path.resolve('./src/tests/resources/', filename) - await generalChannelOwner.uploadFile(filename, uploadFilePath, UploadedFileType.FILE, users.owner.username) + const uploadFilePath = path.resolve(UPLOAD_FILE_DIR, TEST_BIG_FILE_NAME) + createArbitraryFile(uploadFilePath, BIG_FILE_SIZE) + await generalChannelOwner.uploadFile( + TEST_BIG_FILE_NAME, + uploadFilePath, + UploadedFileType.FILE, + users.owner.username + ) }) it(`Guest sees uploaded large file`, async () => { - const filename = 'largeTestFile.bin' largeFileMessageIds = await generalChannelUser1.getMessageIdsByFile( - filename, + TEST_BIG_FILE_NAME, UploadedFileType.FILE, users.owner.username ) diff --git a/packages/e2e-tests/src/tests/oneClient.test.ts b/packages/e2e-tests/src/tests/oneClient.test.ts index b71a314b18..0eb06034c8 100644 --- a/packages/e2e-tests/src/tests/oneClient.test.ts +++ b/packages/e2e-tests/src/tests/oneClient.test.ts @@ -14,6 +14,7 @@ import path from 'path' import { createLogger } from '../logger' import { sleep } from '../utils' import { UploadedFileType } from '../enums' +import { TEST_IMAGE_FILE_NAME, UPLOAD_FILE_DIR } from '../uploadFile.const' const logger = createLogger('oneClient') @@ -208,15 +209,13 @@ describe('One Client', () => { describe('Uploading files', () => { it('Owner uploads an image', async () => { - const filename = 'testImage.gif' - const uploadFilePath = path.resolve('./src/tests/resources/', filename) - await generalChannel.uploadFile(filename, uploadFilePath, UploadedFileType.IMAGE, ownerUserName) + const uploadFilePath = path.resolve(UPLOAD_FILE_DIR, TEST_IMAGE_FILE_NAME) + await generalChannel.uploadFile(TEST_IMAGE_FILE_NAME, uploadFilePath, UploadedFileType.IMAGE, ownerUserName) }) it('Owner uploads a non-image file', async () => { - const filename = 'testFile.pdf' - const uploadFilePath = path.resolve('./src/tests/resources/', filename) - await generalChannel.uploadFile(filename, uploadFilePath, UploadedFileType.FILE, ownerUserName) + const uploadFilePath = path.resolve(UPLOAD_FILE_DIR, TEST_IMAGE_FILE_NAME) + await generalChannel.uploadFile(TEST_IMAGE_FILE_NAME, uploadFilePath, UploadedFileType.FILE, ownerUserName) }) }) }) diff --git a/packages/e2e-tests/src/tests/resources/largeTestFile.bin b/packages/e2e-tests/src/tests/resources/largeTestFile.bin deleted file mode 100644 index 45aecedf2e..0000000000 Binary files a/packages/e2e-tests/src/tests/resources/largeTestFile.bin and /dev/null differ diff --git a/packages/e2e-tests/src/uploadFile.const.ts b/packages/e2e-tests/src/uploadFile.const.ts new file mode 100644 index 0000000000..ba3328e5af --- /dev/null +++ b/packages/e2e-tests/src/uploadFile.const.ts @@ -0,0 +1,6 @@ +export const TEST_IMAGE_FILE_NAME = 'testImage.gif' +export const TEST_FILE_NAME = 'testFile.pdf' +export const TEST_BIG_FILE_NAME = 'bigTestFile.bin' +export const BIG_FILE_SIZE = 15728640 // 15MB + +export const UPLOAD_FILE_DIR = './src/tests/resources/'