Advertisement
Guest User

Untitled

a guest
Mar 29th, 2018
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Licensed under the Apache License, Version 2.0 (the "License");
  3.  * you may not use this file except in compliance with the License.
  4.  * You may obtain a copy of the License at
  5.  *
  6.  * http://www.apache.org/licenses/LICENSE-2.0
  7.  *
  8.  * Unless required by applicable law or agreed to in writing, software
  9.  * distributed under the License is distributed on an "AS IS" BASIS,
  10.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11.  * See the License for the specific language governing permissions and
  12.  * limitations under the License.
  13.  */
  14.  
  15. 'use strict';
  16. /**
  17.  * Write the unit tests for your transction processor functions here
  18.  */
  19.  
  20. const AdminConnection = require('composer-admin').AdminConnection;
  21. const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
  22. const { BusinessNetworkDefinition, CertificateUtil, IdCard } = require('composer-common');
  23. const path = require('path');
  24.  
  25. const chai = require('chai');
  26. chai.should();
  27. chai.use(require('chai-as-promised'));
  28.  
  29. const namespace = 'org.example.biznet';
  30. const assetType = 'SampleAsset';
  31. const assetNS = namespace + '.' + assetType;
  32. const participantType = 'SampleParticipant';
  33. const participantNS = namespace + '.' + participantType;
  34.  
  35. describe('#' + namespace, () => {
  36.     // In-memory card store for testing so cards are not persisted to the file system
  37.     const cardStore = require('composer-common').NetworkCardStoreManager.getCardStore( { type: 'composer-wallet-inmemory' } );
  38.  
  39.     // Embedded connection used for local testing
  40.     const connectionProfile = {
  41.         name: 'embedded',
  42.         'x-type': 'embedded'
  43.     };
  44.  
  45.     // Name of the business network card containing the administrative identity for the business network
  46.     const adminCardName = 'admin';
  47.  
  48.     // Admin connection to the blockchain, used to deploy the business network
  49.     let adminConnection;
  50.  
  51.     // This is the business network connection the tests will use.
  52.     let businessNetworkConnection;
  53.  
  54.     // This is the factory for creating instances of types.
  55.     let factory;
  56.  
  57.     // These are the identities for Alice and Bob.
  58.     const aliceCardName = 'alice';
  59.     const bobCardName = 'bob';
  60.  
  61.     // These are a list of receieved events.
  62.     let events;
  63.  
  64.     let businessNetworkName;
  65.  
  66.     before(async () => {
  67.         // Generate certificates for use with the embedded connection
  68.         const credentials = CertificateUtil.generate({ commonName: 'admin' });
  69.  
  70.         // Identity used with the admin connection to deploy business networks
  71.         const deployerMetadata = {
  72.             version: 1,
  73.             userName: 'PeerAdmin',
  74.             roles: [ 'PeerAdmin', 'ChannelAdmin' ]
  75.         };
  76.         const deployerCard = new IdCard(deployerMetadata, connectionProfile);
  77.         deployerCard.setCredentials(credentials);
  78.         const deployerCardName = 'PeerAdmin';
  79.  
  80.         adminConnection = new AdminConnection({ cardStore: cardStore });
  81.  
  82.         await adminConnection.importCard(deployerCardName, deployerCard);
  83.         await adminConnection.connect(deployerCardName);
  84.     });
  85.  
  86.     /**
  87.      *
  88.      * @param {String} cardName The card name to use for this identity
  89.      * @param {Object} identity The identity details
  90.      */
  91.     async function importCardForIdentity(cardName, identity) {
  92.         const metadata = {
  93.             userName: identity.userID,
  94.             version: 1,
  95.             enrollmentSecret: identity.userSecret,
  96.             businessNetwork: businessNetworkName
  97.         };
  98.         const card = new IdCard(metadata, connectionProfile);
  99.         await adminConnection.importCard(cardName, card);
  100.     }
  101.  
  102.     // This is called before each test is executed.
  103.     beforeEach(async () => {
  104.         // Generate a business network definition from the project directory.
  105.         let businessNetworkDefinition = await BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..'));
  106.         businessNetworkName = businessNetworkDefinition.getName();
  107.         await adminConnection.install(businessNetworkDefinition);
  108.         const startOptions = {
  109.             networkAdmins: [
  110.                 {
  111.                     userName: 'admin',
  112.                     enrollmentSecret: 'adminpw'
  113.                 }
  114.             ]
  115.         };
  116.         const adminCards = await adminConnection.start(businessNetworkName, businessNetworkDefinition.getVersion(), startOptions);
  117.         await adminConnection.importCard(adminCardName, adminCards.get('admin'));
  118.  
  119.         // Create and establish a business network connection
  120.         businessNetworkConnection = new BusinessNetworkConnection({ cardStore: cardStore });
  121.         events = [];
  122.         businessNetworkConnection.on('event', event => {
  123.             events.push(event);
  124.         });
  125.         await businessNetworkConnection.connect(adminCardName);
  126.  
  127.         // Get the factory for the business network.
  128.         factory = businessNetworkConnection.getBusinessNetwork().getFactory();
  129.  
  130.         const participantRegistry = await businessNetworkConnection.getParticipantRegistry(participantNS);
  131.         // Create the participants.
  132.         const alice = factory.newResource(namespace, participantType, 'alice@email.com');
  133.         alice.firstName = 'Alice';
  134.         alice.lastName = 'A';
  135.  
  136.         const bob = factory.newResource(namespace, participantType, 'bob@email.com');
  137.         bob.firstName = 'Bob';
  138.         bob.lastName = 'B';
  139.  
  140.         participantRegistry.addAll([alice, bob]);
  141.  
  142.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  143.         // Create the assets.
  144.         const asset1 = factory.newResource(namespace, assetType, '1');
  145.         asset1.owner = factory.newRelationship(namespace, participantType, 'alice@email.com');
  146.         asset1.value = '10';
  147.  
  148.         const asset2 = factory.newResource(namespace, assetType, '2');
  149.         asset2.owner = factory.newRelationship(namespace, participantType, 'bob@email.com');
  150.         asset2.value = '20';
  151.  
  152.         assetRegistry.addAll([asset1, asset2]);
  153.  
  154.         // Issue the identities.
  155.         let identity = await businessNetworkConnection.issueIdentity(participantNS + '#alice@email.com', 'alice1');
  156.         await importCardForIdentity(aliceCardName, identity);
  157.         identity = await businessNetworkConnection.issueIdentity(participantNS + '#bob@email.com', 'bob1');
  158.         await importCardForIdentity(bobCardName, identity);
  159.     });
  160.  
  161.     /**
  162.      * Reconnect using a different identity.
  163.      * @param {String} cardName The name of the card for the identity to use
  164.      */
  165.     async function useIdentity(cardName) {
  166.         await businessNetworkConnection.disconnect();
  167.         businessNetworkConnection = new BusinessNetworkConnection({ cardStore: cardStore });
  168.         events = [];
  169.         businessNetworkConnection.on('event', (event) => {
  170.             events.push(event);
  171.         });
  172.         await businessNetworkConnection.connect(cardName);
  173.         factory = businessNetworkConnection.getBusinessNetwork().getFactory();
  174.     }
  175.  
  176.     it('Alice can read all of the assets', async () => {
  177.         // Use the identity for Alice.
  178.         await useIdentity(aliceCardName);
  179.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  180.         const assets = await assetRegistry.getAll();
  181.  
  182.         // Validate the assets.
  183.         assets.should.have.lengthOf(2);
  184.         const asset1 = assets[0];
  185.         asset1.owner.getFullyQualifiedIdentifier().should.equal(participantNS + '#alice@email.com');
  186.         asset1.value.should.equal('10');
  187.         const asset2 = assets[1];
  188.         asset2.owner.getFullyQualifiedIdentifier().should.equal(participantNS + '#bob@email.com');
  189.         asset2.value.should.equal('20');
  190.     });
  191.  
  192.     it('Bob can read all of the assets', async () => {
  193.         // Use the identity for Bob.
  194.         await useIdentity(bobCardName);
  195.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  196.         const assets = await assetRegistry.getAll();
  197.  
  198.         // Validate the assets.
  199.         assets.should.have.lengthOf(2);
  200.         const asset1 = assets[0];
  201.         asset1.owner.getFullyQualifiedIdentifier().should.equal(participantNS + '#alice@email.com');
  202.         asset1.value.should.equal('10');
  203.         const asset2 = assets[1];
  204.         asset2.owner.getFullyQualifiedIdentifier().should.equal(participantNS + '#bob@email.com');
  205.         asset2.value.should.equal('20');
  206.     });
  207.  
  208.     it('Alice can add assets that she owns', async () => {
  209.         // Use the identity for Alice.
  210.         await useIdentity(aliceCardName);
  211.  
  212.         // Create the asset.
  213.         let asset3 = factory.newResource(namespace, assetType, '3');
  214.         asset3.owner = factory.newRelationship(namespace, participantType, 'alice@email.com');
  215.         asset3.value = '30';
  216.  
  217.         // Add the asset, then get the asset.
  218.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  219.         await assetRegistry.add(asset3);
  220.  
  221.         // Validate the asset.
  222.         asset3 = await assetRegistry.get('3');
  223.         asset3.owner.getFullyQualifiedIdentifier().should.equal(participantNS + '#alice@email.com');
  224.         asset3.value.should.equal('30');
  225.     });
  226.  
  227.     it('Alice cannot add assets that Bob owns', async () => {
  228.         // Use the identity for Alice.
  229.         await useIdentity(aliceCardName);
  230.  
  231.         // Create the asset.
  232.         const asset3 = factory.newResource(namespace, assetType, '3');
  233.         asset3.owner = factory.newRelationship(namespace, participantType, 'bob@email.com');
  234.         asset3.value = '30';
  235.  
  236.         // Try to add the asset, should fail.
  237.         const assetRegistry = await  businessNetworkConnection.getAssetRegistry(assetNS);
  238.         assetRegistry.add(asset3).should.be.rejectedWith(/does not have .* access to resource/);
  239.     });
  240.  
  241.     it('Bob can add assets that he owns', async () => {
  242.         // Use the identity for Bob.
  243.         await useIdentity(bobCardName);
  244.  
  245.         // Create the asset.
  246.         let asset4 = factory.newResource(namespace, assetType, '4');
  247.         asset4.owner = factory.newRelationship(namespace, participantType, 'bob@email.com');
  248.         asset4.value = '40';
  249.  
  250.         // Add the asset, then get the asset.
  251.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  252.         await assetRegistry.add(asset4);
  253.  
  254.         // Validate the asset.
  255.         asset4 = await assetRegistry.get('4');
  256.         asset4.owner.getFullyQualifiedIdentifier().should.equal(participantNS + '#bob@email.com');
  257.         asset4.value.should.equal('40');
  258.     });
  259.  
  260.     it('Bob cannot add assets that Alice owns', async () => {
  261.         // Use the identity for Bob.
  262.         await useIdentity(bobCardName);
  263.  
  264.         // Create the asset.
  265.         const asset4 = factory.newResource(namespace, assetType, '4');
  266.         asset4.owner = factory.newRelationship(namespace, participantType, 'alice@email.com');
  267.         asset4.value = '40';
  268.  
  269.         // Try to add the asset, should fail.
  270.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  271.         assetRegistry.add(asset4).should.be.rejectedWith(/does not have .* access to resource/);
  272.  
  273.     });
  274.  
  275.     it('Alice can update her assets', async () => {
  276.         // Use the identity for Alice.
  277.         await useIdentity(aliceCardName);
  278.  
  279.         // Create the asset.
  280.         let asset1 = factory.newResource(namespace, assetType, '1');
  281.         asset1.owner = factory.newRelationship(namespace, participantType, 'alice@email.com');
  282.         asset1.value = '50';
  283.  
  284.         // Update the asset, then get the asset.
  285.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  286.         await assetRegistry.update(asset1);
  287.  
  288.         // Validate the asset.
  289.         asset1 = await assetRegistry.get('1');
  290.         asset1.owner.getFullyQualifiedIdentifier().should.equal(participantNS + '#alice@email.com');
  291.         asset1.value.should.equal('50');
  292.     });
  293.  
  294.     it('Alice cannot update Bob\'s assets', async () => {
  295.         // Use the identity for Alice.
  296.         await useIdentity(aliceCardName);
  297.  
  298.         // Create the asset.
  299.         const asset2 = factory.newResource(namespace, assetType, '2');
  300.         asset2.owner = factory.newRelationship(namespace, participantType, 'bob@email.com');
  301.         asset2.value = '50';
  302.  
  303.         // Try to update the asset, should fail.
  304.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  305.         assetRegistry.update(asset2).should.be.rejectedWith(/does not have .* access to resource/);
  306.     });
  307.  
  308.     it('Bob can update his assets', async () => {
  309.         // Use the identity for Bob.
  310.         await useIdentity(bobCardName);
  311.  
  312.         // Create the asset.
  313.         let asset2 = factory.newResource(namespace, assetType, '2');
  314.         asset2.owner = factory.newRelationship(namespace, participantType, 'bob@email.com');
  315.         asset2.value = '60';
  316.  
  317.         // Update the asset, then get the asset.
  318.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  319.         await assetRegistry.update(asset2);
  320.  
  321.         // Validate the asset.
  322.         asset2 = await assetRegistry.get('2');
  323.         asset2.owner.getFullyQualifiedIdentifier().should.equal(participantNS + '#bob@email.com');
  324.         asset2.value.should.equal('60');
  325.     });
  326.  
  327.     it('Bob cannot update Alice\'s assets', async () => {
  328.         // Use the identity for Bob.
  329.         await useIdentity(bobCardName);
  330.  
  331.         // Create the asset.
  332.         const asset1 = factory.newResource(namespace, assetType, '1');
  333.         asset1.owner = factory.newRelationship(namespace, participantType, 'alice@email.com');
  334.         asset1.value = '60';
  335.  
  336.         // Update the asset, then get the asset.
  337.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  338.         assetRegistry.update(asset1).should.be.rejectedWith(/does not have .* access to resource/);
  339.  
  340.     });
  341.  
  342.     it('Alice can remove her assets', async () => {
  343.         // Use the identity for Alice.
  344.         await useIdentity(aliceCardName);
  345.  
  346.         // Remove the asset, then test the asset exists.
  347.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  348.         await assetRegistry.remove('1');
  349.         const exists = await assetRegistry.exists('1');
  350.         exists.should.be.false;
  351.     });
  352.  
  353.     it('Alice cannot remove Bob\'s assets', async () => {
  354.         // Use the identity for Alice.
  355.         await useIdentity(aliceCardName);
  356.  
  357.         // Remove the asset, then test the asset exists.
  358.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  359.         assetRegistry.remove('2').should.be.rejectedWith(/does not have .* access to resource/);
  360.     });
  361.  
  362.     it('Bob can remove his assets', async () => {
  363.         // Use the identity for Bob.
  364.         await useIdentity(bobCardName);
  365.  
  366.         // Remove the asset, then test the asset exists.
  367.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  368.         await assetRegistry.remove('2');
  369.         const exists = await assetRegistry.exists('2');
  370.         exists.should.be.false;
  371.     });
  372.  
  373.     it('Bob cannot remove Alice\'s assets', async () => {
  374.         // Use the identity for Bob.
  375.         await useIdentity(bobCardName);
  376.  
  377.         // Remove the asset, then test the asset exists.
  378.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  379.         assetRegistry.remove('1').should.be.rejectedWith(/does not have .* access to resource/);
  380.     });
  381.  
  382.     it('Alice can submit a transaction for her assets', async () => {
  383.         // Use the identity for Alice.
  384.         await useIdentity(aliceCardName);
  385.  
  386.         // Submit the transaction.
  387.         const transaction = factory.newTransaction(namespace, 'SampleTransaction');
  388.         transaction.asset = factory.newRelationship(namespace, assetType, '1');
  389.         transaction.newValue = '50';
  390.         await businessNetworkConnection.submitTransaction(transaction);
  391.  
  392.         // Get the asset.
  393.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  394.         const asset1 = await assetRegistry.get('1');
  395.  
  396.         // Validate the asset.
  397.         asset1.owner.getFullyQualifiedIdentifier().should.equal(participantNS + '#alice@email.com');
  398.         asset1.value.should.equal('50');
  399.  
  400.         // Validate the events.
  401.         events.should.have.lengthOf(1);
  402.         const event = events[0];
  403.         event.eventId.should.be.a('string');
  404.         event.timestamp.should.be.an.instanceOf(Date);
  405.         event.asset.getFullyQualifiedIdentifier().should.equal(assetNS + '#1');
  406.         event.oldValue.should.equal('10');
  407.         event.newValue.should.equal('50');
  408.     });
  409.  
  410.     it('Alice cannot submit a transaction for Bob\'s assets', async () => {
  411.         // Use the identity for Alice.
  412.         await useIdentity(aliceCardName);
  413.  
  414.         // Submit the transaction.
  415.         const transaction = factory.newTransaction(namespace, 'SampleTransaction');
  416.         transaction.asset = factory.newRelationship(namespace, assetType, '2');
  417.         transaction.newValue = '50';
  418.         businessNetworkConnection.submitTransaction(transaction).should.be.rejectedWith(/does not have .* access to resource/);
  419.     });
  420.  
  421.     it('Bob can submit a transaction for his assets', async () => {
  422.         // Use the identity for Bob.
  423.         await useIdentity(bobCardName);
  424.  
  425.         // Submit the transaction.
  426.         const transaction = factory.newTransaction(namespace, 'SampleTransaction');
  427.         transaction.asset = factory.newRelationship(namespace, assetType, '2');
  428.         transaction.newValue = '60';
  429.         await businessNetworkConnection.submitTransaction(transaction);
  430.  
  431.         // Get the asset.
  432.         const assetRegistry = await businessNetworkConnection.getAssetRegistry(assetNS);
  433.         const asset2 = await assetRegistry.get('2');
  434.  
  435.         // Validate the asset.
  436.         asset2.owner.getFullyQualifiedIdentifier().should.equal(participantNS + '#bob@email.com');
  437.         asset2.value.should.equal('60');
  438.  
  439.         // Validate the events.
  440.         events.should.have.lengthOf(1);
  441.         const event = events[0];
  442.         event.eventId.should.be.a('string');
  443.         event.timestamp.should.be.an.instanceOf(Date);
  444.         event.asset.getFullyQualifiedIdentifier().should.equal(assetNS + '#2');
  445.         event.oldValue.should.equal('20');
  446.         event.newValue.should.equal('60');
  447.     });
  448.  
  449.     it('Bob cannot submit a transaction for Alice\'s assets', async () => {
  450.         // Use the identity for Bob.
  451.         await useIdentity(bobCardName);
  452.  
  453.         // Submit the transaction.
  454.         const transaction = factory.newTransaction(namespace, 'SampleTransaction');
  455.         transaction.asset = factory.newRelationship(namespace, assetType, '1');
  456.         transaction.newValue = '60';
  457.         businessNetworkConnection.submitTransaction(transaction).should.be.rejectedWith(/does not have .* access to resource/);
  458.     });
  459.  
  460. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement