strategy.test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. var chai = require('chai');
  2. var sinon = require('sinon');
  3. var Strategy = require('../lib/strategy');
  4. describe('Strategy', function() {
  5. it('should be named ethereum', function() {
  6. var strategy = new Strategy(function(){});
  7. expect(strategy.name).to.equal('ethereum');
  8. });
  9. it('should verify address', function(done) {
  10. chai.passport.use(new Strategy(function(address, cb) {
  11. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  12. return cb(null, { id: '248289761001' });
  13. }))
  14. .request(function(req) {
  15. req.connection = {};
  16. req.headers.host = 'localhost:3000';
  17. req.body = {
  18. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  19. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  20. '\n' +
  21. 'Sign in with Ethereum to the app.\n' +
  22. '\n' +
  23. 'URI: http://localhost:3000\n' +
  24. 'Version: 1\n' +
  25. 'Chain ID: 1\n' +
  26. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  27. 'Issued At: 2022-06-07T16:28:10.957Z',
  28. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  29. };
  30. req.session = {
  31. messages: [],
  32. 'ethereum:siwe': {
  33. nonce: 'VjglqeaSMDbPSYe0K'
  34. }
  35. };
  36. })
  37. .success(function(user, info) {
  38. expect(user).to.deep.equal({ id: '248289761001' });
  39. expect(info).to.be.undefined;
  40. expect(this.session).to.deep.equal({
  41. messages: []
  42. });
  43. done();
  44. })
  45. .error(done)
  46. .authenticate();
  47. }); // should verify address
  48. it('should fail when address is missing from message', function(done) {
  49. chai.passport.use(new Strategy(function(address, cb) {
  50. expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
  51. return cb(null, { id: '248289761001' });
  52. }))
  53. .request(function(req) {
  54. req.connection = {};
  55. req.headers.host = 'localhost:3000';
  56. req.body = {
  57. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  58. '\n' +
  59. 'Sign in with Ethereum to the app.\n' +
  60. '\n' +
  61. 'URI: http://localhost:3000\n' +
  62. 'Version: 1\n' +
  63. 'Chain ID: 1\n' +
  64. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  65. 'Issued At: 2022-06-07T16:28:10.957Z',
  66. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  67. };
  68. req.session = {
  69. messages: [],
  70. 'ethereum:siwe': {
  71. nonce: 'VjglqeaSMDbPSYe0K'
  72. }
  73. };
  74. })
  75. .fail(function(challenge, status) {
  76. expect(challenge).to.deep.equal({ message: 'Invalid message' });
  77. expect(status).to.equal(403);
  78. done();
  79. })
  80. .error(done)
  81. .authenticate();
  82. }); // should fail when address is missing from message
  83. it('should fail when missing message', function(done) {
  84. chai.passport.use(new Strategy(function(address, cb) {
  85. throw new Error('verify function should not be called');
  86. }))
  87. .request(function(req) {
  88. req.connection = {};
  89. req.headers.host = 'localhost:3000';
  90. req.body = {
  91. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  92. };
  93. req.session = {
  94. messages: []
  95. };
  96. })
  97. .fail(function(challenge, status) {
  98. expect(challenge).to.deep.equal({ message: 'Missing message' });
  99. expect(status).to.equal(400);
  100. done();
  101. })
  102. .error(done)
  103. .authenticate();
  104. });
  105. it('should fail when missing signature', function(done) {
  106. chai.passport.use(new Strategy(function(address, cb) {
  107. throw new Error('verify function should not be called');
  108. }))
  109. .request(function(req) {
  110. req.connection = {};
  111. req.headers.host = 'localhost:3000';
  112. req.body = {
  113. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  114. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  115. '\n' +
  116. 'Sign in with Ethereum to the app.\n' +
  117. '\n' +
  118. 'URI: http://localhost:3000\n' +
  119. 'Version: 1\n' +
  120. 'Chain ID: 1\n' +
  121. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  122. 'Issued At: 2022-06-07T16:28:10.957Z'
  123. };
  124. req.session = {
  125. messages: []
  126. };
  127. })
  128. .fail(function(challenge, status) {
  129. expect(challenge).to.deep.equal({ message: 'Missing signature' });
  130. expect(status).to.equal(400);
  131. done();
  132. })
  133. .error(done)
  134. .authenticate();
  135. });
  136. });