Tests all ported to Vows. No excuses now to not provide tests, I need to go back and write a lot more to cover what has already been done.

This commit is contained in:
ciaranj 2010-10-17 12:23:45 +01:00
parent 8a4b7e6b94
commit 98c2e01a75
2 changed files with 90 additions and 99 deletions

View File

@ -2,6 +2,6 @@
# Run all tests # Run all tests
# #
test: test:
vows tests/* --spec @@vows tests/* --spec
.PHONY: test install .PHONY: test install

View File

@ -1,99 +1,90 @@
/*describe 'node-oauth' var vows = require('vows'),
before_each assert = require('assert'),
OAuth= require('oauth').OAuth OAuth= require('../lib/oauth').OAuth;
end
describe 'Auth'
describe 'OAuth'
it 'should generate the signature base string described in http://oauth.net/core/1.0/#sig_base_example'
oa= new OAuth(null, null, null, null, null, null, "HMAC-SHA1");
var result= oa._createSignatureBase("GET", "http://photos.example.net/photos", vows.describe('OAuth').addBatch({
"file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original") 'When generating the signature base string described in http://oauth.net/core/1.0/#sig_base_example': {
result.should.eql "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal" topic: new OAuth(null, null, null, null, null, null, "HMAC-SHA1"),
end 'we get the expected result string': function (oa) {
describe 'Url normalisation' var result= oa._createSignatureBase("GET", "http://photos.example.net/photos",
before_each "file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original")
oa= new OAuth(null, null, null, null, null, null, "HMAC-SHA1"); assert.equal( result, "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal");
end }
it 'should strip default ports from urls for use in signature generation' },
oa._normalizeUrl("https://somehost.com:443/foo/bar").should_be "https://somehost.com/foo/bar" 'When normalising a url': {
end topic: new OAuth(null, null, null, null, null, null, "HMAC-SHA1"),
it 'should leave in non-default ports from urls for use in signature generation' 'default ports should be stripped': function(oa) {
oa._normalizeUrl("https://somehost.com:446/foo/bar").should_be "https://somehost.com:446/foo/bar" assert.equal( oa._normalizeUrl("https://somehost.com:443/foo/bar"), "https://somehost.com/foo/bar" );
oa._normalizeUrl("http://somehost.com:81/foo/bar").should_be "http://somehost.com:81/foo/bar" },
end 'should leave in non-default ports from urls for use in signature generation': function(oa) {
it 'should ensure that there exists a trailing slash when no path at all is present' assert.equal( oa._normalizeUrl("https://somehost.com:446/foo/bar"), "https://somehost.com:446/foo/bar" );
oa._normalizeUrl("http://somehost.com").should_be "http://somehost.com/" assert.equal( oa._normalizeUrl("http://somehost.com:81/foo/bar"), "http://somehost.com:81/foo/bar" );
end },
end 'should add a trailing slash when no path at all is present': function(oa) {
describe 'Url signing' assert.equal( oa._normalizeUrl("http://somehost.com"), "http://somehost.com/")
it 'should provide a valid signature when no token present' }
oa= new OAuth(null, null, "consumerkey", "consumersecret", "1.0", null, "HMAC-SHA1"); },
oa.stub('_getTimestamp').and_return("1272399856") 'When signing a url': {
oa.stub('_getNonce').and_return("ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp") topic: function() {
oa.signUrl("http://somehost.com:3323/foo/poop?bar=foo").should_be ("http://somehost.com:3323/foo/poop?bar=foo&oauth_consumer_key=consumerkey&oauth_nonce=ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1272399856&oauth_version=1.0&oauth_signature=7ytO8vPSLut2GzHjU9pn1SV9xjc%3D") var oa= new OAuth(null, null, "consumerkey", "consumersecret", "1.0", null, "HMAC-SHA1");
end oa._getTimestamp= function(){ return "1272399856"; }
it 'should provide a valid signature when a token is present' oa._getNonce= function(){ return "ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp"; }
oa= new OAuth(null, null, "consumerkey", "consumersecret", "1.0", null, "HMAC-SHA1"); return oa;
oa.stub('_getTimestamp').and_return("1272399856") },
oa.stub('_getNonce').and_return("ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp") 'Provide a valid signature when no token present': function(oa) {
oa.signUrl("http://somehost.com:3323/foo/poop?bar=foo", "token").should_be ("http://somehost.com:3323/foo/poop?bar=foo&oauth_consumer_key=consumerkey&oauth_nonce=ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1272399856&oauth_token=token&oauth_version=1.0&oauth_signature=9LwCuCWw5sURtpMroIolU3YwsdI%3D") assert.equal( oa.signUrl("http://somehost.com:3323/foo/poop?bar=foo"), "http://somehost.com:3323/foo/poop?bar=foo&oauth_consumer_key=consumerkey&oauth_nonce=ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1272399856&oauth_version=1.0&oauth_signature=7ytO8vPSLut2GzHjU9pn1SV9xjc%3D");
end },
it 'should provide a valid signature when a token and a token secret is present' 'Provide a valid signature when a token is present': function(oa) {
oa= new OAuth(null, null, "consumerkey", "consumersecret", "1.0", null, "HMAC-SHA1"); assert.equal( oa.signUrl("http://somehost.com:3323/foo/poop?bar=foo", "token"), "http://somehost.com:3323/foo/poop?bar=foo&oauth_consumer_key=consumerkey&oauth_nonce=ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1272399856&oauth_token=token&oauth_version=1.0&oauth_signature=9LwCuCWw5sURtpMroIolU3YwsdI%3D");
oa.stub('_getTimestamp').and_return("1272399856") },
oa.stub('_getNonce').and_return("ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp") 'Provide a valid signature when a token and a token secret is present': function(oa) {
oa.signUrl("http://somehost.com:3323/foo/poop?bar=foo", "token", "tokensecret").should_be ("http://somehost.com:3323/foo/poop?bar=foo&oauth_consumer_key=consumerkey&oauth_nonce=ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1272399856&oauth_token=token&oauth_version=1.0&oauth_signature=zeOR0Wsm6EG6XSg0Vw%2FsbpoSib8%3D") assert.equal( oa.signUrl("http://somehost.com:3323/foo/poop?bar=foo", "token", "tokensecret"), "http://somehost.com:3323/foo/poop?bar=foo&oauth_consumer_key=consumerkey&oauth_nonce=ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1272399856&oauth_token=token&oauth_version=1.0&oauth_signature=zeOR0Wsm6EG6XSg0Vw%2FsbpoSib8%3D");
end }
end },
describe 'host headers for non default ports should contain the port' 'When non standard ports are used': {
describe 'when getProtectedResource is called' topic: function() {
it 'should set the correct Host header when provided with an unusual port' var oa= new OAuth(null, null, null, null, null, null, "HMAC-SHA1"),
oa2= new OAuth(null, null, null, null, null, null, "HMAC-SHA1"); mockProvider= {};
var mockProvider= {},
mockRequest= {addListener:function(){},
end:function(){}};
mockProvider.request= function(method, path, headers) {
stub(mockProvider, 'request').and_return(mockRequest) assert.equal(headers.Host, "somehost.com:8080");
mockProvider.should.receive('request', 'once').with_args('GET', an_instance_of(String), {Host:"somehost.com:8080"}) return result= {addListener:function(){},
stub(oa2, '_createClient').and_return(mockProvider) end:function(){},
oa2.should.receive('_createClient', 'once') .with_args('8080', 'somehost.com') socket: {addListener: function(){}}};
}
oa2.getProtectedResource("http://somehost.com:8080", "GET", "oauth_token", null, function(){require('sys').p('dddd')}) oa._createClient= function(port, host) {
end assert.equal(port, '8080');
end assert.equal(host, 'somehost.com');
return mockProvider;
describe 'when getOAuthRequestToken is called' }
it 'should set the correct Host header when provided with an unusual port' return oa;
oa2= new OAuth(null, null, null, null, null, null, "HMAC-SHA1"); },
var mockProvider= {}, 'getProtectedResrouce should correctly define the host headers': function(oa) {
mockRequest= {addListener:function(){}, oa.getProtectedResource("http://somehost.com:8080", "GET", "oauth_token", null, function(){require('sys').p('dddd')})
end:function(){}}; }
},
stub(mockProvider, 'request').and_return(mockRequest) 'When building the OAuth Authorization header': {
mockProvider.should.receive('request', 'once').with_args('GET', an_instance_of(String), {Host:"somehost.com:8080"}) topic: new OAuth(null, null, null, null, null, null, "HMAC-SHA1"),
stub(oa2, '_createClient').and_return(mockProvider) 'All provided oauth arguments should be concatentated correctly' : function(oa) {
oa2.should.receive('_createClient', 'once').with_args('8080', 'somehost.com') var parameters= [
oa2.getProtectedResource("http://somehost.com:8080", "GET", "oauth_token", null, function(){}) ["oauth_timestamp", "1234567"],
end ["oauth_nonce", "ABCDEF"],
end ["oauth_version", "1.0"],
["oauth_signature_method", "HMAC-SHA1"],
describe 'when getOauthAccessToken is called' ["oauth_consumer_key", "asdasdnm2321b3"]];
it 'should set the correct Host header when provided with an unusual port' assert.equal(oa._buildAuthorizationHeaders(parameters), 'OAuth oauth_timestamp="1234567",oauth_nonce="ABCDEF",oauth_version="1.0",oauth_signature_method="HMAC-SHA1",oauth_consumer_key="asdasdnm2321b3"');
oa2= new OAuth(null, null, null, null, null, null, "HMAC-SHA1"); },
var mockProvider= {}, '*Only* Oauth arguments should be concatentated, others should be disregarded' : function(oa) {
mockRequest= {addListener:function(){}, var parameters= [
end:function(){}}; ["foo", "2343"],
["oauth_timestamp", "1234567"],
stub(mockProvider, 'request').and_return(mockRequest) ["oauth_nonce", "ABCDEF"],
mockProvider.should.receive('request', 'once').with_args('GET', an_instance_of(String), {Host:"somehost.com:8080"}) ["bar", "dfsdfd"],
stub(oa2, '_createClient').and_return(mockProvider) ["oauth_version", "1.0"],
oa2.should.receive('_createClient', 'once').with_args('8080', 'somehost.com') ["oauth_signature_method", "HMAC-SHA1"],
oa2.getProtectedResource("http://somehost.com:8080", "GET", "oauth_token", null, function(){}) ["oauth_consumer_key", "asdasdnm2321b3"],
end ["foobar", "asdasdnm2321b3"]];
end assert.equal(oa._buildAuthorizationHeaders(parameters), 'OAuth oauth_timestamp="1234567",oauth_nonce="ABCDEF",oauth_version="1.0",oauth_signature_method="HMAC-SHA1",oauth_consumer_key="asdasdnm2321b3"');
end }
end }
end }).export(module);
end*/