'use strict';
require('../common');
const fc = require('fast-check');
describe('Prompt', function () {
describe('requestPassword & getPassword', function () {
this.timeout(30000);
it('returns the password fed into the dialog', () => {
fc.assert(fc.property(
fc.string(),
function (password) {
password = password.replace(/\r+|\n+/g, '');
const clean = globalThis.cleanup('', {url: 'ftp://example.com/?0000000000000000'});
document.body.innerHTML = `
`;
// Initialize the Prompt module to set up event listeners
PrivateBin.Alert.init();
PrivateBin.TopNav.init();
PrivateBin.Prompt.init();
PrivateBin.Prompt.requestPassword();
// mock request response
const originalFetch = globalThis.fetch;
globalThis.fetch = function (url) {
assert.strictEqual(url, 'ftp://example.com/?pasteid=0000000000000000');
return Promise.resolve({
ok: true,
status: 200,
statusText: 'OK',
json: function () {
return Promise.resolve({});
}
});
};
// Simulate user input
const passwordInput = document.getElementById('passworddecrypt');
passwordInput.value = password;
// Simulate form submission to trigger password capture
const passwordForm = document.getElementById('passwordform');
/** {@type SubmitEvent} */
const submitEvent = new Event('submit', { bubbles: true, cancelable: true });
passwordForm.dispatchEvent(submitEvent);
// Verify that getPassword returns the submitted password
const result = PrivateBin.Prompt.getPassword();
globalThis.fetch = originalFetch;
clean();
return result === password;
}
));
});
});
describe('modal dialogs', function () {
afterEach(function () {
cleanup();
});
it('shows the password dialog until a password was submitted', function () {
const clean = globalThis.cleanup('', {url: 'ftp://example.com/?0000000000000000'});
document.body.innerHTML =
'';
PrivateBin.Alert.init();
PrivateBin.TopNav.init();
PrivateBin.Prompt.init();
const modal = document.getElementById('passwordmodal');
PrivateBin.Prompt.requestPassword();
assert.ok(modal.classList.contains('show'), 'password dialog is shown');
modal.dispatchEvent(new window.KeyboardEvent('keydown', {key: 'Escape', bubbles: true}));
assert.ok(modal.classList.contains('show'), 'password dialog can not be closed with Escape');
const originalFetch = globalThis.fetch;
globalThis.fetch = function () {
return Promise.resolve({
ok: true,
status: 200,
statusText: 'OK',
json: function () {
return Promise.resolve({});
}
});
};
try {
document.getElementById('passworddecrypt').value = 'secret';
document.getElementById('passwordform').dispatchEvent(
new window.Event('submit', {bubbles: true, cancelable: true})
);
assert.ok(!modal.classList.contains('show'), 'password dialog is hidden after submitting');
assert.strictEqual(PrivateBin.Prompt.getPassword(), 'secret');
} finally {
globalThis.fetch = originalFetch;
clean();
}
});
it('asks for confirmation before loading a document', function () {
document.body.innerHTML =
'' +
'
' +
'' +
'
';
const modal = document.getElementById('loadconfirmmodal');
PrivateBin.Prompt.requestLoadConfirmation();
assert.ok(modal.classList.contains('show'), 'confirmation dialog is shown');
assert.strictEqual(document.activeElement, document.getElementById('loadconfirm-open-now'));
});
});
});