x-ray-phantom authentication, unable to effectively login - javascript

I'm really can't find any example of using x-ray and .driver(phantom()) for authentication..
I've trawled through the documentation for x-ray and x-ray-phantom yet can't find any help.

/**
* Module Dependencies
*/
var Crawler = require('x-ray-crawler');
var cheerio = require('cheerio');
var join = require('path').join;
var assert = require('assert');
var phantom = require('../');
var fs = require('fs');
/**
* Tests
*/
describe('phantom driver', function() {
it('should have sensible defaults', function(done) {
var crawler = Crawler()
.driver(phantom())
crawler('http://google.com', function(err, ctx) {
if (err) return done(err);
var $ = cheerio.load(ctx.body);
var title = $('title').text();
assert.equal('Google', title);
done();
})
});
it('should work with client-side pages', function(done) {
var crawler = Crawler()
.driver(phantom());
crawler('https://exchange.coinbase.com/trade', function(err, ctx) {
if (err) return done(err);
var $ = cheerio.load(ctx.body);
var price = $('.market-num').text();
assert.equal(false, isNaN(+price));
done();
})
})
it('should support custom functions', function(done) {
var crawler = Crawler()
.driver(phantom(runner));
crawler('http://mat.io', function(err, ctx) {
if (err) return done(err);
var $ = cheerio.load(ctx.body);
var title = $('title').text();
assert.equal('Lapwing Labs', title);
done();
})
function runner(ctx, nightmare) {
return nightmare
.goto(ctx.url)
.click('.Header-logo-item+ .Header-list-item a')
.wait()
}
})
})
/**
* Read
*/
function get(path) {
return require(join(__dirname, 'fixtures', path));
}
Refer below github links if it helps.
https://github.com/lapwinglabs/x-ray-phantom
https://github.com/lapwinglabs/x-ray/issues/22

Related

Getting TypeError: FabricUserControllers is not a constructor which can not be catch

