Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Oct 4, 2024
1 parent 0d8e4f6 commit 099a9e1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/types/env-type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Static, Type } from "@sinclair/typebox";
import { StandardValidator } from "typebox-validators";

const envConfigSchema = Type.Object({
SUPABASE_URL: Type.String(),
Expand All @@ -15,4 +16,6 @@ const envConfigSchema = Type.Object({

export type EnvConfigType = Static<typeof envConfigSchema>;

export const envValidator = new StandardValidator(envConfigSchema);

export default envConfigSchema;
61 changes: 33 additions & 28 deletions tests/parser/permit-generation-module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,23 @@ describe("permit-generation-module.ts", () => {
process.env.PERMIT_FEE_RATE = "";
const permitGenerationModule = new PermitGenerationModule();
const spyConsoleLog = jest.spyOn(console, "log");
await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS);
await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS, process.env);
expect(spyConsoleLog).toHaveBeenCalledWith("PERMIT_FEE_RATE is not set, skipping permit fee generation");
});

it("Should not apply fees if PERMIT_FEE_RATE is 0", async () => {
process.env.PERMIT_FEE_RATE = "0";
const permitGenerationModule = new PermitGenerationModule();
const spyConsoleLog = jest.spyOn(console, "log");
await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS);
await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS, process.env);
expect(spyConsoleLog).toHaveBeenCalledWith("PERMIT_FEE_RATE is not set, skipping permit fee generation");
});

