'use strict'; var common = require('../common'); describe('TopNav', function () { describe('showViewButtons & hideViewButtons', function () { before(function () { cleanup(); }); it( 'displays & hides navigation elements for viewing an existing paste', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( $('#newbutton').hasClass('hidden') && $('#clonebutton').hasClass('hidden') && $('#rawtextbutton').hasClass('hidden') && $('#qrcodelink').hasClass('hidden') ); $.PrivateBin.TopNav.showViewButtons(); results.push( !$('#newbutton').hasClass('hidden') && !$('#clonebutton').hasClass('hidden') && !$('#rawtextbutton').hasClass('hidden') && !$('#qrcodelink').hasClass('hidden') ); $.PrivateBin.TopNav.hideViewButtons(); results.push( $('#newbutton').hasClass('hidden') && $('#clonebutton').hasClass('hidden') && $('#rawtextbutton').hasClass('hidden') && $('#qrcodelink').hasClass('hidden') ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('showCreateButtons & hideCreateButtons', function () { before(function () { cleanup(); }); it( 'displays & hides navigation elements for creating a paste', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( $('#sendbutton').hasClass('hidden') && $('#expiration').hasClass('hidden') && $('#formatter').hasClass('hidden') && $('#burnafterreadingoption').hasClass('hidden') && $('#opendiscussionoption').hasClass('hidden') && $('#newbutton').hasClass('hidden') && $('#password').hasClass('hidden') && $('#attach').hasClass('hidden') ); $.PrivateBin.TopNav.showCreateButtons(); results.push( !$('#sendbutton').hasClass('hidden') && !$('#expiration').hasClass('hidden') && !$('#formatter').hasClass('hidden') && !$('#burnafterreadingoption').hasClass('hidden') && !$('#opendiscussionoption').hasClass('hidden') && !$('#newbutton').hasClass('hidden') && !$('#password').hasClass('hidden') && !$('#attach').hasClass('hidden') ); $.PrivateBin.TopNav.hideCreateButtons(); results.push( $('#sendbutton').hasClass('hidden') && $('#expiration').hasClass('hidden') && $('#formatter').hasClass('hidden') && $('#burnafterreadingoption').hasClass('hidden') && $('#opendiscussionoption').hasClass('hidden') && $('#newbutton').hasClass('hidden') && $('#password').hasClass('hidden') && $('#attach').hasClass('hidden') ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('showNewPasteButton', function () { before(function () { cleanup(); }); it( 'displays the button for creating a paste', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( $('#newbutton').hasClass('hidden') ); $.PrivateBin.TopNav.showNewPasteButton(); results.push( !$('#newbutton').hasClass('hidden') ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('hideCloneButton', function () { before(function () { cleanup(); }); it( 'hides the button for cloning a paste', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( !$('#clonebutton').hasClass('hidden') ); $.PrivateBin.TopNav.hideCloneButton(); results.push( $('#clonebutton').hasClass('hidden') ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('hideRawButton', function () { before(function () { cleanup(); }); it( 'hides the raw text button', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( !$('#rawtextbutton').hasClass('hidden') ); $.PrivateBin.TopNav.hideRawButton(); results.push( $('#rawtextbutton').hasClass('hidden') ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('hideFileSelector', function () { before(function () { cleanup(); }); it( 'hides the file attachment selection button', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( !$('#filewrap').hasClass('hidden') ); $.PrivateBin.TopNav.hideFileSelector(); results.push( $('#filewrap').hasClass('hidden') ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('showCustomAttachment', function () { before(function () { cleanup(); }); it( 'display the custom file attachment', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( $('#customattachment').hasClass('hidden') ); $.PrivateBin.TopNav.showCustomAttachment(); results.push( !$('#customattachment').hasClass('hidden') ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('collapseBar', function () { before(function () { cleanup(); }); it( 'collapses the navigation when displayed on a small screen', function () { var clean = jsdom(), results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( $('.navbar-toggle').hasClass('collapsed') && $('#navbar').attr('aria-expanded') != 'true' ); $.PrivateBin.TopNav.collapseBar(); results.push( $('.navbar-toggle').hasClass('collapsed') && $('#navbar').attr('aria-expanded') != 'true' ); /* with the upgrade for bootstrap-3.3.7.js to bootstrap-3.4.1.js the mobile interface detection changed to check if the ontouchstart event exists, which broke this section of the test $('.navbar-toggle').trigger('click'); results.push( !$('.navbar-toggle').hasClass('collapsed') && $('#navbar').attr('aria-expanded') == 'true' ); $.PrivateBin.TopNav.collapseBar(); results.push( $('.navbar-toggle').hasClass('collapsed') && $('#navbar').attr('aria-expanded') == 'false' ); */ clean(); assert.ok(results.every(element => element)); } ); }); describe('resetInput', function () { before(function () { cleanup(); }); it( 'reset inputs to defaults (options off)', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( !$.PrivateBin.TopNav.getBurnAfterReading() ); results.push( !$.PrivateBin.TopNav.getOpenDiscussion() ); $('#burnafterreading').attr('checked', 'checked'); $('#opendiscussion').attr('checked', 'checked'); results.push( $.PrivateBin.TopNav.getBurnAfterReading() ); results.push( $.PrivateBin.TopNav.getOpenDiscussion() ); $.PrivateBin.TopNav.resetInput(); results.push( !$.PrivateBin.TopNav.getBurnAfterReading() ); results.push( !$.PrivateBin.TopNav.getOpenDiscussion() ); cleanup(); assert.ok(results.every(element => element)); } ); it( 'reset inputs to defaults (burnafterreading on)', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( $.PrivateBin.TopNav.getBurnAfterReading() ); results.push( !$.PrivateBin.TopNav.getOpenDiscussion() ); $('#burnafterreading').removeAttr('checked'); results.push( !$.PrivateBin.TopNav.getBurnAfterReading() ); results.push( !$.PrivateBin.TopNav.getOpenDiscussion() ); $.PrivateBin.TopNav.resetInput(); results.push( $.PrivateBin.TopNav.getBurnAfterReading() ); results.push( !$.PrivateBin.TopNav.getOpenDiscussion() ); cleanup(); assert.ok(results.every(element => element)); } ); it( 'reset inputs to defaults (opendiscussion on)', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( !$.PrivateBin.TopNav.getBurnAfterReading() ); results.push( $.PrivateBin.TopNav.getOpenDiscussion() ); $('#opendiscussion').removeAttr('checked'); $('#burnafterreading').prop('checked', true); results.push( $.PrivateBin.TopNav.getBurnAfterReading() ); results.push( !$.PrivateBin.TopNav.getOpenDiscussion() ); $.PrivateBin.TopNav.resetInput(); results.push( !$.PrivateBin.TopNav.getBurnAfterReading() ); results.push( $.PrivateBin.TopNav.getOpenDiscussion() ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('getExpiration', function () { before(function () { cleanup(); }); it( 'returns the currently selected expiration date', function () { $('body').html( '' ); $.PrivateBin.TopNav.init(); assert.ok($.PrivateBin.TopNav.getExpiration() === '1day'); cleanup(); } ); }); describe('getFileList', function () { before(function () { cleanup(); }); var File = window.File, FileList = window.FileList, path = require('path'), mime = require('mime-types'); // mocking file input as per https://github.com/jsdom/jsdom/issues/1272 function createFile(file_path) { var file = fs.statSync(file_path), lastModified = file.mtimeMs, size = file.size; return new File( [new fs.readFileSync(file_path)], path.basename(file_path), { lastModified, type: mime.lookup(file_path) || '', } ) } function addFileList(input, file_paths) { if (typeof file_paths === 'string') file_paths = [file_paths] else if (!Array.isArray(file_paths)) { throw new Error('file_paths needs to be a file path string or an Array of file path strings') } const file_list = file_paths.map(fp => createFile(fp)) file_list.__proto__ = Object.create(FileList.prototype) Object.defineProperty(input, 'files', { value: file_list, writeable: false, }) return input } it( 'returns the selected files', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( $.PrivateBin.TopNav.getFileList() === null ); addFileList($('#file')[0], [ '../img/logo.svg', '../img/busy.gif' ]); var files = $.PrivateBin.TopNav.getFileList(); results.push( files[0].name === 'logo.svg' && files[1].name === 'busy.gif' ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('getBurnAfterReading', function () { before(function () { cleanup(); }); it( 'returns if the burn-after-reading checkbox was ticked', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( !$.PrivateBin.TopNav.getBurnAfterReading() ); $('#burnafterreading').attr('checked', 'checked'); results.push( $.PrivateBin.TopNav.getBurnAfterReading() ); $('#burnafterreading').removeAttr('checked'); results.push( !$.PrivateBin.TopNav.getBurnAfterReading() ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('getOpenDiscussion', function () { before(function () { cleanup(); }); it( 'returns if the open-discussion checkbox was ticked', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( !$.PrivateBin.TopNav.getOpenDiscussion() ); $('#opendiscussion').attr('checked', 'checked'); results.push( $.PrivateBin.TopNav.getOpenDiscussion() ); $('#opendiscussion').removeAttr('checked'); results.push( !$.PrivateBin.TopNav.getOpenDiscussion() ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('getPassword', function () { before(function () { cleanup(); }); jsc.property( 'returns the contents of the password input', 'string', function (password) { password = password.replace(/\r+/g, ''); var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( $.PrivateBin.TopNav.getPassword() === '' ); $('#passwordinput').val(password); results.push( $.PrivateBin.TopNav.getPassword() === password ); $('#passwordinput').val(''); results.push( $.PrivateBin.TopNav.getPassword() === '' ); cleanup(); return results.every(element => element); } ); }); describe('getCustomAttachment', function () { before(function () { cleanup(); }); it( 'returns the custom attachment element', function () { var results = []; $('body').html( '' ); $.PrivateBin.TopNav.init(); results.push( !$.PrivateBin.TopNav.getCustomAttachment().hasClass('test') ); $('#customattachment').addClass('test'); results.push( $.PrivateBin.TopNav.getCustomAttachment().hasClass('test') ); cleanup(); assert.ok(results.every(element => element)); } ); }); describe('hideAllButtons', function () { before(function () { cleanup(); }); it( 'hides all buttons correctly', function () { // Insert any setup code needed for the hideAllButtons function // Example: Initialize the DOM elements required for testing $('body').html( '' ); $.PrivateBin.TopNav.init(); $.PrivateBin.TopNav.hideAllButtons(); assert.ok($('#newbutton').hasClass('hidden')); assert.ok($('#clonebutton').hasClass('hidden')); assert.ok($('#rawtextbutton').hasClass('hidden')); assert.ok($('#qrcodelink').hasClass('hidden')); cleanup(); } ); }); describe('rawText', function () { before(function () { cleanup(); }); it( 'displays raw text view correctly', function () { const clean = jsdom('', {url: 'https://privatebin.net/?0123456789abcdef#0'}); global.URL = require('jsdom-url').URL; $('body').html(''); const sample = 'example'; $.PrivateBin.PasteViewer.setText(sample); $.PrivateBin.Helper.reset(); $.PrivateBin.TopNav.init(); $('#rawtextbutton').click(); assert.equal($('pre').text(), sample); clean(); } ); }); });