@selvajs/supabase-provider 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +271 -0
- package/dist/auth/SupabaseAuthProvider.d.ts +81 -0
- package/dist/auth/SupabaseAuthProvider.d.ts.map +1 -0
- package/dist/auth/SupabaseAuthProvider.js +383 -0
- package/dist/auth/SupabaseAuthProvider.js.map +1 -0
- package/dist/auth/__tests__/conformance.test.d.ts +2 -0
- package/dist/auth/__tests__/conformance.test.d.ts.map +1 -0
- package/dist/auth/__tests__/conformance.test.js +76 -0
- package/dist/auth/__tests__/conformance.test.js.map +1 -0
- package/dist/auth/index.d.ts +3 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +2 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/data/SupabaseAuditQuery.d.ts +16 -0
- package/dist/data/SupabaseAuditQuery.d.ts.map +1 -0
- package/dist/data/SupabaseAuditQuery.js +83 -0
- package/dist/data/SupabaseAuditQuery.js.map +1 -0
- package/dist/data/SupabaseComputeServerStore.d.ts +26 -0
- package/dist/data/SupabaseComputeServerStore.d.ts.map +1 -0
- package/dist/data/SupabaseComputeServerStore.js +227 -0
- package/dist/data/SupabaseComputeServerStore.js.map +1 -0
- package/dist/data/SupabaseDataProvider.d.ts +55 -0
- package/dist/data/SupabaseDataProvider.d.ts.map +1 -0
- package/dist/data/SupabaseDataProvider.js +114 -0
- package/dist/data/SupabaseDataProvider.js.map +1 -0
- package/dist/data/SupabaseDefinitionStore.d.ts +39 -0
- package/dist/data/SupabaseDefinitionStore.d.ts.map +1 -0
- package/dist/data/SupabaseDefinitionStore.js +419 -0
- package/dist/data/SupabaseDefinitionStore.js.map +1 -0
- package/dist/data/SupabaseEventSink.d.ts +17 -0
- package/dist/data/SupabaseEventSink.d.ts.map +1 -0
- package/dist/data/SupabaseEventSink.js +31 -0
- package/dist/data/SupabaseEventSink.js.map +1 -0
- package/dist/data/SupabaseInviteStore.d.ts +22 -0
- package/dist/data/SupabaseInviteStore.d.ts.map +1 -0
- package/dist/data/SupabaseInviteStore.js +159 -0
- package/dist/data/SupabaseInviteStore.js.map +1 -0
- package/dist/data/SupabaseOrgStore.d.ts +33 -0
- package/dist/data/SupabaseOrgStore.d.ts.map +1 -0
- package/dist/data/SupabaseOrgStore.js +455 -0
- package/dist/data/SupabaseOrgStore.js.map +1 -0
- package/dist/data/SupabaseProjectStore.d.ts +29 -0
- package/dist/data/SupabaseProjectStore.d.ts.map +1 -0
- package/dist/data/SupabaseProjectStore.js +328 -0
- package/dist/data/SupabaseProjectStore.js.map +1 -0
- package/dist/data/SupabaseShareLinkStore.d.ts +26 -0
- package/dist/data/SupabaseShareLinkStore.d.ts.map +1 -0
- package/dist/data/SupabaseShareLinkStore.js +150 -0
- package/dist/data/SupabaseShareLinkStore.js.map +1 -0
- package/dist/data/__tests__/compute-server-conformance.test.d.ts +2 -0
- package/dist/data/__tests__/compute-server-conformance.test.d.ts.map +1 -0
- package/dist/data/__tests__/compute-server-conformance.test.js +40 -0
- package/dist/data/__tests__/compute-server-conformance.test.js.map +1 -0
- package/dist/data/__tests__/definition-conformance.test.d.ts +2 -0
- package/dist/data/__tests__/definition-conformance.test.d.ts.map +1 -0
- package/dist/data/__tests__/definition-conformance.test.js +85 -0
- package/dist/data/__tests__/definition-conformance.test.js.map +1 -0
- package/dist/data/__tests__/event-sink-conformance.test.d.ts +2 -0
- package/dist/data/__tests__/event-sink-conformance.test.d.ts.map +1 -0
- package/dist/data/__tests__/event-sink-conformance.test.js +27 -0
- package/dist/data/__tests__/event-sink-conformance.test.js.map +1 -0
- package/dist/data/__tests__/invite-conformance.test.d.ts +2 -0
- package/dist/data/__tests__/invite-conformance.test.d.ts.map +1 -0
- package/dist/data/__tests__/invite-conformance.test.js +45 -0
- package/dist/data/__tests__/invite-conformance.test.js.map +1 -0
- package/dist/data/__tests__/org-conformance.test.d.ts +2 -0
- package/dist/data/__tests__/org-conformance.test.d.ts.map +1 -0
- package/dist/data/__tests__/org-conformance.test.js +29 -0
- package/dist/data/__tests__/org-conformance.test.js.map +1 -0
- package/dist/data/__tests__/project-conformance.test.d.ts +2 -0
- package/dist/data/__tests__/project-conformance.test.d.ts.map +1 -0
- package/dist/data/__tests__/project-conformance.test.js +57 -0
- package/dist/data/__tests__/project-conformance.test.js.map +1 -0
- package/dist/data/__tests__/share-link-conformance.test.d.ts +2 -0
- package/dist/data/__tests__/share-link-conformance.test.d.ts.map +1 -0
- package/dist/data/__tests__/share-link-conformance.test.js +88 -0
- package/dist/data/__tests__/share-link-conformance.test.js.map +1 -0
- package/dist/data/__tests__/test-helpers.d.ts +53 -0
- package/dist/data/__tests__/test-helpers.d.ts.map +1 -0
- package/dist/data/__tests__/test-helpers.js +175 -0
- package/dist/data/__tests__/test-helpers.js.map +1 -0
- package/dist/data/client.d.ts +44 -0
- package/dist/data/client.d.ts.map +1 -0
- package/dist/data/client.js +40 -0
- package/dist/data/client.js.map +1 -0
- package/dist/data/index.d.ts +12 -0
- package/dist/data/index.d.ts.map +1 -0
- package/dist/data/index.js +11 -0
- package/dist/data/index.js.map +1 -0
- package/dist/data/pagination.d.ts +30 -0
- package/dist/data/pagination.d.ts.map +1 -0
- package/dist/data/pagination.js +53 -0
- package/dist/data/pagination.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/permissions/SupabasePlatformPermissionStore.d.ts +25 -0
- package/dist/permissions/SupabasePlatformPermissionStore.d.ts.map +1 -0
- package/dist/permissions/SupabasePlatformPermissionStore.js +153 -0
- package/dist/permissions/SupabasePlatformPermissionStore.js.map +1 -0
- package/dist/permissions/__tests__/conformance.test.d.ts +2 -0
- package/dist/permissions/__tests__/conformance.test.d.ts.map +1 -0
- package/dist/permissions/__tests__/conformance.test.js +27 -0
- package/dist/permissions/__tests__/conformance.test.js.map +1 -0
- package/dist/permissions/index.d.ts +2 -0
- package/dist/permissions/index.d.ts.map +1 -0
- package/dist/permissions/index.js +2 -0
- package/dist/permissions/index.js.map +1 -0
- package/dist/storage/SupabaseStorageProvider.d.ts +67 -0
- package/dist/storage/SupabaseStorageProvider.d.ts.map +1 -0
- package/dist/storage/SupabaseStorageProvider.js +136 -0
- package/dist/storage/SupabaseStorageProvider.js.map +1 -0
- package/dist/storage/__tests__/conformance.test.d.ts +2 -0
- package/dist/storage/__tests__/conformance.test.d.ts.map +1 -0
- package/dist/storage/__tests__/conformance.test.js +149 -0
- package/dist/storage/__tests__/conformance.test.js.map +1 -0
- package/dist/storage/index.d.ts +3 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +2 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/userProfile/SupabaseUserProfileProvider.d.ts +16 -0
- package/dist/userProfile/SupabaseUserProfileProvider.d.ts.map +1 -0
- package/dist/userProfile/SupabaseUserProfileProvider.js +148 -0
- package/dist/userProfile/SupabaseUserProfileProvider.js.map +1 -0
- package/dist/userProfile/__tests__/conformance.test.d.ts +2 -0
- package/dist/userProfile/__tests__/conformance.test.d.ts.map +1 -0
- package/dist/userProfile/__tests__/conformance.test.js +38 -0
- package/dist/userProfile/__tests__/conformance.test.js.map +1 -0
- package/dist/userProfile/index.d.ts +2 -0
- package/dist/userProfile/index.d.ts.map +1 -0
- package/dist/userProfile/index.js +2 -0
- package/dist/userProfile/index.js.map +1 -0
- package/package.json +75 -0
- package/src/auth/SupabaseAuthProvider.ts +464 -0
- package/src/auth/__tests__/conformance.test.ts +78 -0
- package/src/auth/index.ts +2 -0
- package/src/data/SupabaseAuditQuery.ts +103 -0
- package/src/data/SupabaseComputeServerStore.ts +262 -0
- package/src/data/SupabaseDataProvider.ts +125 -0
- package/src/data/SupabaseDefinitionStore.ts +504 -0
- package/src/data/SupabaseEventSink.ts +30 -0
- package/src/data/SupabaseInviteStore.ts +183 -0
- package/src/data/SupabaseOrgStore.ts +547 -0
- package/src/data/SupabaseProjectStore.ts +414 -0
- package/src/data/SupabaseShareLinkStore.ts +191 -0
- package/src/data/__tests__/compute-server-conformance.test.ts +40 -0
- package/src/data/__tests__/definition-conformance.test.ts +89 -0
- package/src/data/__tests__/event-sink-conformance.test.ts +32 -0
- package/src/data/__tests__/invite-conformance.test.ts +46 -0
- package/src/data/__tests__/org-conformance.test.ts +30 -0
- package/src/data/__tests__/project-conformance.test.ts +57 -0
- package/src/data/__tests__/share-link-conformance.test.ts +87 -0
- package/src/data/__tests__/test-helpers.ts +186 -0
- package/src/data/client.ts +86 -0
- package/src/data/index.ts +11 -0
- package/src/data/pagination.ts +68 -0
- package/src/index.ts +23 -0
- package/src/permissions/SupabasePlatformPermissionStore.ts +161 -0
- package/src/permissions/__tests__/conformance.test.ts +28 -0
- package/src/permissions/index.ts +1 -0
- package/src/storage/SupabaseStorageProvider.ts +177 -0
- package/src/storage/__tests__/conformance.test.ts +152 -0
- package/src/storage/index.ts +2 -0
- package/src/userProfile/SupabaseUserProfileProvider.ts +177 -0
- package/src/userProfile/__tests__/conformance.test.ts +39 -0
- package/src/userProfile/index.ts +1 -0
- package/supabase/.branches/_current_branch +1 -0
- package/supabase/.temp/cli-latest +1 -0
- package/supabase/config.toml +68 -0
- package/supabase/migrations/0001_initial.sql +1084 -0
- package/supabase/seed.sql +13 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { describe, beforeEach, it } from 'vitest';
|
|
2
|
+
import { runDefinitionStoreConformance } from '@selvajs/platform/testing';
|
|
3
|
+
import { DEFAULT_ORG_PERMISSIONS } from '@selvajs/platform';
|
|
4
|
+
import { SupabaseDefinitionStore } from '../SupabaseDefinitionStore.js';
|
|
5
|
+
import { readEnv, resetAllData, seedUser } from './test-helpers.js';
|
|
6
|
+
|
|
7
|
+
const envCtx = readEnv();
|
|
8
|
+
|
|
9
|
+
if (!envCtx) {
|
|
10
|
+
describe.skip('SupabaseDefinitionStore (skipped: no live stack)', () => {
|
|
11
|
+
it('populate packages/providers/supabase/.env.test with Supabase creds to run these tests', () => {});
|
|
12
|
+
});
|
|
13
|
+
} else {
|
|
14
|
+
describe('SupabaseDefinitionStore', () => {
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
await resetAllData(envCtx);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
runDefinitionStoreConformance({
|
|
20
|
+
name: 'SupabaseDefinitionStore',
|
|
21
|
+
createStore: () => new SupabaseDefinitionStore(envCtx.bundle),
|
|
22
|
+
createScope: async () => {
|
|
23
|
+
// Seed: 1 owner + 1 secondary user + 1 org + 2 projects. Definitions
|
|
24
|
+
// created in the suite FK to one of those two projects.
|
|
25
|
+
const { userId: ownerId, sessionToken: ownerSessionToken } = await seedUser(envCtx, '');
|
|
26
|
+
const { userId: secondaryUserId, sessionToken: secondaryUserSessionToken } = await seedUser(
|
|
27
|
+
envCtx,
|
|
28
|
+
''
|
|
29
|
+
);
|
|
30
|
+
const orgId = crypto.randomUUID();
|
|
31
|
+
const projectId = crypto.randomUUID();
|
|
32
|
+
const secondaryProjectId = crypto.randomUUID();
|
|
33
|
+
const now = new Date().toISOString();
|
|
34
|
+
const admin = envCtx.adminClient;
|
|
35
|
+
|
|
36
|
+
const { error: orgError } = await admin.from('orgs').insert({
|
|
37
|
+
id: orgId,
|
|
38
|
+
name: 'Definition Test Org',
|
|
39
|
+
slug: `def-test-${orgId.slice(0, 8)}`,
|
|
40
|
+
owner_id: ownerId,
|
|
41
|
+
created_at: now,
|
|
42
|
+
updated_at: now
|
|
43
|
+
});
|
|
44
|
+
if (orgError) throw orgError;
|
|
45
|
+
const { error: memberError } = await admin.from('org_members').insert({
|
|
46
|
+
org_id: orgId,
|
|
47
|
+
user_id: ownerId,
|
|
48
|
+
role: 'owner',
|
|
49
|
+
permissions: [...DEFAULT_ORG_PERMISSIONS.owner],
|
|
50
|
+
joined_at: now
|
|
51
|
+
});
|
|
52
|
+
if (memberError) throw memberError;
|
|
53
|
+
|
|
54
|
+
const { error: p1Error } = await admin.from('projects').insert([
|
|
55
|
+
{
|
|
56
|
+
id: projectId,
|
|
57
|
+
org_id: orgId,
|
|
58
|
+
name: 'Project A',
|
|
59
|
+
slug: `proj-a-${projectId.slice(0, 8)}`,
|
|
60
|
+
visibility: 'public',
|
|
61
|
+
owner_id: ownerId,
|
|
62
|
+
created_at: now,
|
|
63
|
+
updated_at: now
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: secondaryProjectId,
|
|
67
|
+
org_id: orgId,
|
|
68
|
+
name: 'Project B',
|
|
69
|
+
slug: `proj-b-${secondaryProjectId.slice(0, 8)}`,
|
|
70
|
+
visibility: 'public',
|
|
71
|
+
owner_id: ownerId,
|
|
72
|
+
created_at: now,
|
|
73
|
+
updated_at: now
|
|
74
|
+
}
|
|
75
|
+
]);
|
|
76
|
+
if (p1Error) throw p1Error;
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
ownerId,
|
|
80
|
+
ownerSessionToken,
|
|
81
|
+
projectId,
|
|
82
|
+
secondaryProjectId,
|
|
83
|
+
secondaryUserId,
|
|
84
|
+
secondaryUserSessionToken
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { describe, beforeEach, it } from 'vitest';
|
|
2
|
+
import { runEventSinkConformance, type RecordingEventSink } from '@selvajs/platform/testing';
|
|
3
|
+
import { SupabaseDataProvider } from '../SupabaseDataProvider.js';
|
|
4
|
+
import { readEnv, resetAllData, seedUser } from './test-helpers.js';
|
|
5
|
+
|
|
6
|
+
const envCtx = readEnv();
|
|
7
|
+
|
|
8
|
+
if (!envCtx) {
|
|
9
|
+
describe.skip('SupabaseDataProvider event-sink (skipped: no live stack)', () => {
|
|
10
|
+
it('populate packages/providers/supabase/.env.test with Supabase creds to run these tests', () => {});
|
|
11
|
+
});
|
|
12
|
+
} else {
|
|
13
|
+
describe('SupabaseDataProvider event-sink', () => {
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
await resetAllData(envCtx);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
runEventSinkConformance({
|
|
19
|
+
name: 'SupabaseDataProvider',
|
|
20
|
+
createProvider: (sink: RecordingEventSink) =>
|
|
21
|
+
SupabaseDataProvider.create(
|
|
22
|
+
{
|
|
23
|
+
supabaseUrl: envCtx.url,
|
|
24
|
+
anonKey: envCtx.anonKey,
|
|
25
|
+
serviceRoleKey: envCtx.serviceRoleKey
|
|
26
|
+
},
|
|
27
|
+
sink
|
|
28
|
+
),
|
|
29
|
+
createActorId: () => seedUser(envCtx, '')
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { describe, beforeEach, it } from 'vitest';
|
|
2
|
+
import { runInviteStoreConformance } from '@selvajs/platform/testing';
|
|
3
|
+
import { DEFAULT_ORG_PERMISSIONS } from '@selvajs/platform';
|
|
4
|
+
import { SupabaseInviteStore } from '../SupabaseInviteStore.js';
|
|
5
|
+
import { readEnv, resetAllData, seedUser } from './test-helpers.js';
|
|
6
|
+
|
|
7
|
+
const envCtx = readEnv();
|
|
8
|
+
|
|
9
|
+
if (!envCtx) {
|
|
10
|
+
describe.skip('SupabaseInviteStore (skipped: no live stack)', () => {
|
|
11
|
+
it('populate packages/providers/supabase/.env.test with Supabase creds to run these tests', () => {});
|
|
12
|
+
});
|
|
13
|
+
} else {
|
|
14
|
+
describe('SupabaseInviteStore', () => {
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
await resetAllData(envCtx);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
runInviteStoreConformance({
|
|
20
|
+
name: 'SupabaseInviteStore',
|
|
21
|
+
createStore: () => new SupabaseInviteStore(envCtx.bundle),
|
|
22
|
+
createScope: async () => {
|
|
23
|
+
const { userId: adminId, sessionToken: adminSessionToken } = await seedUser(envCtx, '');
|
|
24
|
+
const orgId = crypto.randomUUID();
|
|
25
|
+
const now = new Date().toISOString();
|
|
26
|
+
await envCtx.adminClient.from('orgs').insert({
|
|
27
|
+
id: orgId,
|
|
28
|
+
name: 'Invite Test Org',
|
|
29
|
+
slug: `invite-test-${orgId.slice(0, 8)}`,
|
|
30
|
+
owner_id: adminId,
|
|
31
|
+
created_at: now,
|
|
32
|
+
updated_at: now
|
|
33
|
+
});
|
|
34
|
+
await envCtx.adminClient.from('org_members').insert({
|
|
35
|
+
org_id: orgId,
|
|
36
|
+
user_id: adminId,
|
|
37
|
+
role: 'owner',
|
|
38
|
+
permissions: [...DEFAULT_ORG_PERMISSIONS.owner],
|
|
39
|
+
joined_at: now
|
|
40
|
+
});
|
|
41
|
+
return { adminId, adminSessionToken, orgId };
|
|
42
|
+
},
|
|
43
|
+
seedUser: (id) => seedUser(envCtx, id)
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, beforeEach, it } from 'vitest';
|
|
2
|
+
import { runOrgStoreConformance } from '@selvajs/platform/testing';
|
|
3
|
+
import { SupabaseOrgStore } from '../SupabaseOrgStore.js';
|
|
4
|
+
import { SupabaseInviteStore } from '../SupabaseInviteStore.js';
|
|
5
|
+
import { SupabaseComputeServerStore } from '../SupabaseComputeServerStore.js';
|
|
6
|
+
import { readEnv, resetAllData, seedUser } from './test-helpers.js';
|
|
7
|
+
|
|
8
|
+
const envCtx = readEnv();
|
|
9
|
+
|
|
10
|
+
if (!envCtx) {
|
|
11
|
+
describe.skip('SupabaseOrgStore (skipped: no live stack)', () => {
|
|
12
|
+
it('populate packages/providers/supabase/.env.test with Supabase creds to run these tests', () => {});
|
|
13
|
+
});
|
|
14
|
+
} else {
|
|
15
|
+
describe('SupabaseOrgStore', () => {
|
|
16
|
+
beforeEach(async () => {
|
|
17
|
+
await resetAllData(envCtx);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
runOrgStoreConformance({
|
|
21
|
+
name: 'SupabaseOrgStore',
|
|
22
|
+
createStore: () => new SupabaseOrgStore(envCtx.bundle),
|
|
23
|
+
seedUser: (id) => seedUser(envCtx, id),
|
|
24
|
+
createCompanionStores: () => ({
|
|
25
|
+
invites: new SupabaseInviteStore(envCtx.bundle),
|
|
26
|
+
computeServer: new SupabaseComputeServerStore(envCtx.bundle)
|
|
27
|
+
})
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { describe, beforeEach, it } from 'vitest';
|
|
2
|
+
import { runProjectStoreConformance } from '@selvajs/platform/testing';
|
|
3
|
+
import { DEFAULT_ORG_PERMISSIONS } from '@selvajs/platform';
|
|
4
|
+
import { SupabaseProjectStore } from '../SupabaseProjectStore.js';
|
|
5
|
+
import { readEnv, resetAllData, seedUser } from './test-helpers.js';
|
|
6
|
+
|
|
7
|
+
const envCtx = readEnv();
|
|
8
|
+
|
|
9
|
+
if (!envCtx) {
|
|
10
|
+
describe.skip('SupabaseProjectStore (skipped: no live stack)', () => {
|
|
11
|
+
it('populate packages/providers/supabase/.env.test with Supabase creds to run these tests', () => {});
|
|
12
|
+
});
|
|
13
|
+
} else {
|
|
14
|
+
describe('SupabaseProjectStore', () => {
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
await resetAllData(envCtx);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
runProjectStoreConformance({
|
|
20
|
+
name: 'SupabaseProjectStore',
|
|
21
|
+
createStore: async () => {
|
|
22
|
+
// Seed an owner + an org so the project tests have an anchor.
|
|
23
|
+
const { userId: ownerId, sessionToken: ownerSessionToken } = await seedUser(envCtx, '');
|
|
24
|
+
const orgId = crypto.randomUUID();
|
|
25
|
+
const now = new Date().toISOString();
|
|
26
|
+
const admin = envCtx.adminClient;
|
|
27
|
+
const { error: orgError } = await admin.from('orgs').insert({
|
|
28
|
+
id: orgId,
|
|
29
|
+
name: 'Conformance Org',
|
|
30
|
+
slug: `conformance-${orgId.slice(0, 8)}`,
|
|
31
|
+
owner_id: ownerId,
|
|
32
|
+
created_at: now,
|
|
33
|
+
updated_at: now
|
|
34
|
+
});
|
|
35
|
+
if (orgError) throw orgError;
|
|
36
|
+
// And an owner membership with `manage_projects` so project
|
|
37
|
+
// creates pass the `has_org_permission` policy under RLS.
|
|
38
|
+
const { error: memberError } = await admin.from('org_members').insert({
|
|
39
|
+
org_id: orgId,
|
|
40
|
+
user_id: ownerId,
|
|
41
|
+
role: 'owner',
|
|
42
|
+
permissions: [...DEFAULT_ORG_PERMISSIONS.owner],
|
|
43
|
+
joined_at: now
|
|
44
|
+
});
|
|
45
|
+
if (memberError) throw memberError;
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
store: new SupabaseProjectStore(envCtx.bundle),
|
|
49
|
+
orgId,
|
|
50
|
+
ownerId,
|
|
51
|
+
ownerSessionToken
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
seedUser: (id) => seedUser(envCtx, id)
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { describe, beforeEach, it } from 'vitest';
|
|
2
|
+
import { runShareLinkStoreConformance } from '@selvajs/platform/testing';
|
|
3
|
+
import { DEFAULT_ORG_PERMISSIONS } from '@selvajs/platform';
|
|
4
|
+
import { SupabaseShareLinkStore } from '../SupabaseShareLinkStore.js';
|
|
5
|
+
import { readEnv, resetAllData, seedUser } from './test-helpers.js';
|
|
6
|
+
|
|
7
|
+
const envCtx = readEnv();
|
|
8
|
+
|
|
9
|
+
if (!envCtx) {
|
|
10
|
+
describe.skip('SupabaseShareLinkStore (skipped: no live stack)', () => {
|
|
11
|
+
it('populate packages/providers/supabase/.env.test with Supabase creds to run these tests', () => {});
|
|
12
|
+
});
|
|
13
|
+
} else {
|
|
14
|
+
describe('SupabaseShareLinkStore', () => {
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
await resetAllData(envCtx);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
runShareLinkStoreConformance({
|
|
20
|
+
name: 'SupabaseShareLinkStore',
|
|
21
|
+
createStore: () => new SupabaseShareLinkStore(envCtx.bundle),
|
|
22
|
+
createScope: async () => {
|
|
23
|
+
// Seed: 1 owner + 1 org + 1 project + 2 definitions. Share links
|
|
24
|
+
// FK to definitions, definitions FK to projects, projects FK to orgs.
|
|
25
|
+
const { userId: ownerId, sessionToken: ownerSessionToken } = await seedUser(envCtx, '');
|
|
26
|
+
const orgId = crypto.randomUUID();
|
|
27
|
+
const projectId = crypto.randomUUID();
|
|
28
|
+
const definitionId = crypto.randomUUID();
|
|
29
|
+
const otherDefinitionId = crypto.randomUUID();
|
|
30
|
+
const now = new Date().toISOString();
|
|
31
|
+
const admin = envCtx.adminClient;
|
|
32
|
+
|
|
33
|
+
const { error: orgError } = await admin.from('orgs').insert({
|
|
34
|
+
id: orgId,
|
|
35
|
+
name: 'Share Link Test Org',
|
|
36
|
+
slug: `sl-test-${orgId.slice(0, 8)}`,
|
|
37
|
+
owner_id: ownerId,
|
|
38
|
+
created_at: now,
|
|
39
|
+
updated_at: now
|
|
40
|
+
});
|
|
41
|
+
if (orgError) throw orgError;
|
|
42
|
+
const { error: memberError } = await admin.from('org_members').insert({
|
|
43
|
+
org_id: orgId,
|
|
44
|
+
user_id: ownerId,
|
|
45
|
+
role: 'owner',
|
|
46
|
+
permissions: [...DEFAULT_ORG_PERMISSIONS.owner],
|
|
47
|
+
joined_at: now
|
|
48
|
+
});
|
|
49
|
+
if (memberError) throw memberError;
|
|
50
|
+
const { error: projError } = await admin.from('projects').insert({
|
|
51
|
+
id: projectId,
|
|
52
|
+
org_id: orgId,
|
|
53
|
+
name: 'Project',
|
|
54
|
+
slug: `proj-${projectId.slice(0, 8)}`,
|
|
55
|
+
visibility: 'public',
|
|
56
|
+
owner_id: ownerId,
|
|
57
|
+
created_at: now,
|
|
58
|
+
updated_at: now
|
|
59
|
+
});
|
|
60
|
+
if (projError) throw projError;
|
|
61
|
+
const { error: defError } = await admin.from('definitions').insert([
|
|
62
|
+
{
|
|
63
|
+
guid: definitionId,
|
|
64
|
+
project_id: projectId,
|
|
65
|
+
owner_id: ownerId,
|
|
66
|
+
display_name: 'Def A',
|
|
67
|
+
status: 'published',
|
|
68
|
+
created_at: now,
|
|
69
|
+
updated_at: now
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
guid: otherDefinitionId,
|
|
73
|
+
project_id: projectId,
|
|
74
|
+
owner_id: ownerId,
|
|
75
|
+
display_name: 'Def B',
|
|
76
|
+
status: 'published',
|
|
77
|
+
created_at: now,
|
|
78
|
+
updated_at: now
|
|
79
|
+
}
|
|
80
|
+
]);
|
|
81
|
+
if (defError) throw defError;
|
|
82
|
+
|
|
83
|
+
return { ownerId, ownerSessionToken, definitionId, otherDefinitionId };
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared test helpers for Supabase data conformance tests.
|
|
3
|
+
*
|
|
4
|
+
* Every suite starts with a fully wiped DB so tests are isolated. We use the
|
|
5
|
+
* service-role client for both — seeding users in `auth.users` and wiping
|
|
6
|
+
* every relevant table.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createClient, type SupabaseClient } from '@supabase/supabase-js';
|
|
10
|
+
import { buildClientBundle, type ClientBundle } from '../client.js';
|
|
11
|
+
|
|
12
|
+
export interface TestContext {
|
|
13
|
+
url: string;
|
|
14
|
+
anonKey: string;
|
|
15
|
+
serviceRoleKey: string;
|
|
16
|
+
adminClient: SupabaseClient;
|
|
17
|
+
bundle: ClientBundle;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function readEnv(): TestContext | null {
|
|
21
|
+
const url = process.env.SUPABASE_URL;
|
|
22
|
+
const anonKey = process.env.SUPABASE_ANON_KEY;
|
|
23
|
+
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
|
|
24
|
+
if (!url || !anonKey || !serviceRoleKey) return null;
|
|
25
|
+
// `.env.test` is checked in with local-Supabase defaults so contributors can
|
|
26
|
+
// `npx supabase start` and run the suite. But `createClient` transitively
|
|
27
|
+
// loads `@supabase/realtime-js`, which on Node < 22 throws at construction
|
|
28
|
+
// without a native WebSocket. Swallow that so the suite cleanly skips
|
|
29
|
+
// instead of crashing test collection.
|
|
30
|
+
let adminClient: SupabaseClient;
|
|
31
|
+
try {
|
|
32
|
+
adminClient = createClient(url, serviceRoleKey, {
|
|
33
|
+
auth: { persistSession: false, autoRefreshToken: false }
|
|
34
|
+
});
|
|
35
|
+
} catch (err) {
|
|
36
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
37
|
+
if (msg.includes('WebSocket')) return null;
|
|
38
|
+
throw err;
|
|
39
|
+
}
|
|
40
|
+
const bundle = buildClientBundle({ supabaseUrl: url, anonKey, serviceRoleKey });
|
|
41
|
+
return { url, anonKey, serviceRoleKey, adminClient, bundle };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Reset every data table this adapter owns. Leaves Supabase-managed tables
|
|
46
|
+
* (`auth.users`, etc.) alone except via `deleteAllAuthUsers` below.
|
|
47
|
+
*
|
|
48
|
+
* Order-sensitive because some FKs don't cascade: we go bottom up from
|
|
49
|
+
* dependents to parents.
|
|
50
|
+
*/
|
|
51
|
+
export async function resetAllData(ctx: TestContext): Promise<void> {
|
|
52
|
+
await truncate(ctx, 'share_links');
|
|
53
|
+
await truncate(ctx, 'invites');
|
|
54
|
+
// Versions FK to definitions ON DELETE CASCADE, but `definitions` can't
|
|
55
|
+
// be wiped while live/draft pointers reference versions (ON DELETE
|
|
56
|
+
// RESTRICT, spec §6). Null the pointers, drop versions, then drop defs.
|
|
57
|
+
await ctx.adminClient
|
|
58
|
+
.from('definitions')
|
|
59
|
+
.update({ live_version_id: null, draft_version_id: null })
|
|
60
|
+
.not('guid', 'is', null);
|
|
61
|
+
await truncate(ctx, 'definition_versions');
|
|
62
|
+
await truncate(ctx, 'definitions');
|
|
63
|
+
await truncate(ctx, 'project_members');
|
|
64
|
+
// compute defaults have a platform_default sibling — reset all three.
|
|
65
|
+
await truncate(ctx, 'compute_server_org_defaults');
|
|
66
|
+
await truncate(ctx, 'compute_server_shares');
|
|
67
|
+
await resetComputePlatformDefault(ctx);
|
|
68
|
+
await truncate(ctx, 'compute_servers');
|
|
69
|
+
await truncate(ctx, 'org_members');
|
|
70
|
+
await truncate(ctx, 'orgs');
|
|
71
|
+
await truncate(ctx, 'user_profiles');
|
|
72
|
+
await deleteAllAuthUsers(ctx);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function resetComputePlatformDefault(ctx: TestContext): Promise<void> {
|
|
76
|
+
// Singleton row — clear `default_server_id` rather than deleting.
|
|
77
|
+
const { error } = await ctx.adminClient
|
|
78
|
+
.from('compute_server_platform_default')
|
|
79
|
+
.update({ default_server_id: null })
|
|
80
|
+
.eq('singleton', true);
|
|
81
|
+
if (error) throw error;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function truncate(ctx: TestContext, table: string): Promise<void> {
|
|
85
|
+
const { error } = await ctx.adminClient
|
|
86
|
+
.from(table)
|
|
87
|
+
.delete()
|
|
88
|
+
.neq(columnForDeleteAll(table), '00000000-0000-0000-0000-000000000000');
|
|
89
|
+
if (error) throw error;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function columnForDeleteAll(table: string): string {
|
|
93
|
+
if (table === 'org_members') return 'user_id';
|
|
94
|
+
if (table === 'project_members') return 'user_id';
|
|
95
|
+
if (table === 'user_profiles') return 'user_id';
|
|
96
|
+
if (table === 'definitions') return 'guid';
|
|
97
|
+
if (table === 'definition_versions') return 'id';
|
|
98
|
+
if (table === 'compute_server_org_defaults') return 'org_id';
|
|
99
|
+
if (table === 'compute_server_shares') return 'server_id';
|
|
100
|
+
return 'id';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function deleteAllAuthUsers(ctx: TestContext): Promise<void> {
|
|
104
|
+
const { data, error } = await ctx.adminClient.auth.admin.listUsers({ perPage: 1000 });
|
|
105
|
+
if (error) throw error;
|
|
106
|
+
for (const user of data.users) {
|
|
107
|
+
const { error: delError } = await ctx.adminClient.auth.admin.deleteUser(user.id);
|
|
108
|
+
if (delError) throw delError;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Seed an `auth.users` row, mark the user as `instance_admin`, sign in, and
|
|
114
|
+
* return the real id + access token. `auth.admin.createUser` doesn't accept a
|
|
115
|
+
* caller-supplied id — Supabase generates one. The conformance suite calls us
|
|
116
|
+
* with a *suggested* id (for adapters that honor it); we ignore it and return
|
|
117
|
+
* GoTrue's real id.
|
|
118
|
+
*
|
|
119
|
+
* The instance_admin grant is what makes RLS open up for the conformance
|
|
120
|
+
* tests: every policy in the schema accepts `is_instance_admin()` as a
|
|
121
|
+
* universal pass, mirroring the `instance_admin` default in `makeCtx`.
|
|
122
|
+
*
|
|
123
|
+
* The session token is what makes RLS *see* the user at all. Without it, the
|
|
124
|
+
* Supabase client falls back to the anon role and every write fails.
|
|
125
|
+
*/
|
|
126
|
+
export async function seedUser(
|
|
127
|
+
ctx: TestContext,
|
|
128
|
+
suggestedId: string
|
|
129
|
+
): Promise<{ userId: string; sessionToken: string }> {
|
|
130
|
+
return seedUserCore(ctx, suggestedId, { promoteToInstanceAdmin: true });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Variant of `seedUser` that leaves `platform_permissions` empty. Use this
|
|
135
|
+
* when a test specifically asserts on a fresh user's permission state (e.g.
|
|
136
|
+
* the platform-permission store conformance suite).
|
|
137
|
+
*/
|
|
138
|
+
export async function seedPlainUser(
|
|
139
|
+
ctx: TestContext,
|
|
140
|
+
suggestedId: string
|
|
141
|
+
): Promise<{ userId: string; sessionToken: string }> {
|
|
142
|
+
return seedUserCore(ctx, suggestedId, { promoteToInstanceAdmin: false });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function seedUserCore(
|
|
146
|
+
ctx: TestContext,
|
|
147
|
+
_suggestedId: string,
|
|
148
|
+
opts: { promoteToInstanceAdmin: boolean }
|
|
149
|
+
): Promise<{ userId: string; sessionToken: string }> {
|
|
150
|
+
// Unique email per seed so repeated calls don't collide. GoTrue requires
|
|
151
|
+
// an email even for password-less users.
|
|
152
|
+
const email = `conformance-${crypto.randomUUID()}@conformance.test`;
|
|
153
|
+
const password = 'conformance-test-password-1234';
|
|
154
|
+
const { data, error } = await ctx.adminClient.auth.admin.createUser({
|
|
155
|
+
email,
|
|
156
|
+
password,
|
|
157
|
+
email_confirm: true
|
|
158
|
+
});
|
|
159
|
+
if (error) throw error;
|
|
160
|
+
const userId = data.user.id;
|
|
161
|
+
|
|
162
|
+
if (opts.promoteToInstanceAdmin) {
|
|
163
|
+
// Trigger auto-created the user_profiles row; promote to instance_admin
|
|
164
|
+
// so every RLS policy treats this user as fully authorized. Mirrors the
|
|
165
|
+
// `instance_admin` default in `makeCtx`.
|
|
166
|
+
const { error: promoteError } = await ctx.adminClient
|
|
167
|
+
.from('user_profiles')
|
|
168
|
+
.update({ platform_permissions: ['instance_admin'] })
|
|
169
|
+
.eq('user_id', userId);
|
|
170
|
+
if (promoteError) throw promoteError;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Sign in to get an access token. The conformance suite stuffs this into
|
|
174
|
+
// `adapterContext.sessionToken` and our client.ts uses it to scope the
|
|
175
|
+
// request to this user instead of falling back to anon.
|
|
176
|
+
const signInClient = createClient(ctx.url, ctx.anonKey, {
|
|
177
|
+
auth: { persistSession: false, autoRefreshToken: false }
|
|
178
|
+
});
|
|
179
|
+
const { data: session, error: signInError } = await signInClient.auth.signInWithPassword({
|
|
180
|
+
email,
|
|
181
|
+
password
|
|
182
|
+
});
|
|
183
|
+
if (signInError) throw signInError;
|
|
184
|
+
if (!session.session) throw new Error('seedUser: signInWithPassword returned no session');
|
|
185
|
+
return { userId, sessionToken: session.session.access_token };
|
|
186
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { createClient, type SupabaseClient } from '@supabase/supabase-js';
|
|
2
|
+
import type { RequestContext } from '@selvajs/platform';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build a Supabase client for a given `RequestContext`.
|
|
6
|
+
*
|
|
7
|
+
* Three modes — fail-closed:
|
|
8
|
+
* - **Service-role** ONLY when `ctx.system === true`. Bypasses RLS. Used for
|
|
9
|
+
* admin paths, janitors, bootstrap, and any explicitly-trusted server
|
|
10
|
+
* flow. The system flag must never be derived from a user session.
|
|
11
|
+
* - **User-scoped** when `ctx.adapterContext.sessionToken` is a JWT: runs
|
|
12
|
+
* under `authenticated` role with `auth.uid()` from the token. RLS
|
|
13
|
+
* policies enforce per-user visibility.
|
|
14
|
+
* - **Anonymous** otherwise: runs under the `anon` role. RLS is active and
|
|
15
|
+
* will scope reads/writes accordingly. This is the safety net — any code
|
|
16
|
+
* that constructs a context without forwarding the session token AND
|
|
17
|
+
* without setting `system: true` is treated as untrusted.
|
|
18
|
+
*
|
|
19
|
+
* Previous versions fell back to service-role when the session token was
|
|
20
|
+
* absent — a fail-OPEN footgun: any synthetic ctx (e.g. share-token resolve)
|
|
21
|
+
* silently bypassed RLS. The current contract is explicit: opt into
|
|
22
|
+
* service-role with `system: true`, or you get RLS.
|
|
23
|
+
*
|
|
24
|
+
* Per-request WeakMap caching keeps a single client alive for the lifetime
|
|
25
|
+
* of a RequestContext — multiple store calls in one HTTP handler share one
|
|
26
|
+
* fetch connection without leaking auth across requests.
|
|
27
|
+
*/
|
|
28
|
+
export interface ClientBundle {
|
|
29
|
+
/** Long-lived service-role client. Bypasses RLS. */
|
|
30
|
+
serviceClient: SupabaseClient;
|
|
31
|
+
/**
|
|
32
|
+
* Produce (or reuse) a client scoped to this request's identity.
|
|
33
|
+
* - `ctx.system === true` → service-role (RLS bypassed).
|
|
34
|
+
* - `ctx.adapterContext.sessionToken` set → user-scoped (RLS active).
|
|
35
|
+
* - neither → anon client (RLS active, no `auth.uid()`).
|
|
36
|
+
*/
|
|
37
|
+
forRequest(ctx: RequestContext): SupabaseClient;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface BuildClientOptions {
|
|
41
|
+
supabaseUrl: string;
|
|
42
|
+
anonKey: string;
|
|
43
|
+
serviceRoleKey: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function buildClientBundle(opts: BuildClientOptions): ClientBundle {
|
|
47
|
+
const serviceClient = createClient(opts.supabaseUrl, opts.serviceRoleKey, {
|
|
48
|
+
auth: { persistSession: false, autoRefreshToken: false }
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Long-lived anon client — the fail-closed default for ctxes that aren't
|
|
52
|
+
// explicitly system and don't carry a session token.
|
|
53
|
+
const anonClient = createClient(opts.supabaseUrl, opts.anonKey, {
|
|
54
|
+
auth: { persistSession: false, autoRefreshToken: false }
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const perRequestCache = new WeakMap<RequestContext, SupabaseClient>();
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
serviceClient,
|
|
61
|
+
forRequest(ctx: RequestContext): SupabaseClient {
|
|
62
|
+
if (ctx.system) return serviceClient;
|
|
63
|
+
|
|
64
|
+
const token = extractSessionToken(ctx.adapterContext);
|
|
65
|
+
if (!token) return anonClient;
|
|
66
|
+
|
|
67
|
+
const cached = perRequestCache.get(ctx);
|
|
68
|
+
if (cached) return cached;
|
|
69
|
+
|
|
70
|
+
const client = createClient(opts.supabaseUrl, opts.anonKey, {
|
|
71
|
+
auth: { persistSession: false, autoRefreshToken: false },
|
|
72
|
+
global: {
|
|
73
|
+
headers: { Authorization: `Bearer ${token}` }
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
perRequestCache.set(ctx, client);
|
|
77
|
+
return client;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function extractSessionToken(adapterContext: unknown): string | undefined {
|
|
83
|
+
if (!adapterContext || typeof adapterContext !== 'object') return undefined;
|
|
84
|
+
const maybe = (adapterContext as { sessionToken?: unknown }).sessionToken;
|
|
85
|
+
return typeof maybe === 'string' && maybe.length > 0 ? maybe : undefined;
|
|
86
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { SupabaseOrgStore } from './SupabaseOrgStore.js';
|
|
2
|
+
export { SupabaseProjectStore } from './SupabaseProjectStore.js';
|
|
3
|
+
export { SupabaseDefinitionStore } from './SupabaseDefinitionStore.js';
|
|
4
|
+
export { SupabaseInviteStore } from './SupabaseInviteStore.js';
|
|
5
|
+
export { SupabaseComputeServerStore } from './SupabaseComputeServerStore.js';
|
|
6
|
+
export { SupabaseShareLinkStore } from './SupabaseShareLinkStore.js';
|
|
7
|
+
export { SupabaseDataProvider } from './SupabaseDataProvider.js';
|
|
8
|
+
export { SupabaseEventSink } from './SupabaseEventSink.js';
|
|
9
|
+
export { SupabaseAuditQuery } from './SupabaseAuditQuery.js';
|
|
10
|
+
export { buildClientBundle } from './client.js';
|
|
11
|
+
export type { ClientBundle, BuildClientOptions } from './client.js';
|