0.7.205 • Published 6 years ago

@gapi/gapi-testing-util v0.7.205

Weekly downloads
157
License
MIT
Repository
-
Last release
6 years ago
Example E2E testing from API
import { Transaction, Block, Account, Unit, TransactionReceipt, Tx, Personal } from '../../core/services/contract/web3.types';
import { IQuery, IMutation } from '../../../api-types/graphql';
import { AtcTestUtil } from '../../core/testing/test.util';
import { User } from '../../core/models/User';
import { SequelizeController } from '../../core/sequelize.controller';
import { Wallet } from '../../core/models/Wallet';
import { EthereumAPI } from './ethereum.api';
import { AuthModule } from '../../core/services/auth/auth.module';
import { AtcPerson } from './services/person.service';
import { BUY_COINS_MUTATION } from '../../core/testing/mutations/buyCoins.mutation';
import { SEND_TRANSACTION_MUTATION } from '../../core/testing/mutations/sendTransaction.mutation';
import { ServerErrorsList } from '../../core/services/errors/server-errors-list';

const atcTestUtil: AtcTestUtil = new AtcTestUtil();

beforeAll(() => atcTestUtil.init());
afterAll(() => atcTestUtil.destroy());

describe('Ethereum Controller', () => {

  // tslint:disable-next-line:max-line-length
  it(`e2e: mutation -> (buyCoins) : Should sucessfully buy ${1000 * Number(atcTestUtil.defaultTestingAmount)} ATC coin with ${atcTestUtil.defaultTestingAmount} ETH`, async done => {
    atcTestUtil.sendRequest<IMutation>({
      query: BUY_COINS_MUTATION,
      variables: {
        address: atcTestUtil.users.USER.wallets[0].address,
        amount: atcTestUtil.defaultTestingAmount
      },
      signiture: atcTestUtil.users.USER
    })
      .subscribe(async res => {
        if (!res.success) {
          console.error(res.errors[0].name);
        }
        expect(res.success).toBeTruthy();
        expect(res.data.buyCoins.tx).toBeDefined();
        done();
      }, err => {
        expect(err).toBe(null);
        done();
      });
  });

  it(`e2e: mutation -> (buyCoins) : Should throw error 'invalid-address'`, async done => {
    atcTestUtil.sendRequest<IMutation>({
      query: BUY_COINS_MUTATION,
      variables: {
        address: 'g',
        amount: atcTestUtil.defaultTestingAmount
      }
    })
      .subscribe(res => {
        expect(res.success).toBeFalsy();
        expect(res.errors[0].name).toBe('invalid-address');
        done();
      }, err => {
        expect(err).toBe(null);
        done();
      });
  });

  it(`e2e: mutation -> (buyCoins) : Should throw error 'missing-wallet-id' when buying ATC Coins`, async done => {
      atcTestUtil.sendRequest<IMutation>({
        query: BUY_COINS_MUTATION,
        variables: {
          address: '0xB1dCf5215d8f537F47B286699746Bf696A768bbc',
          amount: atcTestUtil.defaultTestingAmount
        }
      })
        .subscribe(res => {
          expect(res.success).toBeFalsy();
          expect(res.data.buyCoins).toBeNull();
          expect(res.errors[0].name).toBe('missing-wallet-id');
          done();
        }, err => {
          expect(err).toBe(null);
          done();
        });
    });

  it(`e2e: mutation -> (sendTransaction) : Should throw error 'account-not-found' when sending transaction from unknown account`, done => {
    atcTestUtil.sendRequest<IMutation>({
      query: SEND_TRANSACTION_MUTATION,
      variables: {
        from: '0x67De18cA13BDd44Bed2ACF1062866E93FB9F479B',
        to: '0xE3b6f16234Ac91d5Cd5A1030d13839a8be671b06',
        amount: atcTestUtil.defaultTestingAmount
      }
    })
      .map(res => {
        expect(res.success).toBeFalsy();
        expect(res.data.sendTransaction).toBeNull();
        expect(res.errors[0].name).toBe('account-not-found');
        done();
      }, (err) => {
        expect(err).toBe(null);
        done();
      }).subscribe();
  });

});
Example Unit testing
import { Block, Account, Unit, TransactionReceipt, Tx, Personal } from '../../core/services/contract/web3.types';
import { IQuery, IMutation } from '../../../api-types/graphql';
import { AtcTestUtil } from '../../core/testing/test.util';
import { User } from '../../core/models/User';
import { SequelizeController } from '../../core/sequelize.controller';
import { Wallet } from '../../core/models/Wallet';
import { FAKE_USERS_WALLETS_COUNT } from '../../core/testing/fakeUsers';
import { EthereumAPI } from './ethereum.api';
import { AuthModule } from '../../core/services/auth/auth.module';
import { AtcPerson } from './services/person.service';
import { Transaction } from '../../core/models/Transaction';

