From 1f06eb4e76fdd2c0708c206f32b98eed31a8eaa0 Mon Sep 17 00:00:00 2001 From: "Abdulmalik A." Date: Thu, 23 Jul 2026 16:26:08 +0100 Subject: [PATCH 1/2] feat(auth): add unit tests for challenge and token-blacklist services (Issue #103) --- src/core/auth/challenge.service.spec.ts | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/core/auth/challenge.service.spec.ts diff --git a/src/core/auth/challenge.service.spec.ts b/src/core/auth/challenge.service.spec.ts new file mode 100644 index 0000000..92b5a3b --- /dev/null +++ b/src/core/auth/challenge.service.spec.ts @@ -0,0 +1,54 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { ChallengeService } from "./challenge.service"; + +describe("ChallengeService", () => { + let service: ChallengeService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ChallengeService], + }).compile(); + service = module.get(ChallengeService); + }); + + it("should be defined", () => { + expect(service).toBeDefined(); + }); + + describe("issueChallengeForAddress", () => { + it("should return a non-empty challenge message", () => { + const address = "0x1234567890abcdef1234567890abcdef12345678"; + const message = service.issueChallengeForAddress(address); + expect(message).toBeDefined(); + expect(typeof message).toBe("string"); + expect(message.length).toBeGreaterThan(0); + }); + + it("should generate unique messages on each call", () => { + const address = "0x1234567890abcdef1234567890abcdef12345678"; + const msg1 = service.issueChallengeForAddress(address); + const msg2 = service.issueChallengeForAddress(address); + expect(msg1).not.toBe(msg2); + }); + + it("should include a timestamp in the challenge", () => { + const address = "0x1234567890abcdef1234567890abcdef12345678"; + const message = service.issueChallengeForAddress(address); + expect(message).toMatch(/\d{4}-\d{2}-\d{2}T/); + }); + }); + + describe("getChallenge", () => { + it("should return null for an unknown challenge ID", async () => { + const result = await service.getChallenge("nonexistent-id"); + expect(result).toBeNull(); + }); + }); + + describe("consumeChallenge", () => { + it("should return null when consuming a nonexistent challenge", async () => { + const result = await service.consumeChallenge("nonexistent"); + expect(result).toBeNull(); + }); + }); +}); \ No newline at end of file From a63b98a5768944e7d7f42795fc5d5eb7b0b16bf8 Mon Sep 17 00:00:00 2001 From: "Abdulmalik A." Date: Thu, 23 Jul 2026 16:26:10 +0100 Subject: [PATCH 2/2] feat(auth): add unit tests for challenge and token-blacklist services (Issue #103) --- src/core/auth/token-blacklist.service.spec.ts | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/core/auth/token-blacklist.service.spec.ts diff --git a/src/core/auth/token-blacklist.service.spec.ts b/src/core/auth/token-blacklist.service.spec.ts new file mode 100644 index 0000000..e7eafad --- /dev/null +++ b/src/core/auth/token-blacklist.service.spec.ts @@ -0,0 +1,44 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { TokenBlacklistService } from "./token-blacklist.service"; + +describe("TokenBlacklistService", () => { + let service: TokenBlacklistService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [TokenBlacklistService], + }).compile(); + service = module.get(TokenBlacklistService); + jest.clearAllMocks(); + }); + + afterEach(() => { jest.restoreAllMocks(); }); + + it("should be defined", () => { + expect(service).toBeDefined(); + }); + + describe("revoke", () => { + it("should revoke a token by jti", () => { + const futureMs = Date.now() + 3600000; + service.revoke("jti-1", futureMs); + expect(service.isRevoked("jti-1")).toBe(true); + }); + }); + + describe("isRevoked", () => { + it("should return false for an unknown jti", () => { + expect(service.isRevoked("unknown-jti")).toBe(false); + }); + + it("should return true for a revoked jti", () => { + service.revoke("jti-2", Date.now() + 3600000); + expect(service.isRevoked("jti-2")).toBe(true); + }); + + it("should return false for an expired revoke entry", () => { + service.revoke("jti-expired", Date.now() - 1000); + expect(service.isRevoked("jti-expired")).toBe(false); + }); + }); +}); \ No newline at end of file