forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidentity-client.test.ts
More file actions
120 lines (104 loc) · 3.83 KB
/
Copy pathidentity-client.test.ts
File metadata and controls
120 lines (104 loc) · 3.83 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { IdentityClient } from '../src/clients/identity-client';
import { LilyValidationError } from '../src/errors/sdk-error';
import type { HttpClient, HttpResponse } from '../src/http/types';
import type { IdentityProfile, ResolveIdentityRequest, VerifyIdentityRequest, VerificationResult } from '../src/models';
function createMockHttpClient(responseData: unknown = {}): HttpClient {
return {
request: vi.fn().mockResolvedValue({
status: 200,
headers: new Headers(),
data: responseData,
} as HttpResponse),
};
}
const mockIdentity: IdentityProfile = {
id: 'id-1',
agentId: 'agent-1',
displayName: 'Test Identity',
stellarAddress: 'GABC...',
domain: 'example.com',
status: 'active',
verificationLevel: 'enhanced',
createdAt: '2024-01-01T00:00:00Z',
updatedAt: '2024-01-01T00:00:00Z',
};
const mockVerification: VerificationResult = {
identityId: 'id-1',
verified: true,
verifiedAt: '2024-01-01T01:00:00Z',
};
describe('IdentityClient', () => {
let httpClient: HttpClient;
let client: IdentityClient;
beforeEach(() => {
httpClient = createMockHttpClient();
client = new IdentityClient(httpClient);
});
describe('resolve', () => {
it('sends POST /v1/identity/resolve with the input body and returns the profile', async () => {
const input: ResolveIdentityRequest = {
agentId: 'agent-1',
};
vi.mocked(httpClient.request).mockResolvedValueOnce({
status: 200,
headers: new Headers(),
data: mockIdentity,
} as HttpResponse);
const result = await client.resolve(input);
expect(result).toEqual(mockIdentity);
expect(httpClient.request).toHaveBeenCalledWith({
method: 'POST',
path: '/v1/identity/resolve',
body: input,
});
});
it('throws LilyValidationError when no resolver key is provided', async () => {
await expect(client.resolve({})).rejects.toBeInstanceOf(LilyValidationError);
expect(httpClient.request).not.toHaveBeenCalled();
});
it('throws LilyValidationError when resolver key is empty', async () => {
await expect(client.resolve({ agentId: ' ' })).rejects.toBeInstanceOf(LilyValidationError);
expect(httpClient.request).not.toHaveBeenCalled();
});
it('throws LilyValidationError when more than one resolver key is provided', async () => {
await expect(
client.resolve({ agentId: 'agent-1', stellarAddress: 'GABC...' }),
).rejects.toBeInstanceOf(LilyValidationError);
expect(httpClient.request).not.toHaveBeenCalled();
});
});
describe('verify', () => {
it('sends POST /v1/identity/verify with the input body and returns the result', async () => {
const input: VerifyIdentityRequest = {
identityId: 'id-1',
challenge: 'test-challenge',
signature: 'test-signature',
};
vi.mocked(httpClient.request).mockResolvedValueOnce({
status: 200,
headers: new Headers(),
data: mockVerification,
} as HttpResponse);
const result = await client.verify(input);
expect(result.verified).toBe(true);
expect(httpClient.request).toHaveBeenCalledWith({
method: 'POST',
path: '/v1/identity/verify',
body: input,
});
});
it('throws LilyValidationError for empty required fields', async () => {
await expect(
client.verify({ identityId: '', challenge: 'c', signature: 's' }),
).rejects.toBeInstanceOf(LilyValidationError);
expect(httpClient.request).not.toHaveBeenCalled();
});
});
describe('get', () => {
it('throws LilyValidationError for empty identityId', async () => {
await expect(client.get(' ')).rejects.toBeInstanceOf(LilyValidationError);
expect(httpClient.request).not.toHaveBeenCalled();
});
});
});