forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar-address.validator.spec.ts
More file actions
54 lines (45 loc) 路 1.79 KB
/
Copy pathstellar-address.validator.spec.ts
File metadata and controls
54 lines (45 loc) 路 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { validate } from 'class-validator';
import { IsOptional } from 'class-validator';
// Mock @stellar/stellar-sdk before importing the validator
jest.mock('@stellar/stellar-sdk', () => ({
StrKey: {
isValidEd25519PublicKey: (value: string) =>
typeof value === 'string' && value.length === 55 && /^G[A-Z2-7]{54}$/.test(value),
},
}));
import { IsStellarAddress } from './stellar-address.validator';
class TestDto {
@IsOptional()
@IsStellarAddress()
authorAddress?: string;
}
const VALID_ADDRESS = 'GAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN';
describe('IsStellarAddress', () => {
it('passes with a valid Stellar public key', async () => {
const dto = Object.assign(new TestDto(), { authorAddress: VALID_ADDRESS });
const errors = await validate(dto);
expect(errors).toHaveLength(0);
});
it('passes when authorAddress is absent (anonymous post)', async () => {
const errors = await validate(new TestDto());
expect(errors).toHaveLength(0);
});
it('fails with an invalid string', async () => {
const dto = Object.assign(new TestDto(), { authorAddress: 'not-a-stellar-key' });
const errors = await validate(dto);
expect(errors).toHaveLength(1);
expect(errors[0].constraints?.isStellarAddress).toBe(
'authorAddress must be a valid Stellar public key',
);
});
it('fails with an address that does not start with G', async () => {
const dto = Object.assign(new TestDto(), { authorAddress: 'XAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN' });
const errors = await validate(dto);
expect(errors).toHaveLength(1);
});
it('fails with an empty string', async () => {
const dto = Object.assign(new TestDto(), { authorAddress: '' });
const errors = await validate(dto);
expect(errors).toHaveLength(1);
});
});