I am new to node.js and Javascript.
I have two javascript files "FabricUserController.js" and "UserController.js". So I have create the class in "FabricUserController.js" and export it into "UserController.js".
I am integrate the GetAll fucntion of "FabricUserController.js" to "UserController.js" in GetAllProduce fucntion.
I am trying run the below code however its giving me "TypeError: FabricUserControllers is not a constructor" error which is not handle in try catch{} block
Please see below code
let FabricUserControllers3 = require("./FabricUserController");
GetAllProduce: function (req, res, next) {
try{
let output = {};
var resArray = new Array();
let VZID = req.body.username;
console.log('test', 'GetAllProduce')
console.log('USername', VZID)
MongoClient.connect(config.Database.TEST.connectString, function (err, client) {
if (err) {
let connError = new Error(500, "Error connecting to TEST database", err);
res.status(connError.status).json(connError);
} else {
let query = {};
client.db(config.Database.TEST.dbName).collection("Produce").find(query).toArray(function (err, response) {
console.log(response);
if (err) {
let roleError = new Error(500, "Error getting Produce information", err);
res.status(500).json(roleError);
} else if (response.length > 0) {
//DO someting here
//FabricUserControllers3 = {};
FabricUserControllers3 = new FabricUserControllers();// getting issue here
FabricUserControllers3.GetAll((VZID), (response) => {
console.log("data result", result)
res.status(200).json(response);
client.close();
})
} else {
output.message = "Produce doesn't exist";
res.status(409).json(output);
client.close();
}
});
}
});
}catch(e){
if (e instanceof TypeError){
console.log('error1', e.message);
printError(e,true);
}else{
console.log("error2", e.message);
printError(e, false);
}
}
},
FabricUserController.js
'use strict';
const {
FileSystemWallet,
Gateway
} = require('fabric-network');
const fs = require('fs');
const path = require('path');
var MongoClient = require('mongodb').MongoClient;
var Client = require('node-rest-client').Client;
var client = new Client();
const configPath = path.resolve(__dirname, '..', 'config', 'Config.json');
const configJSON = fs.readFileSync(configPath, 'utf8');
const config1 = JSON.parse(configJSON);
var connection_file = config1.connection_file;
var appAdmin = config1.appAdmin;
var gatewayDiscovery = config1.gatewayDiscovery;
var appAdminSecret = config1.appAdminSecret;
var orgMSPID = config1.orgMSPID;
var caName = config1.caName;
const ccpPath = path.resolve(__dirname, '..', 'config', 'connection.json');
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);
let response = {};
class FabricUserControllers {
constructor() {
console.log("constructer called")
}
async ProduceRegistration(Username, produceid, callback) {
// Create a new file system based wallet for managing identities.
try {
const setAsyncTimeout = (cb, timeout = 0) => new Promise(resolve => {
setTimeout(() => {
cb();
resolve();
}, timeout);
});
let query2 = {}
query2.PRODUCEID = produceid;
// console.log('PRODUCEID',produceid)
var PRODUCE = {};
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
console.log('Username', Username)
// Check to see if we've already enrolled the user.
const userExists = await wallet.exists(Username);
if (!userExists) {
console.log('An identity for the user: ' + Username + ' does not exist in the wallet');
console.log('call the registerUser before retrying');
response.data = null;
response.httpstatus = 400;
response.message = `An identity for the ${Username} does not exist in the wallet`;
return response;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccpPath, {
wallet,
identity: Username,
discovery: {
enabled: false,
asLocalhost: true
}
});
///
MongoClient.connect(config.Database.TEST.connectString, function (err, client) {
if (err) {
// let connError = new Error(500, "Error connecting to TEST database", err);
response.data=null;
response.httpstatus = 500;
response.message = "Error connecting to TEST database :" + err;
// res.status(connError.status).json(connError);
return response;
} else {
client.db(config.Database.TEST.dbName).collection("Produce").find(query2).toArray(function (err, docs) {
if (err) {
response.httpstatus = 500;
response.message = "Error with DB :" + err;
return response;
}
else{
console.log("blockchain_status", docs[0].blockchain_status)
console.log('Role name DB',docs);
console.log('Role name DB1',docs[0]);
if(docs[0].STATUS)
PRODUCE.produceid = docs[0].PRODUCEID;
PRODUCE.produceName = docs[0].PRODUCE;
PRODUCE.farmLocation = docs[0].FARMLOCATION;
PRODUCE.plantingDate = docs[0].PLANTINGDATE;
PRODUCE.harvestDate = docs[0].HARVESTDATE;
PRODUCE.status = docs[0].STATUS;
PRODUCE.produceQuantites = docs[0].VARIETY;
PRODUCE.gapInfo = docs[0].GAP;
PRODUCE.farmerID = docs[0].farmerID;
console.log('Produce', PRODUCE);
const doStuffAsync = async () => {
setAsyncTimeout(async () => {
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('dfarmchannel');
// Get the contract from the network.
const contract = network.getContract(config1.chaincodeName);
var args = JSON.stringify(PRODUCE)
console.log("type of arg", typeof (args));
// Submit the specified transaction.
// console.log('produceID', args.produceID);
if(args==null || args==''){
console.log('Server not responding please try again');
}else
{
const result = await contract.submitTransaction('ProduceRegistration', args);
var argsJson = JSON.parse(result)
// console.log('result', argsJson)
// console.log('result1', result)
if(argsJson.produceID !="" && argsJson.produceID !=null && argsJson.produceID !="undefined" && argsJson.produceID !=undefined){
// // return false;
response.data = result
response.httpstatus = 200;
response.message = `Transaction has been submitted ansd successfull with Result :${result}`;
return callback(response);
// console.log('result before', response);
// console.log('Transaction has been submitted ansd successfull with Result :' + result);
}else{
console.log('blockchain server not responed')
// return false
response.httpstatus = 500;
response.message = `Please enter produce ID :`;
return response;
}
}
}, 4000);
};
doStuffAsync();
}
client.close();
})
}
})
await gateway.disconnect();
}
catch (error) {
// if(error) throw error;
response.error = error;
response.httpstatus = 500;
response.message = "Failed to enroll admin due to above error";
return response;
}
};
}
module.exports = FabricUserControllers;
#Abhirock, on your main file you have:
let FabricUserControllers3 = require("./FabricUserController");
FabricUserControllers3 = new FabricUserControllers();// getting issue here
You are trying to override FabricUserControllers3 creating a new object FabricUserControllers but you are not importing it. Try next solution to see if it solves your problem:
const FabricUserController = require("./FabricUserController");
const fabricUserControllers3 = new FabricUserController();
Hope it helps :))

