github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/blockapps-ba-master/server/lib/user/test/userManager.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 config = common.config;
     6  const util = common.util;
     7  const should = common.should;
     8  const assert = common.assert;
     9  const constants = common.constants;
    10  const BigNumber = common.BigNumber;
    11  const Promise = common.Promise;
    12  
    13  const ErrorCodes = rest.getEnums(`${config.libPath}/common/ErrorCodes.sol`).ErrorCodes;
    14  const UserRole = rest.getEnums(`${config.libPath}/user/contracts/UserRole.sol`).UserRole;
    15  
    16  const adminName = util.uid('Admin');
    17  const adminPassword = '1234';
    18  const userManagerJs = require('../userManager');
    19  
    20  describe('UserManager tests', function() {
    21    this.timeout(config.timeout);
    22  
    23    let admin;
    24    let contract;
    25  
    26    // get ready:  admin-user and manager-contract
    27    before(function* () {
    28      admin = yield rest.createUser(adminName, adminPassword);
    29      contract = yield userManagerJs.uploadContract(admin);
    30    });
    31  
    32    it('Create User', function* () {
    33      const args = createUserArgs();
    34      const user = yield contract.createUser(args);
    35      assert.equal(user.username, args.username, 'username');
    36      assert.equal(user.role, args.role, 'role');
    37      // test that the account was created
    38      const account = yield rest.getAccount(user.account);
    39    });
    40  
    41    it('Create User - illegal name', function* () {
    42      const args = createUserArgs();
    43      args.username = '123456789012345678901234567890123'
    44      let user;
    45      try {
    46        // create with illegal name - should fail
    47        user = yield contract.createUser(args);
    48      } catch(error) {
    49        // error should be ERROR
    50        const errorCode = error.message;
    51        assert.equal(errorCode, ErrorCodes.ERROR, `Unexpected error ${JSON.stringify(error,null,2)}`);
    52      }
    53      // did not FAIL - that is an error
    54      assert.isUndefined(user, `Illegal username was not detected: ${args.username}`);
    55    });
    56  
    57    it('Test exists()', function* () {
    58      const args = createUserArgs();
    59  
    60      let exists;
    61      // should not exist
    62      exists = yield contract.exists(args.username);
    63      assert.isDefined(exists, 'should be defined');
    64      assert.isNotOk(exists, 'should not exist');
    65      // create user
    66      const user = yield contract.createUser(args);
    67      // should exist
    68      exists = yield contract.exists(args.username);
    69      assert.equal(exists, true, 'should exist')
    70    });
    71  
    72    it('Test exists() with special characters', function* () {
    73      const args = createUserArgs();
    74      args.username += ' ?#%!@*';
    75  
    76      let exists;
    77      // should not exist
    78      exists = yield contract.exists(args.username);
    79      assert.isDefined(exists, 'should be defined');
    80      assert.isNotOk(exists, 'should not exist');
    81      // create user
    82      const user = yield contract.createUser(args);
    83      // should exist
    84      exists = yield contract.exists(args.username);
    85      assert.equal(exists, true, 'should exist')
    86    });
    87  
    88    it('Create Duplicate User', function* () {
    89      const args = createUserArgs();
    90  
    91      // create user
    92      const user = yield contract.createUser(args);
    93      let duplicateUser;
    94      try {
    95        // create duplicate - should fail
    96        duplicateUser = yield contract.createUser(args);
    97      } catch(error) {
    98        // error should be EXISTS
    99        const errorCode = error.message;
   100        assert.equal(errorCode, ErrorCodes.EXISTS, `Unexpected error ${JSON.stringify(error,null,2)}`);
   101      }
   102      // did not FAIL - that is an error
   103      assert.isUndefined(duplicateUser, `Duplicate username was not detected: ${args.username}`);
   104    });
   105  
   106    it('Get User', function *() {
   107      const args = createUserArgs();
   108  
   109      // get non-existing user
   110      let nonExisting;
   111      try {
   112        nonExisting = yield contract.getUser(args.username);
   113      } catch(error) {
   114        // error should be NOT_FOUND
   115        const errorCode = error.message;
   116        assert.equal(errorCode, ErrorCodes.NOT_FOUND, 'should throw ErrorCodes.NOT_FOUND');
   117      }
   118      // did not FAIL - that is an error
   119      assert.isUndefined(nonExisting, `User should not be defined ${args.username}`);
   120  
   121      // create user
   122      yield contract.createUser(args);
   123      // get user - should exist
   124      const user = yield contract.getUser(args.username);
   125      assert.equal(user.username, args.username, 'username should be found');
   126    });
   127  
   128    it('Get Users', function* () {
   129      const args = createUserArgs();
   130  
   131      // get users - should not exist
   132      {
   133        const users = yield contract.getUsers();
   134        const found = users.filter(function(user) {
   135          return user.username === args.username;
   136        });
   137        assert.equal(found.length, 0, 'user list should NOT contain ' + args.username);
   138      }
   139      // create user
   140      const user = yield contract.createUser(args);
   141      // get user - should exist
   142      {
   143        const users = yield contract.getUsers(admin, contract);
   144        const found = users.filter(function(user) {
   145          return user.username === args.username;
   146        });
   147        assert.equal(found.length, 1, 'user list should contain ' + args.username);
   148      }
   149    });
   150  
   151    it.skip('User address leading zeros - load test - skipped', function *() {
   152      this.timeout(60*60*1000);
   153  
   154      const count = 16*4; // leading 0 once every 16
   155      const users = [];
   156      // create users
   157      for (let i = 0; i < count; i++) {
   158        const name = `User_${i}_`;
   159        const args = createUserArgs(name);
   160        const user = yield contract.createUser(args);
   161        users.push(user);
   162      }
   163  
   164      // get single user
   165      for (let user of users) {
   166        const resultUser = yield contract.getUser(user.username);
   167      }
   168  
   169      // get all users
   170      const resultUsers = yield contract.getUsers(admin, contract);
   171      const comparator = function(a, b) { return a.username == b.username; };
   172      const notFound = util.filter.isContained(users, resultUsers, comparator, true);
   173      assert.equal(notFound.length, 0, JSON.stringify(notFound));
   174    });
   175  
   176  
   177    it('User Login', function* () {
   178      const args = createUserArgs();
   179  
   180      // auth non-existing - should fail
   181      {
   182        const result = yield contract.login(args);
   183        assert.isDefined(result, 'auth result should be defined');
   184        assert.isNotOk(result, 'auth should fail');
   185      }
   186  
   187      // create user
   188      const user = yield contract.createUser(args);
   189      // auth
   190      {
   191        const result = yield contract.login(args);
   192        assert.isOk(result, 'auth should be ok');
   193      }
   194    });
   195  
   196    it('Get account', function* () {
   197      const args = createUserArgs();
   198      // create user
   199      const user = yield contract.createUser(args);
   200      // check account
   201      const account = (yield rest.getAccount(user.account))[0];
   202      const balance = new BigNumber(account.balance);
   203      const faucetBalance = new BigNumber(1000).times(constants.ETHER);
   204      balance.should.be.bignumber.equal(faucetBalance);
   205    });
   206  
   207    it('Get balance', function* () {
   208      const args = createUserArgs();
   209      // create user
   210      const user = yield contract.createUser(args);
   211      const balance = yield contract.getBalance(user.username);
   212      const faucetBalance = new BigNumber(1000).times(constants.ETHER);
   213      balance.should.be.bignumber.equal(faucetBalance);
   214    });
   215  
   216    it('Send funds wei', function* () {
   217      // create buyer/seller
   218      const buyerArgs = createUserArgs('Buyer', UserRole.BUYER);
   219      const buyer = yield contract.createUser(buyerArgs);
   220      buyer.startingBalance = yield contract.getBalance(buyer.username);
   221  
   222      const supplierArgs = createUserArgs('Supplier', UserRole.SUPPLIER);
   223      const supplier = yield contract.createUser(supplierArgs);
   224      supplier.startingBalance = yield contract.getBalance(supplier.username);
   225  
   226      // TRANSACTION
   227      // function* send(fromUser, toUser, value, nonce, node)
   228      const value = new BigNumber(12).mul(constants.ETHER);
   229      const receipt = yield rest.send(buyer.blocUser, supplier.blocUser, value);
   230      const txResult = yield rest.transactionResult(receipt.hash);
   231      assert.equal(txResult[0].status, 'success');
   232  
   233      // check balances
   234      buyer.endBalance = yield contract.getBalance(buyer.username);
   235      supplier.endBalance = yield contract.getBalance(supplier.username);
   236      // buyer end balance = start - value - fee
   237      buyer.startingBalance.minus(value).should.be.bignumber.gt(buyer.endBalance);
   238      // supplier end balance = start + value
   239      supplier.startingBalance.plus(value).should.be.bignumber.eq(supplier.endBalance);
   240    });
   241  });
   242  
   243  // function createUser(address account, string username, bytes32 pwHash, UserRole role) returns (ErrorCodes) {
   244  function createUserArgs(_name, _role) {
   245    const uid = util.uid();
   246    const role = _role || UserRole.SUPPLIER;
   247    const name = _name || 'User_';
   248    const args = {
   249      username: name + uid,
   250      password: 'Pass_' + uid,
   251      role: role,
   252    }
   253    return args;
   254  }