11/**
22 * Workspace updates against real Postgres: the session route keeps its legacy
3- * refusals, and Chat's settings tool renames a workspace only as an admin and
4- * never reaches billing or the personal-key policy.
3+ * refusals, and Chat's settings tool renames a workspace only as an admin.
4+ * Neither can change the billed account, and Chat never reaches the
5+ * personal-key policy.
56 */
67import { db } from '@sim/db'
7- import { auditLog , permissions , user , workspace } from '@sim/db/schema'
8+ import { auditLog , permissions , user , userStats , workspace } from '@sim/db/schema'
89import { deleteWorkspaceFixture , insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures'
910import { createDeferred } from '@sim/testing/helpers/deferred'
1011import { authMock , authMockFns } from '@sim/testing/mocks/auth.mock'
@@ -23,6 +24,7 @@ import { PATCH } from '@/app/api/workspaces/[id]/route'
2324
2425const adminId = generateId ( )
2526const secondAdminId = generateId ( )
27+ const thirdAdminId = generateId ( )
2628const writerId = generateId ( )
2729const outsiderId = generateId ( )
2830const workspaceId = generateId ( )
@@ -113,7 +115,7 @@ describe('workspace update operations', () => {
113115 beforeAll ( async ( ) => {
114116 const now = new Date ( )
115117 await db . insert ( user ) . values (
116- [ adminId , secondAdminId , writerId , outsiderId ] . map ( ( id ) => ( {
118+ [ adminId , secondAdminId , thirdAdminId , writerId , outsiderId ] . map ( ( id ) => ( {
117119 id,
118120 name : 'Workspace update fixture' ,
119121 email : `${ id } @workspace-update.test` ,
@@ -122,6 +124,13 @@ describe('workspace update operations', () => {
122124 updatedAt : now ,
123125 } ) )
124126 )
127+ // Every real account has a stats row; without one a payer move would fail on the
128+ // missing storage aggregate instead of committing, and prove nothing.
129+ await db
130+ . insert ( userStats )
131+ . values (
132+ [ adminId , secondAdminId , thirdAdminId ] . map ( ( userId ) => ( { id : generateId ( ) , userId } ) )
133+ )
125134 await insertWorkspaceFixture ( db , [
126135 {
127136 id : workspaceId ,
@@ -159,6 +168,13 @@ describe('workspace update operations', () => {
159168 userId : secondAdminId ,
160169 permissionType : 'admin' as const ,
161170 } ,
171+ {
172+ id : generateId ( ) ,
173+ entityType : 'workspace' ,
174+ entityId : workspaceId ,
175+ userId : thirdAdminId ,
176+ permissionType : 'admin' as const ,
177+ } ,
162178 {
163179 id : generateId ( ) ,
164180 entityType : 'workspace' ,
@@ -173,7 +189,9 @@ describe('workspace update operations', () => {
173189 await db . delete ( auditLog ) . where ( inArray ( auditLog . workspaceId , fixtureWorkspaceIds ) )
174190 await db . delete ( permissions ) . where ( inArray ( permissions . entityId , fixtureWorkspaceIds ) )
175191 await deleteWorkspaceFixture ( db , inArray ( workspace . id , fixtureWorkspaceIds ) )
176- await db . delete ( user ) . where ( inArray ( user . id , [ adminId , secondAdminId , writerId , outsiderId ] ) )
192+ await db
193+ . delete ( user )
194+ . where ( inArray ( user . id , [ adminId , secondAdminId , thirdAdminId , writerId , outsiderId ] ) )
177195 } )
178196
179197 describe ( 'session route' , ( ) => {
@@ -210,13 +228,14 @@ describe('workspace update operations', () => {
210228 expect ( await response . json ( ) ) . toMatchObject ( { error : 'No updates provided' } )
211229 } )
212230
213- it ( 'refuses a billed-account change to a non-admin with the legacy message' , async ( ) => {
214- signIn ( adminId )
215- const response = await patchWorkspace ( workspaceId , { billedAccountUserId : writerId } )
231+ it . each ( [
232+ [ 'a co-admin who is not the payer' , secondAdminId , thirdAdminId ] ,
233+ [ 'the payer, onto an admin who never agreed' , adminId , secondAdminId ] ,
234+ ] ) ( 'never lets %s move the billed account' , async ( _label , actorId , newPayerId ) => {
235+ signIn ( actorId )
236+ const response = await patchWorkspace ( workspaceId , { billedAccountUserId : newPayerId } )
216237 expect ( response . status ) . toBe ( 400 )
217- expect ( await response . json ( ) ) . toMatchObject ( {
218- error : 'Billed account must be a workspace admin' ,
219- } )
238+ expect ( await response . json ( ) ) . toMatchObject ( { error : 'No updates provided' } )
220239 expect ( ( await readWorkspace ( workspaceId ) ) . billedAccountUserId ) . toBe ( adminId )
221240 } )
222241
@@ -287,7 +306,7 @@ describe('workspace update operations', () => {
287306 await expect (
288307 updateWorkspace . execute ( {
289308 principal,
290- input : { workspaceId, patch : { billedAccountUserId : adminId } } ,
309+ input : { workspaceId, patch : { allowPersonalApiKeys : false } } ,
291310 } )
292311 ) . rejects . toThrow ( 'cannot perform operation workspaces.update' )
293312 } )
0 commit comments