Skip to content

Commit

Permalink
refactor: replace console.log with appropriate logger methods
Browse files Browse the repository at this point in the history
Replaced console.log with logger methods for consistent logging.
  • Loading branch information
gentlementlegen committed Oct 15, 2024
1 parent c56fdb3 commit 504eb5c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/parser/permit-generation-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,15 @@ export class PermitGenerationModule implements Module {
// parse decrypted private key
const privateKeyParsed = parseDecryptedPrivateKey(privateKeyDecrypted);
if (!privateKeyParsed.privateKey) {
console.log("Private key could not be decrypted");
logger.error("Private key could not be decrypted");
return false;
}

// private key + owner id
// Format: PRIVATE_KEY:GITHUB_OWNER_ID
if (privateKeyParsed.allowedOrganizationId && !privateKeyParsed.allowedRepositoryId) {
if (privateKeyParsed.allowedOrganizationId !== githubContextOwnerId) {
console.log(`Current organization/user id ${githubContextOwnerId} is not allowed to use this private key`);
logger.info(`Current organization/user id ${githubContextOwnerId} is not allowed to use this private key`);
return false;
}
return true;
Expand Down
50 changes: 27 additions & 23 deletions tests/parser/permit-generation-module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,35 +114,37 @@ describe("permit-generation-module.ts", () => {
it("Should not apply fees if PERMIT_FEE_RATE is empty", async () => {
process.env.PERMIT_FEE_RATE = "";
const permitGenerationModule = new PermitGenerationModule();
const spyConsoleLog = jest.spyOn(console, "log");
const spyConsoleLog = jest.spyOn(console, "info");
await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS, process.env);
expect(spyConsoleLog).toHaveBeenCalledWith("PERMIT_FEE_RATE is not set, skipping permit fee generation");
const logCallArgs = spyConsoleLog.mock.calls.map((call) => call[0]);
expect(logCallArgs[0]).toMatch(/.*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");
const spyConsoleLog = jest.spyOn(console, "info");
await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS, process.env);
expect(spyConsoleLog).toHaveBeenCalledWith("PERMIT_FEE_RATE is not set, skipping permit fee generation");
const logCallArgs = spyConsoleLog.mock.calls.map((call) => call[0]);
expect(logCallArgs[0]).toMatch(/.*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");
const spyConsoleLog = jest.spyOn(console, "info");
await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS, process.env);
expect(spyConsoleLog).toHaveBeenCalledWith(
"PERMIT_TREASURY_GITHUB_USERNAME is not set, skipping permit fee generation"
);
const logCallArgs = spyConsoleLog.mock.calls.map((call) => call[0]);
expect(logCallArgs[0]).toMatch(/.*PERMIT_TREASURY_GITHUB_USERNAME is not set, skipping permit fee generation/);
});

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");
const spyConsoleLog = jest.spyOn(console, "info");
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`
const logCallArgs = spyConsoleLog.mock.calls.map((call) => call[0]);
expect(logCallArgs[0]).toMatch(
new RegExp(`.*Token address ${DOLLAR_ADDRESS} is whitelisted to be fee free, skipping permit fee generation`)
);
});

Expand Down Expand Up @@ -171,7 +173,7 @@ describe("permit-generation-module.ts", () => {

it("Should return false if private key could not be decrypted", async () => {
const permitGenerationModule = new PermitGenerationModule();
const spyConsoleLog = jest.spyOn(console, "log");
const spyConsoleLog = jest.spyOn(console, "warn");

// format: "PRIVATE_KEY"
// encrypted value: ""
Expand All @@ -187,12 +189,13 @@ describe("permit-generation-module.ts", () => {
);

expect(isAllowed).toEqual(false);
expect(spyConsoleLog).toHaveBeenCalledWith("Private key could not be decrypted");
const logCallArgs = spyConsoleLog.mock.calls.map((call) => call[0]);
expect(logCallArgs[0]).toMatch(new RegExp(`.*Private key could not be decrypted`));
});

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

// format: "PRIVATE_KEY:GITHUB_ORGANIZATION_ID"
// encrypted value: "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80:1"
Expand All @@ -209,9 +212,8 @@ describe("permit-generation-module.ts", () => {
);

expect(isAllowed).toEqual(false);
expect(spyConsoleLog).toHaveBeenCalledWith(
"Current organization/user id 99 is not allowed to use this private key"
);
const logCallArgs = spyConsoleLog.mock.calls.map((call) => call[0]);
expect(logCallArgs[0]).toMatch(/.*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 () => {
Expand All @@ -234,9 +236,9 @@ describe("permit-generation-module.ts", () => {
expect(isAllowed).toEqual(true);
});

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

// format: "PRIVATE_KEY:GITHUB_ORGANIZATION_ID:GITHUB_REPOSITORY_ID"
// encrypted value: "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80:1:2"
Expand All @@ -253,8 +255,9 @@ describe("permit-generation-module.ts", () => {
);

expect(isAllowed).toEqual(false);
expect(spyConsoleLog).toHaveBeenCalledWith(
"Current organization/user id 99 and repository id 2 are not allowed to use this private key"
const logCallArgs = spyConsoleLog.mock.calls.map((call) => call[0]);
expect(logCallArgs[0]).toMatch(
new RegExp(`.*Current organization/user id 99 and repository id 2 are not allowed to use this private key`)
);
});

Expand All @@ -277,8 +280,9 @@ describe("permit-generation-module.ts", () => {
);

expect(isAllowed).toEqual(false);
expect(spyConsoleLog).toHaveBeenCalledWith(
"Current organization/user id 1 and repository id 99 are not allowed to use this private key"
const logCallArgs = spyConsoleLog.mock.calls.map((call) => call[0]);
expect(logCallArgs[0]).toMatch(
new RegExp(`.*Current organization/user id 1 and repository id 99 are not allowed to use this private key`)
);
});

Expand Down

0 comments on commit 504eb5c

Please sign in to comment.