Skip to content

Commit 7ff0712

Browse files
committed
new header for entities prod or dev mode
1 parent bc1979b commit 7ff0712

1 file changed

Lines changed: 103 additions & 1 deletion

File tree

tests/unit/client.test.js

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,46 @@ describe('createClientFromRequest', () => {
254254
};
255255

256256
const client = createClientFromRequest(mockRequest);
257-
257+
258+
expect(client).toBeDefined();
259+
const config = client.getConfig();
260+
expect(config.appId).toBe('test-app-id');
261+
});
262+
263+
test('should propagate Base44-Use-Staging-DB header when present', () => {
264+
const mockRequest = {
265+
headers: {
266+
get: (name) => {
267+
const headers = {
268+
'Base44-App-Id': 'test-app-id',
269+
'Base44-Use-Staging-DB': 'true'
270+
};
271+
return headers[name] || null;
272+
}
273+
}
274+
};
275+
276+
const client = createClientFromRequest(mockRequest);
277+
278+
expect(client).toBeDefined();
279+
const config = client.getConfig();
280+
expect(config.appId).toBe('test-app-id');
281+
});
282+
283+
test('should work without Base44-Use-Staging-DB header', () => {
284+
const mockRequest = {
285+
headers: {
286+
get: (name) => {
287+
const headers = {
288+
'Base44-App-Id': 'test-app-id'
289+
};
290+
return headers[name] || null;
291+
}
292+
}
293+
};
294+
295+
const client = createClientFromRequest(mockRequest);
296+
258297
expect(client).toBeDefined();
259298
const config = client.getConfig();
260299
expect(config.appId).toBe('test-app-id');
@@ -551,4 +590,67 @@ describe('Service Role Authorization Headers', () => {
551590
expect(scope.isDone()).toBe(true);
552591
});
553592

593+
test('should propagate Base44-Use-Staging-DB header in API requests when created from request', async () => {
594+
const mockRequest = {
595+
headers: {
596+
get: (name) => {
597+
const headers = {
598+
'Authorization': 'Bearer user-token-123',
599+
'Base44-App-Id': appId,
600+
'Base44-Api-Url': serverUrl,
601+
'Base44-Use-Staging-DB': 'true'
602+
};
603+
return headers[name] || null;
604+
}
605+
}
606+
};
607+
608+
const client = createClientFromRequest(mockRequest);
609+
610+
// Mock entities request and verify Base44-Use-Staging-DB header is present
611+
scope.get(`/api/apps/${appId}/entities/Todo`)
612+
.matchHeader('Base44-Use-Staging-DB', 'true')
613+
.matchHeader('Authorization', 'Bearer user-token-123')
614+
.reply(200, { items: [], total: 0 });
615+
616+
// Make request
617+
await client.entities.Todo.list();
618+
619+
// Verify all mocks were called (including header match)
620+
expect(scope.isDone()).toBe(true);
621+
});
622+
623+
test('should propagate Base44-Use-Staging-DB header in service role API requests', async () => {
624+
const mockRequest = {
625+
headers: {
626+
get: (name) => {
627+
const headers = {
628+
'Base44-Service-Authorization': 'Bearer service-token-123',
629+
'Base44-App-Id': appId,
630+
'Base44-Api-Url': serverUrl,
631+
'Base44-Use-Staging-DB': 'true'
632+
};
633+
return headers[name] || null;
634+
}
635+
}
636+
};
637+
638+
const client = createClientFromRequest(mockRequest);
639+
640+
// Mock service role entities request and verify Base44-Use-Staging-DB header is present
641+
scope.get(`/api/apps/${appId}/entities/User/123`)
642+
.matchHeader('Base44-Use-Staging-DB', 'true')
643+
.matchHeader('Authorization', 'Bearer service-token-123')
644+
.reply(200, { id: '123', name: 'Test User' });
645+
646+
// Make request using service role
647+
const result = await client.asServiceRole.entities.User.get('123');
648+
649+
// Verify response
650+
expect(result.id).toBe('123');
651+
652+
// Verify all mocks were called (including header match)
653+
expect(scope.isDone()).toBe(true);
654+
});
655+
554656
});

0 commit comments

Comments
 (0)