it("Should not apply fees if PERMIT_TREASURY_GITHUB_USERNAME is empty", async () => {
process.env.PERMIT_TREASURY_GITHUB_USERNAME = "";
const permitGenerationModule = new PermitGenerationModule();
const spyConsoleLog = jest.spyOn(console, "log");
await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS);
await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS, process.env);
expect(spyConsoleLog).toHaveBeenCalledWith(
"PERMIT_TREASURY_GITHUB_USERNAME is not set, skipping permit fee generation"
);
Expand All @@ -141,15 +141,15 @@ describe("permit-generation-module.ts", () => {
it("Should not apply fees if ERC20 reward token is included in PERMIT_ERC20_TOKENS_NO_FEE_WHITELIST", async () => {
const permitGenerationModule = new PermitGenerationModule();
const spyConsoleLog = jest.spyOn(console, "log");
await permitGenerationModule._applyFees(resultOriginal, DOLLAR_ADDRESS);
await permitGenerationModule._applyFees(resultOriginal, DOLLAR_ADDRESS, process.env);
expect(spyConsoleLog).toHaveBeenCalledWith(
`Token address ${DOLLAR_ADDRESS} is whitelisted to be fee free, skipping permit fee generation`
);
});

it("Should apply fees", async () => {
const permitGenerationModule = new PermitGenerationModule();
const resultAfterFees = await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS);
const resultAfterFees = await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS, process.env);

// check that 10% fee is subtracted from rewards
expect(resultAfterFees["user1"].total).toEqual(90);
Expand Down Expand Up @@ -180,13 +180,14 @@ describe("permit-generation-module.ts", () => {
const githubContextOrganizationId = 1;
const githubContextRepositoryId = 2;

const result = await permitGenerationModule._isPrivateKeyAllowed(
const isAllowed = await permitGenerationModule._isPrivateKeyAllowed(
privateKeyEncrypted,
githubContextOrganizationId,
githubContextRepositoryId
githubContextRepositoryId,
process.env
);

expect(result).toEqual(false);
expect(isAllowed).toEqual(false);
expect(spyConsoleLog).toHaveBeenCalledWith("Private key could not be decrypted");
});

Expand All @@ -201,21 +202,21 @@ describe("permit-generation-module.ts", () => {
const githubContextOrganizationId = 99;
const githubContextRepositoryId = 2;

const result = await permitGenerationModule._isPrivateKeyAllowed(
const isAllowed = await permitGenerationModule._isPrivateKeyAllowed(
privateKeyEncrypted,
githubContextOrganizationId,
githubContextRepositoryId
githubContextRepositoryId,
process.env
);

expect(result).toEqual(false);
expect(isAllowed).toEqual(false);
expect(spyConsoleLog).toHaveBeenCalledWith(
"Current organization/user id 99 is not allowed to use this private key"
);
});

it("Should return true if private key is used in allowed organization", async () => {
const permitGenerationModule = new PermitGenerationModule();
const spyConsoleLog = jest.spyOn(console, "log");

// format: "PRIVATE_KEY:GITHUB_ORGANIZATION_ID"
// encrypted value: "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80:1"
Expand All @@ -224,13 +225,14 @@ describe("permit-generation-module.ts", () => {
const githubContextOrganizationId = 1;
const githubContextRepositoryId = 2;

const result = await permitGenerationModule._isPrivateKeyAllowed(
const isAllowed = await permitGenerationModule._isPrivateKeyAllowed(
privateKeyEncrypted,
githubContextOrganizationId,
githubContextRepositoryId
githubContextRepositoryId,
process.env
);

expect(result).toEqual(true);
expect(isAllowed).toEqual(true);
});

it("Should return false if private key is used in unallowed organization and allowed repository", async () => {
Expand All @@ -244,13 +246,14 @@ describe("permit-generation-module.ts", () => {
const githubContextOrganizationId = 99;
const githubContextRepositoryId = 2;

const result = await permitGenerationModule._isPrivateKeyAllowed(
const isAllowed = await permitGenerationModule._isPrivateKeyAllowed(
privateKeyEncrypted,
githubContextOrganizationId,
githubContextRepositoryId
githubContextRepositoryId,
process.env
);

expect(result).toEqual(false);
expect(isAllowed).toEqual(false);
expect(spyConsoleLog).toHaveBeenCalledWith(
"Current organization/user id 99 and repository id 2 are not allowed to use this private key"
);
Expand All @@ -267,21 +270,21 @@ describe("permit-generation-module.ts", () => {
const githubContextOrganizationId = 1;
const githubContextRepositoryId = 99;

const result = await permitGenerationModule._isPrivateKeyAllowed(
const isAllowed = await permitGenerationModule._isPrivateKeyAllowed(
privateKeyEncrypted,
githubContextOrganizationId,
githubContextRepositoryId
githubContextRepositoryId,
process.env
);

expect(result).toEqual(false);
expect(isAllowed).toEqual(false);
expect(spyConsoleLog).toHaveBeenCalledWith(
"Current organization/user id 1 and repository id 99 are not allowed to use this private key"
);
});

it("Should return true if private key is used in allowed organization and repository", async () => {
const permitGenerationModule = new PermitGenerationModule();
const spyConsoleLog = jest.spyOn(console, "log");

// format: "PRIVATE_KEY:GITHUB_ORGANIZATION_ID:GITHUB_REPOSITORY_ID"
// encrypted value: "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80:1:2"
Expand All @@ -290,13 +293,14 @@ describe("permit-generation-module.ts", () => {
const githubContextOrganizationId = 1;
const githubContextRepositoryId = 2;

const result = await permitGenerationModule._isPrivateKeyAllowed(
const isAllowed = await permitGenerationModule._isPrivateKeyAllowed(
privateKeyEncrypted,
githubContextOrganizationId,
githubContextRepositoryId
githubContextRepositoryId,
process.env
);

expect(result).toEqual(true);
expect(isAllowed).toEqual(true);
});

it("Should return false if private key format is invalid", async () => {
Expand All @@ -310,13 +314,14 @@ describe("permit-generation-module.ts", () => {
const githubContextOrganizationId = 1;
const githubContextRepositoryId = 2;

const result = await permitGenerationModule._isPrivateKeyAllowed(
const isAllowed = await permitGenerationModule._isPrivateKeyAllowed(
privateKeyEncrypted,
githubContextOrganizationId,
githubContextRepositoryId
githubContextRepositoryId,
process.env
);

expect(result).toEqual(false);
expect(isAllowed).toEqual(false);
expect(spyConsoleLog).toHaveBeenCalledWith("Invalid private key format");
});
});
Expand Down

0 comments on commit 099a9e1

Please sign in to comment.