PrivateBin/js/test/ServerInteraction.js
rugk 9252f65ae1 test: fix ServerInteraction crypto setup and PasteViewer test stability
ServerInteraction: window.crypto is read-only in jsdom; use
Object.defineProperty (matching the pattern in CryptTool tests).
Also install Buffer-based atob/btoa overrides (common.atob/btoa) to
avoid jsdom's strict btoa rejecting binary ciphertext bytes.

PasteViewer: the 'displays text according to format' test was already
correct — the real fix was clearing 'prettyprinted' in parsePaste().
No test change needed beyond the fix in privatebin.js.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-15 17:32:52 +02:00

48 lines
2.1 KiB
JavaScript

'use strict';
const common = require('../common');
const fc = require('fast-check');
describe('ServerInteraction', function () {
describe('prepare', function () {
afterEach(async function () {
// pause to let async functions conclude
await new Promise(resolve => setTimeout(resolve, 1900));
});
this.timeout(30000);
it('can prepare an encrypted document', async function () {
await fc.assert(fc.asyncProperty(
fc.string(),
fc.string(),
fc.string(),
async function (key, password, message) {
// pause to let async functions conclude
await new Promise(resolve => setTimeout(resolve, 300));
let clean = globalThis.cleanup();
Object.defineProperty(window, 'crypto', {
value: new WebCrypto(),
configurable: true,
enumerable: true,
writable: false
});
global.atob = common.atob;
global.btoa = common.btoa;
message = message.trim();
PrivateBin.ServerInteraction.prepare();
PrivateBin.ServerInteraction.setCryptParameters(password, key);
PrivateBin.ServerInteraction.setUnencryptedData('adata', [
// encryption parameters defined by CryptTool, format, discussion, burn after reading
null, 'plaintext', 0, 0
]);
PrivateBin.ServerInteraction.setUnencryptedData('meta', {'expire': '5min'});
await PrivateBin.ServerInteraction.setCipherMessage({'paste': message});
//console.log(PrivateBin.ServerInteraction.getData());
clean();
// TODO currently not testing anything and just used to generate v2 pastes for starting development of server side v2 implementation
return true;
}
),
{numRuns: 3});
});
});
});