2022-10-10 17:19:01 +02:00
|
|
|
// eslint-disable-next-line no-restricted-imports
|
2021-12-16 13:36:21 +01:00
|
|
|
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
|
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
|
2022-10-27 23:38:54 +02:00
|
|
|
import { EncryptService } from "@bitwarden/common/abstractions/encrypt.service";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { FileUploadService } from "@bitwarden/common/abstractions/fileUpload.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
|
|
import { SearchService } from "@bitwarden/common/abstractions/search.service";
|
|
|
|
import { SettingsService } from "@bitwarden/common/abstractions/settings.service";
|
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
|
|
|
import { Cipher } from "@bitwarden/common/models/domain/cipher";
|
2022-10-14 18:25:50 +02:00
|
|
|
import { EncArrayBuffer } from "@bitwarden/common/models/domain/enc-array-buffer";
|
|
|
|
import { EncString } from "@bitwarden/common/models/domain/enc-string";
|
|
|
|
import { SymmetricCryptoKey } from "@bitwarden/common/models/domain/symmetric-crypto-key";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { CipherService } from "@bitwarden/common/services/cipher.service";
|
2021-12-16 13:36:21 +01:00
|
|
|
|
|
|
|
const ENCRYPTED_TEXT = "This data has been encrypted";
|
2022-07-26 03:40:32 +02:00
|
|
|
const ENCRYPTED_BYTES = Substitute.for<EncArrayBuffer>();
|
2021-04-14 17:47:10 +02:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
describe("Cipher Service", () => {
|
|
|
|
let cryptoService: SubstituteOf<CryptoService>;
|
|
|
|
let stateService: SubstituteOf<StateService>;
|
|
|
|
let settingsService: SubstituteOf<SettingsService>;
|
|
|
|
let apiService: SubstituteOf<ApiService>;
|
|
|
|
let fileUploadService: SubstituteOf<FileUploadService>;
|
|
|
|
let i18nService: SubstituteOf<I18nService>;
|
|
|
|
let searchService: SubstituteOf<SearchService>;
|
|
|
|
let logService: SubstituteOf<LogService>;
|
2022-10-27 23:38:54 +02:00
|
|
|
let encryptService: SubstituteOf<EncryptService>;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
|
|
|
let cipherService: CipherService;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
cryptoService = Substitute.for<CryptoService>();
|
|
|
|
stateService = Substitute.for<StateService>();
|
|
|
|
settingsService = Substitute.for<SettingsService>();
|
|
|
|
apiService = Substitute.for<ApiService>();
|
|
|
|
fileUploadService = Substitute.for<FileUploadService>();
|
|
|
|
i18nService = Substitute.for<I18nService>();
|
|
|
|
searchService = Substitute.for<SearchService>();
|
|
|
|
logService = Substitute.for<LogService>();
|
2022-10-27 23:38:54 +02:00
|
|
|
encryptService = Substitute.for<EncryptService>();
|
2021-12-16 13:36:21 +01:00
|
|
|
|
|
|
|
cryptoService.encryptToBytes(Arg.any(), Arg.any()).resolves(ENCRYPTED_BYTES);
|
|
|
|
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(new EncString(ENCRYPTED_TEXT));
|
|
|
|
|
|
|
|
cipherService = new CipherService(
|
|
|
|
cryptoService,
|
|
|
|
settingsService,
|
|
|
|
apiService,
|
|
|
|
fileUploadService,
|
|
|
|
i18nService,
|
|
|
|
() => searchService,
|
|
|
|
logService,
|
2022-10-27 23:38:54 +02:00
|
|
|
stateService,
|
|
|
|
encryptService
|
2021-12-16 13:36:21 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("attachments upload encrypted file contents", async () => {
|
|
|
|
const fileName = "filename";
|
|
|
|
const fileData = new Uint8Array(10).buffer;
|
|
|
|
cryptoService.getOrgKey(Arg.any()).resolves(new SymmetricCryptoKey(new Uint8Array(32).buffer));
|
|
|
|
|
|
|
|
await cipherService.saveAttachmentRawWithServer(new Cipher(), fileName, fileData);
|
|
|
|
|
|
|
|
fileUploadService
|
|
|
|
.received(1)
|
|
|
|
.uploadCipherAttachment(Arg.any(), Arg.any(), new EncString(ENCRYPTED_TEXT), ENCRYPTED_BYTES);
|
|
|
|
});
|
2021-04-14 17:47:10 +02:00
|
|
|
});
|