github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/test/ethereum/payments/unauthored.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 request from 'supertest';
    17  import assert from 'assert';
    18  import { IEventPaymentDefinitionCreated, IDBPaymentDefinition, IEventPaymentInstanceCreated, IDBPaymentInstance } from '../../../lib/interfaces';
    19  import * as utils from '../../../lib/utils';
    20  
    21  export const testUnauthored = async () => {
    22  
    23  describe('Payment definitions: unauthored', async () => {
    24  
    25    const paymentDefinitionID = 'f9812952-50f8-4090-9412-e7b0f3eeb930';
    26    const timestamp = utils.getTimestamp();
    27  
    28    describe('Payment definition', async () => {
    29  
    30      it('Checks that the event stream notification for confirming the payment definition creation is handled', async () => {
    31  
    32        const eventPromise = new Promise<void>((resolve) => {
    33          mockEventStreamWebSocket.once('send', message => {
    34            assert.strictEqual(message, '{"type":"ack","topic":"dev"}');
    35            resolve();
    36          })
    37        });
    38        const data: IEventPaymentDefinitionCreated = {
    39          paymentDefinitionID: utils.uuidToHex(paymentDefinitionID),
    40          author: '0x0000000000000000000000000000000000000002',
    41          name: 'unauthored',
    42          timestamp: timestamp.toString()
    43        };
    44        mockEventStreamWebSocket.emit('message', JSON.stringify([{
    45          signature: utils.contractEventSignatures.PAYMENT_DEFINITION_CREATED,
    46          data,
    47          blockNumber: '123',
    48          transactionHash: '0x0000000000000000000000000000000000000000000000000000000000000000'
    49        }]));
    50        await eventPromise;
    51      });
    52  
    53      it('Checks that the payment definition is confirmed', async () => {
    54        const getPaymentDefinitionsResponse = await request(app)
    55          .get('/api/v1/payments/definitions')
    56          .expect(200);
    57        const paymentDefinition = getPaymentDefinitionsResponse.body.find((paymentDefinition: IDBPaymentDefinition) => paymentDefinition.name === 'unauthored');
    58        assert.strictEqual(paymentDefinition.paymentDefinitionID, paymentDefinitionID);
    59        assert.strictEqual(paymentDefinition.author, '0x0000000000000000000000000000000000000002');
    60        assert.strictEqual(paymentDefinition.name, 'unauthored');
    61        assert.strictEqual(paymentDefinition.timestamp, timestamp);
    62        assert.strictEqual(paymentDefinition.blockNumber, 123);
    63        assert.strictEqual(paymentDefinition.transactionHash, '0x0000000000000000000000000000000000000000000000000000000000000000');
    64  
    65        const getPaymentDefinitionResponse = await request(app)
    66        .get(`/api/v1/payments/definitions/${paymentDefinitionID}`)
    67        .expect(200);
    68        assert.deepStrictEqual(paymentDefinition, getPaymentDefinitionResponse.body);
    69      });
    70  
    71    });
    72  
    73    describe('Payment instances', async () => {
    74  
    75      const paymentInstanceID = '646a2a24-319e-408b-a099-fc163ff3e692';
    76  
    77      it('Checks that the event stream notification for confirming the payment instance creation is handled', async () => {
    78        const eventPromise = new Promise<void>((resolve) => {
    79          mockEventStreamWebSocket.once('send', message => {
    80            assert.strictEqual(message, '{"type":"ack","topic":"dev"}');
    81            resolve();
    82          })
    83        });
    84        const data: IEventPaymentInstanceCreated = {
    85          paymentDefinitionID: utils.uuidToHex(paymentDefinitionID),
    86          author: '0x0000000000000000000000000000000000000001',
    87          paymentInstanceID: utils.uuidToHex(paymentInstanceID),
    88          amount: '10',
    89          member: '0x0000000000000000000000000000000000000002',
    90          timestamp: timestamp.toString()
    91        };
    92        mockEventStreamWebSocket.emit('message', JSON.stringify([{
    93          signature: utils.contractEventSignatures.PAYMENT_INSTANCE_CREATED,
    94          data,
    95          blockNumber: '123',
    96          transactionHash: '0x0000000000000000000000000000000000000000000000000000000000000000'
    97        }]));
    98        await eventPromise;
    99      });
   100  
   101      it('Checks that the payment instance is confirmed', async () => {
   102        const getAssetInstancesResponse = await request(app)
   103          .get('/api/v1/payments/instances')
   104          .expect(200);
   105        const paymentInstance = getAssetInstancesResponse.body.find((paymentInstance: IDBPaymentInstance) => paymentInstance.paymentInstanceID === paymentInstanceID);
   106        assert.strictEqual(paymentInstance.author, '0x0000000000000000000000000000000000000001');
   107        assert.strictEqual(paymentInstance.member, '0x0000000000000000000000000000000000000002');
   108        assert.strictEqual(paymentInstance.paymentDefinitionID, paymentDefinitionID);
   109        assert.strictEqual(paymentInstance.submitted, undefined);
   110        assert.strictEqual(paymentInstance.receipt, undefined);
   111        assert.strictEqual(paymentInstance.amount, 10);
   112        assert.strictEqual(paymentInstance.timestamp, timestamp);
   113        assert.strictEqual(paymentInstance.blockNumber, 123);
   114        assert.strictEqual(paymentInstance.transactionHash, '0x0000000000000000000000000000000000000000000000000000000000000000');
   115  
   116        const getAssetInstanceResponse = await request(app)
   117          .get(`/api/v1/payments/instances/${paymentInstanceID}`)
   118          .expect(200);
   119        assert.deepStrictEqual(paymentInstance, getAssetInstanceResponse.body);
   120      });
   121  
   122    });
   123  
   124  });
   125  };