Why is my Mongo DB not catching more than 1 of my links?

Update on issue. I now have everything in my mongo db but nothing is showing up on my page. Appears to be something to do with bootbox. If I click scrape new articles previous dialog disappears and scrape happens successfully. Upon inspect a new bootbox section appears with an empty h3 tag.
https://github.com/TylerCEdge/tester
Currently I get all 20 of the results console logged and all are going to mongo but nothing is showing up on my page. It's probably a spelling error but I'm having trouble with it.
// Scrape Script
// =============
// Require axios and cheerio
var axios = require("axios");
var cheerio = require("cheerio");
var URL = "https://thehackernews.com/search?"
function scrape(cb) {
// First, we grab the body of the html with axios
axios.get(URL).then(function (response) {
// Then, we load that into cheerio and save it to $ for a shorthand selector
var $ = cheerio.load(response.data);
var articles = [];
// Now, we grab every h2 within an article tag, and do the following:
$("h2").each(function () {
// Save an empty dataToAdd object
var dataToAdd = {};
// Add the text and href of every link, and save them as properties of the dataToAdd object
dataToAdd.title = $(this)
.text()
// Removes unwanted characters from the titles
.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, ' ');
dataToAdd.link = $(this)
.parent().parent().parent().parent()
.find("a")
.attr("href");
dataToAdd.img = $(this)
.parent().parent().parent()
.find("div.home-img").find("div.img-ratio").find("img")
.attr("data-src")
dataToAdd.summary = $(this)
.parent()
.find("div.home-desc")
.text()
.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, ' ')
// console.log(dataToAdd);
articles.push(dataToAdd);
// for (i = 0; i = dataToAdd.length; i++) {
// articles.push(dataToAdd[i]);
// }
});
cb(articles);
console.log(articles);
});
};
module.exports = scrape;
// Server routes
// Import Scrape
var scrape = require("../scripts/scrape");
// Import headlines/notes
var headlinesController = require("../controllers/headlines");
var notesController = require("../controllers/notes");
module.exports = function (router) {
router.get("/", function (req, res) {
res.render("home");
});
router.get("/saved", function (req, res) {
res.render("saved");
});
router.get("/api/fetch", function (req, res) {
headlinesController.fetch(function (err, docs) {
if (!docs || docs.insertedCount === 0) {
res.json({
message: "no new articles today. Check back tomorrow!"
});
}
else {
res.json({
message: "Added " + docs.insertedCount + " new articles!"
});
}
});
});
router.get("/api/headlines", function (req, res) {
var query = {};
if (req.query.saved) {
query = req.query;
}
headlinesController.get(query, function (data) {
res.json(data);
});
});
router.delete("/api/headlines/:id", function (req, res) {
var query = {};
query._id = req.params.id;
headlinesController.delete(query, function (err, data) {
res.json(data);
});
});
router.get("/api/notes/:headline_id?", function (req, res) {
var query = {};
if (req.params.headline_id) {
query._id = req.params.headline_id;
}
notesController.get(query, function (err, data) {
res.json(data);
});
});
router.delete("/api/notes/:id", function (req, res) {
var query = {};
query._id = req.params.id;
notesController.delete(query, function (err, data) {
res.json(data);
});
});
router.post("/api/notes", function (req, res) {
notesController.save(req.body, function (data) {
res.json(data);
});
});
}

How do i create two directory at same time using nodejs

