From 5ffffbff202ab037b22a72c64fe34673fe49ecaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20S=C3=B3jko?= Date: Thu, 29 Sep 2022 15:11:30 +0200 Subject: [PATCH] feat(snjs): add e2e test for subsequent subscriptions settings persistance --- packages/snjs/mocha/files.test.js | 44 +++------------ packages/snjs/mocha/lib/Files.js | 31 ++++++++++ packages/snjs/mocha/settings.test.js | 84 +++++++++++++++++++++++++++- 3 files changed, 121 insertions(+), 38 deletions(-) create mode 100644 packages/snjs/mocha/lib/Files.js diff --git a/packages/snjs/mocha/files.test.js b/packages/snjs/mocha/files.test.js index 4e038901e..fb7f6be23 100644 --- a/packages/snjs/mocha/files.test.js +++ b/packages/snjs/mocha/files.test.js @@ -1,5 +1,7 @@ import * as Factory from './lib/factory.js' import * as Utils from './lib/Utils.js' +import * as Files from './lib/Files.js' + chai.use(chaiAsPromised) const expect = chai.expect @@ -52,38 +54,6 @@ describe('files', function () { localStorage.clear() }) - const uploadFile = async (fileService, buffer, name, ext, chunkSize) => { - const operation = await fileService.beginNewFileUpload(buffer.byteLength) - - let chunkId = 1 - for (let i = 0; i < buffer.length; i += chunkSize) { - const readUntil = i + chunkSize > buffer.length ? buffer.length : i + chunkSize - const chunk = buffer.slice(i, readUntil) - const isFinalChunk = readUntil === buffer.length - - const error = await fileService.pushBytesForUpload(operation, chunk, chunkId++, isFinalChunk) - if (error) { - throw new Error('Could not upload file chunk') - } - } - - const file = await fileService.finishUpload(operation, name, ext) - - return file - } - - const downloadFile = async (fileService, itemManager, remoteIdentifier) => { - const file = itemManager.getItems(ContentType.File).find((file) => file.remoteIdentifier === remoteIdentifier) - - let receivedBytes = new Uint8Array() - - await fileService.downloadFile(file, (decryptedBytes) => { - receivedBytes = new Uint8Array([...receivedBytes, ...decryptedBytes]) - }) - - return receivedBytes - } - it('should create valet token from server', async function () { await setup({ fakeCrypto: true, subscription: true }) const remoteIdentifier = Utils.generateUuid() @@ -141,9 +111,9 @@ describe('files', function () { const response = await fetch('/packages/snjs/mocha/assets/small_file.md') const buffer = new Uint8Array(await response.arrayBuffer()) - const file = await uploadFile(fileService, buffer, 'my-file', 'md', 1000) + const file = await Files.uploadFile(fileService, buffer, 'my-file', 'md', 1000) - const downloadedBytes = await downloadFile(fileService, itemManager, file.remoteIdentifier) + const downloadedBytes = await Files.downloadFile(fileService, itemManager, file.remoteIdentifier) expect(downloadedBytes).to.eql(buffer) }) @@ -154,9 +124,9 @@ describe('files', function () { const response = await fetch('/packages/snjs/mocha/assets/two_mb_file.md') const buffer = new Uint8Array(await response.arrayBuffer()) - const file = await uploadFile(fileService, buffer, 'my-file', 'md', 100000) + const file = await Files.uploadFile(fileService, buffer, 'my-file', 'md', 100000) - const downloadedBytes = await downloadFile(fileService, itemManager, file.remoteIdentifier) + const downloadedBytes = await Files.downloadFile(fileService, itemManager, file.remoteIdentifier) expect(downloadedBytes).to.eql(buffer) }) @@ -167,7 +137,7 @@ describe('files', function () { const response = await fetch('/packages/snjs/mocha/assets/small_file.md') const buffer = new Uint8Array(await response.arrayBuffer()) - const file = await uploadFile(fileService, buffer, 'my-file', 'md', 1000) + const file = await Files.uploadFile(fileService, buffer, 'my-file', 'md', 1000) const error = await fileService.deleteFile(file) diff --git a/packages/snjs/mocha/lib/Files.js b/packages/snjs/mocha/lib/Files.js new file mode 100644 index 000000000..6c4cabfcd --- /dev/null +++ b/packages/snjs/mocha/lib/Files.js @@ -0,0 +1,31 @@ +export async function uploadFile(fileService, buffer, name, ext, chunkSize) { + const operation = await fileService.beginNewFileUpload(buffer.byteLength) + + let chunkId = 1 + for (let i = 0; i < buffer.length; i += chunkSize) { + const readUntil = i + chunkSize > buffer.length ? buffer.length : i + chunkSize + const chunk = buffer.slice(i, readUntil) + const isFinalChunk = readUntil === buffer.length + + const error = await fileService.pushBytesForUpload(operation, chunk, chunkId++, isFinalChunk) + if (error) { + throw new Error('Could not upload file chunk') + } + } + + const file = await fileService.finishUpload(operation, name, ext) + + return file +} + +export async function downloadFile(fileService, itemManager, remoteIdentifier) { + const file = itemManager.getItems(ContentType.File).find((file) => file.remoteIdentifier === remoteIdentifier) + + let receivedBytes = new Uint8Array() + + await fileService.downloadFile(file, (decryptedBytes) => { + receivedBytes = new Uint8Array([...receivedBytes, ...decryptedBytes]) + }) + + return receivedBytes +} diff --git a/packages/snjs/mocha/settings.test.js b/packages/snjs/mocha/settings.test.js index d4153abb7..f05e8b56e 100644 --- a/packages/snjs/mocha/settings.test.js +++ b/packages/snjs/mocha/settings.test.js @@ -1,14 +1,19 @@ import * as Factory from './lib/factory.js' +import * as Files from './lib/Files.js' + chai.use(chaiAsPromised) const expect = chai.expect describe('settings service', function () { + this.timeout(Factory.TwentySecondTimeout) + const validSetting = SettingName.GoogleDriveBackupFrequency const fakePayload = 'Im so meta even this acronym' const updatedFakePayload = 'is meta' let application let context + let user beforeEach(async function () { context = await Factory.createAppContextWithFakeCrypto() @@ -17,13 +22,31 @@ describe('settings service', function () { application = context.application - await Factory.registerUserToApplication({ + const registerResponse = await Factory.registerUserToApplication({ application: context.application, email: context.email, password: context.password, }) + user = registerResponse.user }) + const reInitializeApplicationWithRealCrypto = async () => { + await Factory.safeDeinit(application) + + context = await Factory.createAppContextWithRealCrypto() + + await context.launch() + + application = context.application + + const registerResponse = await Factory.registerUserToApplication({ + application: context.application, + email: context.email, + password: context.password, + }) + user = registerResponse.user + } + afterEach(async function () { await Factory.safeDeinit(application) }) @@ -108,4 +131,63 @@ describe('settings service', function () { const setting = await application.settings.getSubscriptionSetting('FILE_UPLOAD_BYTES_LIMIT') expect(setting).to.be.a('string') }) + + it('persist irreplaceable subscription settings between subsequent subscriptions', async () => { + await reInitializeApplicationWithRealCrypto() + + await Factory.publishMockedEvent('SUBSCRIPTION_PURCHASED', { + userEmail: context.email, + subscriptionId: 1, + subscriptionName: 'PRO_PLAN', + subscriptionExpiresAt: (new Date().getTime() + 3_600_000) * 1_000, + timestamp: Date.now(), + offline: false, + }) + await Factory.sleep(1) + + const response = await fetch('/packages/snjs/mocha/assets/small_file.md') + const buffer = new Uint8Array(await response.arrayBuffer()) + + await Files.uploadFile(application.fileService, buffer, 'my-file', 'md', 1000) + + await Factory.publishMockedEvent('FILE_UPLOADED', { + userUuid: user.uuid, + fileByteSize: 123, + filePath: 'foobar', + fileName: 'barbuzz', + }) + await Factory.sleep(1) + + const limitSettingBefore = await application.settings.getSubscriptionSetting('FILE_UPLOAD_BYTES_LIMIT') + expect(limitSettingBefore).to.equal('107374182400') + + const usedSettingBefore = await application.settings.getSubscriptionSetting('FILE_UPLOAD_BYTES_USED') + expect(usedSettingBefore).to.equal('123') + + + await Factory.publishMockedEvent('SUBSCRIPTION_EXPIRED', { + userEmail: context.email, + subscriptionId: 1, + subscriptionName: 'PRO_PLAN', + timestamp: Date.now(), + offline: false, + }) + await Factory.sleep(1) + + await Factory.publishMockedEvent('SUBSCRIPTION_PURCHASED', { + userEmail: context.email, + subscriptionId: 2, + subscriptionName: 'PRO_PLAN', + subscriptionExpiresAt: (new Date().getTime() + 3_600_000) * 1_000, + timestamp: Date.now(), + offline: false, + }) + await Factory.sleep(1) + + const limitSettingAfter = await application.settings.getSubscriptionSetting('FILE_UPLOAD_BYTES_LIMIT') + expect(limitSettingAfter).to.equal(limitSettingBefore) + + const usedSettingAfter = await application.settings.getSubscriptionSetting('FILE_UPLOAD_BYTES_USED') + expect(usedSettingAfter).to.equal(usedSettingBefore) + }) })