Skip to content

Commit e2406fb

Browse files
committed
fix(allowlist.test): allowlist controller unit tests
wraps business logic in try-catches and updates response types where needed
1 parent 87878fe commit e2406fb

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/controllers/AllowListController.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,32 @@ export class AllowListController extends Controller {
8585
public async validateAllowList(
8686
@Body() requestBody: ValidateAllowListRequest,
8787
): Promise<ValidationResponse> {
88-
const result = parseAndValidateMerkleTree(requestBody);
88+
try {
89+
const result = parseAndValidateMerkleTree(requestBody);
90+
91+
if (!result.valid || !result.data) {
92+
this.setStatus(422);
93+
return {
94+
success: true,
95+
valid: false,
96+
message: "Errors while validating allow list",
97+
errors: result.errors,
98+
};
99+
}
89100

90-
if (!result.valid || !result.data) {
91-
this.setStatus(422);
101+
this.setStatus(201);
102+
return {
103+
success: true,
104+
valid: true,
105+
};
106+
} catch (error) {
107+
this.setStatus(500);
92108
return {
93109
success: false,
94110
valid: false,
95-
message: "Errors while validating allow list",
96-
errors: result.errors,
111+
message: "Error uploading data",
112+
errors: { allowList: "Error uploading data" },
97113
};
98114
}
99-
100-
this.setStatus(201);
101-
return {
102-
success: true,
103-
valid: true,
104-
};
105115
}
106116
}

test/api/v1/allowlist.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,17 @@ describe("Allow list validation at v1/allowlists/validate", async () => {
8383
test("Validates correctness of allowlist and returns results", async () => {
8484
const requestBody = {
8585
allowList: mockMerkleTree,
86-
totalUnits: "100",
86+
totalUnits: "100000000",
8787
};
8888
const response = await controller.validateAllowList(requestBody);
8989
expect(response.valid).to.be.true;
9090
expect(response.success).to.be.true;
91-
expect(response.message).to.be.eq(
92-
"Allowlist is a valid hypercerts allowlist object.",
93-
);
9491
});
9592

9693
test("Returns errors and message when allowlist is invalid", async () => {
9794
const requestBody = {
9895
allowList: incorrectMerkleTree,
99-
totalUnits: "100",
96+
totalUnits: "100000000",
10097
};
10198
const response = await controller.validateAllowList(requestBody);
10299

@@ -119,8 +116,7 @@ describe("Allow list validation at v1/allowlists/validate", async () => {
119116
expect(response.valid).to.be.false;
120117
expect(response.message).to.eq("Errors while validating allow list");
121118
expect(response.errors).to.deep.eq({
122-
units:
123-
"Total units in allowlist must match total units [expected: 99, got: 100]",
119+
totalUnits: "Total units do not match the sum of units in the allowlist",
124120
});
125121
});
126122
});

0 commit comments

Comments
 (0)