Skip to content

Commit 1c4841e

Browse files
committed
Test cases for host option.
1 parent 19e6d45 commit 1c4841e

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed

test/strategies/consumer-test.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,67 @@ vows.describe('ConsumerStrategy').addBatch({
7777
},
7878
},
7979

80+
'strategy handling a valid request without a request token using host option instead of host header': {
81+
topic: function() {
82+
var strategy = new ConsumerStrategy(
83+
{ host: '127.0.0.1:3000' },
84+
// consumer callback
85+
function(consumerKey, done) {
86+
if (consumerKey == 'abc123') {
87+
done(null, { id: '1' }, 'ssh-secret');
88+
} else {
89+
done(new Error('something is wrong'))
90+
}
91+
},
92+
// token callback
93+
function(requestToken, done) {
94+
done(new Error('token callback should not be called'));
95+
}
96+
);
97+
return strategy;
98+
},
99+
100+
'after augmenting with actions': {
101+
topic: function(strategy) {
102+
var self = this;
103+
var req = {};
104+
strategy.success = function(user, info) {
105+
self.callback(null, user, info);
106+
}
107+
strategy.fail = function(challenge, status) {
108+
self.callback(new Error('should not be called'));
109+
}
110+
strategy.error = function(err) {
111+
self.callback(new Error('should not be called'));
112+
}
113+
114+
req.url = '/oauth/request_token';
115+
req.method = 'POST';
116+
req.headers = {};
117+
//req.headers['host'] = '127.0.0.1:3000';
118+
req.headers['authorization'] = 'OAuth oauth_callback="http%3A%2F%2Fmacbook-air.local.jaredhanson.net%3A3001%2Foauth%2Fcallback",oauth_consumer_key="abc123",oauth_nonce="fNyKdt8ZTgTVdEABtUMFzcXRxF4a230q",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1341176111",oauth_version="1.0",oauth_signature="tgsFsPL%2BDDQmfEz6hbCywhO%2BrE4%3D"';
119+
req.query = url.parse(req.url, true).query;
120+
req.connection = { encrypted: false };
121+
process.nextTick(function () {
122+
strategy.authenticate(req);
123+
});
124+
},
125+
126+
'should not generate an error' : function(err, user, info) {
127+
assert.isNull(err);
128+
},
129+
'should authenticate' : function(err, user, info) {
130+
assert.equal(user.id, '1');
131+
},
132+
'should set scheme to OAuth' : function(err, user, info) {
133+
assert.equal(info.scheme, 'OAuth');
134+
},
135+
'should set callbackURL' : function(err, user, info) {
136+
assert.equal(info.oauth.callbackURL, 'http://macbook-air.local.jaredhanson.net:3001/oauth/callback');
137+
},
138+
},
139+
},
140+
80141
'strategy handling a valid request without a request token using PLAINTEXT signature': {
81142
topic: function() {
82143
var strategy = new ConsumerStrategy(
@@ -699,6 +760,72 @@ vows.describe('ConsumerStrategy').addBatch({
699760
},
700761
},
701762

763+
'strategy handling a valid request with a request token using host option instead of host header': {
764+
topic: function() {
765+
var strategy = new ConsumerStrategy(
766+
{ host: '127.0.0.1:3000' },
767+
// consumer callback
768+
function(consumerKey, done) {
769+
if (consumerKey == 'abc123') {
770+
done(null, { id: '1' }, 'ssh-secret');
771+
} else {
772+
done(new Error('something is wrong'))
773+
}
774+
},
775+
// token callback
776+
function(requestToken, done) {
777+
if (requestToken == 'wM9YRRm5') {
778+
done(null, 'rxt0E5hKbslOEtzxD43hclL28XBZLJsF');
779+
} else {
780+
done(new Error('something is wrong'))
781+
}
782+
}
783+
);
784+
return strategy;
785+
},
786+
787+
'after augmenting with actions': {
788+
topic: function(strategy) {
789+
var self = this;
790+
var req = {};
791+
strategy.success = function(user, info) {
792+
self.callback(null, user, info);
793+
}
794+
strategy.fail = function(challenge, status) {
795+
self.callback(new Error('should not be called'));
796+
}
797+
strategy.error = function(err) {
798+
self.callback(new Error('should not be called'));
799+
}
800+
801+
req.url = '/oauth/access_token';
802+
req.method = 'POST';
803+
req.headers = {};
804+
//req.headers['host'] = '127.0.0.1:3000';
805+
req.headers['authorization'] = 'OAuth oauth_consumer_key="abc123",oauth_nonce="KyEf2M5ptWGDcz04jMScA2iJHkXHzkUW",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1341178687",oauth_token="wM9YRRm5",oauth_verifier="qriPjOnc",oauth_version="1.0",oauth_signature="ZP5%2FtXZcUiiD2HXKrevCL5FjY%2FM%3D"';
806+
req.query = url.parse(req.url, true).query;
807+
req.connection = { encrypted: false };
808+
process.nextTick(function () {
809+
strategy.authenticate(req);
810+
});
811+
},
812+
813+
'should not generate an error' : function(err, user, info) {
814+
assert.isNull(err);
815+
},
816+
'should authenticate' : function(err, user, info) {
817+
assert.equal(user.id, '1');
818+
},
819+
'should set scheme to OAuth' : function(err, user, info) {
820+
assert.equal(info.scheme, 'OAuth');
821+
},
822+
'should include token and verifier' : function(err, user, info) {
823+
assert.equal(info.oauth.token, 'wM9YRRm5');
824+
assert.equal(info.oauth.verifier, 'qriPjOnc');
825+
},
826+
},
827+
},
828+
702829
'strategy handling a valid request with a request token using PLAINTEXT signature': {
703830
topic: function() {
704831
var strategy = new ConsumerStrategy(

test/strategies/token-test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,72 @@ vows.describe('TokenStrategy').addBatch({
8282
},
8383
},
8484

85+
'strategy handling a valid request using host option instead of host header': {
86+
topic: function() {
87+
var strategy = new TokenStrategy(
88+
{ host: '127.0.0.1:3000' },
89+
// consumer callback
90+
function(consumerKey, done) {
91+
if (consumerKey == '1234') {
92+
done(null, { id: '1' }, 'keep-this-secret');
93+
} else {
94+
done(new Error('something is wrong'))
95+
}
96+
},
97+
// verify callback
98+
function(accessToken, done) {
99+
if (accessToken == 'abc-123-xyz-789') {
100+
done(null, { username: 'bob' }, 'lips-zipped');
101+
} else {
102+
done(new Error('something is wrong'))
103+
}
104+
}
105+
);
106+
return strategy;
107+
},
108+
109+
'after augmenting with actions': {
110+
topic: function(strategy) {
111+
var self = this;
112+
var req = {};
113+
strategy.success = function(user, info) {
114+
self.callback(null, user, info);
115+
}
116+
strategy.fail = function(challenge, status) {
117+
self.callback(new Error('should not be called'));
118+
}
119+
strategy.error = function(err) {
120+
self.callback(new Error('should not be called'));
121+
}
122+
123+
req.url = '/1/users/show.json?screen_name=jaredhanson&user_id=1705';
124+
req.method = 'GET';
125+
req.headers = {};
126+
//req.headers['host'] = '127.0.0.1:3000';
127+
req.headers['authorization'] = 'OAuth oauth_consumer_key="1234",oauth_nonce="A7E738D9A9684A60A40607017735ADAD",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1339004912",oauth_token="abc-123-xyz-789",oauth_version="1.0",oauth_signature="TBrJJJWS896yWrbklSbhEd9MGQc%3D"';
128+
req.query = url.parse(req.url, true).query;
129+
req.connection = { encrypted: false };
130+
process.nextTick(function () {
131+
strategy.authenticate(req);
132+
});
133+
},
134+
135+
'should not generate an error' : function(err, user, info) {
136+
assert.isNull(err);
137+
},
138+
'should authenticate' : function(err, user, info) {
139+
assert.equal(user.username, 'bob');
140+
},
141+
'should set scheme to OAuth' : function(err, user, info) {
142+
assert.equal(info.scheme, 'OAuth');
143+
},
144+
'should set consumer' : function(err, user, info) {
145+
assert.equal(info.consumer.id, '1');
146+
assert.strictEqual(info.client, info.consumer);
147+
},
148+
},
149+
},
150+
85151
'strategy handling a valid request with credentials in header using PLAINTEXT signature': {
86152
topic: function() {
87153
var strategy = new TokenStrategy(

0 commit comments

Comments
 (0)