-
-
Save mmahalwy/f23fbb13664dd9b1b2a0 to your computer and use it in GitHub Desktop.
Nightwatch Command for Creating Facebook Test User
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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