Skip to content

Commit 67bc830

Browse files
committed
Additional test case.
1 parent d175fee commit 67bc830

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

test/strategy.test.js

+64
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,70 @@ describe('Strategy', function() {
7272
});
7373
});
7474

75+
describe('handling a response with an authorization code', function() {
76+
var OAuth2Strategy = require('passport-oauth2').Strategy;
77+
var OAuth2;
78+
if (existsSync('node_modules/oauth')) { // npm 3.x
79+
OAuth2 = require('oauth').OAuth2;
80+
} else {
81+
OAuth2 = require('passport-oauth2/node_modules/oauth').OAuth2;
82+
}
83+
84+
var MockOAuth2Strategy = function(options, verify) {
85+
OAuth2Strategy.call(this, options, verify);
86+
87+
this._oauth2 = new OAuth2(options.clientID, options.clientSecret,
88+
'', options.authorizationURL, options.tokenURL, options.customHeaders);
89+
this._oauth2.getOAuthAccessToken = function(code, options, callback) {
90+
if (code != 'SplxlOBeZQQYbYS6WxSbIA+ALT1') { return callback(new Error('wrong code argument')); }
91+
92+
return callback(null, 's3cr1t-t0k3n', undefined, {});
93+
};
94+
this._oauth2.get = function(url, accessToken, callback) {
95+
if (url != 'https://api.github.com/user') { return callback(new Error('wrong url argument')); }
96+
if (accessToken != 's3cr1t-t0k3n') { return callback(new Error('wrong token argument')); }
97+
98+
var body = '{ "login": "octocat", "id": 1, "name": "monalisa octocat", "email": "[email protected]", "html_url": "https://github.com/octocat" }';
99+
callback(null, body, undefined);
100+
};
101+
}
102+
util.inherits(MockOAuth2Strategy, OAuth2Strategy);
103+
104+
var GitHubStrategy = $require('../lib/strategy', {
105+
'passport-oauth2': MockOAuth2Strategy
106+
})
107+
108+
var strategy = new GitHubStrategy({
109+
clientID: 'ABC123',
110+
clientSecret: 'secret'
111+
}, function verify(accessToken, refreshToken, profile, done) {
112+
process.nextTick(function() {
113+
return done(null, profile);
114+
})
115+
});
116+
117+
118+
var user;
119+
120+
before(function(done) {
121+
chai.passport.use(strategy)
122+
.success(function(u) {
123+
user = u;
124+
done();
125+
})
126+
.req(function(req) {
127+
req.query = {};
128+
req.query.code = 'SplxlOBeZQQYbYS6WxSbIA+ALT1';
129+
})
130+
.authenticate({ display: 'mobile' });
131+
});
132+
133+
it('should authenticate user', function() {
134+
expect(user.id).to.equal('1');
135+
expect(user.username).to.equal('octocat');
136+
});
137+
});
138+
75139
describe('error caused by invalid code sent to token endpoint, with response erroneously indicating success', function() {
76140
var OAuth2Strategy = require('passport-oauth2').Strategy;
77141
var OAuth2;

0 commit comments

Comments
 (0)