forked from PalisadoesFoundation/talawa-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9737ca1
commit fa3a3ab
Showing
4 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { hashEmail } from "../../src/utilities/hashEmail"; | ||
import { describe, it, expect, vi } from "vitest"; | ||
import { compareHashedEmails, hashEmail } from "../../src/utilities/hashEmail"; | ||
import { setHashPepper } from "../../setup"; | ||
|
||
describe("hashingModule", () => { | ||
|
@@ -50,5 +50,76 @@ describe("hashingModule", () => { | |
process.env.HASH_PEPPER = originalPepper; | ||
} | ||
}); | ||
it("should throw an error for an invalid email format", () => { | ||
const invalidEmails = [ | ||
"plainaddress", | ||
"missing@domain", | ||
"@missinglocal.com", | ||
"[email protected]", | ||
]; | ||
|
||
invalidEmails.forEach((email) => { | ||
expect(() => hashEmail(email)).toThrow("Invalid email format"); | ||
}); | ||
}); | ||
|
||
it("should throw an error if HASH_PEPPER is missing", () => { | ||
const originalPepper = process.env.HASH_PEPPER; | ||
delete process.env.HASH_PEPPER; | ||
|
||
expect(() => hashEmail("[email protected]")).toThrow( | ||
"Missing HASH_PEPPER environment variable required for secure email hashing", | ||
); | ||
|
||
process.env.HASH_PEPPER = originalPepper; | ||
}); | ||
|
||
it("should throw an error if HASH_PEPPER is shorter than 32 characters", () => { | ||
const originalPepper = process.env.HASH_PEPPER; | ||
process.env.HASH_PEPPER = "short_pepper"; | ||
|
||
expect(() => hashEmail("[email protected]")).toThrow( | ||
"HASH_PEPPER must be at least 32 characters long", | ||
); | ||
|
||
process.env.HASH_PEPPER = originalPepper; | ||
}); | ||
}); | ||
|
||
describe("compareHashedEmails function error handling", () => { | ||
it("should return false for invalid hashed email formats", () => { | ||
const validHash = "a".repeat(64); | ||
const invalidHashes = [ | ||
"short", | ||
"invalid_characters_!@#", | ||
"", | ||
null, | ||
undefined, | ||
]; | ||
|
||
invalidHashes.forEach((invalidHash) => { | ||
expect( | ||
compareHashedEmails(invalidHash as unknown as string, validHash), | ||
).toBe(false); | ||
expect( | ||
compareHashedEmails(validHash, invalidHash as unknown as string), | ||
).toBe(false); | ||
}); | ||
}); | ||
|
||
it("should log an error and return false if crypto.timingSafeEqual fails due to invalid hex encoding", () => { | ||
const invalidHash = "z".repeat(64); // deliberately invalid hex | ||
let result; | ||
try { | ||
result = compareHashedEmails(invalidHash, invalidHash); | ||
} catch (error) { | ||
expect(result).toBe(false); | ||
if (error instanceof Error) { | ||
expect(error.message).toBe( | ||
"Failed to compare hashes, likely due to invalid hex encoding", | ||
); | ||
} | ||
} | ||
}); | ||
}); | ||
}); |