Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cast id to string, port test cases to mocha.

  • Loading branch information...
commit 1a2450db6843661ffff517612fd9f1a08474c9d4 1 parent b113e80
@jaredhanson authored
Showing with 80 additions and 2 deletions.
  1. +2 −2 lib/strategy.js
  2. +78 −0 test/strategy.profile.test.js
View
4 lib/strategy.js
@@ -88,13 +88,13 @@ util.inherits(Strategy, OAuth2Strategy);
*/
Strategy.prototype.userProfile = function(accessToken, done) {
this._oauth2.get(this._userProfileURL, accessToken, function (err, body, res) {
- if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
+ if (err) { return done(new InternalOAuthError('Failed to fetch user profile', err)); }
try {
var json = JSON.parse(body);
var profile = { provider: 'github' };
- profile.id = json.id;
+ profile.id = String(json.id);
profile.displayName = json.name;
profile.username = json.login;
profile.profileUrl = json.html_url;
View
78 test/strategy.profile.test.js
@@ -0,0 +1,78 @@
+/* global describe, it, expect, before */
+/* jshint expr: true */
+
+var GitHubStrategy = require('../lib/strategy');
+
+
+describe('Strategy#userProfile', function() {
+
+ var strategy = new GitHubStrategy({
+ clientID: 'ABC123',
+ clientSecret: 'secret'
+ },
+ function() {});
+
+ // mock
+ strategy._oauth2.get = function(url, accessToken, callback) {
+ if (url != 'https://api.github.com/user') { return callback(new Error('wrong url argument')); }
+ if (accessToken != 'token') { return callback(new Error('wrong token argument')); }
+
+ var body = '{ "login": "octocat", "id": 1, "name": "monalisa octocat", "email": "octocat@github.com", "html_url": "https://github.com/octocat" }';
+
+ callback(null, body, undefined);
+ };
+
+ describe('loading profile', function() {
+ var profile;
+
+ before(function(done) {
+ strategy.userProfile('token', function(err, p) {
+ if (err) { return done(err); }
+ profile = p;
+ done();
+ });
+ });
+
+ it('should parse profile', function() {
+ expect(profile.provider).to.equal('github');
+
+ expect(profile.id).to.equal('1');
+ expect(profile.username).to.equal('octocat');
+ expect(profile.displayName).to.equal('monalisa octocat');
+ expect(profile.profileUrl).to.equal('https://github.com/octocat');
+ expect(profile.emails).to.have.length(1);
+ expect(profile.emails[0].value).to.equal('octocat@github.com');
+ });
+
+ it('should set raw property', function() {
+ expect(profile._raw).to.be.a('string');
+ });
+
+ it('should set json property', function() {
+ expect(profile._json).to.be.an('object');
+ });
+ });
+
+ describe('encountering an error', function() {
+ var err, profile;
+
+ before(function(done) {
+ strategy.userProfile('wrong-token', function(e, p) {
+ err = e;
+ profile = p;
+ done();
+ });
+ });
+
+ it('should error', function() {
+ expect(err).to.be.an.instanceOf(Error);
+ expect(err.constructor.name).to.equal('InternalOAuthError');
+ expect(err.message).to.equal('Failed to fetch user profile');
+ });
+
+ it('should not load profile', function() {
+ expect(profile).to.be.undefined;
+ });
+ });
+
+});
Please sign in to comment.
Something went wrong with that request. Please try again.