I am using this npm to create directory
var mkdirp = require('mkdirp');
var dest = denotes the path
mkdirp(dest, function (err) {
if (err) cb(err, dest);
else cb(null, dest);
});
My doubt is how do I create two directory in different path?
I have tried following code:
var dest = first path;
var dest2 = second path;
mkdirp(dest,dest2, function (err) {
if (err) cb(err, dest);
else cb(null, dest);
});
But its not working how do I do that?Also, I need to rename for those folder which is in different path at same time.
Updated
var mkdirp = require('mkdirp');
var destArray = [ './root/dest1', './dest2' ]
destArray.map( path => {
mkdirp(path, function (err) {
if (err) console.error(err)
else {
res.json("ok")
}
});
})
I have used this solution, Because of loop am getting can't set headers again
you can try this
var mkdirp = require('mkdirp');
var destArray = [ './root/dest1', './dest2' ]
destArray.forEach( path => {
mkdirp(path, function (err) {
if (err) console.error(err)
else console.log('Directory created: ' + path )
});
})
You can do this using the following code
const fs = require('fs');
const {promisify} = require('util');
const mkdir = promisify(fs.mkdir);
const destArray = [ 'dest1', 'dest2'];
Promise.all(destArray.map(destPath => mkdir(destPath))).
then(res => {
console.log('Directories created');
}).
catch(err => {
console.error(err);
});

"x" is not a constructor

