@@ -26,99 +26,26 @@ describe("getRawEventType", () => {
2626 const createdResources : {
2727 eventTypes : number [ ] ;
2828 users : number [ ] ;
29- teams : number [ ] ;
30- memberships : number [ ] ;
31- profiles : number [ ] ;
3229 } = {
3330 eventTypes : [ ] ,
3431 users : [ ] ,
35- teams : [ ] ,
36- memberships : [ ] ,
37- profiles : [ ] ,
3832 } ;
3933
40- // Helper functions to create test data
4134 const createTestUser = async ( overrides ?: {
42- organizationId ?: number ;
4335 username ?: string ;
44- withProfile ?: boolean ;
4536 } ) => {
4637 const timestamp = Date . now ( ) + Math . random ( ) ;
4738 const username = overrides ?. username ?? `testuser-${ timestamp } ` ;
4839 const user = await prisma . user . create ( {
4940 data : {
5041 username,
5142 email : `testuser-${ timestamp } @example.com` ,
52- organizationId : overrides ?. organizationId ,
53- ...( overrides ?. withProfile &&
54- overrides . organizationId && {
55- profiles : {
56- create : {
57- organizationId : overrides . organizationId ,
58- uid : username ,
59- username,
60- } ,
61- } ,
62- } ) ,
6343 } ,
6444 } ) ;
6545 createdResources . users . push ( user . id ) ;
6646 return user ;
6747 } ;
6848
69- const createTestOrganization = async ( overrides ?: { isPlatform ?: boolean } ) => {
70- const timestamp = Date . now ( ) + Math . random ( ) ;
71- const team = await prisma . team . create ( {
72- data : {
73- name : `Test Organization ${ timestamp } ` ,
74- slug : `test-org-${ timestamp } ` ,
75- isOrganization : true ,
76- isPlatform : overrides ?. isPlatform ?? false ,
77- } ,
78- } ) ;
79- createdResources . teams . push ( team . id ) ;
80- return team ;
81- } ;
82-
83- const createTestTeam = async ( parentId ?: number ) => {
84- const timestamp = Date . now ( ) + Math . random ( ) ;
85- const team = await prisma . team . create ( {
86- data : {
87- name : `Test Team ${ timestamp } ` ,
88- slug : `test-team-${ timestamp } ` ,
89- parentId : parentId ?? null ,
90- } ,
91- } ) ;
92- createdResources . teams . push ( team . id ) ;
93- return team ;
94- } ;
95-
96- const createTestOrgAdmin = async ( organizationId : number ) => {
97- const timestamp = Date . now ( ) + Math . random ( ) ;
98- const user = await prisma . user . create ( {
99- data : {
100- username : `orgadmin-${ timestamp } ` ,
101- email : `orgadmin-${ timestamp } @example.com` ,
102- organizationId,
103- } ,
104- } ) ;
105- createdResources . users . push ( user . id ) ;
106- return user ;
107- } ;
108-
109- const createTestTeamMember = async ( teamId : number , userId : number ) => {
110- const membership = await prisma . membership . create ( {
111- data : {
112- teamId,
113- userId,
114- role : "MEMBER" ,
115- accepted : true ,
116- } ,
117- } ) ;
118- createdResources . memberships . push ( membership . id ) ;
119- return membership ;
120- } ;
121-
12249 const createTestEventType = async ( userId : number , overrides ?: { slug ?: string ; title ?: string } ) => {
12350 const timestamp = Date . now ( ) + Math . random ( ) ;
12451 const eventType = await prisma . eventType . create ( {
@@ -139,56 +66,18 @@ describe("getRawEventType", () => {
13966 return eventType ;
14067 } ;
14168
142- const createTestTeamEventType = async ( teamId : number ) => {
143- const timestamp = Date . now ( ) + Math . random ( ) ;
144- const eventType = await prisma . eventType . create ( {
145- data : {
146- title : `Team Event ${ timestamp } ` ,
147- slug : `team-event-${ timestamp } ` ,
148- length : 30 ,
149- teamId,
150- } ,
151- include : {
152- team : true ,
153- users : true ,
154- } ,
155- } ) ;
156- createdResources . eventTypes . push ( eventType . id ) ;
157- return eventType ;
158- } ;
159-
16069 beforeEach ( ( ) => {
16170 mockNoTranslations ( ) ;
162- // Reset tracking arrays
16371 createdResources . eventTypes = [ ] ;
16472 createdResources . users = [ ] ;
165- createdResources . teams = [ ] ;
166- createdResources . memberships = [ ] ;
167- createdResources . profiles = [ ] ;
16873 } ) ;
16974
17075 afterEach ( async ( ) => {
171- // Clean up in reverse order to avoid foreign key violations
17276 if ( createdResources . eventTypes . length > 0 ) {
17377 await prisma . eventType . deleteMany ( {
17478 where : { id : { in : createdResources . eventTypes } } ,
17579 } ) ;
17680 }
177- if ( createdResources . memberships . length > 0 ) {
178- await prisma . membership . deleteMany ( {
179- where : { id : { in : createdResources . memberships } } ,
180- } ) ;
181- }
182- if ( createdResources . profiles . length > 0 ) {
183- await prisma . profile . deleteMany ( {
184- where : { id : { in : createdResources . profiles } } ,
185- } ) ;
186- }
187- if ( createdResources . teams . length > 0 ) {
188- await prisma . team . deleteMany ( {
189- where : { id : { in : createdResources . teams } } ,
190- } ) ;
191- }
19281 if ( createdResources . users . length > 0 ) {
19382 await prisma . user . deleteMany ( {
19483 where : { id : { in : createdResources . users } } ,
@@ -270,158 +159,4 @@ describe("getRawEventType", () => {
270159 expect ( result ) . toBeNull ( ) ;
271160 } ) ;
272161 } ) ;
273-
274- describe ( "Organization admin access" , ( ) => {
275- test ( "should fetch team event type when user is org admin and is a team member" , async ( ) => {
276- const organization = await createTestOrganization ( ) ;
277- const team = await createTestTeam ( organization . id ) ;
278- const orgAdmin = await createTestOrgAdmin ( organization . id ) ;
279- await createTestTeamMember ( team . id , orgAdmin . id ) ;
280- const eventType = await createTestTeamEventType ( team . id ) ;
281-
282- const result = await getRawEventType ( {
283- userId : orgAdmin . id ,
284- eventTypeId : eventType . id ,
285- isUserOrganizationAdmin : true ,
286- currentOrganizationId : organization . id ,
287- prisma : prisma as unknown as PrismaClient ,
288- } ) ;
289-
290- expect ( result ) . toBeDefined ( ) ;
291- expect ( result ?. id ) . toBe ( eventType . id ) ;
292- expect ( result ?. title ) . toContain ( "Team Event" ) ;
293- expect ( result ?. teamId ) . toBe ( team . id ) ;
294- } ) ;
295-
296- test ( "should return null when org admin tries to access event type from different org" , async ( ) => {
297- const org1 = await createTestOrganization ( ) ;
298- const org2 = await createTestOrganization ( ) ;
299- const team1 = await createTestTeam ( org1 . id ) ;
300- const org2Admin = await createTestOrgAdmin ( org2 . id ) ;
301- const eventType = await createTestTeamEventType ( team1 . id ) ;
302-
303- const result = await getRawEventType ( {
304- userId : org2Admin . id ,
305- eventTypeId : eventType . id ,
306- isUserOrganizationAdmin : true ,
307- currentOrganizationId : org2 . id ,
308- prisma : prisma as unknown as PrismaClient ,
309- } ) ;
310-
311- expect ( result ) . toBeNull ( ) ;
312- } ) ;
313-
314- test ( "should fallback to regular user access when org admin flag is true but no organizationId" , async ( ) => {
315- const user = await createTestUser ( ) ;
316- const eventType = await createTestEventType ( user . id , { title : "Regular User Event" } ) ;
317-
318- const result = await getRawEventType ( {
319- userId : user . id ,
320- eventTypeId : eventType . id ,
321- isUserOrganizationAdmin : true ,
322- currentOrganizationId : null ,
323- prisma : prisma as unknown as PrismaClient ,
324- } ) ;
325-
326- expect ( result ) . toBeDefined ( ) ;
327- expect ( result ?. id ) . toBe ( eventType . id ) ;
328- expect ( result ?. userId ) . toBe ( user . id ) ;
329- } ) ;
330- } ) ;
331-
332- describe ( "when user is platform organization admin" , ( ) => {
333- test ( "should access any team event type within the platform organization" , async ( ) => {
334- const platformOrg = await createTestOrganization ( { isPlatform : true } ) ;
335- const orgSubTeam = await createTestTeam ( platformOrg . id ) ;
336- const platformAdmin = await createTestOrgAdmin ( platformOrg . id ) ;
337- const teamEvent = await createTestTeamEventType ( orgSubTeam . id ) ;
338-
339- const result = await getRawEventType ( {
340- userId : platformAdmin . id ,
341- eventTypeId : teamEvent . id ,
342- isUserOrganizationAdmin : true ,
343- currentOrganizationId : platformOrg . id ,
344- prisma : prisma as unknown as PrismaClient ,
345- } ) ;
346-
347- expect ( result ) . toBeDefined ( ) ;
348- expect ( result ?. id ) . toBe ( teamEvent . id ) ;
349- expect ( result ?. teamId ) . toBe ( orgSubTeam . id ) ;
350- } ) ;
351-
352- test ( "should access user event types within the platform organization" , async ( ) => {
353- const platformOrg = await createTestOrganization ( { isPlatform : true } ) ;
354- const platformAdmin = await createTestOrgAdmin ( platformOrg . id ) ;
355- const orgUser = await createTestUser ( { organizationId : platformOrg . id , withProfile : true } ) ;
356- const userEvent = await createTestEventType ( orgUser . id , { title : "Platform User Event" } ) ;
357-
358- const result = await getRawEventType ( {
359- userId : platformAdmin . id ,
360- eventTypeId : userEvent . id ,
361- isUserOrganizationAdmin : true ,
362- currentOrganizationId : platformOrg . id ,
363- prisma : prisma as unknown as PrismaClient ,
364- } ) ;
365-
366- expect ( result ) . toBeDefined ( ) ;
367- expect ( result ?. id ) . toBe ( userEvent . id ) ;
368- expect ( result ?. userId ) . toBe ( orgUser . id ) ;
369- } ) ;
370- } ) ;
371-
372- describe ( "when user is non-platform organization admin" , ( ) => {
373- test ( "should access regular team event when admin is a team member" , async ( ) => {
374- const regularOrg = await createTestOrganization ( { isPlatform : false } ) ;
375- const standaloneTeam = await createTestTeam ( ) ;
376- const orgAdmin = await createTestOrgAdmin ( regularOrg . id ) ;
377- await createTestTeamMember ( standaloneTeam . id , orgAdmin . id ) ;
378- const teamEvent = await createTestTeamEventType ( standaloneTeam . id ) ;
379-
380- const result = await getRawEventType ( {
381- userId : orgAdmin . id ,
382- eventTypeId : teamEvent . id ,
383- isUserOrganizationAdmin : true ,
384- currentOrganizationId : regularOrg . id ,
385- prisma : prisma as unknown as PrismaClient ,
386- } ) ;
387-
388- expect ( result ) . toBeDefined ( ) ;
389- expect ( result ?. id ) . toBe ( teamEvent . id ) ;
390- expect ( result ?. teamId ) . toBe ( standaloneTeam . id ) ;
391- } ) ;
392-
393- test ( "should not access regular team event when admin is not a team member" , async ( ) => {
394- const regularOrg = await createTestOrganization ( { isPlatform : false } ) ;
395- const standaloneTeam = await createTestTeam ( ) ;
396- const orgAdmin = await createTestOrgAdmin ( regularOrg . id ) ;
397- const teamEvent = await createTestTeamEventType ( standaloneTeam . id ) ;
398-
399- const result = await getRawEventType ( {
400- userId : orgAdmin . id ,
401- eventTypeId : teamEvent . id ,
402- isUserOrganizationAdmin : true ,
403- currentOrganizationId : regularOrg . id ,
404- prisma : prisma as unknown as PrismaClient ,
405- } ) ;
406-
407- expect ( result ) . toBeNull ( ) ;
408- } ) ;
409-
410- test ( "should not access organization sub-team event when admin is not a team member" , async ( ) => {
411- const regularOrg = await createTestOrganization ( { isPlatform : false } ) ;
412- const orgSubTeam = await createTestTeam ( regularOrg . id ) ;
413- const orgAdmin = await createTestOrgAdmin ( regularOrg . id ) ;
414- const subTeamEvent = await createTestTeamEventType ( orgSubTeam . id ) ;
415-
416- const result = await getRawEventType ( {
417- userId : orgAdmin . id ,
418- eventTypeId : subTeamEvent . id ,
419- isUserOrganizationAdmin : true ,
420- currentOrganizationId : regularOrg . id ,
421- prisma : prisma as unknown as PrismaClient ,
422- } ) ;
423-
424- expect ( result ) . toBeNull ( ) ;
425- } ) ;
426- } ) ;
427162} ) ;
0 commit comments