Skip to content

Commit 639c8ca

Browse files
committed
test(projects): align fixture lifecycle with enforced project ownership
1 parent ecfdf8b commit 639c8ca

4 files changed

Lines changed: 58 additions & 27 deletions

File tree

‎apps/sim/background/cleanup-soft-deletes.integration.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,28 @@ async function seedTest(deletedAt: string | null, sizeBytes: number) {
2828

2929
describe('Retention cleanup of deleted workflow tests', () => {
3030
beforeAll(async () => {
31-
await control`INSERT INTO "user" (id, name, email, email_verified, created_at, updated_at)
32-
VALUES (${userId}, 'Test cleanup fixture', ${`${userId}@example.test`}, true, now(), now())`
33-
await control`INSERT INTO user_stats (id, user_id, storage_used_bytes)
34-
VALUES (${generateId()}, ${userId}, 100)`
35-
await control`INSERT INTO workspace (id, name, owner_id, billed_account_user_id, storage_used_bytes)
36-
VALUES (${workspaceId}, 'Test cleanup fixtures', ${userId}, ${userId}, 100)`
31+
await control.begin(async (tx) => {
32+
await tx`INSERT INTO "user" (id, name, email, email_verified, created_at, updated_at)
33+
VALUES (${userId}, 'Test cleanup fixture', ${`${userId}@example.test`}, true, now(), now())`
34+
await tx`INSERT INTO user_stats (id, user_id, storage_used_bytes)
35+
VALUES (${generateId()}, ${userId}, 100)`
36+
await tx`INSERT INTO project (id, name, owner_id)
37+
VALUES (${workspaceId}, 'Test cleanup project', ${userId})`
38+
await tx`INSERT INTO workspace (id, project_id, name, owner_id, billed_account_user_id, storage_used_bytes)
39+
VALUES (${workspaceId}, ${workspaceId}, 'Test cleanup fixtures', ${userId}, ${userId}, 100)`
40+
})
3741
})
3842

3943
afterAll(async () => {
40-
await control`DELETE FROM workspace WHERE id = ${workspaceId}`
41-
await control`DELETE FROM "user" WHERE id = ${userId}`
42-
await control.end()
44+
try {
45+
await control.begin(async (tx) => {
46+
await tx`DELETE FROM workspace WHERE id = ${workspaceId}`
47+
await tx`DELETE FROM project WHERE id = ${workspaceId} AND owner_id = ${userId}`
48+
await tx`DELETE FROM "user" WHERE id = ${userId}`
49+
})
50+
} finally {
51+
await control.end()
52+
}
4353
})
4454

4555
it('purges an expired test with its source and releases the source bytes', async () => {

‎apps/sim/lib/knowledge/__integration__/search-source-setup.integration.ts‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import {
1111
user,
1212
workspace,
1313
} from '@sim/db/schema'
14-
import { deleteWorkspaceFixture } from '@sim/db/testing/workspace-fixtures'
1514
import { generateId } from '@sim/utils/id'
16-
import { and, eq, isNull } from 'drizzle-orm'
15+
import { and, eq, inArray, isNull } from 'drizzle-orm'
1716
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
1817

1918
const fixture = vi.hoisted(() => ({ dispatch: vi.fn() }))
@@ -95,12 +94,12 @@ describe('Search source identity and concurrent creation', () => {
9594
})
9695

9796
afterAll(async () => {
98-
await deleteWorkspaceFixture(db, eq(workspace.id, ids.workspaceId))
99-
await deleteWorkspaceFixture(db, eq(workspace.id, other.workspaceId))
100-
for (const id of [ids.aliceId, ids.bobId, other.aliceId, other.bobId]) {
101-
await db.delete(project).where(eq(project.ownerId, id))
102-
await db.delete(user).where(eq(user.id, id))
103-
}
97+
const userIds = [ids.aliceId, ids.bobId, other.aliceId, other.bobId]
98+
await db.transaction(async (tx) => {
99+
await tx.delete(workspace).where(inArray(workspace.ownerId, userIds))
100+
await tx.delete(project).where(inArray(project.ownerId, userIds))
101+
await tx.delete(user).where(inArray(user.id, userIds))
102+
})
104103
})
105104

106105
async function createSource(folderId: string) {
@@ -179,8 +178,18 @@ describe('Search source identity and concurrent creation', () => {
179178
.where(eq(resourcePolicy.workspaceId, created.id))
180179
expect(policies).toHaveLength(0)
181180
} finally {
182-
await db.delete(permissions).where(eq(permissions.entityId, created.id))
183-
await deleteWorkspaceFixture(db, eq(workspace.id, created.id))
181+
await db.transaction(async (tx) => {
182+
await tx.delete(permissions).where(eq(permissions.entityId, created.id))
183+
const [removed] = await tx
184+
.delete(workspace)
185+
.where(eq(workspace.id, created.id))
186+
.returning({ projectId: workspace.projectId })
187+
if (removed) {
188+
await tx
189+
.delete(project)
190+
.where(and(eq(project.id, removed.projectId), eq(project.ownerId, ids.aliceId)))
191+
}
192+
})
184193
}
185194
})
186195

‎apps/sim/lib/workspaces/__integration__/fork-sync.integration.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
knowledgeBase,
1010
outboxEvent,
1111
permissions,
12+
project,
1213
user,
1314
userTableDefinitions,
1415
workflow,
@@ -21,7 +22,7 @@ import {
2122
workspaceOperationReceipt,
2223
workspaceSandbox,
2324
} from '@sim/db/schema'
24-
import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures'
25+
import { insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures'
2526
import { generateId } from '@sim/utils/id'
2627
import { and, eq, inArray, sql } from 'drizzle-orm'
2728
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
@@ -184,11 +185,13 @@ describe('authorized fork and sync against PostgreSQL', () => {
184185
})
185186
})
186187
afterAll(async () => {
187-
await deleteWorkspaceFixture(
188-
db,
189-
inArray(workspace.id, [sourceWorkspaceId, ...createdWorkspaceIds])
190-
)
191-
await db.delete(user).where(eq(user.id, userId))
188+
await db.transaction(async (tx) => {
189+
await tx
190+
.delete(workspace)
191+
.where(inArray(workspace.id, [sourceWorkspaceId, ...createdWorkspaceIds]))
192+
await tx.delete(project).where(eq(project.ownerId, userId))
193+
await tx.delete(user).where(eq(user.id, userId))
194+
})
192195
await db.$client.end()
193196
})
194197

‎apps/sim/scripts/test-mobile-e2e.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,10 @@ async function seed(): Promise<string> {
204204
await tx`insert into "user" (id, name, email, normalized_email, email_verified, created_at, updated_at)
205205
values (${ownerId}, 'Mobile E2E', ${email}, ${email}, true, now(), now())`
206206
await tx`insert into user_stats (id, user_id) values (${generateId()}, ${ownerId})`
207-
await tx`insert into workspace (id, name, owner_id, billed_account_user_id)
208-
values (${workspaceId}, 'Mobile E2E workspace', ${ownerId}, ${ownerId})`
207+
await tx`insert into project (id, name, owner_id)
208+
values (${workspaceId}, 'Mobile E2E project', ${ownerId})`
209+
await tx`insert into workspace (id, project_id, name, owner_id, billed_account_user_id)
210+
values (${workspaceId}, ${workspaceId}, 'Mobile E2E workspace', ${ownerId}, ${ownerId})`
209211
await tx`insert into permissions (id, user_id, entity_type, entity_id, permission_type)
210212
values (${generateId()}, ${ownerId}, 'workspace', ${workspaceId}, 'admin')`
211213
await tx`insert into session (id, token, user_id, expires_at, created_at, updated_at)
@@ -1071,9 +1073,16 @@ try {
10711073
await sql.begin(async (tx) => {
10721074
await tx`delete from permissions where entity_type = 'workspace' and entity_id = ${workspaceId}`
10731075
await tx`delete from workspace where id = ${workspaceId}`
1076+
await tx`delete from project where id = ${workspaceId} and owner_id = ${ownerId}`
10741077
await tx`delete from workflow_execution_snapshots where id = ${snapshotId}`
10751078
await tx`delete from "user" where id = ${ownerId}`
10761079
})
1080+
const [remaining] = await sql`select
1081+
exists(select 1 from "user" where id = ${ownerId}) as owner,
1082+
exists(select 1 from workspace where id = ${workspaceId}) as workspace,
1083+
exists(select 1 from project where id = ${workspaceId}) as project,
1084+
exists(select 1 from workflow_execution_snapshots where id = ${snapshotId}) as snapshot`
1085+
assert.deepEqual(remaining, { owner: false, workspace: false, project: false, snapshot: false })
10771086
})
10781087
await sql.end()
10791088
await check('same-origin HTTP responses succeed', undefined, async () => {

0 commit comments

Comments
 (0)