UNPKG

lx-scan

Version:

License eXtension to find 5-tuples of all installed packages: name, version, project home page, license (e. g. Apache v2, BSD) and required notice. It includes a GUI to edit information for each package and to enter information if necessary.

396 lines (386 loc) 19.4 kB
// Start app var jsdom = require("jsdom"); var expect = require("chai").expect; var os = require("os"); var path = require("path"); var rest = require("restler"); var _ = require("underscore"); require("../index"); try { var fs = require("graceful-fs"); } catch(e) { var fs = require("fs"); } // Creates a partial binding of the jsdom.env function with the desired settings pre-filled var jsd = function(url, callback) { jsdom.env(url,[],{features: {FetchExternalResources: ["script","css","link"],ProcessExternalResources:["script"]}},function (errors,window) { var document = window.document; callback(errors,window,document); }); }; describe("LX GUI", function() { this.timeout(20000); var test_files_path = path.join(__dirname,"input_folder"); var test_file_name = "lx.json"; var invalid_file_name = "invalid.txt"; var test_output_dir = "output_folder" var test_output_path = path.join(__dirname,"output_folder"); var test_python_path = path.join(__dirname,"test_python","bin","python"); var test_python_package_name = "zope.app.apidoc"; var number_of_packages; var test_file_name2 = "phonegap.json"; describe("Open route", function() { it("should open valid files and render index when double clicking", function(done) { jsd("http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+test_files_path, function(errors,window,document) { expect(errors).to.be.null; document.getElementById(test_file_name).ondblclick(); var url = document.getElementById("confirm").href; jsd(url,function(errors,window,document) { expect(errors).to.be.null; expect(document.getElementsByTagName("p").length).not.to.equal(0); done(); }); }); }); it("should open valid files and render the index when pressing enter in the file box", function(done) { jsd("http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+test_files_path, function(errors,window,document) { expect(errors).to.be.null; document.activeElement = document.getElementById("path"); document.getElementById("path").value = path.join(test_files_path,test_file_name); document.getElementById("path").onkeydown({which: 13, keyCode: 13}); url = document.getElementById("confirm").href; jsd(url,function(errors,window,document) { expect(errors).to.be.null; expect(document.getElementsByTagName("p").length).not.to.equal(0); done(); }); }); }); it("should render the index with the previously open file and an error when an invalid file is selected", function(done) { jsd("http://localhost:8888/open?jfile=1&oldpath=&path="+path.join(test_files_path,test_file_name),function (errors,window,document) { var jfile = document.getElementById("openlink").href.split("jfile=")[1].split("&")[0]; jsd("http://localhost:8888/browse?caller=/open&jfile="+jfile+"&path="+path.join(test_files_path,invalid_file_name), function (errors,window,document) { expect(document.getElementsByTagName("p").length).not.to.equal(0); done(); }); }); }); }); describe("File browser", function() { it("should allow navigation by double-clicking",function (done) { jsd("http://localhost:8888/browse?jfile=1&path="+test_files_path, function(errors,window,document) { expect(errors).to.be.null; document.getElementById("..").ondblclick(); jsd(window.location,function(errors,window,document) { expect(errors).to.be.null; document.getElementById(path.basename(test_files_path)).ondblclick(); jsd(window.location,function(errors,window,document) { expect(errors).to.be.null; expect(document.getElementById(test_file_name)).not.to.be.null; done(); }); }); }); }); it("should allow navigation by typing in a valid path and pressing enter", function (done) { jsd("http://localhost:8888/browse?jfile=1&path=", function (errors,window,document) { expect(errors).to.be.null; document.activeElement = document.getElementById("path"); document.getElementById("path").value = test_files_path; document.getElementById("path").onkeydown({which: 13, keyCode: 13}); jsd(document.getElementById("confirm").href,function(errors,window,document) { expect(errors).to.be.null; expect(document.getElementById(test_file_name)).not.to.be.null; done(); }); }); }); it("should redirect to the deepest valid path entry when handed an invalid path",function (done) { jsd("http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+path.join(test_files_path,"notveryvalid"), function (errors,window,document) { expect(errors).to.be.null; expect(document.getElementById(test_file_name)).not.to.be.null; done(); }); }); it("should redirect to the deepest valid path entry and retain the name box while navigating to a python binary",function (done) { jsd("http://localhost:8888/browse?scantype=pythonjfile=1&caller=/scan?scan=true&path="+path.join(test_files_path,"notveryvalid"), function (errors,window,document) { expect(errors, "JavaScript errors on page").to.be.null; expect(document.getElementById("filediv").style.display, "File box style").not.to.equal("none") done(); }); }); }); describe("Index page", function() { it("should show the lightbox on pressing the 'manually update' button for a particular license, and upon submission of new information update the working copy of the document",function (done) { var url = "http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+test_files_path+"/"+test_file_name; jsd(url, function (errors,window,document) { window.debug = true; document.getElementById("thumbnail_utf8@2.0.0").onclick(); $ = window.$; expect($("#lightbox").hasClass("in"),"lightbox visibility check").to.be.true; rest.post("http://localhost:8888/upload", { multipart: true, data: { "lnumber": 0, "entry": document.getElementById("entry").value, "type": "bsd", "notice": "Hellur", "jfile": document.getElementById("jfile").value, "path": document.getElementById("path").value }, followRedirects: false }).on("complete", function (data,response) { var postfix = data.split("Moved Temporarily. Redirecting to ")[1]; var url = "http://localhost:8888"+postfix; jsd(url,function(errors,window,document) { expect(errors).to.be.null; expect(document.getElementById("utf8@2.0.0/type").innerHTML).to.equal("BSD"); expect(document.getElementById("utf8@2.0.0/notice1").innerHTML).to.equal(" Hellur"); done(); }); }); }); }); it("should not append spurious license numbers to the dropdown", function (done) { var url = "http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+test_files_path+"/"+test_file_name; jsd(url, function (errors,window,document) { window.debug = true; var card = document.getElementById("thumbnail_utf8@2.0.0"); card.onclick(); var dropdown = document.getElementById("lnumber"); var dropdown_length = dropdown.length; $ = window.$; $(".close").click(); card.onclick(); expect(dropdown_length,"Number of licenses in dropdown").to.equal(dropdown.length); done(); }); }); }); describe("HTML save route", function() { it("should save an html report when given valid options and clicking the save button", function(done) { jsd("http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+test_files_path+"/"+test_file_name,function(errors,window,document) { jsd(document.getElementById("htmllink").href,function(errors,window,document) { expect(errors).to.be.null; document.getElementById("path").value = test_output_path; document.getElementById("path").onkeyup(); document.getElementById("filename").value = "test_html"; document.getElementById("filename").onkeyup(); window.$("#sholder").click(); jsd(document.getElementById("sbutton").href, function(errors,window,document) { expect(errors).to.be.null; expect(fs.existsSync(path.join(test_output_path,"test_html.html"))).to.be.true; fs.unlinkSync(path.join(test_output_path,"test_html.html")); done(); }); }); }); }); it("should redirect to the index with an error when passed an invalid save path", function (done) { jsd("http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+test_files_path+"/"+test_file_name,function(errors,window,document) { jsd(document.getElementById("htmllink").href, function (errors,window,document) { document.getElementById("path").value = path.join(test_output_path,"I_love_lamp"); document.getElementById("path").onkeyup(); document.getElementById("filename").value = "test_html"; window.$("#sholder").click(); jsd(document.getElementById("sbutton").href, function (errors,window,document) { expect(document.getElementsByTagName("p").length, "P length").not.to.equal(0); expect(window.errortext.length, "Number of errors").not.to.equal(0); done(); }); }); }); }); }); describe("JSON save route", function() { var test_json_path = path.join(__dirname,test_output_dir,"save_test.json"); var test_overwrite_file = path.join(__dirname,test_output_dir,"overwrite.json"); var open_url = "http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+test_files_path+"/"+test_file_name; it("should save correctly when pressing the enter button with focus on the 'file name' text box, and a valid path in the 'path' textbox", function (done) { if(fs.existsSync(test_json_path)) fs.unlinkSync(test_json_path); jsd(open_url,function (errors,window,document) { var jfile = document.getElementById("jsonlink").href.split("jfile")[1].split("=")[1].split("&")[0]; var url = "http://localhost:8888/browse?jfile="+jfile+"&verbose=false&type=&oldpath=&caller=/save?type=json&path="+path.join(__dirname,test_output_dir); jsd(url,function (errors,window,document) { document.getElementById("filename2").value = "save_test.json"; document.activeElement = {id: "filename2"}; document.getElementById("filename2").onkeydown({which: 55}); document.getElementById("filename2").onkeydown({which: 13,keyCode:13}); jsd(window.location,function (errors,window,document) { expect(fs.existsSync(test_json_path)).to.be.true; fs.unlinkSync(test_json_path); done(); }); }); }); }); it("should save correctly when pressing the 'save' button with a valid path and file name filled out", function (done) { if(fs.existsSync(test_json_path)) fs.unlinkSync(test_json_path); jsd(open_url,function (errors,window,document) { var jfile = document.getElementById("jsonlink").href.split("jfile")[1].split("=")[1].split("&")[0]; var url = "http://localhost:8888/browse?jfile="+jfile+"&verbose=false&type=&oldpath=&caller=/save?type=json&path="+path.join(__dirname,test_output_dir); jsd(url,function (errors,window,document) { $ = window.$; $("#savemodal").modal("show"); document.getElementById("filename2").value = "save_test.json"; document.getElementById("filename2").onkeydown({which: 55}); jsd(document.getElementById("sbutton").href,function (errors,window,document) { expect(fs.existsSync(test_json_path)).to.be.true; fs.unlinkSync(test_json_path); done(); }); }); }); }); it("should save correctly when an existing file is double clicked while the directory currently being viewed matches the 'path' box", function (done) { fs.writeFileSync(test_overwrite_file,""); jsd(open_url, function (errors,window,document) { var jfile = document.getElementById("jsonlink").href.split("jfile")[1].split("=")[1].split("&")[0]; var url = "http://localhost:8888/browse?jfile="+jfile+"&verbose=false&type=&oldpath=&caller=/save?type=json&path="+path.join(__dirname,test_output_dir); jsd(url,function (errors,window,document) { document.getElementById("overwrite.json").ondblclick(); jsd(window.location,function (errors,window,document) { expect(fs.readFileSync(test_overwrite_file).toString().length).not.to.equal(0); done(); }); }); }); }); }); describe("Log page", function() { it("should not be blank when passed a valid file", function(done) { var url = "http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+test_files_path+"/"+test_file_name; jsd(url, function (errors,window,document) { jsd(document.getElementById("loglink").href, function (errors,window,document) { expect(errors).to.be.null; expect(document.getElementsByTagName("p").length).not.to.equal(0); done(); }); }); }); it("should be blank when passed an invalid file", function (done) { jsd("http://localhost:8888", function (errors,window,document) { jsd(document.getElementById("loglink").href, function (errors,window,document) { expect(errors).to.be.null; expect(document.getElementsByTagName("p").length).to.equal(0); done(); }); }); }); it("should have an appropriate back button when entering from the standard index", function(done) { var url = "http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+test_files_path+"/"+test_file_name; jsd(url, function (errors,window,document) { jsd(document.getElementById("terselink").href, function (errors,window,document) { jsd(document.getElementById("verboselink").href,function (errors,window,document) { jsd(document.getElementById("loglink").href,function (errors,window,document) { expect(/^.*terse.*$/.test(document.getElementById("backlink").href)).to.be.false; done(); }); }); }); }); }); it("should have an appropriate back button when entering from a terse index", function (done) { var url = "http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+test_files_path+"/"+test_file_name; jsd(url, function (errors,window,document) { jsd(document.getElementById("terselink").href, function (errors,window,document) { jsd(document.getElementById("loglink").href,function (errors,window,document) { expect(/^.*terse.*$/.test(document.getElementById("backlink").href)).to.be.true; done(); }); }); }); }); }); describe("Scan route", function() { it("should scan and redirect when a folder with a node_modules subdirectory is indicated", function (done) { jsd("http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/scan?scan=true&path=" + test_files_path, function (errors,window,document) { window.test_jsdom_debug = true; window.debugScanFunc = function (jfile) { jsd("http://localhost:8888/?jfile="+jfile, function (errors,window,document) { number_of_packages = document.getElementsByTagName("p").length; expect(document.getElementsByTagName("p").length, "Number of paragraphs").not.to.equal(0); done(); }); }; document.getElementById("sbutton").onclick(); }); }); // it("should scan and redirect when a python environment and valid package name is indicated", function (done) { // jsd("http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&scantype=python&caller=/scan?scan=true&path=" + test_python_path, function (errors,window,document) { // window.test_jsdom_debug = true; // window.debugScanFunc = function(jfile) { // jsd("http://localhost:8888/?jfile="+jfile,function (errors,window,document) { // expect(document.getElementsByTagName("p"),"Number of paragraphs").not.to.equal(0); // done(); // }); // }; // document.getElementById("filename").value = test_python_package_name; // document.getElementById("sbutton").onclick(); // }); // }); it("should correctly perform a diff scan using the currently open scan if no other file is supplied", function (done) { var url = "http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/open&path="+test_files_path+"/"+test_file_name2; jsd(url,function (errors,window,document) { url = document.getElementById("nodelink").href; var length = document.getElementsByTagName("p").length; jsd(url,function (errors,window,document) { if(errors) return done(errors); window.test_jsdom_debug = true; // Real unclear here why this gets called twice; // it's fine per browsing testing window.debugScanFunc = _.once(function (jfile) { jsd("http://localhost:8888/?jfile="+jfile, function (errors,window,document) { expect(document.getElementsByTagName("p").length,"Number of paragraphs").to.be.below(number_of_packages); done(); return; }); }); document.getElementById("path").value = test_files_path; window.$("#diffbutton").click(); window.$("#sbutton").click(); }); }); }); it("should alert with an error when a folder with no node_modules subfolder is indicated", function (done) { jsd("http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/scan?scan=true&path=" + __dirname, function (errors,window,document) { window.test_jsdom_debug = true; window.debugErrorFunc = _.once(function (err) { expect(err, "Scanner error").not.to.be.null; done(); }); document.getElementById("sbutton").onclick(); }); }); it("should alert with an error when an invalid file path is indicated", function (done) { jsd("http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/scan?scan=true",function (errors,window,document) { window.test_jsdom_debug = true; window.debugErrorFunc = _.once(function (err) { expect(err, "Scanner error").not.to.be.null; done(); }); document.getElementById("path").value = "/tesagtds/aldsuiho"; window.$("#sbutton").click(); }); }); it("should preserve the caller when typing an alternative path into the path box and pressing enter", function (done) { jsd("http://localhost:8888/browse?jfile=1&verbose=false&type=&oldpath=&caller=/scan?scan=true&path=", function (errors,window,document) { document.activeElement = document.getElementById("path"); document.getElementById("path").value = "/usr/local/lib"; document.getElementById("path").onkeydown({which: 13, keyCode: 13}); jsd(window.location, function (errors,window,document) { expect(Boolean(/\/scan\?scan=true/.test(document.URL)),"Scan caller is in the URL").to.be.true; done(); }); }); }); }); describe("Close route",function() { it("should close and remove all temp files.",function(done) { jsd("http://localhost:8888/close",function(errors) { expect(fs.existsSync(path.join(os.tmpdir(),"lxgui"))).to.be.false; done(); }); }); }); });