Skip to content

Commit cb1ec6b

Browse files
authored
test: added tests for Rosetta to increase coverage (#1141)
1 parent e34120f commit cb1ec6b

File tree

2 files changed

+210
-5
lines changed

2 files changed

+210
-5
lines changed

src/api/routes/rosetta/account.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ export function createRosettaAccountRouter(db: DataStore, chainId: ChainID): exp
3737
let blockQuery: FoundOrNot<DbBlock>;
3838
let blockHash: string = '0x';
3939

40-
if (accountIdentifier === undefined) {
41-
res.status(400).json(RosettaErrors[RosettaErrorsTypes.emptyAccountIdentifier]);
42-
return;
43-
}
44-
4540
// we need to return the block height/hash in the response, so we
4641
// need to fetch the block first.
4742
if (blockIdentifier === undefined) {

src/tests-rosetta/api.ts

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
RosettaErrors,
2525
RosettaOperationTypes,
2626
RosettaOperationStatuses,
27+
RosettaConstants,
2728
} from '../api/rosetta-constants';
2829
import { TestBlockBuilder } from '../test-utils/test-builders';
2930

@@ -985,6 +986,215 @@ describe('Rosetta API', () => {
985986
expect(JSON.parse(result.text)).toEqual(expectResponse);
986987
});
987988

989+
test('search balance by block_hash', async () => {
990+
const block_hash = '0x123456';
991+
const request: RosettaAccountBalanceRequest = {
992+
network_identifier: {
993+
blockchain: 'stacks',
994+
network: 'testnet',
995+
},
996+
account_identifier: {
997+
address: 'SP2QXJDSWYFGT9022M6NCA9SS4XNQM79D8E7EDSPQ',
998+
metadata: {},
999+
},
1000+
block_identifier: {
1001+
hash: block_hash,
1002+
},
1003+
};
1004+
await db.update(new TestBlockBuilder({ block_hash }).build());
1005+
const result = await supertest(api.server).post(`/rosetta/v1/account/balance/`).send(request);
1006+
expect(result.status).toBe(200);
1007+
expect(result.type).toBe('application/json');
1008+
1009+
const expectResponse = {
1010+
block_identifier: { index: 1, hash: block_hash },
1011+
balances: [
1012+
{
1013+
value: '0',
1014+
currency: {
1015+
decimals: 6,
1016+
symbol: "STX",
1017+
}
1018+
}
1019+
],
1020+
metadata: { sequence_number: 0 }
1021+
}
1022+
expect(JSON.parse(result.text)).toEqual(expectResponse);
1023+
});
1024+
1025+
test('missing account_identifier for balance', async () => {
1026+
const request = {
1027+
network_identifier: {
1028+
blockchain: 'stacks',
1029+
network: 'testnet',
1030+
},
1031+
block_identifier: {
1032+
hash: 'afd',
1033+
},
1034+
};
1035+
const result = await supertest(api.server).post(`/rosetta/v1/account/balance/`).send(request);
1036+
expect(result.status).toBe(400);
1037+
expect(result.type).toBe('application/json');
1038+
1039+
const expectResponse = {
1040+
code: 609,
1041+
message: 'Invalid params.',
1042+
retriable: false,
1043+
details: { message: "should have required property 'account_identifier'" }
1044+
}
1045+
expect(JSON.parse(result.text)).toEqual(expectResponse);
1046+
});
1047+
1048+
test('block not found', async () => {
1049+
const request: RosettaAccountBalanceRequest = {
1050+
network_identifier: {
1051+
blockchain: 'stacks',
1052+
network: 'testnet',
1053+
},
1054+
account_identifier: {
1055+
address: 'SP2QXJDSWYFGT9022M6NCA9SS4XNQM79D8E7EDSPQ',
1056+
metadata: {},
1057+
},
1058+
block_identifier: {
1059+
hash: '0x123456',
1060+
},
1061+
};
1062+
const result = await supertest(api.server).post(`/rosetta/v1/account/balance/`).send(request);
1063+
expect(result.status).toBe(500);
1064+
expect(result.type).toBe('application/json');
1065+
1066+
const expectResponse = {
1067+
code: 605,
1068+
message: 'Block not found.',
1069+
retriable: true
1070+
}
1071+
expect(JSON.parse(result.text)).toEqual(expectResponse);
1072+
});
1073+
1074+
test('invalid block_hash', async () => {
1075+
const block_hash = '123456';
1076+
const request: RosettaAccountBalanceRequest = {
1077+
network_identifier: {
1078+
blockchain: 'stacks',
1079+
network: 'testnet',
1080+
},
1081+
account_identifier: {
1082+
address: 'SP2QXJDSWYFGT9022M6NCA9SS4XNQM79D8E7EDSPQ',
1083+
metadata: {},
1084+
},
1085+
block_identifier: {
1086+
hash: block_hash,
1087+
},
1088+
};
1089+
await db.update(new TestBlockBuilder({ block_hash: '0x' + block_hash }).build());
1090+
const result = await supertest(api.server).post(`/rosetta/v1/account/balance/`).send(request);
1091+
expect(result.status).toBe(400);
1092+
expect(result.type).toBe('application/json');
1093+
1094+
const expectResponse = {
1095+
code: 606,
1096+
message: 'Invalid block hash.',
1097+
retriable: true
1098+
}
1099+
expect(JSON.parse(result.text)).toEqual(expectResponse);
1100+
});
1101+
1102+
test('invalid sub_account_indentifier', async () => {
1103+
const block_hash = '0x123456';
1104+
const request: RosettaAccountBalanceRequest = {
1105+
network_identifier: {
1106+
blockchain: 'stacks',
1107+
network: 'testnet',
1108+
},
1109+
account_identifier: {
1110+
address: 'SP2QXJDSWYFGT9022M6NCA9SS4XNQM79D8E7EDSPQ',
1111+
sub_account: {
1112+
address: 'invalid account'
1113+
},
1114+
metadata: {},
1115+
},
1116+
block_identifier: {
1117+
hash: block_hash,
1118+
},
1119+
};
1120+
await db.update(new TestBlockBuilder({ block_hash: block_hash }).build());
1121+
const result = await supertest(api.server).post(`/rosetta/v1/account/balance/`).send(request);
1122+
expect(result.status).toBe(400);
1123+
expect(result.type).toBe('application/json');
1124+
1125+
const expectResponse = {
1126+
code: 641,
1127+
message: 'Invalid sub-account',
1128+
retriable: false
1129+
}
1130+
expect(JSON.parse(result.text)).toEqual(expectResponse);
1131+
});
1132+
1133+
test('vesting schedule amount', async () => {
1134+
const block_hash = '0x123456';
1135+
const address = 'SP2QXJDSWYFGT9022M6NCA9SS4XNQM79D8E7EDSPQ';
1136+
const block = new TestBlockBuilder({ block_hash }).build();
1137+
const request: RosettaAccountBalanceRequest = {
1138+
network_identifier: {
1139+
blockchain: 'stacks',
1140+
network: 'testnet',
1141+
},
1142+
account_identifier: {
1143+
address: 'SP2QXJDSWYFGT9022M6NCA9SS4XNQM79D8E7EDSPQ',
1144+
sub_account: {
1145+
address: RosettaConstants.VestingLockedBalance
1146+
},
1147+
metadata: {},
1148+
},
1149+
block_identifier: {
1150+
hash: block_hash,
1151+
},
1152+
};
1153+
await db.update(block);
1154+
await db.updateBatchTokenOfferingLocked(client, [{ address, block: block.block.block_height, value: 50n }]);
1155+
const result = await supertest(api.server).post(`/rosetta/v1/account/balance/`).send(request);
1156+
expect(result.status).toBe(200);
1157+
expect(result.type).toBe('application/json');
1158+
1159+
const expectResponse = {
1160+
block_identifier: { index: 1, hash: block_hash },
1161+
balances: [
1162+
{
1163+
value: '0',
1164+
currency: {
1165+
decimals: 6,
1166+
symbol: 'STX'
1167+
},
1168+
metadata: {
1169+
VestingSchedule: [
1170+
JSON.stringify({
1171+
amount: '50',
1172+
unlock_height: 1
1173+
})
1174+
]
1175+
}
1176+
}
1177+
],
1178+
metadata: { sequence_number: 0 }
1179+
}
1180+
expect(JSON.parse(result.text)).toEqual(expectResponse);
1181+
});
1182+
1183+
test('invalid rosetta block request', async () => {
1184+
const result = await supertest(api.address)
1185+
.post(`/rosetta/v1/block`)
1186+
.send({
1187+
network_identifier: { blockchain: 'stacks', network: 'testnet' }
1188+
});
1189+
const expectResponse = {
1190+
code: 615,
1191+
message: 'Block identifier is null.',
1192+
retriable: false,
1193+
details: { message: "should have required property 'block_identifier'" }
1194+
}
1195+
expect(JSON.parse(result.text)).toEqual(expectResponse);
1196+
});
1197+
9881198
afterEach(async () => {
9891199
await api.terminate();
9901200
client.release();

0 commit comments

Comments
 (0)