const atcTestUtil: AtcTestUtil = new AtcTestUtil();

beforeAll(() => atcTestUtil.init());
afterAll(() => atcTestUtil.destroy());

describe('Ethereum Api', () => {

  it(`unit: (FakeWalletsCount) : Should have ${FAKE_USERS_WALLETS_COUNT.count} wallets inside personal web3`, async done => {
    expect(await atcTestUtil.getEtherAccounts()).toHaveLength(FAKE_USERS_WALLETS_COUNT.count);
    done();
  });

  // tslint:disable-next-line:max-line-length
  it(`unit: (sendTransaction) : Should send transaction from USER to ADMIN ${atcTestUtil.defaultTestingAmount} ETH`, async done => {
    const person = new AtcPerson(
      AuthModule.decrypt(atcTestUtil.users.USER.credential.password),
      atcTestUtil.users.USER.wallets[1].address
    );
    await person.unlockAccount();
    EthereumAPI.sendTransaction(
      atcTestUtil.users.USER.wallets[1].address,
      atcTestUtil.users.ADMIN.wallets[1].address,
      atcTestUtil.defaultTestingAmount,
      atcTestUtil.users.USER.credential,
      null,
      null
    ).then(async transaction => {
      expect(transaction.blockHash).toBeTruthy();
      const t = await Transaction.find({where: {hash: transaction.transactionHash}});
      expect(transaction.transactionHash).toBe(t.hash);
      await t.destroy();
      await person.lockAccount();
      done();
    }).catch(e => {
      expect(e).toBe(null);
      done();
    });
  });

  // tslint:disable-next-line:max-line-length
  it(`unit: (sendTransaction) : Should throw error invalid-account-password when sending from ADMIN to USER ${atcTestUtil.defaultTestingAmount} ETH`, async done => {
    EthereumAPI.sendTransaction(
      atcTestUtil.users.ADMIN.wallets[1].address,
      atcTestUtil.users.USER.wallets[1].address,
      atcTestUtil.defaultTestingAmount,
      atcTestUtil.users.ADMIN.credential,
      null,
      null
    ).then(async transaction => {
      expect(transaction.blockHash).toBeTruthy();
      done();
    }).catch(e => {
      expect(e.name).toBe('invalid-account-password');
      done();
    });
  });

});
0.7.205

6 years ago

0.7.204

6 years ago

0.7.203

6 years ago

0.7.202

6 years ago

0.7.201

6 years ago

0.7.200

6 years ago

0.7.199

6 years ago

0.7.198

6 years ago

0.7.197

6 years ago

0.7.196

6 years ago

0.7.195

6 years ago

0.7.194

6 years ago

0.7.193

6 years ago

0.7.192

6 years ago

0.7.191

6 years ago

0.7.190

6 years ago

0.7.189

6 years ago

0.7.188

6 years ago

0.7.187

6 years ago

0.7.186

6 years ago

0.7.185

6 years ago

0.7.184

6 years ago

0.7.183

6 years ago

0.7.182

6 years ago

0.7.181

6 years ago

