PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/plugins/OAuth/test/OAuth.signature.js

https://github.com/Kapinko/ExpressMVC
JavaScript | 131 lines | 116 code | 11 blank | 4 comment | 1 complexity | c987055904afe49d127b2281a19b80bd MD5 | raw file
  1. /**
  2. * Unit test for the OAuth signature module.
  3. */
  4. /*global describe:false*/
  5. (function () {
  6. var signature = require(__dirname + '/../signature'),
  7. OAuth = require(__dirname + '/../lib/oauth'),
  8. twitter = {
  9. 'req': {
  10. 'method': 'POST',
  11. 'headers': {'content-type': 'application/x-www-form-urlencoded'},
  12. 'url': 'https://api.twitter.com/1/statuses/update.json?include_entities=true',
  13. 'body': 'status=Hello%20Ladies%20%2b%20Gentlemen%2c%20a%20signed%20OAuth%20request%21'
  14. },
  15. 'oauth': (function (){
  16. var oauth = new OAuth();
  17. oauth.consumer_key = 'xvz1evFS4wEEPTGEFPHBog';
  18. oauth.nonce = 'kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg';
  19. oauth.timestamp = 1318622958;
  20. oauth.token = '370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb';
  21. oauth.version = true;
  22. return oauth;
  23. }()),
  24. 'consumer_secret': 'kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw',
  25. 'token_secret': 'LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE'
  26. },
  27. rfc5849 = {
  28. 'req': {
  29. 'method': 'POST',
  30. 'headers': {'content-type': 'application/x-www-form-urlencoded'},
  31. 'url': 'http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b',
  32. 'body': 'c2&a3=2+q'
  33. },
  34. 'oauth': (function () {
  35. var oauth = new OAuth();
  36. oauth.consumer_key = '9djdj82h48djs9d2';
  37. oauth.token = 'kkk9d7dh3k39sjv7';
  38. oauth.timestamp = '137131201';
  39. oauth.nonce = '7d8f3e4a';
  40. return oauth;
  41. }())
  42. };
  43. describe('Signature', function () {
  44. describe('#parse_url()', function () {
  45. it('should return an object with the protocol and base_url properties',
  46. function () {
  47. var url = 'http://foo.com/blah',
  48. parsed = signature.parse_url(url);
  49. parsed.should.have.property('protocol');
  50. parsed.should.have.property('base_url');
  51. });
  52. it('should return the proper protocol based upon the given url',
  53. function () {
  54. var urls = [
  55. {'url': 'http://foo.com/blah', 'protocol': 'http:'},
  56. {'url': 'https://blah.com/foo', 'protocol': 'https:'}
  57. ];
  58. urls.forEach(function (test) {
  59. var parsed = signature.parse_url(test.url);
  60. parsed.protocol.should.equal(test.protocol);
  61. });
  62. });
  63. it('should return the proper base url',
  64. function () {
  65. var urls = [
  66. { 'test': 'http://foo.com/blah', 'expected': 'http://foo.com/blah'},
  67. { 'test': 'https://blah.com/foo', 'expected': 'https://blah.com/foo'},
  68. { 'test': 'https://api.twitter.com/1/statuses/update.json?include_entities=true', 'expected': 'https://api.twitter.com/1/statuses/update.json'}
  69. ];
  70. urls.forEach(function(test) {
  71. var parsed = signature.parse_url(test.test);
  72. parsed.base_url.should.equal(test.expected);
  73. });
  74. });
  75. });
  76. describe('#parameter_string()', function () {
  77. it('should correctly produce the parameter string from the twitter example',
  78. function () {
  79. var expected = 'include_entities=true&oauth_consumer_key=xvz1evFS4wEEPTGEFPHBog&oauth_nonce=kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1318622958&oauth_token=370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb&oauth_version=1.0&status=Hello%20Ladies%20%2B%20Gentlemen%2C%20a%20signed%20OAuth%20request%21',
  80. parameter_string;
  81. parameter_string = signature.parameter_string(
  82. twitter.req, twitter.oauth
  83. );
  84. parameter_string.should.equal(expected);
  85. });
  86. it('should correctly produce the parameter string from the RFC5849 example',
  87. function () {
  88. var expected = 'a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9djdj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7',
  89. parameter_string = signature.parameter_string(
  90. rfc5849.req, rfc5849.oauth
  91. );
  92. parameter_string.should.equal(expected);
  93. });
  94. });
  95. describe('#base_string()', function () {
  96. it('should correctly produce the signature base string from the ' +
  97. 'twitter example', function () {
  98. var expected = 'POST&https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fupdate.json&include_entities%3Dtrue%26oauth_consumer_key%3Dxvz1evFS4wEEPTGEFPHBog%26oauth_nonce%3DkYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1318622958%26oauth_token%3D370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb%26oauth_version%3D1.0%26status%3DHello%2520Ladies%2520%252B%2520Gentlemen%252C%2520a%2520signed%2520OAuth%2520request%2521',
  99. base_string = signature.base_string(twitter.req, twitter.oauth);
  100. base_string.should.equal(expected);
  101. });
  102. it('should correctly produce the signature base string from the ' +
  103. 'RFC 5849 example', function () {
  104. var expected = 'POST&http%3A%2F%2Fexample.com%2Frequest&a2%3Dr%2520b%26a3%3D2%2520q%26a3%3Da%26b5%3D%253D%25253D%26c%2540%3D%26c2%3D%26oauth_consumer_key%3D9djdj82h48djs9d2%26oauth_nonce%3D7d8f3e4a%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D137131201%26oauth_token%3Dkkk9d7dh3k39sjv7',
  105. base_string = signature.base_string(rfc5849.req, rfc5849.oauth);
  106. base_string.should.equal(expected);
  107. });
  108. });
  109. describe('#sign()', function () {
  110. it('should produce the proper signature for the twitter example',
  111. function () {
  112. var expected = 'tnnArxj06cWHq44gCs1OSKk/jLY=',
  113. produced = signature.sign(twitter.req, twitter.oauth,
  114. twitter.consumer_secret, twitter.token_secret
  115. );
  116. produced.should.equal(expected);
  117. });
  118. });
  119. });
  120. }());