strategy.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 missing message', function(done) {
  49. chai.passport.use(new Strategy(function(address, cb) {
  50. throw new Error('verify function should not be called');
  51. }))
  52. .request(function(req) {
  53. req.connection = {};
  54. req.headers.host = 'localhost:3000';
  55. req.body = {
  56. signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
  57. };
  58. req.session = {
  59. messages: []
  60. };
  61. })
  62. .fail(function(challenge, status) {
  63. expect(challenge).to.deep.equal({ message: 'Missing message' });
  64. expect(status).to.equal(400);
  65. done();
  66. })
  67. .error(done)
  68. .authenticate();
  69. });
  70. it('should fail when missing signature', function(done) {
  71. chai.passport.use(new Strategy(function(address, cb) {
  72. throw new Error('verify function should not be called');
  73. }))
  74. .request(function(req) {
  75. req.connection = {};
  76. req.headers.host = 'localhost:3000';
  77. req.body = {
  78. message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
  79. '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
  80. '\n' +
  81. 'Sign in with Ethereum to the app.\n' +
  82. '\n' +
  83. 'URI: http://localhost:3000\n' +
  84. 'Version: 1\n' +
  85. 'Chain ID: 1\n' +
  86. 'Nonce: VjglqeaSMDbPSYe0K\n' +
  87. 'Issued At: 2022-06-07T16:28:10.957Z'
  88. };
  89. req.session = {
  90. messages: []
  91. };
  92. })
  93. .fail(function(challenge, status) {
  94. expect(challenge).to.deep.equal({ message: 'Missing signature' });
  95. expect(status).to.equal(400);
  96. done();
  97. })
  98. .error(done)
  99. .authenticate();
  100. });
  101. });