github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/test/ethereum/payments/unauthored-described.ts (about) 1 // Copyright © 2021 Kaleido, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 import { app, mockEventStreamWebSocket } from '../../common'; 16 import { testDescription } from '../../samples'; 17 import nock from 'nock'; 18 import request from 'supertest'; 19 import assert from 'assert'; 20 import { IEventPaymentDefinitionCreated, IDBPaymentDefinition, IDBPaymentInstance, IEventPaymentInstanceCreated } from '../../../lib/interfaces'; 21 import * as utils from '../../../lib/utils'; 22 23 export const testUnauthoredDescribed = async () => { 24 describe('Payment definitions: unauthored - described', async () => { 25 26 const paymentDefinitionID = '4b4a3be1-0732-4ba1-b492-6f315bb82f53'; 27 const timestamp = utils.getTimestamp(); 28 29 describe('Payment definition event', async () => { 30 31 it('Checks that the event stream notification for confirming the payment definition creation is handled', async () => { 32 33 nock('https://ipfs.kaleido.io') 34 .get(`/ipfs/${testDescription.schema.ipfsMultiHash}`) 35 .reply(200, testDescription.schema.object); 36 37 const eventPromise = new Promise<void>((resolve) => { 38 mockEventStreamWebSocket.once('send', message => { 39 assert.strictEqual(message, '{"type":"ack","topic":"dev"}'); 40 resolve(); 41 }) 42 }); 43 const data: IEventPaymentDefinitionCreated = { 44 paymentDefinitionID: utils.uuidToHex(paymentDefinitionID), 45 author: '0x0000000000000000000000000000000000000002', 46 name: 'unauthored - described', 47 descriptionSchemaHash: testDescription.schema.ipfsSha256, 48 timestamp: timestamp.toString() 49 }; 50 mockEventStreamWebSocket.emit('message', JSON.stringify([{ 51 signature: utils.contractEventSignatures.DESCRIBED_PAYMENT_DEFINITION_CREATED, 52 data, 53 blockNumber: '123', 54 transactionHash: '0x0000000000000000000000000000000000000000000000000000000000000000' 55 }])); 56 await eventPromise; 57 }); 58 59 it('Checks that the payment definition event has been processed', async () => { 60 const getPaymentDefinitionsResponse = await request(app) 61 .get('/api/v1/payments/definitions') 62 .expect(200); 63 const paymentDefinition = getPaymentDefinitionsResponse.body.find((paymentDefinition: IDBPaymentDefinition) => paymentDefinition.name === 'unauthored - described'); 64 assert.strictEqual(paymentDefinition.paymentDefinitionID, paymentDefinitionID); 65 assert.strictEqual(paymentDefinition.author, '0x0000000000000000000000000000000000000002'); 66 assert.deepStrictEqual(paymentDefinition.descriptionSchema, testDescription.schema.object); 67 assert.strictEqual(paymentDefinition.name, 'unauthored - described'); 68 assert.strictEqual(paymentDefinition.timestamp, timestamp); 69 assert.strictEqual(paymentDefinition.blockNumber, 123); 70 assert.strictEqual(paymentDefinition.transactionHash, '0x0000000000000000000000000000000000000000000000000000000000000000'); 71 72 const getPaymentDefinitionResponse = await request(app) 73 .get(`/api/v1/payments/definitions/${paymentDefinitionID}`) 74 .expect(200); 75 assert.deepStrictEqual(paymentDefinition, getPaymentDefinitionResponse.body); 76 }); 77 78 }); 79 80 describe('Payment instances', async () => { 81 82 const paymentInstanceID = 'f831b208-9567-4fe2-b388-8e994a6163e1'; 83 84 it('Checks that the event stream notification for confirming the payment instance creation is handled', async () => { 85 nock('https://ipfs.kaleido.io') 86 .get(`/ipfs/${testDescription.sample.ipfsMultiHash}`) 87 .reply(200, testDescription.sample.object); 88 89 const eventPromise = new Promise<void>((resolve) => { 90 mockEventStreamWebSocket.once('send', message => { 91 assert.strictEqual(message, '{"type":"ack","topic":"dev"}'); 92 resolve(); 93 }) 94 }); 95 const data: IEventPaymentInstanceCreated = { 96 paymentDefinitionID: utils.uuidToHex(paymentDefinitionID), 97 author: '0x0000000000000000000000000000000000000001', 98 paymentInstanceID: utils.uuidToHex(paymentInstanceID), 99 descriptionHash: testDescription.sample.ipfsSha256, 100 amount: '10', 101 member: '0x0000000000000000000000000000000000000002', 102 timestamp: timestamp.toString() 103 }; 104 mockEventStreamWebSocket.emit('message', JSON.stringify([{ 105 signature: utils.contractEventSignatures.DESCRIBED_PAYMENT_INSTANCE_CREATED, 106 data, 107 blockNumber: '123', 108 transactionHash: '0x0000000000000000000000000000000000000000000000000000000000000000' 109 }])); 110 await eventPromise; 111 }); 112 113 it('Checks that the payment instance is confirmed', async () => { 114 const getAssetInstancesResponse = await request(app) 115 .get('/api/v1/payments/instances') 116 .expect(200); 117 const paymentInstance = getAssetInstancesResponse.body.find((paymentInstance: IDBPaymentInstance) => paymentInstance.paymentInstanceID === paymentInstanceID); 118 assert.strictEqual(paymentInstance.author, '0x0000000000000000000000000000000000000001'); 119 assert.strictEqual(paymentInstance.member, '0x0000000000000000000000000000000000000002'); 120 assert.strictEqual(paymentInstance.paymentDefinitionID, paymentDefinitionID); 121 assert.strictEqual(paymentInstance.descriptionHash, testDescription.sample.ipfsSha256); 122 assert.deepStrictEqual(paymentInstance.description, testDescription.sample.object); 123 assert.strictEqual(paymentInstance.submitted, undefined); 124 assert.strictEqual(paymentInstance.receipt, undefined); 125 assert.strictEqual(paymentInstance.amount, 10); 126 assert.strictEqual(paymentInstance.timestamp, timestamp); 127 assert.strictEqual(paymentInstance.blockNumber, 123); 128 assert.strictEqual(paymentInstance.transactionHash, '0x0000000000000000000000000000000000000000000000000000000000000000'); 129 130 const getAssetInstanceResponse = await request(app) 131 .get(`/api/v1/payments/instances/${paymentInstanceID}`) 132 .expect(200); 133 assert.deepStrictEqual(paymentInstance, getAssetInstanceResponse.body); 134 }); 135 136 }); 137 138 }); 139 };