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