|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import type { Request, Response } from 'express'; |
| 3 | +import { ZodError } from 'zod'; |
| 4 | +import { |
| 5 | + registerUserSchema, |
| 6 | + STELLAR_PUBLIC_KEY_ERROR, |
| 7 | +} from '../src/validators/user.validator.js'; |
| 8 | +import { errorHandler } from '../src/middleware/error.middleware.js'; |
| 9 | + |
| 10 | +vi.mock('../src/logger.js', () => ({ |
| 11 | + default: { info: vi.fn(), error: vi.fn(), warn: vi.fn() }, |
| 12 | +})); |
| 13 | + |
| 14 | +const VALID_KEY = 'GD2XP6FNWL6IWULVMPNA2RV2T7GLCJHK3RH75GBCY7TSVIWDITJN4FXJ'; |
| 15 | + |
| 16 | +describe('User Validator', () => { |
| 17 | + describe('registerUserSchema', () => { |
| 18 | + it('should accept a well-formed Stellar public key', () => { |
| 19 | + const result = registerUserSchema.safeParse({ publicKey: VALID_KEY }); |
| 20 | + expect(result.success).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should reject a malformed public key', () => { |
| 24 | + const result = registerUserSchema.safeParse({ publicKey: 'not-a-stellar-key' }); |
| 25 | + |
| 26 | + expect(result.success).toBe(false); |
| 27 | + expect(result.error?.issues[0]?.path).toEqual(['publicKey']); |
| 28 | + expect(result.error?.issues[0]?.message).toBe(STELLAR_PUBLIC_KEY_ERROR); |
| 29 | + }); |
| 30 | + |
| 31 | + it.each([ |
| 32 | + ['too short', 'GD2XP6FNWL6IWULV'], |
| 33 | + ['too long', `${VALID_KEY}AAAA`], |
| 34 | + ['wrong version prefix', `S${VALID_KEY.slice(1)}`], |
| 35 | + ['lowercase characters', VALID_KEY.toLowerCase()], |
| 36 | + ['character outside the base32 alphabet', `${VALID_KEY.slice(0, 55)}1`], |
| 37 | + ['empty string', ''], |
| 38 | + ['whitespace padded', ` ${VALID_KEY} `], |
| 39 | + ])('should reject a key that is %s', (_label, publicKey) => { |
| 40 | + const result = registerUserSchema.safeParse({ publicKey }); |
| 41 | + |
| 42 | + expect(result.success).toBe(false); |
| 43 | + expect(result.error?.issues[0]?.message).toBe(STELLAR_PUBLIC_KEY_ERROR); |
| 44 | + }); |
| 45 | + |
| 46 | + it.each([ |
| 47 | + ['missing', undefined], |
| 48 | + ['a number', 12345], |
| 49 | + ['null', null], |
| 50 | + ])('should reject a publicKey that is %s', (_label, publicKey) => { |
| 51 | + const result = registerUserSchema.safeParse({ publicKey }); |
| 52 | + |
| 53 | + expect(result.success).toBe(false); |
| 54 | + expect(result.error?.issues[0]?.message).toBe( |
| 55 | + 'publicKey is required and must be a string', |
| 56 | + ); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('error response', () => { |
| 61 | + it('should surface a malformed key as a 400 with a descriptive message', () => { |
| 62 | + const error = registerUserSchema.safeParse({ publicKey: 'not-a-stellar-key' }).error; |
| 63 | + expect(error).toBeInstanceOf(ZodError); |
| 64 | + |
| 65 | + const res = { |
| 66 | + headersSent: false, |
| 67 | + status: vi.fn().mockReturnThis(), |
| 68 | + json: vi.fn().mockReturnThis(), |
| 69 | + }; |
| 70 | + |
| 71 | + errorHandler(error, {} as Request, res as unknown as Response, vi.fn()); |
| 72 | + |
| 73 | + expect(res.status).toHaveBeenCalledWith(400); |
| 74 | + expect(res.json).toHaveBeenCalledWith({ |
| 75 | + error: 'Validation Error', |
| 76 | + details: [{ path: 'publicKey', message: STELLAR_PUBLIC_KEY_ERROR }], |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments