Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/core/auth/challenge.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(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();
});
});
});
44 changes: 44 additions & 0 deletions src/core/auth/token-blacklist.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(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);
});
});
});
Loading