arrays.spec.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import expect from 'expect';
  2. import crypto from 'crypto';
  3. import { gasLimit, createConnection, deploy, transaction, aliceKeypair, } from './index';
  4. import { ContractPromise } from '@polkadot/api-contract';
  5. import { ApiPromise } from '@polkadot/api';
  6. describe('Deploy arrays contract and test', () => {
  7. let conn: ApiPromise;
  8. before(async function () {
  9. conn = await createConnection();
  10. });
  11. after(async function () {
  12. await conn.disconnect();
  13. });
  14. it('arrays in account storage', async function () {
  15. this.timeout(50000);
  16. const alice = aliceKeypair();
  17. let deployed_contract = await deploy(conn, alice, 'arrays.contract');
  18. let contract = new ContractPromise(conn, deployed_contract.abi, deployed_contract.address);
  19. let users = [];
  20. for (let i = 0; i < 3; i++) {
  21. let addr = '0x' + crypto.randomBytes(32).toString('hex');
  22. let name = `name${i}`;
  23. let id = crypto.randomBytes(4).readUInt32BE(0);
  24. let perms: string[] = [];
  25. for (let j = 0; j < Math.random() * 3; j++) {
  26. let p = Math.floor(Math.random() * 8);
  27. perms.push(`Perm${p + 1}`);
  28. }
  29. const tx1 = contract.tx.addUser({ gasLimit }, id, addr, name, perms);
  30. await transaction(tx1, alice);
  31. users.push({ "name": name, "addr": addr, "id": id, "perms": perms });
  32. }
  33. let user = users[Math.floor(Math.random() * users.length)];
  34. let res1 = await contract.query.getUserById(alice.address, {}, user.id);
  35. expect(res1.output?.toJSON()).toStrictEqual(user);
  36. if (user.perms.length > 0) {
  37. let perms = user.perms;
  38. let p = perms[Math.floor(Math.random() * perms.length)];
  39. let res2 = await contract.query.hasPermission(alice.address, {}, user.id, p);
  40. expect(res2.output?.toJSON()).toBe(true);
  41. }
  42. user = users[Math.floor(Math.random() * users.length)];
  43. let res3 = await contract.query.getUserByAddress(alice.address, {}, user.addr);
  44. expect(res3.output?.toJSON()).toStrictEqual(user);
  45. const tx2 = contract.tx.removeUser({ gasLimit }, user.id);
  46. await transaction(tx2, alice);
  47. let res4 = await contract.query.userExists(alice.address, {}, user.id);
  48. expect(res4.output?.toJSON()).toBe(false);
  49. });
  50. });