|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `client.keys` / `client.shareLinks` / `client.security` — #3563 PR-3 gap |
| 5 | + * closures. Before these surfaces, the SDK had no way to mint an API key, |
| 6 | + * manage share links, or resolve suggested audience bindings; all three |
| 7 | + * domains were `gap` in the route ledger. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect, vi } from 'vitest'; |
| 11 | +import { ObjectStackClient } from './index'; |
| 12 | + |
| 13 | +function createMockClient(body: any, status = 200) { |
| 14 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 15 | + ok: status >= 200 && status < 300, |
| 16 | + status, |
| 17 | + statusText: status === 200 ? 'OK' : 'Error', |
| 18 | + json: async () => body, |
| 19 | + headers: new Headers() |
| 20 | + }); |
| 21 | + const client = new ObjectStackClient({ |
| 22 | + baseUrl: 'http://localhost:3000', |
| 23 | + fetch: fetchMock |
| 24 | + }); |
| 25 | + return { client, fetchMock }; |
| 26 | +} |
| 27 | + |
| 28 | +describe('client.keys', () => { |
| 29 | + it('create POSTs name + expires_at to /api/v1/keys and returns the one-time secret', async () => { |
| 30 | + const { client, fetchMock } = createMockClient({ |
| 31 | + success: true, |
| 32 | + data: { id: 'k1', name: 'CI key', prefix: 'osk_abc', key: 'osk_abc.RAW', expires_at: '2027-01-01T00:00:00.000Z' }, |
| 33 | + }); |
| 34 | + |
| 35 | + const key = await client.keys.create({ name: 'CI key', expiresAt: '2027-01-01T00:00:00.000Z' }); |
| 36 | + |
| 37 | + const [url, init] = fetchMock.mock.calls[0]; |
| 38 | + expect(String(url)).toBe('http://localhost:3000/api/v1/keys'); |
| 39 | + expect(init.method).toBe('POST'); |
| 40 | + expect(JSON.parse(init.body)).toEqual({ name: 'CI key', expires_at: '2027-01-01T00:00:00.000Z' }); |
| 41 | + expect(key.key).toBe('osk_abc.RAW'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('create sends an empty body object when no options are given', async () => { |
| 45 | + const { client, fetchMock } = createMockClient({ success: true, data: { id: 'k1', key: 'raw' } }); |
| 46 | + await client.keys.create(); |
| 47 | + expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({}); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe('client.shareLinks', () => { |
| 52 | + it('create POSTs object + recordId + options to /api/v1/share-links', async () => { |
| 53 | + const { client, fetchMock } = createMockClient({ success: true, data: { id: 'sl1', token: 'tok_1' } }); |
| 54 | + |
| 55 | + const link = await client.shareLinks.create('crm_account', 'acc_1', { |
| 56 | + permission: 'view', |
| 57 | + password: 'hunter2', |
| 58 | + label: 'For the auditor', |
| 59 | + }); |
| 60 | + |
| 61 | + const [url, init] = fetchMock.mock.calls[0]; |
| 62 | + expect(String(url)).toBe('http://localhost:3000/api/v1/share-links'); |
| 63 | + expect(JSON.parse(init.body)).toEqual({ |
| 64 | + object: 'crm_account', |
| 65 | + recordId: 'acc_1', |
| 66 | + permission: 'view', |
| 67 | + password: 'hunter2', |
| 68 | + label: 'For the auditor', |
| 69 | + }); |
| 70 | + expect(link.token).toBe('tok_1'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('list builds the query string and omits empty filters', async () => { |
| 74 | + const { client, fetchMock } = createMockClient({ success: true, data: [] }); |
| 75 | + await client.shareLinks.list({ object: 'crm_account', includeRevoked: true }); |
| 76 | + expect(String(fetchMock.mock.calls[0][0])).toBe( |
| 77 | + 'http://localhost:3000/api/v1/share-links?object=crm_account&includeRevoked=true', |
| 78 | + ); |
| 79 | + |
| 80 | + await client.shareLinks.list(); |
| 81 | + expect(String(fetchMock.mock.calls[1][0])).toBe('http://localhost:3000/api/v1/share-links'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('revoke DELETEs by id or token, URL-encoded', async () => { |
| 85 | + const { client, fetchMock } = createMockClient({ success: true, data: { ok: true } }); |
| 86 | + await client.shareLinks.revoke('tok/with slash'); |
| 87 | + const [url, init] = fetchMock.mock.calls[0]; |
| 88 | + expect(String(url)).toBe('http://localhost:3000/api/v1/share-links/tok%2Fwith%20slash'); |
| 89 | + expect(init.method).toBe('DELETE'); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('client.security.suggestedBindings', () => { |
| 94 | + it('list GETs /api/v1/security/suggested-bindings with optional filters', async () => { |
| 95 | + const { client, fetchMock } = createMockClient({ success: true, data: { suggestions: [] } }); |
| 96 | + await client.security.suggestedBindings.list({ status: 'pending', packageId: 'com.acme.crm' }); |
| 97 | + expect(String(fetchMock.mock.calls[0][0])).toBe( |
| 98 | + 'http://localhost:3000/api/v1/security/suggested-bindings?status=pending&packageId=com.acme.crm', |
| 99 | + ); |
| 100 | + |
| 101 | + await client.security.suggestedBindings.list(); |
| 102 | + expect(String(fetchMock.mock.calls[1][0])).toBe( |
| 103 | + 'http://localhost:3000/api/v1/security/suggested-bindings', |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it('confirm and dismiss POST to the id-scoped subroutes', async () => { |
| 108 | + const { client, fetchMock } = createMockClient({ success: true, data: { ok: true } }); |
| 109 | + await client.security.suggestedBindings.confirm('sug_1'); |
| 110 | + await client.security.suggestedBindings.dismiss('sug_2'); |
| 111 | + expect(String(fetchMock.mock.calls[0][0])).toBe( |
| 112 | + 'http://localhost:3000/api/v1/security/suggested-bindings/sug_1/confirm', |
| 113 | + ); |
| 114 | + expect(fetchMock.mock.calls[0][1].method).toBe('POST'); |
| 115 | + expect(String(fetchMock.mock.calls[1][0])).toBe( |
| 116 | + 'http://localhost:3000/api/v1/security/suggested-bindings/sug_2/dismiss', |
| 117 | + ); |
| 118 | + }); |
| 119 | +}); |
0 commit comments