Skip to content

Commit

Permalink
Update integration tests (#670)
Browse files Browse the repository at this point in the history
* [e2e]: update service integration tests

* [e2e]: remove console.log
  • Loading branch information
cfaur09 authored Jun 8, 2022
1 parent 136e793 commit e2f913c
Show file tree
Hide file tree
Showing 10 changed files with 1,036 additions and 71 deletions.
52 changes: 50 additions & 2 deletions src/test/integration/accounts.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { PublicAppModule } from 'src/public.app.module';
import { ElasticService } from 'src/common/elastic/elastic.service';
import { DeployedContract } from 'src/endpoints/accounts/entities/deployed.contract';
import '../../utils/extensions/jest.extensions';
import { ApiConfigService } from 'src/common/api-config/api.config.service';

describe('Account Service', () => {
let accountService: AccountService;
Expand Down Expand Up @@ -78,7 +79,6 @@ describe('Account Service', () => {

describe("getAccount", () => {
it("should return null because test simulates that address is not valid ", async () => {

const mock_isAddressValid = jest.spyOn(AddressUtils, 'isAddressValid');
mock_isAddressValid.mockImplementation(() => false);

Expand All @@ -89,10 +89,45 @@ describe('Account Service', () => {
});

it("should return account details", async () => {
const mock_isAddressValid = jest.spyOn(AddressUtils, 'isAddressValid');
mock_isAddressValid.mockImplementation(() => true);

const address: string = "erd1cnyng48s8lrjn95rpdfgykxl5993c5qhn5jqt0ar960f7v3umnrsy9yx0s";
const results = await accountService.getAccount(address);

expect(results).toHaveProperties(
['address', 'balance', 'nonce', 'shard', 'code',
'codeHash', 'rootHash', 'txCount', 'scrCount',
'username', 'shard', 'developerReward', 'ownerAddress', 'scamInfo',
]);
});

it("should return account details if IndexerV3Flag is active", async () => {
jest.spyOn(ApiConfigService.prototype, 'getIsIndexerV3FlagActive')
// eslint-disable-next-line require-await
.mockImplementation(jest.fn(() => true));

const mock_isAddressValid = jest.spyOn(AddressUtils, 'isAddressValid');
mock_isAddressValid.mockImplementation(() => true);

const address: string = "erd1cnyng48s8lrjn95rpdfgykxl5993c5qhn5jqt0ar960f7v3umnrsy9yx0s";
const results = await accountService.getAccount(address);

expect(results).toHaveProperties(
['address', 'balance', 'nonce', 'shard', 'code',
'codeHash', 'rootHash', 'txCount', 'scrCount',
'username', 'shard', 'developerReward', 'ownerAddress', 'scamInfo',
]);
});

it("should return account details if getUseLegacyElastic is active", async () => {
const mock_isAddressValid = jest.spyOn(AddressUtils, 'isAddressValid');
mock_isAddressValid.mockImplementation(() => true);

jest.spyOn(ApiConfigService.prototype, 'getUseLegacyElastic')
// eslint-disable-next-line require-await
.mockImplementation(jest.fn(() => true));

const address: string = "erd1cnyng48s8lrjn95rpdfgykxl5993c5qhn5jqt0ar960f7v3umnrsy9yx0s";
const results = await accountService.getAccount(address);

Expand Down Expand Up @@ -147,8 +182,14 @@ describe('Account Service', () => {
});

describe("getAccountDeployedAtRaw", () => {
it("should return null because test simulates that scDeployed is undefined", async () => {
it("should return account deployed timestamp because test simulates that account is a smart-contract", async () => {
const address: string = "erd1qqqqqqqqqqqqqpgqvc7gdl0p4s97guh498wgz75k8sav6sjfjlwqh679jy";
const results = await accountService.getAccountDeployedAtRaw(address);

expect(results).toStrictEqual(1636897470);
});

it("should return null because test simulates that scDeployed is undefined and should return null", async () => {
jest
.spyOn(ElasticService.prototype, 'getItem')
// eslint-disable-next-line require-await
Expand Down Expand Up @@ -333,5 +374,12 @@ describe('Account Service', () => {

expect(results).toBeNull();
});

it('should return account username details', async () => {
const address: string = "erd1qga7ze0l03chfgru0a32wxqf2226nzrxnyhzer9lmudqhjgy7ycqjjyknz";
const results = await accountService.getAccountUsernameRaw(address);

expect(results).toStrictEqual('alice.elrond');
});
});
});
196 changes: 195 additions & 1 deletion src/test/integration/api.config.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ describe('API Config', () => {
expect(results).toEqual(3);
});

it("should return default value 1 if nft process max retries is not defined", () => {
it("should return default value 3 if nft process max retries is not defined", () => {
jest
.spyOn(ConfigService.prototype, 'get')
.mockImplementation(jest.fn(() => undefined));
Expand All @@ -1428,4 +1428,198 @@ describe('API Config', () => {
expect(results).toEqual(3);
});
});

describe("getGithubToken", () => {
it("should return GitHub token details", () => {
jest
.spyOn(ConfigService.prototype, "get")
.mockImplementation(jest.fn(() => 'test'));

const results = apiConfigService.getGithubToken();
expect(results).toEqual('test');
});

it("should return undefined if GitHub token is not defined", () => {
jest
.spyOn(ConfigService.prototype, 'get')
.mockImplementation(jest.fn(() => undefined));

const results = apiConfigService.getGithubToken();
expect(results).toStrictEqual(undefined);
});
});

describe("getMaiarExchangeUrlMandatory", () => {
it("should return Maiar Exchange Url", () => {
jest
.spyOn(ConfigService.prototype, "get")
.mockImplementation(jest.fn(() => 'https://graph.maiar.exchange/graphql'));

const results = apiConfigService.getMaiarExchangeUrlMandatory();
expect(results).toEqual('https://graph.maiar.exchange/graphql');
});

it("should throw new error because test simulates that Maiar Exchange Url is not defined", () => {
jest
.spyOn(ConfigService.prototype, 'get')
.mockImplementation(jest.fn(() => undefined));

expect(() => apiConfigService.getMaiarExchangeUrlMandatory()).toThrowError('No transaction-action.mex.microServiceUrl present');
});
});

describe("getDatabaseType", () => {
it("should return database type details", () => {
jest
.spyOn(ConfigService.prototype, "get")
.mockImplementation(jest.fn(() => 'mysql'));

const results = apiConfigService.getDatabaseType();
expect(results).toEqual('mysql');
});

it("should throw new error because test simulates that database type is not defined", () => {
jest
.spyOn(ConfigService.prototype, 'get')
.mockImplementation(jest.fn(() => false));

expect(() => apiConfigService.getDatabaseType()).toThrowError('No database.type present');
});
});

describe("getEventsNotifierExchange", () => {
it("should return events notifier exchange details ( all_events) ", () => {
jest
.spyOn(ConfigService.prototype, "get")
.mockImplementation(jest.fn(() => 'all_events'));

const results = apiConfigService.getEventsNotifierExchange();
expect(results).toEqual('all_events');
});

it("should throw new error because test simulates that events notifier exchange is not defined", () => {
jest
.spyOn(ConfigService.prototype, 'get')
.mockImplementation(jest.fn(() => false));

expect(() => apiConfigService.getEventsNotifierExchange()).toThrowError('No events notifier exchange present');
});
});

describe("getEventsNotifierUrl", () => {
it("should return events notifier url", () => {
jest
.spyOn(ConfigService.prototype, "get")
.mockImplementation(jest.fn(() => 'amqp://guest:[email protected]:5672'));

const results = apiConfigService.getEventsNotifierUrl();
expect(results).toEqual('amqp://guest:[email protected]:5672');
});

it("should throw new error because test simulates that events notifier url is not defined", () => {
jest
.spyOn(ConfigService.prototype, 'get')
.mockImplementation(jest.fn(() => false));

expect(() => apiConfigService.getEventsNotifierUrl()).toThrowError('No events notifier url present');
});
});

describe("getEventsNotifierFeaturePort", () => {
it("should return events notifier port", () => {
jest
.spyOn(ConfigService.prototype, "get")
.mockImplementation(jest.fn(() => '5674'));

const results = apiConfigService.getEventsNotifierFeaturePort();
expect(results).toEqual('5674');
});

it("should throw new error because test simulates that events notifier port is not defined", () => {
jest
.spyOn(ConfigService.prototype, 'get')
.mockImplementation(jest.fn(() => undefined));

expect(() => apiConfigService.getEventsNotifierFeaturePort()).toThrowError('No events notifier port present');
});
});

describe("isEventsNotifierFeatureActive", () => {
it("should return events notifier event flag", () => {
jest
.spyOn(ConfigService.prototype, "get")
.mockImplementation(jest.fn(() => true));

const results = apiConfigService.isEventsNotifierFeatureActive();
expect(results).toEqual(true);
});

it("should return false because test simulates that events notifier flag is false", () => {
jest
.spyOn(ConfigService.prototype, 'get')
.mockImplementation(jest.fn(() => undefined));

const results = apiConfigService.isEventsNotifierFeatureActive();
expect(results).toEqual(false);
});
});

describe("getIsElasticUpdaterCronActive", () => {
it("should return true if elastic updater cron active is enabled", () => {
jest
.spyOn(ConfigService.prototype, "get")
.mockImplementation(jest.fn(() => true));

const results = apiConfigService.getIsElasticUpdaterCronActive();
expect(results).toEqual(true);
});

it("should return false because test simulates that elastic updater cron is not active ( false )", () => {
jest
.spyOn(ConfigService.prototype, 'get')
.mockImplementation(jest.fn(() => false));

const results = apiConfigService.getIsElasticUpdaterCronActive();
expect(results).toEqual(false);
});
});

describe("getConfig", () => {
it("should return true if elastic updater cron active is enabled", () => {
jest
.spyOn(ConfigService.prototype, "get")
.mockImplementation(jest.fn(() => [
'https://api.elrond.com',
'https://devnet-api.elrond.com',
'https://testnet-api.elrond.com',
]));

const results = apiConfigService.getConfig('urls.api');

expect(results).toEqual(expect.arrayContaining([
'https://api.elrond.com',
'https://devnet-api.elrond.com',
'https://testnet-api.elrond.com',
]));
});
});

describe("getDatabaseUrl", () => {
it("should return database url details", () => {
jest
.spyOn(ConfigService.prototype, "get")
.mockImplementation(jest.fn(() => 'localhost'));

const results = apiConfigService.getDatabaseUrl();
expect(results).toEqual('localhost');
});

it("should throw new error because test simulates that database url is not defined", () => {
jest
.spyOn(ConfigService.prototype, 'get')
.mockImplementation(jest.fn(() => false));

expect(() => apiConfigService.getDatabaseUrl()).toThrowError('No database.url present');
});
});
});
30 changes: 29 additions & 1 deletion src/test/integration/controllers/accounts.controller.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { PublicAppModule } from 'src/public.app.module';
import { AddressUtils } from 'src/utils/address.utils';
import request = require('supertest');

describe("Accounts Controller", () => {
Expand All @@ -17,6 +18,8 @@ describe("Accounts Controller", () => {
await app.init();
});

beforeEach(() => { jest.restoreAllMocks(); });

it("/accounts - should return 200 for request of 100 accounts", async () => {
const params = new URLSearchParams({
'from': '0',
Expand All @@ -37,14 +40,31 @@ describe("Accounts Controller", () => {
.expect(200);
});

it("/accounts/:address - should return 200 status code and return address details fora specific address", async () => {
it("/accounts/c - should return 200 for request of accounts alternative count", async () => {
await request(app.getHttpServer())
.get(route + "/c")
.expect(200);
});

it("/accounts/:address - should return 200 status code and return address details for a specific address", async () => {
const address: string = "erd1vup7q384decm8l8mu4ehz75c5mfs089nd32fteru95tm8d0a8dqs8g0yst";
await request(app.getHttpServer())
.get(route + "/" + address)
.set("header", "content-type")
.expect(200);
});

it("/accounts/:address - should return 404 status code if account is not found", async () => {
const mock_isAddressValid = jest.spyOn(AddressUtils, 'isAddressValid');
mock_isAddressValid.mockImplementation(() => false);

const address: string = "erd1vup7q384decm8l8mu4ehz75c5mfs089nd32fteru95tm8d0a8dqs8g0yst";
await request(app.getHttpServer())
.get(route + "/" + address)
.set("header", "content-type")
.expect(404);
});

it("/accounts/:address - should return 400 status code of a specific invalid address account", async () => {
const address: string = "erd1sea63y47u569ns315mqjf4vnygn9whkk7p6ry4rfpqyd6rd5addqyd9lf2";
await request(app.getHttpServer())
Expand All @@ -56,6 +76,14 @@ describe("Accounts Controller", () => {
});
});

it("/accounts/:address/deffered - should return 200 status code and return address deffered details for a specific address", async () => {
const address: string = "erd1vup7q384decm8l8mu4ehz75c5mfs089nd32fteru95tm8d0a8dqs8g0yst";
await request(app.getHttpServer())
.get(route + "/" + address + "/deferred")
.set("header", "content-type")
.expect(200);
});

it("/accounts/:address/tokens - should return 200 status code and return a list of tokens for a specifc address", async () => {
const address: string = "erd19w6f7jqnf4nqrdmq0m548crrc4v3dmrxtn7u3dngep2r078v30aqzzu6nc";
await request(app.getHttpServer())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { PublicAppModule } from 'src/public.app.module';
import request = require('supertest');

describe("Usernames Controller", () => {
let app: INestApplication;
const route: string = "/hello";

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [PublicAppModule],
}).compile();

app = moduleRef.createNestApplication();

await app.init();
});

it("/hello - should return status code 200 and response message 'hello' ", async () => {

await request(app.getHttpServer())
.get(route)
.expect(200)
.then(res => {
expect(res.text).toStrictEqual('hello');
});
});
});
Loading

0 comments on commit e2f913c

Please sign in to comment.