github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/test/ethereum/payments/authored.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 nock from 'nock'; 17 import request from 'supertest'; 18 import assert from 'assert'; 19 import { IDBPaymentDefinition, IDBPaymentInstance, IEventPaymentDefinitionCreated, IEventPaymentInstanceCreated } from '../../../lib/interfaces'; 20 import * as utils from '../../../lib/utils'; 21 22 export const testAuthored = async () => { 23 describe('Payment definitions: authored', async () => { 24 25 let paymentDefinitionID: string; 26 const timestamp = utils.getTimestamp(); 27 28 describe('Create payment definition', () => { 29 30 it('Checks that the payment definition can be added', async () => { 31 32 nock('https://apigateway.kaleido.io') 33 .post('/createPaymentDefinition?kld-from=0x0000000000000000000000000000000000000001&kld-sync=false') 34 .reply(200, { id: 'my-receipt-id' }); 35 36 const result = await request(app) 37 .post('/api/v1/payments/definitions') 38 .send({ 39 name: 'authored', 40 author: '0x0000000000000000000000000000000000000001', 41 amount: 1 42 }) 43 .expect(200); 44 assert.deepStrictEqual(result.body.status, 'submitted'); 45 paymentDefinitionID = result.body.paymentDefinitionID; 46 47 const getPaymentDefinitionsResponse = await request(app) 48 .get('/api/v1/payments/definitions') 49 .expect(200); 50 const paymentDefinition = getPaymentDefinitionsResponse.body.find((paymentDefinition: IDBPaymentDefinition) => paymentDefinition.name === 'authored'); 51 assert.strictEqual(paymentDefinition.author, '0x0000000000000000000000000000000000000001'); 52 assert.strictEqual(paymentDefinition.name, 'authored'); 53 assert.strictEqual(paymentDefinition.receipt, 'my-receipt-id'); 54 assert.strictEqual(typeof paymentDefinition.submitted, 'number'); 55 }); 56 57 it('Checks that the event stream notification for confirming the payment definition creation is handled', async () => { 58 const eventPromise = new Promise<void>((resolve) => { 59 mockEventStreamWebSocket.once('send', message => { 60 assert.strictEqual(message, '{"type":"ack","topic":"dev"}'); 61 resolve(); 62 }) 63 }); 64 const data: IEventPaymentDefinitionCreated = { 65 paymentDefinitionID: utils.uuidToHex(paymentDefinitionID), 66 author: '0x0000000000000000000000000000000000000001', 67 name: 'authored', 68 timestamp: timestamp.toString() 69 }; 70 mockEventStreamWebSocket.emit('message', JSON.stringify([{ 71 signature: utils.contractEventSignatures.DESCRIBED_PAYMENT_DEFINITION_CREATED, 72 data, 73 blockNumber: '123', 74 transactionHash: '0x0000000000000000000000000000000000000000000000000000000000000000' 75 }])); 76 await eventPromise; 77 }); 78 79 it('Checks that the payment definition is confirmed', async () => { 80 const getPaymentDefinitionsResponse = await request(app) 81 .get('/api/v1/payments/definitions') 82 .expect(200); 83 const paymentDefinition = getPaymentDefinitionsResponse.body.find((paymentDefinition: IDBPaymentDefinition) => paymentDefinition.name === 'authored'); 84 assert.strictEqual(paymentDefinition.paymentDefinitionID, paymentDefinitionID); 85 assert.strictEqual(paymentDefinition.author, '0x0000000000000000000000000000000000000001'); 86 assert.strictEqual(paymentDefinition.name, 'authored'); 87 assert.strictEqual(paymentDefinition.receipt, 'my-receipt-id'); 88 assert.strictEqual(typeof paymentDefinition.submitted, 'number'); 89 assert.strictEqual(paymentDefinition.timestamp, timestamp); 90 assert.strictEqual(paymentDefinition.blockNumber, 123); 91 assert.strictEqual(paymentDefinition.transactionHash, '0x0000000000000000000000000000000000000000000000000000000000000000'); 92 93 const getPaymentDefinitionResponse = await request(app) 94 .get(`/api/v1/payments/definitions/${paymentDefinitionID}`) 95 .expect(200); 96 assert.deepStrictEqual(paymentDefinition, getPaymentDefinitionResponse.body); 97 }); 98 99 }); 100 101 describe('Payment instances', async () => { 102 103 let paymentInstanceID: string; 104 105 it('Checks that a payment instance can be created', async () => { 106 107 nock('https://apigateway.kaleido.io') 108 .post('/createPaymentInstance?kld-from=0x0000000000000000000000000000000000000001&kld-sync=false') 109 .reply(200, { id: 'my-receipt-id' }); 110 111 const result = await request(app) 112 .post('/api/v1/payments/instances') 113 .send({ 114 paymentDefinitionID, 115 author: '0x0000000000000000000000000000000000000001', 116 member: '0x0000000000000000000000000000000000000002', 117 amount: 10 118 }) 119 .expect(200); 120 assert.deepStrictEqual(result.body.status, 'submitted'); 121 paymentInstanceID = result.body.paymentInstanceID; 122 123 const getPaymentInstancesResponse = await request(app) 124 .get('/api/v1/payments/instances') 125 .expect(200); 126 const paymentInstance = getPaymentInstancesResponse.body.find((paymentInstance: IDBPaymentInstance) => paymentInstance.paymentInstanceID === paymentInstanceID); 127 assert.strictEqual(paymentInstance.author, '0x0000000000000000000000000000000000000001'); 128 assert.strictEqual(paymentInstance.paymentDefinitionID, paymentDefinitionID); 129 assert.strictEqual(paymentInstance.member, '0x0000000000000000000000000000000000000002'); 130 assert.strictEqual(paymentInstance.amount, 10); 131 assert.strictEqual(paymentInstance.receipt, 'my-receipt-id'); 132 assert.strictEqual(typeof paymentInstance.submitted, 'number'); 133 134 const getPaymentInstanceResponse = await request(app) 135 .get(`/api/v1/payments/instances/${paymentInstanceID}`) 136 .expect(200); 137 assert.deepStrictEqual(paymentInstance, getPaymentInstanceResponse.body); 138 139 }); 140 141 it('Checks that the event stream notification for confirming the payment instance creation is handled', async () => { 142 const eventPromise = new Promise<void>((resolve) => { 143 mockEventStreamWebSocket.once('send', message => { 144 assert.strictEqual(message, '{"type":"ack","topic":"dev"}'); 145 resolve(); 146 }) 147 }); 148 const data: IEventPaymentInstanceCreated = { 149 paymentDefinitionID: utils.uuidToHex(paymentDefinitionID), 150 author: '0x0000000000000000000000000000000000000001', 151 paymentInstanceID: utils.uuidToHex(paymentInstanceID), 152 amount: '10', 153 member: '0x0000000000000000000000000000000000000002', 154 timestamp: timestamp.toString() 155 }; 156 mockEventStreamWebSocket.emit('message', JSON.stringify([{ 157 signature: utils.contractEventSignatures.PAYMENT_INSTANCE_CREATED, 158 data, 159 blockNumber: '123', 160 transactionHash: '0x0000000000000000000000000000000000000000000000000000000000000000' 161 }])); 162 await eventPromise; 163 }); 164 165 it('Checks that the payment instance is confirmed', async () => { 166 const getAssetInstancesResponse = await request(app) 167 .get('/api/v1/payments/instances') 168 .expect(200); 169 const paymentInstance = getAssetInstancesResponse.body.find((paymentInstance: IDBPaymentInstance) => paymentInstance.paymentInstanceID === paymentInstanceID); 170 assert.strictEqual(paymentInstance.author, '0x0000000000000000000000000000000000000001'); 171 assert.strictEqual(paymentInstance.member, '0x0000000000000000000000000000000000000002'); 172 assert.strictEqual(paymentInstance.paymentDefinitionID, paymentDefinitionID); 173 assert.strictEqual(paymentInstance.amount, 10); 174 assert.strictEqual(paymentInstance.receipt, 'my-receipt-id'); 175 assert.strictEqual(typeof paymentInstance.submitted, 'number'); 176 assert.strictEqual(paymentInstance.timestamp, timestamp); 177 assert.strictEqual(paymentInstance.blockNumber, 123); 178 assert.strictEqual(paymentInstance.transactionHash, '0x0000000000000000000000000000000000000000000000000000000000000000'); 179 180 const getAssetInstanceResponse = await request(app) 181 .get(`/api/v1/payments/instances/${paymentInstanceID}`) 182 .expect(200); 183 assert.deepStrictEqual(paymentInstance, getAssetInstanceResponse.body); 184 }); 185 186 }); 187 188 }); 189 };