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

8 years ago

0.7.184

8 years ago

0.7.183

8 years ago

0.7.182

8 years ago

0.7.181

8 years ago

0.7.180

8 years ago

0.7.179

8 years ago

0.7.178

8 years ago

0.7.177

8 years ago

0.7.176

8 years ago

0.7.175

8 years ago

0.7.174

8 years ago

0.7.173

8 years ago

0.7.172

8 years ago

0.7.171

8 years ago

0.7.170

8 years ago

0.7.169

8 years ago

0.7.168

8 years ago

0.7.167

8 years ago

0.7.166

8 years ago

0.7.165

8 years ago

0.7.164

8 years ago

0.7.163

8 years ago

0.7.162

8 years ago

0.7.161

8 years ago

0.7.160

8 years ago

0.7.159

8 years ago

0.7.158

8 years ago

0.7.157

8 years ago

0.7.156

8 years ago

0.7.155

8 years ago

0.7.154

8 years ago

0.7.153

8 years ago

0.7.152

8 years ago

0.7.151

8 years ago

0.7.150

8 years ago

0.7.149

8 years ago

0.7.148

8 years ago

0.7.147

8 years ago

0.7.146

8 years ago

0.7.145

8 years ago

0.7.144

8 years ago

0.7.143

8 years ago

0.7.142

8 years ago

0.7.141

8 years ago

0.7.140

8 years ago

0.7.139

8 years ago

0.7.138

8 years ago

0.7.137

8 years ago

0.7.136

8 years ago

0.7.135

8 years ago

0.7.134

8 years ago

0.7.133

8 years ago

0.7.132

8 years ago

0.7.131

8 years ago

0.7.130

8 years ago

0.7.129

8 years ago

0.7.128

8 years ago

0.7.127

8 years ago

0.7.126

8 years ago

0.7.125

8 years ago

0.7.124

8 years ago

0.7.123

8 years ago

0.7.122

8 years ago

0.7.121

8 years ago

0.7.120

8 years ago

0.7.119

8 years ago

0.7.118

8 years ago

0.7.117

8 years ago

0.7.116

8 years ago

0.7.115

8 years ago

0.7.114

8 years ago

0.7.113

8 years ago

0.7.112

8 years ago

0.7.111

8 years ago

0.7.110

8 years ago

0.7.109

8 years ago

0.7.108

8 years ago

0.7.107

8 years ago

0.7.106

8 years ago

0.7.105

8 years ago

0.7.104

8 years ago

0.7.103

8 years ago

0.7.102

8 years ago

0.7.101

8 years ago

0.7.100

8 years ago

0.7.97

8 years ago

0.7.96

8 years ago

0.7.95

8 years ago

0.7.94

8 years ago

0.7.93

8 years ago

0.7.92

8 years ago

0.7.91

8 years ago

0.7.86

8 years ago

0.7.85

8 years ago

0.7.84

8 years ago

0.7.83

8 years ago

0.7.82

8 years ago

0.7.81

8 years ago

0.7.72

8 years ago

0.7.71

8 years ago

0.7.68

8 years ago

0.7.67

8 years ago

0.7.66

8 years ago

0.7.65

8 years ago

0.7.64

8 years ago

0.7.63

8 years ago

0.7.62

8 years ago

0.7.61

8 years ago

0.7.52

8 years ago

0.7.51

8 years ago

0.7.44

8 years ago

0.7.43

8 years ago

0.7.42

8 years ago

0.7.41

8 years ago

0.7.33

8 years ago

0.7.32

8 years ago

0.7.30

8 years ago

0.7.29

8 years ago

0.7.28

8 years ago

0.7.27

8 years ago

0.7.26

8 years ago

0.7.25

8 years ago

0.7.22

8 years ago

0.7.21

8 years ago

0.7.20

8 years ago

0.7.18

8 years ago

0.7.17

8 years ago

0.7.16

8 years ago

0.7.15

8 years ago

0.7.14

8 years ago

0.7.13

8 years ago

0.7.12

8 years ago

0.7.8

8 years ago

0.7.7

8 years ago

0.0.4

8 years ago