0.7.180

6 years ago

0.7.179

6 years ago

0.7.178

6 years ago

0.7.177

6 years ago

0.7.176

6 years ago

0.7.175

6 years ago

0.7.174

6 years ago

0.7.173

6 years ago

0.7.172

6 years ago

0.7.171

6 years ago

0.7.170

6 years ago

0.7.169

6 years ago

0.7.168

6 years ago

0.7.167

6 years ago

0.7.166

6 years ago

0.7.165

6 years ago

0.7.164

6 years ago

0.7.163

6 years ago

0.7.162

6 years ago

0.7.161

6 years ago

0.7.160

6 years ago

0.7.159

6 years ago

0.7.158

6 years ago

0.7.157

6 years ago

0.7.156

6 years ago

0.7.155

6 years ago

0.7.154

6 years ago

0.7.153

6 years ago

0.7.152

6 years ago

0.7.151

6 years ago

0.7.150

6 years ago

0.7.149

6 years ago

0.7.148

6 years ago

0.7.147

6 years ago

0.7.146

6 years ago

0.7.145

6 years ago

0.7.144

6 years ago

0.7.143

6 years ago

0.7.142

6 years ago

0.7.141

6 years ago

0.7.140

6 years ago

0.7.139

6 years ago

0.7.138

6 years ago

0.7.137

6 years ago

0.7.136

6 years ago

0.7.135

6 years ago

0.7.134

6 years ago

0.7.133

6 years ago

0.7.132

6 years ago

0.7.131

6 years ago

0.7.130

6 years ago

0.7.129

6 years ago

0.7.128

6 years ago

0.7.127

6 years ago

0.7.126

6 years ago

0.7.125

6 years ago

0.7.124

6 years ago

0.7.123

6 years ago

0.7.122

6 years ago

0.7.121

6 years ago

0.7.120

6 years ago

0.7.119

6 years ago

0.7.118

6 years ago

0.7.117

6 years ago

0.7.116

6 years ago

0.7.115

6 years ago

0.7.114

6 years ago

0.7.113

6 years ago

0.7.112

6 years ago

0.7.111

6 years ago

0.7.110

6 years ago

0.7.109

6 years ago

0.7.108

6 years ago

0.7.107

6 years ago

0.7.106

6 years ago

0.7.105

6 years ago

0.7.104

6 years ago

0.7.103

6 years ago

0.7.102

6 years ago

0.7.101

6 years ago

0.7.100

6 years ago

0.7.97

6 years ago

0.7.96

6 years ago

0.7.95

6 years ago

0.7.94

6 years ago

0.7.93

6 years ago

0.7.92

6 years ago

0.7.91

6 years ago

0.7.86

6 years ago

0.7.85

6 years ago

0.7.84

6 years ago

0.7.83

6 years ago

0.7.82

6 years ago

0.7.81

6 years ago

0.7.72

6 years ago

0.7.71

6 years ago

0.7.68

6 years ago

0.7.67

6 years ago

0.7.66

6 years ago

0.7.65

6 years ago

0.7.64

6 years ago

0.7.63

6 years ago

0.7.62

6 years ago

0.7.61

6 years ago

0.7.52

6 years ago

0.7.51

6 years ago

0.7.44

6 years ago

0.7.43

6 years ago

0.7.42

6 years ago

0.7.41

6 years ago

0.7.33

6 years ago

0.7.32

6 years ago

0.7.30

6 years ago

0.7.29

6 years ago

0.7.28

6 years ago

0.7.27

6 years ago

0.7.26

6 years ago

0.7.25

6 years ago

0.7.22

6 years ago

0.7.21

6 years ago

0.7.20

6 years ago

0.7.18

6 years ago

0.7.17

6 years ago

0.7.16

6 years ago

0.7.15

6 years ago

0.7.14

6 years ago

0.7.13

6 years ago

0.7.12

6 years ago

0.7.8

6 years ago

0.7.7

6 years ago

0.0.4

6 years ago