git.delta.rocks / unique-network / refs/commits / d6234d028cb3

difftreelog

source

tests/src/addCollectionAdmin.test.ts6.1 KiBsourcehistory
1//
2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.
4//
5
6import { ApiPromise } from '@polkadot/api';
7import BN from 'bn.js';
8import chai from 'chai';
9import chaiAsPromised from 'chai-as-promised';
10import privateKey from './substrate/privateKey';
11import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';
12import {createCollectionExpectSuccess, destroyCollectionExpectSuccess} from './util/helpers';
13
14chai.use(chaiAsPromised);
15const expect = chai.expect;
16
17describe('Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {
18  it('Add collection admin.', async () => {
19    await usingApi(async (api) => {
20      const collectionId = await createCollectionExpectSuccess();
21      const alice = privateKey('//Alice');
22      const bob = privateKey('//Bob');
23
24      const collection: any = (await api.query.nft.collection(collectionId));
25      expect(collection.Owner.toString()).to.be.eq(alice.address);
26
27      const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, bob.address);
28      await submitTransactionAsync(alice, changeAdminTx);
29
30      const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
31      expect(adminListAfterAddAdmin).to.be.contains(bob.address);
32    });
33  });
34
35  it('Add admin using added collection admin.', async () => {
36    await usingApi(async (api) => {
37      const collectionId = await createCollectionExpectSuccess();
38      const Alice = privateKey('//Alice');
39      const Bob = privateKey('//Bob');
40      const Charlie = privateKey('//CHARLIE');
41
42      const collection: any = (await api.query.nft.collection(collectionId));
43      expect(collection.Owner.toString()).to.be.eq(Alice.address);
44
45      const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address);
46      await submitTransactionAsync(Alice, changeAdminTx);
47
48      const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
49      expect(adminListAfterAddAdmin).to.be.contains(Bob.address);
50
51      const changeAdminTxCharlie = api.tx.nft.addCollectionAdmin(collectionId, Charlie.address);
52      await submitTransactionAsync(Bob, changeAdminTxCharlie);
53      const adminListAfterAddNewAdmin: any = (await api.query.nft.adminList(collectionId));
54      expect(adminListAfterAddNewAdmin).to.be.contains(Bob.address);
55      expect(adminListAfterAddNewAdmin).to.be.contains(Charlie.address);
56    });
57  });
58});
59
60describe('Negative Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {
61  it("Not owner can't add collection admin.", async () => {
62    await usingApi(async (api) => {
63      const collectionId = await createCollectionExpectSuccess();
64      const alice = privateKey('//Alice');
65      const nonOwner = privateKey('//Bob_stash');
66
67      const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, alice.address);
68      await expect(submitTransactionExpectFailAsync(nonOwner, changeAdminTx)).to.be.rejected;
69
70      const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
71      expect(adminListAfterAddAdmin).not.to.be.contains(alice.address);
72
73      // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
74      await createCollectionExpectSuccess();
75    });
76  });
77  it("Can't add collection admin of not existing collection.", async () => {
78    await usingApi(async (api) => {
79      // tslint:disable-next-line: no-bitwise
80      const collectionId = (1 << 32) - 1;
81      const alice = privateKey('//Alice');
82      const bob = privateKey('//Bob');
83
84      const changeOwnerTx = api.tx.nft.addCollectionAdmin(collectionId, bob.address);
85      await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;
86
87      // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
88      await createCollectionExpectSuccess();
89    });
90  });
91
92  it("Can't add an admin to a destroyed collection.", async () => {
93    await usingApi(async (api) => {
94      const collectionId = await createCollectionExpectSuccess();
95      const Alice = privateKey('//Alice');
96      const Bob = privateKey('//Bob');
97      await destroyCollectionExpectSuccess(collectionId);
98      const changeOwnerTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address);
99      await expect(submitTransactionExpectFailAsync(Alice, changeOwnerTx)).to.be.rejected;
100
101      // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
102      await createCollectionExpectSuccess();
103    });
104  });
105
106  it('Add an admin to a collection that has reached the maximum number of admins limit', async () => {
107    await usingApi(async (api: ApiPromise) => {
108      const Alice = privateKey('//Alice');
109      const accounts = [
110        'GsvVmjr1CBHwQHw84pPHMDxgNY3iBLz6Qn7qS3CH8qPhrHz',
111        'FoQJpPyadYccjavVdTWxpxU7rUEaYhfLCPwXgkfD6Zat9QP',
112        'JKspFU6ohf1Grg3Phdzj2pSgWvsYWzSfKghhfzMbdhNBWs5',
113        'Fr4NzY1udSFFLzb2R3qxVQkwz9cZraWkyfH4h3mVVk7BK7P',
114        'DfnTB4z7eUvYRqcGtTpFsLC69o6tvBSC1pEv8vWPZFtCkaK',
115        'HnMAUz7r2G8G3hB27SYNyit5aJmh2a5P4eMdDtACtMFDbam',
116        'DE14BzQ1bDXWPKeLoAqdLAm1GpyAWaWF1knF74cEZeomTBM',
117      ];
118      const collectionId = await createCollectionExpectSuccess();
119
120      const chainLimit = await api.query.nft.chainLimit() as unknown as { CollectionAdminsLimit: BN };
121      const chainAdminLimit = chainLimit.CollectionAdminsLimit.toNumber();
122      expect(chainAdminLimit).to.be.equal(5);
123
124      for (let i = 0; i < chainAdminLimit; i++) {
125        const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, accounts[i]);
126        await submitTransactionAsync(Alice, changeAdminTx);
127        const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
128        expect(adminListAfterAddAdmin).to.be.contains(accounts[i]);
129      }
130
131      const tx = api.tx.nft.addCollectionAdmin(collectionId, accounts[chainAdminLimit]);
132      await expect(submitTransactionExpectFailAsync(Alice, tx)).to.be.rejected;
133    });
134  });
135});