From 1cbb2ea5515f5d3394f1b00dacab13e05b5018e8 Mon Sep 17 00:00:00 2001 From: 0age <37939117+0age@users.noreply.github.com> Date: Wed, 4 Dec 2024 21:11:56 -0800 Subject: [PATCH] minor health improvement --- README.md | 44 ++++++++++++++++++++++++++++++++++++ src/__tests__/routes.test.ts | 38 +++++++++++++++++++++++++++---- src/routes.ts | 2 ++ 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c3db640..9844775 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,50 @@ All compact operations require a valid session ID in the `x-session-id` header. - `"0"` if `allocatedBalance` >= `allocatableBalance` - `allocatableBalance - allocatedBalance` otherwise +## API Endpoints + +1. **Health Check** + + ```http + GET /health + ``` + + Example response: + + ```json + { + "status": "healthy", + "allocatorAddress": "0x1234567890123456789012345678901234567890", + "signingAddress": "0x9876543210987654321098765432109876543210", + "timestamp": "2024-03-07T12:00:00.000Z" + } + ``` + +2. **Get Session Payload** + + ```http + GET /session/:address + ``` + + Returns an EIP-4361 payload for signing. Example response: + + ```json + { + "payload": { + "domain": "localhost:3000", + "address": "0x...", + "uri": "http://localhost:3000", + "statement": "Sign in to Smallocator", + "version": "1", + "chainId": 1, + "nonce": "unique_nonce", + "issuedAt": "2024-12-03T12:00:00Z", + "expirationTime": "2024-12-03T13:00:00Z", + "resources": ["http://localhost:3000/resources"] + } + } + ``` + ## Development ### Prerequisites diff --git a/src/__tests__/routes.test.ts b/src/__tests__/routes.test.ts index d3d35c0..3c35e29 100644 --- a/src/__tests__/routes.test.ts +++ b/src/__tests__/routes.test.ts @@ -60,17 +60,45 @@ describe('API Routes', () => { }); describe('GET /health', () => { - it('should return server health status', async () => { + it('should return health status and addresses', async () => { const response = await server.inject({ method: 'GET', url: '/health', }); expect(response.statusCode).toBe(200); - const result = JSON.parse(response.payload); - expect(result.status).toBe('healthy'); - expect(result.signingAddress).toBe(process.env.SIGNING_ADDRESS); - expect(result.allocatorAddress).toBe(process.env.ALLOCATOR_ADDRESS); + const body = JSON.parse(response.payload); + + expect(body).toHaveProperty('status', 'healthy'); + expect(body).toHaveProperty('allocatorAddress'); + expect(body).toHaveProperty('signingAddress'); + expect(body).toHaveProperty('timestamp'); + + // Verify timestamp is a valid ISO 8601 date + expect(new Date(body.timestamp).toISOString()).toBe(body.timestamp); + }); + + it('should fail if environment variables are not set', async () => { + // Store original env vars + const originalAllocator = process.env.ALLOCATOR_ADDRESS; + const originalSigning = process.env.SIGNING_ADDRESS; + + // Unset env vars + delete process.env.ALLOCATOR_ADDRESS; + delete process.env.SIGNING_ADDRESS; + + try { + const response = await server.inject({ + method: 'GET', + url: '/health', + }); + + expect(response.statusCode).toBe(500); + } finally { + // Restore env vars + process.env.ALLOCATOR_ADDRESS = originalAllocator; + process.env.SIGNING_ADDRESS = originalSigning; + } }); }); diff --git a/src/routes.ts b/src/routes.ts index 1a2c958..509236c 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -101,6 +101,7 @@ export async function setupRoutes(server: FastifyInstance): Promise { status: string; allocatorAddress: string; signingAddress: string; + timestamp: string; }> => { if (!process.env.ALLOCATOR_ADDRESS || !process.env.SIGNING_ADDRESS) { throw new Error('Required environment variables are not set'); @@ -109,6 +110,7 @@ export async function setupRoutes(server: FastifyInstance): Promise { status: 'healthy', allocatorAddress: process.env.ALLOCATOR_ADDRESS, signingAddress: process.env.SIGNING_ADDRESS, + timestamp: new Date().toISOString(), }; } );