|
| 1 | +import { validate } from 'class-validator'; |
| 2 | +import { UpdateProfileDto } from './update-profile.dto'; |
| 3 | + |
| 4 | +const VALID_PUBLIC_KEY = |
| 5 | + 'GAGD6MFNQ5IZQQQW6MOS6JWZALYR5P7ITCZP3NUQPNYLHWYBQVC6NNXB'; |
| 6 | + |
| 7 | +async function getValidationErrors(dto: object): Promise<string[]> { |
| 8 | + const errors = await validate(dto, { |
| 9 | + whitelist: true, |
| 10 | + forbidNonWhitelisted: false, |
| 11 | + }); |
| 12 | + return errors.flatMap((error) => Object.values(error.constraints ?? {})); |
| 13 | +} |
| 14 | + |
| 15 | +describe('UpdateProfileDto', () => { |
| 16 | + it('accepts a valid Stellar Ed25519 public key', async () => { |
| 17 | + const dto = new UpdateProfileDto(); |
| 18 | + dto.walletAddress = VALID_PUBLIC_KEY; |
| 19 | + |
| 20 | + expect(await getValidationErrors(dto)).toHaveLength(0); |
| 21 | + }); |
| 22 | + |
| 23 | + it.each([ |
| 24 | + 'not-a-stellar-key', |
| 25 | + 'SAGD6MFNQ5IZQQQW6MOS6JWZALYR5P7ITCZP3NUQPNYLHWYBQVC6NNXB', |
| 26 | + 'GAGD6MFNQ5IZQQQW6MOS6JWZALYR5P7ITCZP3NUQPNYLHWYBQVC6NNX', |
| 27 | + // Same length/prefix as the valid key, but with a bad checksum. |
| 28 | + 'GAGD6MFNQ5IZQQQW6MOS6JWZALYR5P7ITCZP3NUQPNYLHWYBQVC6NNXC', |
| 29 | + ])('rejects invalid Stellar public key %s', async (walletAddress) => { |
| 30 | + const dto = new UpdateProfileDto(); |
| 31 | + dto.walletAddress = walletAddress; |
| 32 | + |
| 33 | + const messages = await getValidationErrors(dto); |
| 34 | + expect(messages).toEqual( |
| 35 | + expect.arrayContaining([ |
| 36 | + expect.stringContaining('valid Stellar Ed25519 public key'), |
| 37 | + ]), |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it('keeps the field optional when it is omitted or null', async () => { |
| 42 | + const omitted = new UpdateProfileDto(); |
| 43 | + const cleared = new UpdateProfileDto(); |
| 44 | + cleared.walletAddress = null; |
| 45 | + |
| 46 | + expect(await getValidationErrors(omitted)).toHaveLength(0); |
| 47 | + expect(await getValidationErrors(cleared)).toHaveLength(0); |
| 48 | + }); |
| 49 | + |
| 50 | + it('rejects non-string values when the field is supplied', async () => { |
| 51 | + const dto = new UpdateProfileDto(); |
| 52 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 53 | + (dto as any).walletAddress = 123; |
| 54 | + |
| 55 | + expect(await getValidationErrors(dto)).not.toHaveLength(0); |
| 56 | + }); |
| 57 | +}); |
0 commit comments