diff --git a/src/test/integration/accounts.e2e-spec.ts b/src/test/integration/accounts.e2e-spec.ts index 3008e3ae7..5064850a2 100644 --- a/src/test/integration/accounts.e2e-spec.ts +++ b/src/test/integration/accounts.e2e-spec.ts @@ -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; @@ -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); @@ -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); @@ -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 @@ -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'); + }); }); }); diff --git a/src/test/integration/api.config.e2e-spec.ts b/src/test/integration/api.config.e2e-spec.ts index e6cdc239d..5e4154213 100644 --- a/src/test/integration/api.config.e2e-spec.ts +++ b/src/test/integration/api.config.e2e-spec.ts @@ -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)); @@ -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:guest@127.0.0.1:5672')); + + const results = apiConfigService.getEventsNotifierUrl(); + expect(results).toEqual('amqp://guest:guest@127.0.0.1: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'); + }); + }); }); diff --git a/src/test/integration/controllers/accounts.controller.e2e-spec.ts b/src/test/integration/controllers/accounts.controller.e2e-spec.ts index 17c951aa5..b2776b758 100644 --- a/src/test/integration/controllers/accounts.controller.e2e-spec.ts +++ b/src/test/integration/controllers/accounts.controller.e2e-spec.ts @@ -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", () => { @@ -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', @@ -37,7 +40,13 @@ 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) @@ -45,6 +54,17 @@ describe("Accounts Controller", () => { .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()) @@ -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()) diff --git a/src/test/integration/controllers/health.check.controller.e2e-spec.ts b/src/test/integration/controllers/health.check.controller.e2e-spec.ts new file mode 100644 index 000000000..3b77a4c86 --- /dev/null +++ b/src/test/integration/controllers/health.check.controller.e2e-spec.ts @@ -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'); + }); + }); +}); diff --git a/src/test/integration/controllers/waiting-list.controller.e2e-spec.ts b/src/test/integration/controllers/waiting-list.controller.e2e-spec.ts index 02361bd54..bd55996f3 100644 --- a/src/test/integration/controllers/waiting-list.controller.e2e-spec.ts +++ b/src/test/integration/controllers/waiting-list.controller.e2e-spec.ts @@ -28,4 +28,10 @@ describe("Waiting-List Controller", () => { .get(route + "/count") .expect(200); }); + + it("/waiting-list/c - should return 200 status code and waiting-lists total count (alternative)", async () => { + await request(app.getHttpServer()) + .get(route + "/c") + .expect(200); + }); }); diff --git a/src/test/integration/identities.e2e-spec.ts b/src/test/integration/identities.e2e-spec.ts index 11f300c78..0afcb22d0 100644 --- a/src/test/integration/identities.e2e-spec.ts +++ b/src/test/integration/identities.e2e-spec.ts @@ -102,23 +102,13 @@ describe('Identities Service', () => { const results = await identityService.getIdentities(ids); for (const result of results) { - expect(result).toBeInstanceOf(Object); - expect(result.hasOwnProperty('apr')).toBe(true); - expect(result.hasOwnProperty('avatar')).toBe(true); - expect(result.hasOwnProperty('description')).toBe(true); - expect(result.hasOwnProperty('distribution')).toBe(true); - expect(result.hasOwnProperty('identity')).toBe(true); - expect(result.hasOwnProperty('location')).toBe(true); - expect(result.hasOwnProperty('locked')).toBe(true); - expect(result.hasOwnProperty('name')).toBe(true); - expect(result.hasOwnProperty('providers')).toBe(true); - expect(result.hasOwnProperty('rank')).toBe(true); - expect(result.hasOwnProperty('score')).toBe(true); - expect(result.hasOwnProperty('stake')).toBe(true); - expect(result.hasOwnProperty('stakePercent')).toBe(true); - expect(result.hasOwnProperty('topUp')).toBe(true); - expect(result.hasOwnProperty('validators')).toBe(true); - expect(result.hasOwnProperty('website')).toBe(true); + expect(result).toHaveProperties([ + 'apr', 'avatar', 'description', + 'distribution', 'identity', 'location', + 'locked', 'name', 'providers', + 'rank', 'score', 'stake', + 'stakePercent', 'topUp', 'validators', 'website', + ]); } }); }); diff --git a/src/test/integration/nft.e2e-spec.ts b/src/test/integration/nft.e2e-spec.ts index abacc7afd..98b4d90aa 100644 --- a/src/test/integration/nft.e2e-spec.ts +++ b/src/test/integration/nft.e2e-spec.ts @@ -9,6 +9,8 @@ import { NftType } from "src/endpoints/nfts/entities/nft.type"; import { NftOwner } from 'src/endpoints/nfts/entities/nft.owner'; import { EsdtAddressService } from 'src/endpoints/esdt/esdt.address.service'; import { NftAccount } from 'src/endpoints/nfts/entities/nft.account'; +import { CachingService } from 'src/common/caching/caching.service'; +import { ApiConfigService } from 'src/common/api-config/api.config.service'; describe('Nft Service', () => { @@ -54,6 +56,9 @@ describe('Nft Service', () => { }); + beforeEach(() => { jest.restoreAllMocks(); }); + + describe("NFT List", () => { it(`should return a list with 25 nfts and verify if nft contains property`, async () => { const nfts = await nftService.getNfts({ from: 0, size: 25 }, new NftFilter()); @@ -238,7 +243,7 @@ describe('Nft Service', () => { expect(nftOwners).toHaveLength(25); }); - it("should verify if all esdt of type MetaEsdt contains owner property and need to bedefined ", async () => { + it("should verify if all esdt of type MetaEsdt contains owner property and need to be defined ", async () => { const options = new NftQueryOptions(); options.withOwner = true; @@ -257,6 +262,68 @@ describe('Nft Service', () => { expect(nftOwners).toHaveLength(25); }); + + it('should return all esdt of type NonFungibleESDT details from two collections', async () => { + const filter = new NftFilter(); + filter.collections = ["EROBOT-527a29", "COLLARV2-467a53"]; + + const results = await nftService.getNfts({ from: 0, size: 10 }, filter); + + for (const result of results) { + expect(result.type).toStrictEqual('NonFungibleESDT'); + } + }); + + it('should return nft details with search filter applied and expect that collection name to be found based on search term', async () => { + const filter = new NftFilter(); + filter.search = 'eRobots'; + + const results = await nftService.getNfts({ from: 0, size: 1 }, filter); + + for (const result of results) { + expect(result.collection).toStrictEqual('EROBOT-527a29'); + } + }); + + it('should return 10 nfts that have uris defined', async () => { + const filter = new NftFilter(); + filter.hasUris = true; + + const results = await nftService.getNfts({ from: 0, size: 10 }, filter); + expect(results.length).toStrictEqual(10); + + for (const result of results) { + expect(result.uris).toBeDefined(); + } + }); + + it('should return 10 nfts that have isWhitelisted property true', async () => { + jest.spyOn(ApiConfigService.prototype, 'getIsIndexerV3FlagActive') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(() => true)); + + const filter = new NftFilter(); + filter.hasUris = true; + + const results = await nftService.getNfts({ from: 0, size: 10 }, filter); + expect(results.length).toStrictEqual(10); + + for (const result of results) { + expect(result.isWhitelistedStorage).toStrictEqual(true); + } + }); + + it('should return 10 nfts that have timestamp > 1654630698 ', async () => { + const filter = new NftFilter(); + filter.after = 1654630698; + + const results = await nftService.getNfts({ from: 0, size: 10 }, filter); + expect(results.length).toStrictEqual(10); + + for (const result of results) { + expect(result.timestamp).toBeGreaterThanOrEqual(1654630698); + } + }); }); describe("NFT Count", () => { @@ -300,6 +367,15 @@ describe('Nft Service', () => { expect(count).toBeGreaterThanOrEqual(10000); }); + it("should return the number of nfts with collection filter applied", async () => { + const filters = new NftFilter(); + filters.collections = ['EROBOT-527a29', 'MEDAL-ae074f']; + + const count = await nftService.getNftCount(filters); + + expect(count).toBeGreaterThanOrEqual(100); + }); + it("should return 0 if one collection isWhitelistedStorage = true ", async () => { const filters = new NftFilter(); filters.collection = "LKMEX-f4d898"; @@ -388,14 +464,13 @@ describe('Nft Service', () => { describe("NFT Address", () => { it("should return one nft for a specific account", async () => { - const address: string = "erd15gculjmu3r62ldlwyguqdgddez35r2lv6ka8j7s6pwhqlc80httqljzwgm"; - const identifier: string = "MOS-b9b4b2-2710"; - jest .spyOn(EsdtAddressService.prototype, 'getNftsForAddress') // eslint-disable-next-line require-await .mockImplementation(jest.fn(async () => [mockNftAccount])); + const address: string = "erd15gculjmu3r62ldlwyguqdgddez35r2lv6ka8j7s6pwhqlc80httqljzwgm"; + const identifier: string = "MOS-b9b4b2-2710"; const results = await nftService.getNftForAddress(address, identifier); if (!results) { @@ -413,17 +488,16 @@ describe('Nft Service', () => { }); it("should return undefined if account does not contains an nft", async () => { - const address: string = "erd15gculjmu3r62ldlwyguqdgddez35r2lv6ka8j7s6pwhqlc80httqljzwgm"; - const identifier: string = "MOS-b9b4b2-2710"; - jest .spyOn(EsdtAddressService.prototype, 'getNftsForAddress') // eslint-disable-next-line require-await .mockImplementation(jest.fn(async () => [])); + const address: string = "erd15gculjmu3r62ldlwyguqdgddez35r2lv6ka8j7s6pwhqlc80httqljzwgm"; + const identifier: string = "MOS-b9b4b2-2710"; const results = await nftService.getNftForAddress(address, identifier); - expect(results).toBeUndefined(); + expect(results).toBeUndefined(); }); }); @@ -443,6 +517,13 @@ describe('Nft Service', () => { expect(owner.balance).toStrictEqual("1"); } }); + + it('should return undefined because test simulates that the given identifier is not an nft', async () => { + const identifier: string = 'WEGLD-bd4d79'; + const results = await nftService.getNftOwners(identifier, { from: 0, size: 1 }); + + expect(results).toBeUndefined(); + }); }); describe("Single NFT", () => { @@ -458,6 +539,17 @@ describe('Nft Service', () => { expect(result.identifier).toStrictEqual("EROBOT-527a29-c4"); expect(result.creator).toBeDefined(); }); + + it('should return undefined', async () => { + jest.spyOn(NftService.prototype, 'getNftsInternal') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_from: number, _size: number, _filter: NftFilter, _identifier?: string) => [])); + + const identifier: string = " "; + const result = await nftService.getSingleNft(identifier); + + expect(result).toBeUndefined(); + }); }); describe("Get NFT Identifier Media", () => { @@ -477,4 +569,71 @@ describe('Nft Service', () => { } }); }); + + describe('getNftSupply', () => { + it('should return nft supply details', async () => { + const identifier: string = "EROBOT-527a29-c4"; + const result = await nftService.getNftSupply(identifier); + + expect(result).toStrictEqual("1"); + }); + + it('should return undefined because test simulates that identifier does not have the correct format', async () => { + const identifier: string = "EROBOT527a29c4"; + const result = await nftService.getNftSupply(identifier); + + expect(result).toBeUndefined(); + }); + + it('should return undefined because test simulates that nft length is equal with 0', async () => { + jest.spyOn(NftService.prototype, 'getNftsInternal') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_from: number, _size: number, _filter: NftFilter, _identifier?: string) => [])); + + const identifier: string = 'EROBOT-527a29-c4'; + const result = await nftService.getNftSupply(identifier); + + expect(result).toBeUndefined(); + }); + }); + + describe('getNftOwnersCount', () => { + it('should return total number of esdts token', async () => { + jest + .spyOn(CachingService.prototype, 'getOrSetCache') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_key: string, promise: any) => promise())); + + jest.spyOn(NftService.prototype, 'getNftOwnersCountRaw') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_dentifier: string) => 1)); + + const identifier: string = "EROBOT-527a29"; + const result = await nftService.getNftOwnersCount(identifier); + + expect(result).toStrictEqual(1); + }); + + it('should return undefined because test simulates that esdt owners are null', async () => { + jest + .spyOn(CachingService.prototype, 'getOrSetCache') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_key: string, promise: any) => promise())); + + jest.spyOn(NftService.prototype, 'getNftOwnersCountRaw') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_dentifier: string) => null)); + + const identifier: string = "EROBOT-527a29"; + const result = await nftService.getNftOwnersCount(identifier); + + expect(result).toBeUndefined(); + }); + }); + + describe('buildElasticNftFilter', () => { + it('build elastic NFT Filter', async () => { + + }); + }); }); diff --git a/src/test/integration/nft.tag.e2e-spec.ts b/src/test/integration/nft.tag.e2e-spec.ts index 434a67db9..e50007117 100644 --- a/src/test/integration/nft.tag.e2e-spec.ts +++ b/src/test/integration/nft.tag.e2e-spec.ts @@ -16,28 +16,55 @@ describe('NFT Tag Service', () => { }); - it(`should return list of tags for 2 nft`, async () => { - const tags = await tagService.getNftTags({ from: 0, size: 2 }); + describe('getNftTags', () => { + it(`should return list of tags for 2 nft`, async () => { + const tags = await tagService.getNftTags({ from: 0, size: 2 }); + + expect(tags).toHaveLength(2); + + for (const tag of tags) { + expect(tag).toHaveStructure(Object.keys(new Tag())); + } + }); + + it(`should return list of tags for 2 nft`, async () => { + const tags = await tagService.getNftTags({ from: 0, size: 2 }); + + expect(tags).toHaveLength(2); + + for (const tag of tags) { + expect(tag).toHaveStructure(Object.keys(new Tag())); + } + }); + + it(`should return list of tags for 2 nft`, async () => { + const tags = await tagService.getNftTags({ from: 0, size: 2 }); + + expect(tags).toHaveLength(2); + + for (const tag of tags) { + expect(tag).toHaveStructure(Object.keys(new Tag())); + } + }); - for (const tag of tags) { - expect(tag).toHaveStructure(Object.keys(new Tag())); - } - expect(tags).toHaveLength(2); }); - it(`should return a list of tags raw for 2 nft`, async () => { - const tagsRaw = await tagService.getNftTagsRaw({ from: 0, size: 2 }); - for (const tag of tagsRaw) { - expect(tag).toHaveStructure(Object.keys(new Tag())); - } - expect(tagsRaw).toHaveLength(2); + describe('getNftTagsRaw', () => { + it(`should return a list of tags raw for 2 nft`, async () => { + const tagsRaw = await tagService.getNftTagsRaw({ from: 0, size: 2 }); + + for (const tag of tagsRaw) { + expect(tag).toHaveStructure(Object.keys(new Tag())); + } + expect(tagsRaw).toHaveLength(2); + }); }); + describe('Get Nft Tag', () => { it('should return a specific tag', async () => { const tag = await tagService.getNftTag('elrond'); - expect(tag.tag).toStrictEqual('elrond'); }); }); diff --git a/src/test/integration/nft.worker.e2e-spec.ts b/src/test/integration/nft.worker.e2e-spec.ts index 810be37ff..5330b7824 100644 --- a/src/test/integration/nft.worker.e2e-spec.ts +++ b/src/test/integration/nft.worker.e2e-spec.ts @@ -1,10 +1,10 @@ import { Test } from "@nestjs/testing"; import { NftWorkerService } from "../../queue.worker/nft.worker/nft.worker.service"; import { Nft } from "../../endpoints/nfts/entities/nft"; -import nftExample from "../data/esdt/nft/nft.example"; import { ProcessNftSettings } from "../../endpoints/process-nfts/entities/process.nft.settings"; import { NftWorkerModule } from "src/queue.worker/nft.worker/nft.worker.module"; import { NftType } from "../../endpoints/nfts/entities/nft.type"; +import { NftThumbnailService } from "src/queue.worker/nft.worker/queue/job-services/thumbnails/nft.thumbnail.service"; describe('Nft Worker Service', () => { let nftWorkerService: NftWorkerService; @@ -17,45 +17,79 @@ describe('Nft Worker Service', () => { nftWorkerService = moduleRef.get(NftWorkerService); }); - describe('Add Process Nft Queue Job', () => { - it('should force refresh media for nft', async () => { - const nft = new Nft(); - nft.identifier = nftExample.identifier; + beforeEach(() => { jest.restoreAllMocks(); }); - const nftOptions = new ProcessNftSettings(); - nftOptions.forceRefreshMedia = true; - const result = await nftWorkerService.addProcessNftQueueJob(nft, nftOptions); + describe('addProcessNftQueueJob', () => { + it('should return true if nft is processed', async () => { + const nft: Nft = new Nft(); + nft.identifier = 'MAW-894a92-0270'; + + const settings = new ProcessNftSettings(); + settings.forceRefreshMedia = true; + + const result = await nftWorkerService.addProcessNftQueueJob(nft, settings); + expect(result).toStrictEqual(true); }); - it("should return null if nft type is MetaESDT", async () => { - const nft = new Nft(); - nft.type = NftType.MetaESDT; + it('should return false if nft does not need to be processed', async () => { + jest.spyOn(NftWorkerService.prototype, 'needsProcessing') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_nft: Nft, _settings: ProcessNftSettings) => false)); - const nftOptions = new ProcessNftSettings(); - nftOptions.forceRefreshMedia = true; + const nft: Nft = new Nft(); + nft.identifier = 'MAW-894a92-0270'; + + const settings: ProcessNftSettings = new ProcessNftSettings(); + settings.forceRefreshMedia = true; + + const result = await nftWorkerService.addProcessNftQueueJob(nft, settings); - const result = await nftWorkerService.addProcessNftQueueJob(nft, nftOptions); - expect(result).toBeFalsy(); + expect(result).toStrictEqual(false); }); }); - describe('Needs Processing', () => { - it('should return true on nft processing', async () => { - const nft = new Nft(); - nft.identifier = nftExample.identifier; + describe('needsProcessing', () => { + it('should return false if esdt is the type MetaESDT', async () => { + const nft: Nft = new Nft(); + nft.type = NftType.MetaESDT; - const nftOptions = new ProcessNftSettings(); - nftOptions.forceRefreshMedia = true; + const settings: ProcessNftSettings = new ProcessNftSettings(); + settings.forceRefreshMedia = true; + + const result = await nftWorkerService.needsProcessing(nft, settings); + + expect(result).toStrictEqual(false); + }); + + it('should return true if one of the "forceRefresh" settings is active', async () => { + const nft: Nft = new Nft(); + nft.identifier = 'MAW-894a92-0270'; + + const settings: ProcessNftSettings = new ProcessNftSettings(); + settings.forceRefreshMedia = true; + + const result = await nftWorkerService.needsProcessing(nft, settings); + + expect(result).toStrictEqual(true); + }); + + it('should return true if skipRefreshThumbnail settings is active', async () => { + const nft: Nft = new Nft(); + nft.identifier = 'MAW-894a92-0270'; + + const settings: ProcessNftSettings = new ProcessNftSettings(); + settings.skipRefreshThumbnail = true; + + const result = await nftWorkerService.needsProcessing(nft, settings); - const result = await nftWorkerService.needsProcessing(nft, nftOptions); expect(result).toStrictEqual(true); }); - it('should return true if ProcessNftSettings are set to true', async () => { + it('should return true if media is undefined', async () => { const nft = new Nft(); - nft.identifier = nftExample.identifier; + nft.media = undefined; const nftOptions = new ProcessNftSettings(); nftOptions.forceRefreshMedia = true; @@ -64,12 +98,24 @@ describe('Nft Worker Service', () => { expect(result).toStrictEqual(true); }); - it('should return true if nft.media.length == 0', async () => { + it('should return true if metadata is undefined', async () => { const nft = new Nft(); - nft.identifier = 'LKMEX-aab910-23049b'; + nft.metadata = undefined; + + const result = await nftWorkerService.needsProcessing(nft, new ProcessNftSettings()); + expect(result).toStrictEqual(true); + }); + + it('should return true if media is defined and has thumbnail is generated', async () => { + jest.spyOn(NftThumbnailService.prototype, 'hasThumbnailGenerated') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_identifier: string, _fileUrl: string) => false)); + + const nft = new Nft(); + nft.identifier = 'MAW-894a92-0270'; const nftOptions = new ProcessNftSettings(); - nftOptions.forceRefreshMedia = true; + nftOptions.skipRefreshThumbnail = false; const result = await nftWorkerService.needsProcessing(nft, nftOptions); expect(result).toStrictEqual(true); diff --git a/src/test/integration/tokens.e2e-spec.ts b/src/test/integration/tokens.e2e-spec.ts index 74fe45819..e1e139779 100644 --- a/src/test/integration/tokens.e2e-spec.ts +++ b/src/test/integration/tokens.e2e-spec.ts @@ -11,6 +11,11 @@ import { Test } from '@nestjs/testing'; import { FileUtils } from 'src/utils/file.utils'; import '../../utils/extensions/jest.extensions'; import { TokenDetailedWithBalance } from 'src/endpoints/tokens/entities/token.detailed.with.balance'; +import { TokenSort } from 'src/endpoints/tokens/entities/token.sort'; +import { SortOrder } from 'src/common/entities/sort.order'; +import { ElasticQuery } from 'src/common/elastic/entities/elastic.query'; +import { TokenWithRolesFilter } from 'src/endpoints/tokens/entities/token.with.roles.filter'; +import { QueryPagination } from 'src/common/entities/query.pagination'; describe('Token Service', () => { let tokenService: TokenService; @@ -269,7 +274,7 @@ describe('Token Service', () => { ); }); - it("should return a list of tokens with identifier filter applied", async () => { + it("should return a list of tokens with identifiers filter applied", async () => { const MOCK_PATH = apiConfigService.getMockPath(); const filter = new TokenFilter(); filter.identifiers = ["WEGLD-bd4d79", "RIDE-7d18e9"]; @@ -290,7 +295,7 @@ describe('Token Service', () => { ); }); - it("should return an empty token list if tokens are not present", async () => { + it("should return an empty token list if tokens are undefined", async () => { jest .spyOn(CachingService.prototype, 'getOrSetCache') // eslint-disable-next-line require-await @@ -300,6 +305,110 @@ describe('Token Service', () => { expect(results).toStrictEqual([]); }); + + it("should verify if first returned elemenent (accounts) is always 0 because test simulates that order of the list is ascending by accounts", async () => { + const MOCK_PATH = apiConfigService.getMockPath(); + const filter = new TokenFilter(); + filter.sort = TokenSort.accounts; + filter.order = SortOrder.asc; + + jest + .spyOn(CachingService.prototype, 'getOrSetCache') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async () => + FileUtils.parseJSONFile(`${MOCK_PATH}tokens.mock.json`))); + + const results = await tokenService.getFilteredTokens(filter); + + expect(results).toEqual( + expect.arrayContaining([ + expect.objectContaining({ identifier: "CRB-62d525" }), + expect.objectContaining({ accounts: 1 }), + ]) + ); + }); + + it("should verify if first returned elemenent (transactions) is always 1 because test simulates that order of the list is ascending by transactions", async () => { + const MOCK_PATH = apiConfigService.getMockPath(); + const filter = new TokenFilter(); + filter.sort = TokenSort.transactions; + filter.order = SortOrder.asc; + + jest + .spyOn(CachingService.prototype, 'getOrSetCache') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async () => + FileUtils.parseJSONFile(`${MOCK_PATH}tokens.mock.json`))); + + const results = await tokenService.getFilteredTokens(filter); + + expect(results).toEqual( + expect.arrayContaining([ + expect.objectContaining({ identifier: "CRB-62d525" }), + expect.objectContaining({ transactions: 1 }), + ]) + ); + }); + + it("should return a list of tokens in ascending order sorted by price and first element in array should be WEGLD-bd4d79", async () => { + const MOCK_PATH = apiConfigService.getMockPath(); + const filter = new TokenFilter(); + filter.sort = TokenSort.price; + filter.order = SortOrder.asc; + + jest + .spyOn(CachingService.prototype, 'getOrSetCache') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async () => + FileUtils.parseJSONFile(`${MOCK_PATH}tokens.mock.json`))); + + const results = await tokenService.getFilteredTokens(filter); + + expect(results).toEqual( + expect.arrayContaining([ + expect.objectContaining({ identifier: "WEGLD-bd4d79" }), + ]) + ); + }); + }); + + it("should return a list of tokens ordered descending by accounts", async () => { + const MOCK_PATH = apiConfigService.getMockPath(); + const filter = new TokenFilter(); + filter.sort = TokenSort.accounts; + filter.order = SortOrder.desc; + + jest + .spyOn(CachingService.prototype, 'getOrSetCache') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async () => + FileUtils.parseJSONFile(`${MOCK_PATH}tokens.mock.json`))); + + const results = await tokenService.getFilteredTokens(filter); + + expect(results).toEqual( + expect.arrayContaining([ + expect.objectContaining({ identifier: "WEGLD-bd4d79" }), + ]) + ); + }); + + + it("should return a list of tokens in ascending order sorted by marketCap and first element in array should be WEGLD-bd4d79", async () => { + const MOCK_PATH = apiConfigService.getMockPath(); + const filter = new TokenFilter(); + filter.sort = TokenSort.marketCap; + filter.order = SortOrder.asc; + + jest + .spyOn(CachingService.prototype, 'getOrSetCache') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async () => + FileUtils.parseJSONFile(`${MOCK_PATH}tokens.mock.json`))); + + const results = await tokenService.getFilteredTokens(filter); + + expect(results[0].identifier).toStrictEqual('WEGLD-bd4d79'); }); describe("getTokenCount", () => { @@ -426,6 +535,13 @@ describe('Token Service', () => { }); //ToDo: properties of TokenType.FungibleESDT + + it('should return undefined because test simulates that token type is not NonfugibleESDT', async () => { + const identifier: string = 'METAUTKLK-e6a445'; + const result = await tokenService.getTokenProperties(identifier); + + expect(result).toBeUndefined(); + }); }); describe("getTokenCountForAddress", () => { @@ -561,6 +677,50 @@ describe('Token Service', () => { ]) ); }); + + it.skip('should return token details with name filter applied ', async () => { + const MOCK_PATH = apiConfigService.getMockPath(); + const address: string = "erd1qqqqqqqqqqqqqpgq6wegs2xkypfpync8mn2sa5cmpqjlvrhwz5nqgepyg8"; + + const filter: TokenFilter = new TokenFilter(); + filter.name = 'Water'; + + jest + .spyOn(ElasticService.prototype, 'get') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async () => + FileUtils.parseJSONFile(`${MOCK_PATH}tokens.mock.json`))); + + const results = await tokenService.getTokensForAddressFromElastic(address, { from: 0, size: 1 }, filter); + + expect(results).toEqual( + expect.arrayContaining([ + expect.objectContaining({ identifier: "WATER-9ed400" }), + ]) + ); + }); + + it.skip('should return token details with search filter applied ', async () => { + const MOCK_PATH = apiConfigService.getMockPath(); + const address: string = "erd1qqqqqqqqqqqqqpgq6wegs2xkypfpync8mn2sa5cmpqjlvrhwz5nqgepyg8"; + + const filter: TokenFilter = new TokenFilter(); + filter.search = 'Water'; + + jest + .spyOn(ElasticService.prototype, 'get') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async () => + FileUtils.parseJSONFile(`${MOCK_PATH}tokens.mock.json`))); + + const results = await tokenService.getTokensForAddressFromElastic(address, { from: 0, size: 1 }, filter); + + expect(results).toEqual( + expect.arrayContaining([ + expect.objectContaining({ identifier: "WATER-9ed400" }), + ]) + ); + }); }); describe("getTokenSupply", () => { @@ -695,6 +855,76 @@ describe('Token Service', () => { ); }); + it('should return tokens roles from elastic source is IndexerV3FlagActive = true', async () => { + const identifier: string = token.identifier; + + jest.spyOn(ApiConfigService.prototype, 'getIsIndexerV3FlagActive') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(() => true)); + + const results = await tokenService.getTokenRoles(identifier); + expect(results).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + canLocalMint: true, + canLocalBurn: true, + roles: ['ESDTRoleLocalMint', 'ESDTRoleLocalBurn'], + }), + expect.objectContaining({ + canLocalMint: true, + canLocalBurn: true, + roles: ['ESDTRoleLocalMint', 'ESDTRoleLocalBurn'], + }), + expect.objectContaining({ + canLocalMint: true, + canLocalBurn: true, + roles: ['ESDTRoleLocalMint', 'ESDTRoleLocalBurn'], + }), + ]) + ); + }); + + it('should return undefined if token identifier is not defined', async () => { + jest.spyOn(ApiConfigService.prototype, 'getIsIndexerV3FlagActive') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(() => true)); + + jest.spyOn(ElasticService.prototype, 'getItem') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_collection: string, _key: string, _identifier: string) => undefined)); + + const results = await tokenService.getTokenRoles(''); + + expect(results).toBeUndefined(); + }); + + it('should return undefined if token roles are not defined', async () => { + const identifier: string = token.identifier; + + jest.spyOn(ApiConfigService.prototype, 'getIsIndexerV3FlagActive') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(() => true)); + + jest + .spyOn(EsdtService.prototype, 'getEsdtAddressesRoles') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_identifier: string) => [ + { + address: 'erd1qqqqqqqqqqqqqpgqvc7gdl0p4s97guh498wgz75k8sav6sjfjlwqh679jy', + canLocalMint: true, + canLocalBurn: true, + roles: [''], + }])); + + jest.spyOn(ElasticService.prototype, 'getItem') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_collection: string, _key: string, _identifier: string) => identifier)); + + const results = await tokenService.getTokenRoles(''); + + expect(results).toBeUndefined(); + }); + it("should return undefined because test simulates that roles are not defined for token", async () => { const results = await tokenService.getTokenRoles('UNKNOWN'); expect(results).toStrictEqual([]); @@ -725,7 +955,33 @@ describe('Token Service', () => { canLocalBurn: true, roles: ['ESDTRoleLocalMint', 'ESDTRoleLocalBurn'], })); + }); + + it('should return undefined if token roles are not defined', async () => { + jest.spyOn(ApiConfigService.prototype, 'getIsIndexerV3FlagActive') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(() => true)); + jest.spyOn(ElasticService.prototype, 'getItem') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_collection: string, _key: string, _identifier: string) => identifier)); + + jest + .spyOn(EsdtService.prototype, 'getEsdtAddressesRoles') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_identifier: string) => [ + { + address: 'erd1qqqqqqqqqqqqqpgqvc7gdl0p4s97guh498wgz75k8sav6sjfjlwqh679jy', + canLocalMint: true, + canLocalBurn: true, + roles: [''], + }])); + + const address: string = "erd1qqqqqqqqqqqqqpgqvc7gdl0p4s97guh498wgz75k8sav6sjfjlwqh679jy"; + const identifier: string = "RIDE-7d18e9"; + const result = await tokenService.getTokenRolesForIdentifierAndAddress(identifier, address); + + expect(result).toBeUndefined(); }); it("should return undefined because test simulates that roles are not defined for address", async () => { @@ -770,4 +1026,186 @@ describe('Token Service', () => { expect(result).toBeUndefined(); }); }); + + describe('isToken', () => { + it('should verify if given identifier is a token and should return true', async () => { + const result = await tokenService.isToken('RIDE-7d18e9'); + + expect(result).toStrictEqual(true); + }); + + it('should return false because esdt identifier is an NonFungibleESDT', async () => { + const result = await tokenService.isToken('EROBOT-527a29'); + + expect(result).toStrictEqual(false); + }); + }); + + describe('getTokenCountForAddressFromElastic', () => { + it('should return total number of tokens for a specific address from Elastic source', async () => { + const address: string = 'erd1qqqqqqqqqqqqqpgqa0fsfshnff4n76jhcye6k7uvd7qacsq42jpsp6shh2'; + const result = await tokenService.getTokenCountForAddressFromElastic(address); + + jest.spyOn(ElasticService.prototype, 'getCount') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_collection: string, _elasticQuery: ElasticQuery | undefined) => 3)); + + expect(result).toStrictEqual(3); + }); + }); + + describe('getTokenCountForAddressFromGateway', () => { + it('should return total number of tokens for a specific address from Gateway source', async () => { + const address: string = 'erd1qqqqqqqqqqqqqpgqa0fsfshnff4n76jhcye6k7uvd7qacsq42jpsp6shh2'; + const result = await tokenService.getTokenCountForAddressFromGateway(address); + + expect(result).toStrictEqual(3); + }); + }); + + describe('getTokenCountForAddressFromGateway & getTokenCountForAddressFromElastic', () => { + it('should return total number of tokens for a specific address from Gateway source', async () => { + const address: string = 'erd1qqqqqqqqqqqqqpgqa0fsfshnff4n76jhcye6k7uvd7qacsq42jpsp6shh2'; + const elasticResult = await tokenService.getTokenCountForAddressFromElastic(address); + const gatewayResult = await tokenService.getTokenCountForAddressFromGateway(address); + + expect(elasticResult).toStrictEqual(gatewayResult); + }); + }); + + describe('getTokensWithRolesForAddressCount', () => { + it('should return total number of tokens with roles with identifier filter for a specific address', async () => { + const address: string = 'erd19w6f7jqnf4nqrdmq0m548crrc4v3dmrxtn7u3dngep2r078v30aqzzu6nc'; + const filter: TokenWithRolesFilter = new TokenWithRolesFilter(); + filter.identifier = 'RIDE-7d18e9'; + + jest.spyOn(ElasticService.prototype, 'getCount') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_collection: string, _elasticQuery: ElasticQuery | undefined) => 3)); + + const result = await tokenService.getTokensWithRolesForAddressCount(address, filter); + + expect(result).toStrictEqual(3); + }); + + it('should return total number of tokens with roles with identifier filter for a specific address', async () => { + const address: string = 'erd19w6f7jqnf4nqrdmq0m548crrc4v3dmrxtn7u3dngep2r078v30aqzzu6nc'; + const filter: TokenWithRolesFilter = new TokenWithRolesFilter(); + filter.search = 'RIDE'; + + jest.spyOn(ElasticService.prototype, 'getCount') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_collection: string, _elasticQuery: ElasticQuery | undefined) => 1)); + + const result = await tokenService.getTokensWithRolesForAddressCount(address, filter); + + expect(result).toStrictEqual(1); + }); + + it('should return total number of tokens with roles with owner filter for a specific address', async () => { + const address: string = 'erd19w6f7jqnf4nqrdmq0m548crrc4v3dmrxtn7u3dngep2r078v30aqzzu6nc'; + const filter: TokenWithRolesFilter = new TokenWithRolesFilter(); + filter.owner = 'erd1ss6u80ruas2phpmr82r42xnkd6rxy40g9jl69frppl4qez9w2jpsqj8x97'; + + jest.spyOn(ElasticService.prototype, 'getCount') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_collection: string, _elasticQuery: ElasticQuery | undefined) => 1)); + + const result = await tokenService.getTokensWithRolesForAddressCount(address, filter); + + expect(result).toStrictEqual(1); + }); + + it('should return total number of tokens with roles with canMint = true property filter for a specific address', async () => { + const address: string = 'erd19w6f7jqnf4nqrdmq0m548crrc4v3dmrxtn7u3dngep2r078v30aqzzu6nc'; + const filter: TokenWithRolesFilter = new TokenWithRolesFilter(); + filter.canMint = true; + + jest.spyOn(ElasticService.prototype, 'getCount') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_collection: string, _elasticQuery: ElasticQuery | undefined) => 2)); + + const result = await tokenService.getTokensWithRolesForAddressCount(address, filter); + + expect(result).toStrictEqual(2); + }); + + it('should return total number of tokens with roles with canMint = false property filter for a specific address', async () => { + const address: string = 'erd19w6f7jqnf4nqrdmq0m548crrc4v3dmrxtn7u3dngep2r078v30aqzzu6nc'; + const filter: TokenWithRolesFilter = new TokenWithRolesFilter(); + filter.canMint = false; + + jest.spyOn(ElasticService.prototype, 'getCount') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_collection: string, _elasticQuery: ElasticQuery | undefined) => 1)); + + const result = await tokenService.getTokensWithRolesForAddressCount(address, filter); + + expect(result).toStrictEqual(1); + }); + + it('should return total number of tokens with roles with canBurn = true property filter for a specific address', async () => { + const address: string = 'erd19w6f7jqnf4nqrdmq0m548crrc4v3dmrxtn7u3dngep2r078v30aqzzu6nc'; + const filter: TokenWithRolesFilter = new TokenWithRolesFilter(); + filter.canBurn = true; + + jest.spyOn(ElasticService.prototype, 'getCount') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_collection: string, _elasticQuery: ElasticQuery | undefined) => 5)); + + const result = await tokenService.getTokensWithRolesForAddressCount(address, filter); + + expect(result).toStrictEqual(5); + }); + + it('should return total number of tokens with roles with canBurn = false property filter for a specific address', async () => { + const address: string = 'erd19w6f7jqnf4nqrdmq0m548crrc4v3dmrxtn7u3dngep2r078v30aqzzu6nc'; + const filter: TokenWithRolesFilter = new TokenWithRolesFilter(); + filter.canBurn = false; + + jest.spyOn(ElasticService.prototype, 'getCount') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_collection: string, _elasticQuery: ElasticQuery | undefined) => 4)); + + const result = await tokenService.getTokensWithRolesForAddressCount(address, filter); + + expect(result).toStrictEqual(4); + }); + }); + + describe('getTokenWithRolesForAddress', () => { + it('should return tokens details with roles for a specific address', async () => { + const address: string = 'erd1qqqqqqqqqqqqqpgqvc7gdl0p4s97guh498wgz75k8sav6sjfjlwqh679jy'; + const identifier: string = 'WEGLD-bd4d79'; + + const result = await tokenService.getTokenWithRolesForAddress(address, identifier); + + if (!result) { + throw new Error('Properties are not defined'); + } + expect(result.identifier).toStrictEqual('WEGLD-bd4d79'); + expect(result.canLocalMint).toStrictEqual(true); + expect(result.canLocalBurn).toStrictEqual(true); + expect(result.canWipe).toStrictEqual(true); + expect(result.canFreeze).toStrictEqual(true); + expect(result.canPause).toStrictEqual(true); + expect(result.canChangeOwner).toStrictEqual(true); + expect(result.canBurn).toStrictEqual(true); + expect(result.canMint).toStrictEqual(true); + expect(result.canUpgrade).toStrictEqual(true); + }); + + it('should return undefined because test simulates that identifier is not defined', async () => { + const address: string = 'erd1qqqqqqqqqqqqqpgqvc7gdl0p4s97guh498wgz75k8sav6sjfjlwqh679jy'; + const identifier: string = ''; + + jest.spyOn(TokenService.prototype, 'getTokensWithRolesForAddress') + // eslint-disable-next-line require-await + .mockImplementation(jest.fn(async (_address: string, _filter: TokenWithRolesFilter, _pagination: QueryPagination) => [])); + + const result = await tokenService.getTokenWithRolesForAddress(address, identifier); + + expect(result).toBeUndefined(); + }); + }); });