I'm trying to get a result from Sql query.
I have the following class which should execute the Sql connection and query:
'use strict';
//db.js
const sql = require('mssql');
require('dotenv').config();
var utils = function(){
var config = {
server: 'sql01',
database: 'db123',
options: {
instanceName: 'in1',
encrypt: true
}
};
module.exports = {
/** Define sql queries here */
GetEmpName(id) {
let my_query = `Select filed1 FROM db123 WHERE id='${id}'`;
sql.connect(config).then(function () {
new sql.Request()
.query(my_query).then(function (recordset) {}).catch(function (err) {
console.log(err);
});
});
}
};
};
And that's the main page:
'use strict;'
let HomePage = require('../page/home_page.js');
let utilsPage = require('../utils/utils.js');
describe("login to website",function(){
let employeeId;
let employeeBday;
let home = new HomePage();
//let utils = new utilsPage();
it('get an employee's Id', function (done) {
utilsPage.GetEmpName('100001387');
done();
})
});
I'm getting an error says: utilsPage is not a constructor.
What am I doing wrong?
In Node.js there are two ways of exporting and accessing function variables. One is by creating a global function and putting all your functions, variables into that and export that module as a whole by using module.exports.
Another way is just exporting the function by exports and then accessing that in your specific file. So in your case you could do it in either of the below 2 ways-
1st Approach: Using your Utils function globally -
'use strict';
//db.js
const sql = require('mssql');
require('dotenv').config();
var utils = function(){
this.config = {
server: 'sql01',
database: 'db123',
options: {
instanceName: 'in1',
encrypt: true
}
};
/** Define sql queries here */
this.getEmpName = function(id) {
let my_query = `Select filed1 FROM db123 WHERE id='${id}'`;
sql.connect(config).then(function () {
new sql.Request()
.query(my_query).then(function (recordset) {}).catch(function (err) {
console.log(err);
});
});
}
};
module.exports = new utils();
You could use it the same way as you have used -
'use strict;'
let HomePage = require('../page/home_page.js');
let utilsPage = require('../utils/utils.js');
describe("login to website",function(){
let employeeId;
let employeeBday;
let home = new HomePage();
it('get an employee's Id', function (done) {
utilsPage.getEmpName('100001387');
done();
})
});
2nd Approach: Exporting only the getEmpName function -
'use strict';
//db.js
const sql = require('mssql');
require('dotenv').config();
var config = {
server: 'sql01',
database: 'db123',
options: {
instanceName: 'in1',
encrypt: true
}
};
/** Define sql queries here */
exports.getEmpName = function(id) {
let my_query = `Select filed1 FROM db123 WHERE id='${id}'`;
sql.connect(config).then(function () {
new sql.Request()
.query(my_query).then(function (recordset) {}).catch(function (err) {
console.log(err);
});
});
}
You would use it the same as well:
'use strict;'
let HomePage = require('../page/home_page.js');
let utilsPage = require('../utils/utils.js');
describe("login to website",function(){
let employeeId;
let employeeBday;
let home = new HomePage();
it('get an employee's Id', function (done) {
utilsPage.getEmpName('100001387');
done();
})
In your utils.js file you shouldn't wrap your code in the utils function
'use strict';
//db.js
const sql = require('mssql');
require('dotenv').config();
var config = {
server: 'sql01',
database: 'db123',
options: {
instanceName: 'in1',
encrypt: true
}
};
module.exports = {
/** Define sql queries here */
GetEmpName(id) {
let my_query = `Select filed1 FROM db123 WHERE id='${id}'`;
sql.connect(config).then(function () {
new sql.Request()
.query(my_query).then(function (recordset) { }).catch(function (err) {
console.log(err);
});
});
}
};
In your main page file you can access your exported module like this
utilsPage.GetEmpName('100001387')
There's no need to call new utilsPage(). In order to use the keyword new you must either export a class or a constructor function.

NodeJS memory usage

I am playing with NodeJS and for this purpose created an email extractor. Somehow when i create multiple http requests the node.exe memory useage in windows task manager keeps increasing. I understand that the node needs more memory to process the requests but what i noticed that this memory usage does not come down even after all requests have been successfully processed.
When i start nodejs it consumes about 35000K memory but after about 80-100 request this goes upto 50000K and stays.
Here is my simple email extractor module:
var request = require('request'),
cheerio = require('cheerio'),
async = require('async'),
urlHelper = require('url');
function Extractor(config) {
this.baseUrl = config.url;
this.parsedUrl = urlHelper.parse(config.url);
this.urls = [];
this.emails = [];
}
Extractor.prototype.getEmails = function getEmails(html) {
var foundEmails = html.match(/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi) || [];
if(foundEmails.length) this.emails = this.emails.concat(foundEmails);
}
Extractor.prototype.extract = function extract(html) {
var $ = cheerio.load(html),
that = this;
if($('body')){
this.getEmails($('body').html());
}
if(!this.emails.length){
$("a[href^='http://" + this.parsedUrl.host + "'], a[href^='https://" + this.parsedUrl.host + "'], a[href^='/'], a[href^='./'], a[href^='../']").each(function(k, v) {
that.urls.push(urlHelper.resolve(that.baseUrl, $(v).attr('href')));
});
}
};
/**
* Process the base URL
*/
Extractor.prototype.processBase = function processBase(next) {
request(this.baseUrl, function(err, response, body) {
return next(err, body);
});
}
/**
* Process the internal pages
*/
Extractor.prototype.processInternal = function processInternal(cb) {
var that = this;
async.whilst(
// while this condition returns true
function () { return that.emails.length === 0 && that.urls.length > 0; },
// do this
function (callback) {
request(that.urls.shift(), function (err, response, body) {
var $ = cheerio.load(body);
if($(body)){
that.getEmails($('body').html());
}
callback(); // async internal, needs to be called after we are done with our thing
});
},
// call this if any errors occur. An error also stops the series
// this is also called on successful completion of the series
function (err) {
cb(that);
}
);
}
Extractor.prototype.process = function process(next) {
var that = this;
this.processBase(function(err, html) {
if(err) {
console.log(err);
} else {
that.extract(html);
if(!that.emails.length) {
that.processInternal(function(res) {
return next(null, that);
});
}
}
});
}
module.exports = Extractor;
and here is how i call it:
var express = require('express');
var router = express.Router();
var Extractor = require('../services/Extractor');
router.get('/', function(req, res) {
res.json({msg: 'okay'});
var extractor = new Extractor({url: 'http://lior-197784.use1-2.nitrousbox.com:4000/crawl'});
extractor.process(function(err, res) {});
});
module.exports = router;

Categories

Resources