github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/solidity_kat/test/AssetTrail.js (about)

     1  'use strict';
     2  
     3  const AssetTrailContract = artifacts.require('./AssetTrail.sol');
     4  const Payment = artifacts.require('./Payment.sol');
     5  
     6  const initialSupply = 10000;
     7  
     8  /// Member constants
     9  
    10  const testMemberNames = [
    11    'member-1',
    12    'member-2',
    13    'member-2-update'
    14  ];
    15  
    16  const testAssetTrailInstanceIDs = [
    17    'xxxxxxxxxx-xxxxxxxxxx'
    18  ];
    19  
    20  const testApp2AppDestinations = [
    21    'kld://app2app-destination-1',
    22    'kld://app2app-destination-2',
    23    'kld://app2app-destination-2-update'
    24  ];
    25  
    26  const testDocExchangeDestinations = [
    27    'kld://documentstore-destination-1',
    28    'kld://documentstore-destination-2',
    29    'kld://documentstore-destination-2-update'
    30  ];
    31  
    32  // Asset definition constants
    33  
    34  const testAssetDefinitionIDs = [
    35    '0x6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b',
    36    '0xd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35',
    37    '0x4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce',
    38    '0x4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a'
    39  ];
    40  
    41  const testAssetDefinitionHashes = [
    42    '0x6ab9f1eb8f7d3388f4f9d586f66e99fd54080df2c446f0e58668b09c08a16dd0'
    43  ];
    44  
    45  // Asset instance constants
    46  
    47  const testAssetInstanceBatchHashes = [
    48    '0xa15abc51152dad05919a79966dc784f37491fa1c394b7935afcc5516b65ec98c',
    49    '0xd5cc610cc1373b3f26238641e51bf4d3cb20068ea3f589bfac413417b4d46f23'
    50  ];
    51  
    52  const testAssetInstanceIDs = [
    53    '0x1ebb3de9307e8c992e7b18f1f48767f6b83eb6befb72985a4f3043609ffa1e96',
    54    '0x82459a7f3e4b132093db4d651c74e384e72f8a808c2b321d7b3343d6fb872187'
    55  ];
    56  
    57  const testDescriptionHashes = [
    58    '0x27a84712e4b22c415fc544d55cdee82327a829f96d03329457f76ebf9af4dcaa',
    59    '0x13609d74cc8ea07555856a54ba51b01f831af4af89bd39847babe5bf6cb665df'
    60  ];
    61  
    62  const testContentHashes = [
    63    '0x4fb431659a5b45f4e7b1a69bacb4101a11b82777de6857c3e40d7fe217307285',
    64    '0x1e335362351e60f908f58ac674f8c0967dca8af41c3b696066b49810d399d795'
    65  ];
    66  
    67  // Payment definition constants
    68  
    69  const testPaymentDefinitionIDs = [
    70    '0xf64551fcd6f07823cb87971cfb91446425da18286b3ab1ef935e0cbd7a69f68a',
    71    '0x3946ca64ff78d93ca61090a437cbb6b3d2ca0d488f5f9ccf3059608368b27693'
    72  ];
    73  
    74  const testPaymentDefinitionNames = [
    75    'My subscriptions',
    76    'My purchases'
    77  ];
    78  
    79  const testPaymentSchemas = [
    80    '0x6dee8ee9d5a7743e2a86e03e652f072f520d0955d6c7551ba4c85f71011d0896'
    81  ];
    82  
    83  // Payment instance constants
    84  
    85  const testPaymentInstanceIDs = [
    86    '0x38f31ec46e5bcf5f86502bb4985963c7cf5b821f60ca140c2562636a136dd376',
    87    '0xc18a1517995c76e2104fb6d977661a2b2322340cd92d2c98af4779cf8a92bb3a'
    88  ];
    89  
    90  const testPaymentHashes = [
    91    '0x6c2d640aaf679ecf258150e2ceb742c48e8c1e71391d05a75e5e041ffd51c377'
    92  ]
    93  
    94  // Asset property constants
    95  
    96  const testAssetPropertyKeys = [
    97    'test-key'
    98  ];
    99  
   100  const testAssetPropertyValues = [
   101    'test-value'
   102  ];
   103  
   104  contract('AssetTrail.sol', accounts => {
   105    let payment;
   106    let assetTrailContract;
   107  
   108    before(async () => {
   109      payment = await Payment.new(initialSupply);
   110      assetTrailContract = await AssetTrailContract.new(payment.address);
   111      await payment.approve(assetTrailContract.address, initialSupply);
   112    });
   113  
   114    describe('Asset Trail', () => {
   115  
   116      describe('Members', () => {
   117  
   118        it('registerMember should raise an error if the name is empty', async () => {
   119          let exceptionMessage;
   120          try {
   121            await assetTrailContract.registerMember('', testAssetTrailInstanceIDs[0], testApp2AppDestinations[0], testDocExchangeDestinations[0]);
   122          } catch (err) {
   123            exceptionMessage = err.message;
   124          }
   125          assert(exceptionMessage.includes('Invalid name'));
   126        });
   127  
   128        it('registerMember should register a member and emit the corresponding event (member 1)', async () => {
   129          const result = await assetTrailContract.registerMember(testMemberNames[0], testAssetTrailInstanceIDs[0], testApp2AppDestinations[0], testDocExchangeDestinations[0]);
   130          const logArgs = result.logs[0].args;
   131          assert.equal(logArgs.member, accounts[0]);
   132          assert.equal(logArgs.name, testMemberNames[0]);
   133          assert.equal(logArgs.assetTrailInstanceID, testAssetTrailInstanceIDs[0]);
   134          assert.equal(logArgs.app2appDestination, testApp2AppDestinations[0]);
   135          assert.equal(logArgs.docExchangeDestination, testDocExchangeDestinations[0]);
   136          assert(logArgs.timestamp.toNumber() > 0);
   137        });
   138  
   139        it('registerMember should register a member and emit the corresponding event (member 2)', async () => {
   140          const result = await assetTrailContract.registerMember(testMemberNames[1], testAssetTrailInstanceIDs[0], testApp2AppDestinations[1], testDocExchangeDestinations[1], { from: accounts[1] });
   141          const logArgs = result.logs[0].args;
   142          assert.equal(logArgs.member, accounts[1]);
   143          assert.equal(logArgs.name, testMemberNames[1]);
   144          assert.equal(logArgs.assetTrailInstanceID, testAssetTrailInstanceIDs[0]);
   145          assert.equal(logArgs.app2appDestination, testApp2AppDestinations[1]);
   146          assert.equal(logArgs.docExchangeDestination, testDocExchangeDestinations[1]);
   147          assert(logArgs.timestamp.toNumber() > 0);
   148        });
   149  
   150        it('registerMember should allow members to update their name and destinations (member 2)', async () => {
   151          const result = await assetTrailContract.registerMember(testMemberNames[2], testAssetTrailInstanceIDs[0], testApp2AppDestinations[2], testDocExchangeDestinations[2], { from: accounts[1] });
   152          const logArgs = result.logs[0].args;
   153          assert.equal(logArgs.member, accounts[1]);
   154          assert.equal(logArgs.name, testMemberNames[2]);
   155          assert.equal(logArgs.assetTrailInstanceID, testAssetTrailInstanceIDs[0]);
   156          assert.equal(logArgs.app2appDestination, testApp2AppDestinations[2]);
   157          assert.equal(logArgs.docExchangeDestination, testDocExchangeDestinations[2]);
   158          assert(logArgs.timestamp.toNumber() > 0);
   159        });
   160  
   161      });
   162  
   163      describe('Assets definitions', () => {
   164  
   165        it('createAssetDefiniton should create a new asset definition and emit the corresponding event', async () => {
   166          const result = await assetTrailContract.createAssetDefinition(testAssetDefinitionHashes[0]);
   167          const logArgs = result.logs[0].args;
   168          assert.equal(logArgs.assetDefinitionHash, testAssetDefinitionHashes[0]);
   169        });
   170  
   171      });
   172  
   173      describe('Asset instances', () => {
   174  
   175        it('createDescribedAssetInstance should create a new described asset instance and emit the corresponding event', async () => {
   176          const result = await assetTrailContract.createDescribedAssetInstance(testAssetInstanceIDs[0], testAssetDefinitionIDs[0], testDescriptionHashes[0], testContentHashes[0]);
   177          const logArgs = result.logs[0].args;
   178          assert.equal(logArgs.assetInstanceID, testAssetInstanceIDs[0]);
   179          assert.equal(logArgs.assetDefinitionID, testAssetDefinitionIDs[0]);
   180          assert.equal(logArgs.author, accounts[0]);
   181          assert.equal(logArgs.descriptionHash, testDescriptionHashes[0]);
   182          assert.equal(logArgs.contentHash, testContentHashes[0]);
   183          assert(logArgs.timestamp.toNumber() > 0);
   184        });
   185  
   186        it('createAssetInstance should create a new asset instance and emit the corresponding event', async () => {
   187          const result = await assetTrailContract.createAssetInstance(testAssetInstanceIDs[1], testAssetDefinitionIDs[2], testContentHashes[0]);
   188          const logArgs = result.logs[0].args;
   189          assert.equal(logArgs.assetInstanceID, testAssetInstanceIDs[1]);
   190          assert.equal(logArgs.assetDefinitionID, testAssetDefinitionIDs[2]);
   191          assert.equal(logArgs.author, accounts[0]);
   192          assert.equal(logArgs.contentHash, testContentHashes[0]);
   193          assert(logArgs.timestamp.toNumber() > 0);
   194        });
   195  
   196      });
   197  
   198      describe('Asset instance batches', () => {
   199  
   200        it('createAssetInstanceBatch should emit the corresponding event', async () => {
   201          const result = await assetTrailContract.createAssetInstanceBatch(testAssetInstanceBatchHashes[0]);
   202          const logArgs = result.logs[0].args;
   203          assert.equal(logArgs.batchHash, testAssetInstanceBatchHashes[0]);
   204          assert.equal(logArgs.author, accounts[0]);
   205          assert(logArgs.timestamp.toNumber() > 0);
   206        });
   207  
   208      });
   209  
   210      describe('Described payment definitions', () => {
   211  
   212        it('createDescribedPaymentDefinition should raise an error if the name is empty', async () => {
   213          let exceptionMessage;
   214          try {
   215            await assetTrailContract.createDescribedPaymentDefinition(testPaymentDefinitionIDs[0], '', testPaymentSchemas[0]);
   216          } catch (err) {
   217            exceptionMessage = err.message;
   218          }
   219          assert(exceptionMessage.includes('Invalid name'));
   220        });
   221  
   222        it('createDescribedPaymentDefinition should create a new payment definition and emit the corresponding event', async () => {
   223          const result = await assetTrailContract.createDescribedPaymentDefinition(testPaymentDefinitionIDs[0], testPaymentDefinitionNames[0], testPaymentSchemas[0]);
   224          const logArgs = result.logs[0].args;
   225          assert.equal(logArgs.author, accounts[0]);
   226          assert.equal(logArgs.paymentDefinitionID, testPaymentDefinitionIDs[0]);
   227          assert.equal(logArgs.name, testPaymentDefinitionNames[0]);
   228          assert.equal(logArgs.descriptionSchemaHash, testPaymentSchemas[0]);
   229          assert(logArgs.timestamp.toNumber() > 0);
   230        });
   231  
   232      });
   233  
   234  
   235      describe('Payment definitions', () => {
   236  
   237        it('createPaymentDefinition should raise an error if the name is empty', async () => {
   238          let exceptionMessage;
   239          try {
   240            await assetTrailContract.createPaymentDefinition(testPaymentDefinitionIDs[1], '');
   241          } catch (err) {
   242            exceptionMessage = err.message;
   243          }
   244          assert(exceptionMessage.includes('Invalid name'));
   245        });
   246  
   247        it('createPaymentDefinition should create a new payment definition and emit the corresponding event', async () => {
   248          const result = await assetTrailContract.createPaymentDefinition(testPaymentDefinitionIDs[1], testPaymentDefinitionNames[1]);
   249          const logArgs = result.logs[0].args;
   250          assert.equal(logArgs.author, accounts[0]);
   251          assert.equal(logArgs.paymentDefinitionID, testPaymentDefinitionIDs[1]);
   252          assert.equal(logArgs.name, testPaymentDefinitionNames[1]);
   253          assert(logArgs.timestamp.toNumber() > 0);
   254        });
   255  
   256      });
   257  
   258      describe('Described payment instances', () => {
   259  
   260        it('createDescribedPaymentInstance should raise an error if author and member are the same', async () => {
   261          let exceptionMessage;
   262          try {
   263            await assetTrailContract.createDescribedPaymentInstance(testPaymentInstanceIDs[0], testPaymentDefinitionIDs[0], accounts[0], 1, testPaymentHashes[0]);
   264          } catch (err) {
   265            exceptionMessage = err.message;
   266          }
   267          assert(exceptionMessage.includes('Author and member cannot be the same'));
   268        });
   269  
   270        it('createDescribedPaymentInstance should raise an error if amount is 0', async () => {
   271          let exceptionMessage;
   272          try {
   273            await assetTrailContract.createDescribedPaymentInstance(testPaymentInstanceIDs[0], testPaymentDefinitionIDs[0], accounts[1], 0, testPaymentHashes[0]);
   274          } catch (err) {
   275            exceptionMessage = err.message;
   276          }
   277          assert(exceptionMessage.includes('Amount must be greater than 0'));
   278        });
   279  
   280        it('createDescribedPaymentInstance should create a new payment instance and emit the corresponding event', async () => {
   281          const balanceAccount0Before = await payment.balanceOf(accounts[0]);
   282          const balanceAccount1Before = await payment.balanceOf(accounts[1]);
   283  
   284          const result = await assetTrailContract.createDescribedPaymentInstance(testPaymentInstanceIDs[0], testPaymentDefinitionIDs[0], accounts[1], 1, testPaymentHashes[0]);
   285          const logArgs = result.logs[0].args;
   286          assert.equal(logArgs.paymentInstanceID, testPaymentInstanceIDs[0]);
   287          assert.equal(logArgs.paymentDefinitionID, testPaymentDefinitionIDs[0]);
   288          assert.equal(logArgs.author, accounts[0]);
   289          assert.equal(logArgs.member, accounts[1]);
   290          assert.equal(logArgs.amount, 1);
   291          assert.equal(logArgs.descriptionHash, testPaymentHashes[0]);
   292          assert(logArgs.timestamp.toNumber() > 0);
   293  
   294          const balanceAccount0After = await payment.balanceOf(accounts[0]);
   295          const balanceAccount1After = await payment.balanceOf(accounts[1]);
   296          assert(balanceAccount0Before.toNumber() === balanceAccount0After.toNumber() + 1);
   297          assert(balanceAccount1Before.toNumber() === balanceAccount1After.toNumber() - 1);
   298        });
   299  
   300      });
   301  
   302      describe('Payment instances', () => {
   303  
   304        it('createPaymentInstance should raise an error if author and member are the same', async () => {
   305          let exceptionMessage;
   306          try {
   307            await assetTrailContract.createPaymentInstance(testPaymentInstanceIDs[0], testPaymentDefinitionIDs[1], accounts[0], 1);
   308          } catch (err) {
   309            exceptionMessage = err.message;
   310          }
   311          assert(exceptionMessage.includes('Author and member cannot be the same'));
   312        });
   313  
   314        it('createPaymentInstance should raise an error if the amount is 0', async () => {
   315          let exceptionMessage;
   316          try {
   317            await assetTrailContract.createPaymentInstance(testPaymentInstanceIDs[0], testPaymentDefinitionIDs[1], accounts[1], 0);
   318          } catch (err) {
   319            exceptionMessage = err.message;
   320          }
   321          assert(exceptionMessage.includes('Amount must be greater than 0'));
   322        });
   323  
   324        it('createPaymentInstance should create a new payment instance and emit the corresponding event', async () => {
   325          const balanceAccount0Before = await payment.balanceOf(accounts[0]);
   326          const balanceAccount1Before = await payment.balanceOf(accounts[1]);
   327  
   328          const result = await assetTrailContract.createPaymentInstance(testPaymentInstanceIDs[1], testPaymentDefinitionIDs[1], accounts[1], 1);
   329          const logArgs = result.logs[0].args;
   330          assert.equal(logArgs.paymentInstanceID, testPaymentInstanceIDs[1]);
   331          assert.equal(logArgs.paymentDefinitionID, testPaymentDefinitionIDs[1]);
   332          assert.equal(logArgs.author, accounts[0]);
   333          assert.equal(logArgs.member, accounts[1]);
   334          assert.equal(logArgs.amount, 1);
   335          assert(logArgs.timestamp.toNumber() > 0);
   336  
   337          const balanceAccount0After = await payment.balanceOf(accounts[0]);
   338          const balanceAccount1After = await payment.balanceOf(accounts[1]);
   339          assert(balanceAccount0Before.toNumber() === balanceAccount0After.toNumber() + 1);
   340          assert(balanceAccount1Before.toNumber() === balanceAccount1After.toNumber() - 1);
   341        });
   342  
   343      });
   344  
   345      describe('Asset properties', () => {
   346  
   347        it('setAssetInstanceProperty should raise an error if the key is empty', async () => {
   348          let exceptionMessage;
   349          try {
   350            await assetTrailContract.setAssetInstanceProperty(testAssetDefinitionIDs[0], testAssetInstanceIDs[0], '', testAssetPropertyValues[0]);
   351          } catch (err) {
   352            exceptionMessage = err.message;
   353          }
   354          assert(exceptionMessage.includes('Invalid key'));
   355        });
   356  
   357        it('setAssetInstanceProperty should set an asset property and emit the corresponding event', async () => {
   358          const result = await assetTrailContract.setAssetInstanceProperty(testAssetDefinitionIDs[0], testAssetInstanceIDs[0], testAssetPropertyKeys[0], testAssetPropertyValues[0]);
   359          const logArgs = result.logs[0].args;
   360          assert.equal(logArgs.assetDefinitionID, testAssetDefinitionIDs[0]);
   361          assert.equal(logArgs.assetInstanceID, testAssetInstanceIDs[0]);
   362          assert.equal(logArgs.author, accounts[0]);
   363          assert.equal(logArgs.key, testAssetPropertyKeys[0]);
   364          assert.equal(logArgs.value, testAssetPropertyValues[0]);
   365          assert(logArgs.timestamp.toNumber() > 0);
   366        });
   367  
   368      });
   369  
   370    });
   371  
   372  });