0.7.205 • Published 7 years ago

@gapi/gapi-testing-util v0.7.205

Weekly downloads
157
License
MIT
Repository
-
Last release
7 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

7 years ago

0.7.204

7 years ago

0.7.203

7 years ago

0.7.202

7 years ago

0.7.201

7 years ago

0.7.200

7 years ago

0.7.199

7 years ago

0.7.198

7 years ago

0.7.197

7 years ago

0.7.196

7 years ago

0.7.195

7 years ago

0.7.194

7 years ago

0.7.193

7 years ago

0.7.192

7 years ago

0.7.191

7 years ago

0.7.190

7 years ago

0.7.189

7 years ago

0.7.188

7 years ago

0.7.187

7 years ago

0.7.186

7 years ago

0.7.185

7 years ago

0.7.184

7 years ago

0.7.183

7 years ago

0.7.182

7 years ago

0.7.181

7 years ago

0.7.180

7 years ago

0.7.179

7 years ago

0.7.178

7 years ago

0.7.177

7 years ago

0.7.176

7 years ago

0.7.175

7 years ago

0.7.174

7 years ago

0.7.173

7 years ago

0.7.172

7 years ago

0.7.171

7 years ago

0.7.170

7 years ago

0.7.169

7 years ago

0.7.168

7 years ago

0.7.167

7 years ago

0.7.166

7 years ago

0.7.165

7 years ago

0.7.164

7 years ago

0.7.163

7 years ago

0.7.162

7 years ago

0.7.161

7 years ago

0.7.160

7 years ago

0.7.159

7 years ago

0.7.158

7 years ago

0.7.157

7 years ago

0.7.156

7 years ago

0.7.155

7 years ago

0.7.154

7 years ago

0.7.153

7 years ago

0.7.152

7 years ago

0.7.151

7 years ago

0.7.150

7 years ago

0.7.149

7 years ago

0.7.148

7 years ago

0.7.147

7 years ago

0.7.146

7 years ago

0.7.145

7 years ago

0.7.144

7 years ago

0.7.143

7 years ago

0.7.142

7 years ago

0.7.141

7 years ago

0.7.140

7 years ago

0.7.139

7 years ago

0.7.138

7 years ago

0.7.137

7 years ago

0.7.136

7 years ago

0.7.135

7 years ago

0.7.134

7 years ago

0.7.133

7 years ago

0.7.132

7 years ago

0.7.131

7 years ago

0.7.130

7 years ago

0.7.129

7 years ago

0.7.128

7 years ago

0.7.127

7 years ago

0.7.126

7 years ago

0.7.125

7 years ago

0.7.124

7 years ago

0.7.123

7 years ago

0.7.122

7 years ago

0.7.121

7 years ago

0.7.120

7 years ago

0.7.119

7 years ago

0.7.118

7 years ago

0.7.117

7 years ago

0.7.116

7 years ago

0.7.115

7 years ago

0.7.114

7 years ago

0.7.113

7 years ago

0.7.112

7 years ago

0.7.111

7 years ago

0.7.110

7 years ago

0.7.109

7 years ago

0.7.108

7 years ago

0.7.107

7 years ago

0.7.106

7 years ago

0.7.105

7 years ago

0.7.104

7 years ago

0.7.103

7 years ago

0.7.102

7 years ago

0.7.101

7 years ago

0.7.100

7 years ago

0.7.97

7 years ago

0.7.96

7 years ago

0.7.95

7 years ago

0.7.94

7 years ago

0.7.93

7 years ago

0.7.92

7 years ago

0.7.91

7 years ago

0.7.86

7 years ago

0.7.85

7 years ago

0.7.84

7 years ago

0.7.83

7 years ago

0.7.82

7 years ago

0.7.81

7 years ago

0.7.72

7 years ago

0.7.71

7 years ago

0.7.68

7 years ago

0.7.67

7 years ago

0.7.66

7 years ago

0.7.65

7 years ago

0.7.64

7 years ago

0.7.63

7 years ago

0.7.62

7 years ago

0.7.61

7 years ago

0.7.52

7 years ago

0.7.51

7 years ago

0.7.44

7 years ago

0.7.43

7 years ago

0.7.42

7 years ago

0.7.41

7 years ago

0.7.33

7 years ago

0.7.32

7 years ago

0.7.30

7 years ago

0.7.29

7 years ago

0.7.28

7 years ago

0.7.27

7 years ago

0.7.26

7 years ago

0.7.25

7 years ago

0.7.22

7 years ago

0.7.21

7 years ago

0.7.20

7 years ago

0.7.18

7 years ago

0.7.17

7 years ago

0.7.16

7 years ago

0.7.15

7 years ago

0.7.14

7 years ago

0.7.13

7 years ago

0.7.12

7 years ago

0.7.8

7 years ago

0.7.7

7 years ago

0.0.4

7 years ago