Skip to content

Instantly share code, notes, and snippets.

@tildedave
Last active May 25, 2016 22:29
Show Gist options
  • Save tildedave/9a4b52037267ff0a80d2 to your computer and use it in GitHub Desktop.
Save tildedave/9a4b52037267ff0a80d2 to your computer and use it in GitHub Desktop.
Nightwatch Command for Creating Facebook Test User
var util = require('util');
var events = require('events');
var Promise = require('bluebird');
var request = Promise.promisify(require('request'));
var CreateFacebookTestUser = function() {
events.EventEmitter.call(this);
};
util.inherits(CreateFacebookTestUser, events.EventEmitter);
var requestAccessToken = function(appSecret, appId) {
return request({
url:'https://graph.facebook.com/oauth/access_token?client_id=' + appId + '&client_secret=' + appSecret + '&grant_type=client_credentials'
}).then(function (args) {
var body = args[1],
token = body.split('=')[1];
if (!token) {
throw new Error('Could not extract token from body ' + body);
}
return token;
});
};
var createTestUser = function(appId) {
return function(token) {
return request({
method: 'POST',
url: 'https://graph.facebook.com/' + appId + '/accounts/test-users?access_token=' + token,
json: true
});
};
};
CreateFacebookTestUser.prototype.command = function(callback) {
var self = this;
var appSecret = this.client.options.globals.facebookAppSecret,
appId = this.client.options.globals.facebookAppId;
requestAccessToken(appSecret, appId)
.then(createTestUser(appId))
.then(function (user) {
console.info('Successfully created Facebook Test User', user[1]);
if (callback) {
callback.call(self, null, user[1]);
}
self.emit('complete');
}).error(function (err) {
console.error('Exception in CreateFacebookTestUser', err);
if (callback) {
callback.call(self, err);
}
self.emit('complete');
});
};
module.exports = CreateFacebookTestUser;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment