Jared Hanson 3 年 前
コミット
4888e76e1e
3 ファイル変更81 行追加0 行削除
  1. 5 0
      test/bootstrap/node.js
  2. 22 0
      test/package.test.js
  3. 54 0
      test/strategy.test.js

+ 5 - 0
test/bootstrap/node.js

@@ -0,0 +1,5 @@
+var chai = require('chai');
+
+chai.use(require('chai-passport-strategy'));
+
+global.expect = chai.expect;

+ 22 - 0
test/package.test.js

@@ -0,0 +1,22 @@
+/* global describe, it */
+
+var sinon = require('sinon');
+var pkg = require('..');
+
+
+describe('passport-ethereum', function() {
+  
+  it('should export Strategy constructor as module', function() {
+    expect(pkg).to.be.a('function');
+    expect(pkg).to.equal(pkg.Strategy);
+  });
+  
+  it('should export Strategy constructor', function() {
+    expect(pkg.Strategy).to.be.a('function');
+  });
+  
+});
+
+afterEach(function() {
+  sinon.restore();
+});

+ 54 - 0
test/strategy.test.js

@@ -0,0 +1,54 @@
+var chai = require('chai');
+var sinon = require('sinon');
+var Strategy = require('../lib/strategy');
+
+
+describe('Strategy', function() {
+  
+  it('should be named ethereum', function() {
+    var strategy = new Strategy(function(){});
+    
+    expect(strategy.name).to.equal('ethereum');
+  });
+  
+  it('should verify address', function(done) {
+    chai.passport.use(new Strategy(function(address, cb) {
+      expect(address).to.equal('0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758');
+      return cb(null, { id: '248289761001' });
+    }))
+      .request(function(req) {
+        req.connection = {};
+        req.headers.host = 'localhost:3000';
+        req.body = {
+          message: 'localhost:3000 wants you to sign in with your Ethereum account:\n' +
+            '0xCC6F4DF4B758C4DE3203e8842E2d8CAc564D7758\n' +
+            '\n' +
+            'Sign in with Ethereum to the app.\n' +
+            '\n' +
+            'URI: http://localhost:3000\n' +
+            'Version: 1\n' +
+            'Chain ID: 1\n' +
+            'Nonce: VjglqeaSMDbPSYe0K\n' +
+            'Issued At: 2022-06-07T16:28:10.957Z',
+          signature: '0xb303d03782c532e2371e3d75a8b2b093c2dceb5faed5d07d6506be96be783245515db6ad55ad6d598ebdf1f7e1c5cb0d24e7147bbad47d3b9d8dfbcfab2ddcc71b'
+        };
+        req.session = {
+          messages: [],
+          ethereum: {
+            nonce: 'VjglqeaSMDbPSYe0K'
+          }
+        };
+      })
+      .success(function(user, info) {
+        expect(user).to.deep.equal({ id: '248289761001' });
+        expect(info).to.be.undefined;
+        expect(this.session).to.deep.equal({
+          messages: []
+        });
+        done();
+      })
+      .error(done)
+      .authenticate();
+  }); // should verify address
+  
+});