Skip to content

Commit c3a1d6f

Browse files
test: add comprehensive unit tests for service role feature
- Add tests for serviceToken parameter in createClient - Add tests for createClientFromRequest function with various header scenarios - Add tests for asServiceRole API functionality and module separation - Test edge cases including malformed headers and missing parameters - All 13 client tests now pass including service role functionality Co-authored-by: Netanel Gilad <netanelgilad@users.noreply.github.com>
1 parent bc9a374 commit c3a1d6f

1 file changed

Lines changed: 231 additions & 1 deletion

File tree

tests/unit/client.test.js

Lines changed: 231 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createClient } from '../../src/index.ts';
1+
import { createClient, createClientFromRequest } from '../../src/index.ts';
22
import { describe, test, expect } from 'vitest';
33

44
describe('Client Creation', () => {
@@ -33,4 +33,234 @@ describe('Client Creation', () => {
3333
expect(config.serverUrl).toBe('https://custom-server.com');
3434
expect(config.requiresAuth).toBe(true);
3535
});
36+
37+
test('should create a client with service token', () => {
38+
const client = createClient({
39+
appId: 'test-app-id',
40+
serviceToken: 'service-token-123',
41+
});
42+
43+
expect(client).toBeDefined();
44+
expect(client.entities).toBeDefined();
45+
expect(client.integrations).toBeDefined();
46+
expect(client.auth).toBeDefined();
47+
expect(client.asServiceRole).toBeDefined();
48+
expect(client.asServiceRole.entities).toBeDefined();
49+
expect(client.asServiceRole.integrations).toBeDefined();
50+
expect(client.asServiceRole.functions).toBeDefined();
51+
// Service role should not have auth module
52+
expect(client.asServiceRole.auth).toBeUndefined();
53+
});
54+
55+
test('should create a client with both user token and service token', () => {
56+
const client = createClient({
57+
appId: 'test-app-id',
58+
token: 'user-token-123',
59+
serviceToken: 'service-token-123',
60+
requiresAuth: true,
61+
});
62+
63+
expect(client).toBeDefined();
64+
expect(client.entities).toBeDefined();
65+
expect(client.integrations).toBeDefined();
66+
expect(client.auth).toBeDefined();
67+
expect(client.asServiceRole).toBeDefined();
68+
expect(client.asServiceRole.entities).toBeDefined();
69+
expect(client.asServiceRole.integrations).toBeDefined();
70+
expect(client.asServiceRole.functions).toBeDefined();
71+
expect(client.asServiceRole.auth).toBeUndefined();
72+
});
73+
});
74+
75+
describe('createClientFromRequest', () => {
76+
test('should create client from request with all headers', () => {
77+
const mockRequest = {
78+
headers: {
79+
get: (name) => {
80+
const headers = {
81+
'Authorization': 'Bearer user-token-123',
82+
'Base44-Service-Authorization': 'Bearer service-token-123',
83+
'Base44-App-Id': 'test-app-id',
84+
'Base44-Api-Url': 'https://custom-server.com'
85+
};
86+
return headers[name] || null;
87+
}
88+
}
89+
};
90+
91+
const client = createClientFromRequest(mockRequest);
92+
93+
expect(client).toBeDefined();
94+
expect(client.entities).toBeDefined();
95+
expect(client.integrations).toBeDefined();
96+
expect(client.auth).toBeDefined();
97+
expect(client.asServiceRole).toBeDefined();
98+
99+
const config = client.getConfig();
100+
expect(config.appId).toBe('test-app-id');
101+
expect(config.serverUrl).toBe('https://custom-server.com');
102+
});
103+
104+
test('should create client from request with minimal headers', () => {
105+
const mockRequest = {
106+
headers: {
107+
get: (name) => {
108+
const headers = {
109+
'Base44-App-Id': 'minimal-app-id'
110+
};
111+
return headers[name] || null;
112+
}
113+
}
114+
};
115+
116+
const client = createClientFromRequest(mockRequest);
117+
118+
expect(client).toBeDefined();
119+
const config = client.getConfig();
120+
expect(config.appId).toBe('minimal-app-id');
121+
expect(config.serverUrl).toBe('https://base44.app'); // Default value
122+
});
123+
124+
test('should create client with only user token', () => {
125+
const mockRequest = {
126+
headers: {
127+
get: (name) => {
128+
const headers = {
129+
'Authorization': 'Bearer user-only-token',
130+
'Base44-App-Id': 'user-app-id'
131+
};
132+
return headers[name] || null;
133+
}
134+
}
135+
};
136+
137+
const client = createClientFromRequest(mockRequest);
138+
139+
expect(client).toBeDefined();
140+
expect(client.auth).toBeDefined();
141+
expect(client.asServiceRole).toBeDefined();
142+
});
143+
144+
test('should create client with only service token', () => {
145+
const mockRequest = {
146+
headers: {
147+
get: (name) => {
148+
const headers = {
149+
'Base44-Service-Authorization': 'Bearer service-only-token',
150+
'Base44-App-Id': 'service-app-id'
151+
};
152+
return headers[name] || null;
153+
}
154+
}
155+
};
156+
157+
const client = createClientFromRequest(mockRequest);
158+
159+
expect(client).toBeDefined();
160+
expect(client.auth).toBeDefined();
161+
expect(client.asServiceRole).toBeDefined();
162+
});
163+
164+
test('should throw error when Base44-App-Id header is missing', () => {
165+
const mockRequest = {
166+
headers: {
167+
get: (name) => {
168+
const headers = {
169+
'Authorization': 'Bearer some-token'
170+
};
171+
return headers[name] || null;
172+
}
173+
}
174+
};
175+
176+
expect(() => createClientFromRequest(mockRequest)).toThrow(
177+
'Base44-App-Id header is required, but is was not found on the request'
178+
);
179+
});
180+
181+
test('should handle malformed authorization headers gracefully', () => {
182+
const mockRequest = {
183+
headers: {
184+
get: (name) => {
185+
const headers = {
186+
'Authorization': 'InvalidFormat',
187+
'Base44-Service-Authorization': 'AlsoInvalid',
188+
'Base44-App-Id': 'test-app-id'
189+
};
190+
return headers[name] || null;
191+
}
192+
}
193+
};
194+
195+
const client = createClientFromRequest(mockRequest);
196+
197+
expect(client).toBeDefined();
198+
// Client should still be created even with malformed headers
199+
expect(client.entities).toBeDefined();
200+
expect(client.asServiceRole).toBeDefined();
201+
});
202+
203+
test('should handle empty authorization headers', () => {
204+
const mockRequest = {
205+
headers: {
206+
get: (name) => {
207+
const headers = {
208+
'Authorization': '',
209+
'Base44-Service-Authorization': '',
210+
'Base44-App-Id': 'test-app-id'
211+
};
212+
return headers[name] || null;
213+
}
214+
}
215+
};
216+
217+
const client = createClientFromRequest(mockRequest);
218+
219+
expect(client).toBeDefined();
220+
expect(client.entities).toBeDefined();
221+
expect(client.asServiceRole).toBeDefined();
222+
});
223+
});
224+
225+
describe('Service Role API', () => {
226+
test('should have separate service role modules', () => {
227+
const client = createClient({
228+
appId: 'test-app-id',
229+
serviceToken: 'service-token-123',
230+
});
231+
232+
// User modules should exist
233+
expect(client.entities).toBeDefined();
234+
expect(client.integrations).toBeDefined();
235+
expect(client.auth).toBeDefined();
236+
expect(client.functions).toBeDefined();
237+
238+
// Service role modules should exist
239+
expect(client.asServiceRole).toBeDefined();
240+
expect(client.asServiceRole.entities).toBeDefined();
241+
expect(client.asServiceRole.integrations).toBeDefined();
242+
expect(client.asServiceRole.functions).toBeDefined();
243+
244+
// Service role should NOT have auth module
245+
expect(client.asServiceRole.auth).toBeUndefined();
246+
247+
// They should be different instances
248+
expect(client.entities).not.toBe(client.asServiceRole.entities);
249+
expect(client.integrations).not.toBe(client.asServiceRole.integrations);
250+
expect(client.functions).not.toBe(client.asServiceRole.functions);
251+
});
252+
253+
test('should work without service token', () => {
254+
const client = createClient({
255+
appId: 'test-app-id',
256+
token: 'user-token-123',
257+
});
258+
259+
// Service role should still exist but without token
260+
expect(client.asServiceRole).toBeDefined();
261+
expect(client.asServiceRole.entities).toBeDefined();
262+
expect(client.asServiceRole.integrations).toBeDefined();
263+
expect(client.asServiceRole.functions).toBeDefined();
264+
expect(client.asServiceRole.auth).toBeUndefined();
265+
});
36266
});

0 commit comments

Comments
 (0)