github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/blockapps-ba-master/server/lib/user/test/user.test.js (about)

     1  require('co-mocha');
     2  const ba = require('blockapps-rest');
     3  const rest = ba.rest;
     4  const common = ba.common;
     5  const api = common.api;
     6  const config = common.config;
     7  const util = common.util;
     8  const should = common.should;
     9  const assert = common.assert;
    10  const Promise = common.Promise;
    11  
    12  const userJs = require('../user');
    13  
    14  const adminName = util.uid('Admin');
    15  const adminPassword = '1234';
    16  
    17  describe('User tests', function() {
    18    this.timeout(config.timeout);
    19  
    20    let admin;
    21  
    22    before(function*() {
    23      admin = yield rest.createUser(adminName, adminPassword);
    24    });
    25  
    26    it('Create Contract', function* () {
    27      const id = 123;
    28      const username = util.uid('User'+id);
    29      const pwHash = util.toBytes32('1234'); // FIXME this is not a hash
    30      const role = userJs.UserRole.SUPPLIER;
    31      const account = `3db01104b0c639556a3e1757f1ee1f7a1d3541d5`;
    32  
    33      // function User(address _account, string _username, bytes32 _pwHash, uint _id, UserRole _role) {
    34      const args = {
    35        _account: account,
    36        _username: username,
    37        _pwHash: pwHash,
    38        _id: id,
    39        _role: role,
    40      };
    41  
    42      // create the user with constructor args
    43      const contract = yield userJs.uploadContract(admin, args);
    44      const user = yield contract.getState();
    45      assert.equal(user.account, account, 'account');
    46      assert.equal(user.username, username, 'username');
    47      assert.equal(user.pwHash, pwHash, 'pwHash');
    48      assert.equal(user.id, id, 'id');
    49      assert.equal(user.role, role, 'role');
    50    });
    51  
    52    it('Search Contract', function* () {
    53      const id = new Date().getTime();
    54      const username = util.uid('User'+id);
    55      const pwHash = util.toBytes32('1234'); // FIXME this is not a hash
    56      const role = userJs.UserRole.SUPPLIER;
    57      const account = `3db01104b0c639556a3e1757f1ee1f7a1d3541d5`;
    58  
    59      // function User(address _account, string _username, bytes32 _pwHash, uint _id, UserRole _role) {
    60      const args = {
    61        _account: account,
    62        _username: username,
    63        _pwHash: pwHash,
    64        _id: id,
    65        _role: role,
    66      };
    67  
    68      // create the user with constructor args
    69      const contract = yield userJs.uploadContract(admin, args);
    70      // search
    71      const user = yield userJs.getUserById(id);
    72  
    73      assert.equal(user.account, account, 'account');
    74      assert.equal(user.username, username, 'username');
    75      assert.equal(user.pwHash, pwHash, 'pwHash');
    76      assert.equal(user.id, id, 'id');
    77      assert.equal(user.role, role, 'role');
    78    });
    79  
    80    it('Auth', function* () {
    81      const id = new Date().getTime();
    82      const username = util.uid('User'+id);
    83      const pwHash = util.toBytes32('1234'); // FIXME this is not a hash
    84      const role = userJs.UserRole.SUPPLIER;
    85      const account = `3db01104b0c639556a3e1757f1ee1f7a1d3541d5`;
    86  
    87      // function User(address _account, string _username, bytes32 _pwHash, uint _id, UserRole _role) {
    88      const args = {
    89        _account: account,
    90        _username: username,
    91        _pwHash: pwHash,
    92        _id: id,
    93        _role: role,
    94      };
    95  
    96      // create the user with constructor args
    97      let isAuthenticated;
    98      const contract = yield userJs.uploadContract(admin, args);
    99      isAuthenticated = yield contract.authenticate(pwHash);
   100      assert.isOk(isAuthenticated, 'authenticated');
   101      isAuthenticated = yield contract.authenticate(util.toBytes32('666'));
   102      assert.isNotOk(isAuthenticated, 'not authenticated');
   103   });
   104  
   105  });