Skip to content

Commit

Permalink
[explorer] fix: chrome console errors #1117
Browse files Browse the repository at this point in the history
* [explorer] task: fix restriction property issue

* [explorer] task: fix pagination errors issue

* [explorer] feat: getMosaicArtifactExpiryReceipt unit test

* [explorer] task: fix lint

* [explorer] task: added getAccountRestrictions unit test
  • Loading branch information
AnthonyLaw authored Oct 3, 2022
1 parent c6340d5 commit 3cd1ba5
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 16 deletions.
89 changes: 82 additions & 7 deletions __tests__/infrastructure/MosaicService.spec.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import { MosaicService, NamespaceService } from '../../src/infrastructure';
import { MosaicService, NamespaceService, ReceiptService } from '../../src/infrastructure';
import TestHelper from '../TestHelper';
import { restore, stub } from 'sinon';
import { MosaicNonce, MosaicId } from 'symbol-sdk';

describe('Mosaic Service', () => {
let getMosaic = {};
let getMosaicsNames = [];
let searchReceipts = {};
let createReceiptTransactionStatement = [];

beforeEach(() => {
getMosaic = stub(MosaicService, 'getMosaic');
getMosaicsNames = stub(NamespaceService, 'getMosaicsNames');
searchReceipts = stub(ReceiptService, 'searchReceipts');
createReceiptTransactionStatement = stub(ReceiptService, 'createReceiptTransactionStatement');
});

afterEach(restore);

describe('getMosaicInfo should', () => {
describe('getMosaicInfo', () => {
// Arrange:
const {address} = TestHelper.generateAccount(1)[0];

it('return mosaic object', async () => {
it('returns mosaic object', async () => {
// Arrange:
const mosaicId = MosaicId.createFromNonce(MosaicNonce.createRandom(), address);
const mockMosaic = TestHelper.mockMosaicInfo(mosaicId.toHex(), address.plain(), 1, 100);
Expand All @@ -39,7 +43,7 @@ describe('Mosaic Service', () => {
expect(expiredInBlock).toEqual(101);
});

it('return native network mosaic object', async () => {
it('returns native network mosaic object', async () => {
// Arrange:
const mockNativeMosaicId = '6BED913FA20223F8';
const mockMosaic = TestHelper.mockMosaicInfo(mockNativeMosaicId, address.plain(), 1, 0);
Expand All @@ -61,8 +65,8 @@ describe('Mosaic Service', () => {
});
});

describe('getMosaicList should', () => {
it('return mosaics', async () => {
describe('getMosaicList', () => {
it('returns mosaics', async () => {
// Arrange:
const pageInfo = {
pageNumber: 1,
Expand Down Expand Up @@ -114,4 +118,75 @@ describe('Mosaic Service', () => {
});
});
});
});

describe('getMosaicArtifactExpiryReceipt', () => {
// Arrange:
const pageInfo = {
pageNumber: 1,
pageSize: 10
};
const {address} = TestHelper.generateAccount(1)[0];
const mosaicId = MosaicId.createFromNonce(MosaicNonce.createRandom(), address);

it('returns default info and skip receipt service request when mosaic duration is 0', async () => {
// Arrange:
const mosaicInfo = TestHelper.mockMosaicInfo(mosaicId.toHex(), address.plain(), 100, 0);

getMosaic.returns(Promise.resolve(mosaicInfo));

jest.spyOn(ReceiptService, 'searchReceipts');
jest.spyOn(ReceiptService, 'createReceiptTransactionStatement');

// Act:
const result = await MosaicService.getMosaicArtifactExpiryReceipt(pageInfo, mosaicId.toHex());

// Assert:
expect(ReceiptService.searchReceipts).not.toHaveBeenCalled();
expect(ReceiptService.createReceiptTransactionStatement).not.toHaveBeenCalled();
expect(result).toEqual({
data: [],
...pageInfo,
isLastPage: true
});
});

it('returns artifact expiry receipt when duration is nonzero ', async () => {
// Arrange:
const mosaicInfo = TestHelper.mockMosaicInfo(mosaicId.toHex(), address.plain(), 900, 100);

getMosaic.returns(Promise.resolve(mosaicInfo));

searchReceipts.returns(Promise.resolve({
data: {
artifactExpiryStatement: {
data: [TestHelper.mockArtifactExpiryReceipt(mosaicId.toHex(), 16717)],
receiptTransactionStatementType: 'Artifact Expiry Receipt'
}
},
...pageInfo,
isLastPage: true
}));

const receiptDto = {
artifactId: mosaicId.toHex(),
height: 1000,
mosaicArtifactId: mosaicId.toHex(),
receiptType: 'Mosaic Expired',
type: 16717,
version: 1
};

createReceiptTransactionStatement.returns(Promise.resolve([receiptDto]));

// Act:
const result = await MosaicService.getMosaicArtifactExpiryReceipt(pageInfo, mosaicId.toHex());

// Assert:
expect(result).toEqual({
data: [receiptDto],
...pageInfo,
isLastPage: true
});
});
});
});
136 changes: 134 additions & 2 deletions __tests__/infrastructure/RestrictionService.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,83 @@
import { RestrictionService } from '../../src/infrastructure';
import TestHelper from '../TestHelper';
import { restore, stub } from 'sinon';
import { MosaicId, UInt64, MosaicRestrictionType, Address } from 'symbol-sdk';
import { MosaicId, UInt64, MosaicRestrictionType } from 'symbol-sdk';

// Arrange:
jest.mock('../../src/infrastructure/http', () => {
const {
MosaicId,
Address,
TransactionType,
AddressRestrictionFlag,
MosaicRestrictionFlag,
OperationRestrictionFlag
} = require('symbol-sdk');

const createAccountRestriction = (restrictionFlag, value) => {
return {
restrictionFlags: restrictionFlag,
values: [value]
};
};

const addressRestriction = Address.createFromRawAddress('TAF4PFLYK6R3JTKU5SQ5LWG7YUO56BBKHJHBLJY');
const mosaicRestriction = new MosaicId('3A8416DB2D53B6C8');

return {
createRepositoryFactory: {
createRestrictionAccountRepository: () => {
return {
getAccountRestrictions: address => {
if ('TB2ZGMKWLBZ62VAXP2NPMEQL44UWMADDET6HW6Y' === address.plain()) {
return {
toPromise: () => {
return {
version: 1,
address: Address.createFromRawAddress('TB2ZGMKWLBZ62VAXP2NPMEQL44UWMADDET6HW6Y'),
restrictions: [
createAccountRestriction(
AddressRestrictionFlag.AllowIncomingAddress,
addressRestriction
),
createAccountRestriction(
AddressRestrictionFlag.AllowOutgoingAddress,
addressRestriction
),
createAccountRestriction(
AddressRestrictionFlag.BlockIncomingAddress,
addressRestriction
),
createAccountRestriction(
AddressRestrictionFlag.BlockOutgoingAddress,
addressRestriction
),
createAccountRestriction(
MosaicRestrictionFlag.AllowMosaic,
mosaicRestriction
),
createAccountRestriction(
MosaicRestrictionFlag.BlockMosaic,
mosaicRestriction
),
createAccountRestriction(
OperationRestrictionFlag.AllowOutgoingTransactionType,
TransactionType.TRANSFER
),
createAccountRestriction(
OperationRestrictionFlag.BlockOutgoingTransactionType,
TransactionType.TRANSFER
)
]
};
}
};
}
}
};
}
}
};
});

describe('RestrictionService', () => {
describe('formatMosaicGlobalRestriction', () => {
Expand Down Expand Up @@ -158,4 +234,60 @@ describe('RestrictionService', () => {
});
});
});

describe('getAccountRestrictions', () => {
it('returns account restrictions dto', async () => {
// Arrange + Act:
const result = await RestrictionService.getAccountRestrictions('TB2ZGMKWLBZ62VAXP2NPMEQL44UWMADDET6HW6Y');

// Assert:
const expectedRestrictionAddress = [
'Allow Incoming Addresses',
'Allow Outgoing Addresses',
'Block Incoming Addresses',
'Block Outgoing Addresses'
];

const expectedRestrictionMosaic = [
'Allow Mosaics',
'Block Mosaics'
];

const expectedRestrictionOperation = [
'Allow Outgoing Transactions',
'Block Outgoing Transactions'
];

expect(result).toEqual([
...expectedRestrictionAddress.map(restriction => {
return {
restrictionType: restriction,
restrictionAddressValues: ['TAF4PFLYK6R3JTKU5SQ5LWG7YUO56BBKHJHBLJY']
};
}),
...expectedRestrictionMosaic.map(restriction => {
return {
restrictionType: restriction,
restrictionMosaicValues: ['3A8416DB2D53B6C8']
};
}),
...expectedRestrictionOperation.map(restriction => {
return {
restrictionType: restriction,
restrictionTransactionValues: ['Transfer']
};
})
]);
});

it('throws error when account restrictions are not available', () => {
// Arrange:
const nonRestrictionsAccount = TestHelper.generateAccount(1)[0].address.plain();

// Act: + Assert:
expect(async () => {
await RestrictionService.getAccountRestrictions(nonRestrictionsAccount).toThrow();
});
});
});
});
13 changes: 10 additions & 3 deletions src/infrastructure/MosaicService.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,17 @@ class MosaicService {

const { pageNumber, pageSize } = pageInfo;

const endHeight = startHeight + duration;
// Returns empty, artifact expiry receipt does not exist when duration is infinity
if (0 === duration) {
return {
data: [],
pageNumber,
pageSize,
isLastPage: true
};
}

if (endHeight === startHeight)
return {};
const endHeight = startHeight + duration;

// Todo: Should filter with with ArtifactId rather than height.
// Bug: https://github.com/nemtech/catapult-rest/issues/517
Expand Down
6 changes: 2 additions & 4 deletions src/infrastructure/RestrictionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ class RestrictionService {
.getAccountRestrictions(Address.createFromRawAddress(address))
.toPromise();
} catch (e) {
// To Catach statusCode 404 if Account restrictions are not available.
// To catch statusCode 404
throw Error('Account restrictions are not available.');
}

const formattedAccountRestrictions = accountRestrictions.map(accountRestriction => this.formatAccountRestriction(accountRestriction));

return formattedAccountRestrictions;
return accountRestrictions.restrictions.map(restriction => this.formatAccountRestriction(restriction));
}

/**
Expand Down

0 comments on commit 3cd1ba5

Please sign in to comment.