Skip to content

Instantly share code, notes, and snippets.

@simong
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save simong/ac2c5083f496b22e8359 to your computer and use it in GitHub Desktop.

Select an option

Save simong/ac2c5083f496b22e8359 to your computer and use it in GitHub Desktop.
/**
* Mock the requests that the Passport library would make when verifying a user signing in through Google
*
* @param {String} email The user's email address
* @param {String} displayName The user's display name
*/
var mockGoogleSignInRequests = function(email, displayName) {
// Require nock inline as it messes with the HTTP stack
var nock = require('nock');
// Ensure we can still perform regular HTTP requests during our tests
nock.enableNetConnect();
// Mock the "get access token" request in the OAuth2 cycle
var accessToken = _.random(10000);
nock('https://accounts.google.com')
.post('/o/oauth2/token')
.reply(200, {
'access_token': accessToken,
'refresh_token': 'foo'
});
// Mock the request where we ask for a user's profile
nock('https://www.googleapis.com')
.get('/plus/v1/people/me?access_token=' + accessToken)
.reply(200, {
'kind': 'plus#person',
'etag': 'RqKWnRU4WW46-6W3rWhLR9',
'gender': 'male',
'emails': [{'value': email, 'type': 'account'}],
'urls': [
{'value': 'http://www.youtube.com/user/abc123','type': 'otherProfile','label': 'ABC 123'},
],
'objectType': 'person',
'id': _.random(100000),
'displayName': displayName,
'name': {
'familyName': 'Bar',
'givenName': 'Foo'
},
'url': 'https://plus.google.com/' + _.random(10000000),
'image': {
'url': 'https://lh5.googleusercontent.com/-wfVubfsOBV0/AAAAAAAAAAI/AAAAAAAAAGQ/rEb5FmsQuiA/photo.jpg?sz=50',
'isDefault': false
},
'isPlusUser': true,
'language': 'en',
'verified': false
});
};
/**
* Test that verifies that authentication can be scoped to a set of domains
*/
it('verify authentication can be scoped to a set of domains', function(callback) {
_enableStrategy('google', function(done) {
_setGoogleDomains('foo.com', function() {
// Mock a google sign in
mockGoogleSignInRequests('simon@foo.com', 'Simon');
// A user returns from Google sign-in to the application
var restContext = TestsUtil.createTenantRestContext(global.oaeTests.tenants.localhost.host);
restContext.followRedirect = false;
RestAPI.Authentication.googleCallback(restContext, {'code': 'foo'}, function(err, body, response) {
assert.ok(!err);
// Assert we're signed in
RestAPI.User.getMe(restContext, function(err, me) {
assert.ok(!err);
assert.ok(!me.anon);
assert.strictEqual(me.displayName, 'Simon');
assert.strictEqual(me.email, 'simon@foo.com');
assert.strictEqual(me.authenticationStrategy, 'google');
return done();
});
});
});
}, callback);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment