@@ -22,6 +22,7 @@ import { ForbiddenError } from '@/errors/response-errors/forbidden.error';
2222import config from '@/config' ;
2323import type { AuthlessRequest } from '@/requests' ;
2424import { v4 as uuidv4 } from 'uuid' ;
25+ import { OwnershipService } from '@/services/ownership.service' ;
2526
2627describe ( 'InvitationController' , ( ) => {
2728 const logger : Logger = mockInstance ( Logger ) ;
@@ -33,22 +34,29 @@ describe('InvitationController', () => {
3334 const userRepository : UserRepository = mockInstance ( UserRepository ) ;
3435 const postHog : PostHogClient = mockInstance ( PostHogClient ) ;
3536 const eventService : EventService = mockInstance ( EventService ) ;
37+ const ownershipService : OwnershipService = mockInstance ( OwnershipService ) ;
38+
39+ function defaultInvitationController ( ) {
40+ return new InvitationController (
41+ logger ,
42+ externalHooks ,
43+ authService ,
44+ userService ,
45+ license ,
46+ passwordUtility ,
47+ userRepository ,
48+ postHog ,
49+ eventService ,
50+ ownershipService ,
51+ ) ;
52+ }
3653
3754 describe ( 'inviteUser' , ( ) => {
3855 it ( 'throws a BadRequestError if SSO is enabled' , async ( ) => {
3956 jest . spyOn ( ssoHelpers , 'isSsoCurrentAuthenticationMethod' ) . mockReturnValue ( true ) ;
57+ jest . spyOn ( ownershipService , 'hasInstanceOwner' ) . mockReturnValue ( Promise . resolve ( true ) ) ;
4058
41- const invitationController = new InvitationController (
42- logger ,
43- externalHooks ,
44- authService ,
45- userService ,
46- license ,
47- passwordUtility ,
48- userRepository ,
49- postHog ,
50- eventService ,
51- ) ;
59+ const invitationController = defaultInvitationController ( ) ;
5260
5361 const user = mock < User > ( {
5462 id : '123' ,
@@ -77,18 +85,9 @@ describe('InvitationController', () => {
7785 it ( 'throws a ForbiddenError if the user limit quota has been reached' , async ( ) => {
7886 jest . spyOn ( ssoHelpers , 'isSsoCurrentAuthenticationMethod' ) . mockReturnValue ( false ) ;
7987 jest . spyOn ( license , 'isWithinUsersLimit' ) . mockReturnValue ( false ) ;
88+ jest . spyOn ( ownershipService , 'hasInstanceOwner' ) . mockReturnValue ( Promise . resolve ( true ) ) ;
8089
81- const invitationController = new InvitationController (
82- logger ,
83- externalHooks ,
84- authService ,
85- userService ,
86- license ,
87- passwordUtility ,
88- userRepository ,
89- postHog ,
90- eventService ,
91- ) ;
90+ const invitationController = defaultInvitationController ( ) ;
9291
9392 const user = mock < User > ( {
9493 id : '123' ,
@@ -112,18 +111,9 @@ describe('InvitationController', () => {
112111 jest . spyOn ( ssoHelpers , 'isSsoCurrentAuthenticationMethod' ) . mockReturnValue ( false ) ;
113112 jest . spyOn ( license , 'isWithinUsersLimit' ) . mockReturnValue ( true ) ;
114113 jest . spyOn ( config , 'getEnv' ) . mockReturnValue ( false ) ;
114+ jest . spyOn ( ownershipService , 'hasInstanceOwner' ) . mockReturnValue ( Promise . resolve ( false ) ) ;
115115
116- const invitationController = new InvitationController (
117- logger ,
118- externalHooks ,
119- authService ,
120- userService ,
121- license ,
122- passwordUtility ,
123- userRepository ,
124- postHog ,
125- eventService ,
126- ) ;
116+ const invitationController = defaultInvitationController ( ) ;
127117
128118 const user = mock < User > ( {
129119 id : '123' ,
@@ -148,18 +138,9 @@ describe('InvitationController', () => {
148138 jest . spyOn ( license , 'isWithinUsersLimit' ) . mockReturnValue ( true ) ;
149139 jest . spyOn ( config , 'getEnv' ) . mockReturnValue ( true ) ;
150140 jest . spyOn ( license , 'isAdvancedPermissionsLicensed' ) . mockReturnValue ( false ) ;
141+ jest . spyOn ( ownershipService , 'hasInstanceOwner' ) . mockReturnValue ( Promise . resolve ( true ) ) ;
151142
152- const invitationController = new InvitationController (
153- logger ,
154- externalHooks ,
155- authService ,
156- userService ,
157- license ,
158- passwordUtility ,
159- userRepository ,
160- postHog ,
161- eventService ,
162- ) ;
143+ const invitationController = defaultInvitationController ( ) ;
163144
164145 const user = mock < User > ( {
165146 id : '123' ,
@@ -209,17 +190,9 @@ describe('InvitationController', () => {
209190 jest . spyOn ( config , 'getEnv' ) . mockReturnValue ( true ) ;
210191 jest . spyOn ( license , 'isAdvancedPermissionsLicensed' ) . mockReturnValue ( true ) ;
211192 jest . spyOn ( userService , 'inviteUsers' ) . mockResolvedValue ( inviteUsersResult ) ;
212- const invitationController = new InvitationController (
213- logger ,
214- externalHooks ,
215- authService ,
216- userService ,
217- license ,
218- passwordUtility ,
219- userRepository ,
220- postHog ,
221- eventService ,
222- ) ;
193+ jest . spyOn ( ownershipService , 'hasInstanceOwner' ) . mockReturnValue ( Promise . resolve ( true ) ) ;
194+
195+ const invitationController = defaultInvitationController ( ) ;
223196
224197 const user = mock < User > ( {
225198 id : '123' ,
@@ -255,19 +228,11 @@ describe('InvitationController', () => {
255228 describe ( 'acceptInvitation' , ( ) => {
256229 it ( 'throws a BadRequestError if SSO is enabled' , async ( ) => {
257230 jest . spyOn ( ssoHelpers , 'isSsoCurrentAuthenticationMethod' ) . mockReturnValue ( true ) ;
231+ jest . spyOn ( ownershipService , 'hasInstanceOwner' ) . mockReturnValue ( Promise . resolve ( true ) ) ;
232+
258233 const id = uuidv4 ( ) ;
259234
260- const invitationController = new InvitationController (
261- logger ,
262- externalHooks ,
263- authService ,
264- userService ,
265- license ,
266- passwordUtility ,
267- userRepository ,
268- postHog ,
269- eventService ,
270- ) ;
235+ const invitationController = defaultInvitationController ( ) ;
271236
272237 const payload = new AcceptInvitationRequestDto ( {
273238 inviterId : id ,
@@ -291,19 +256,11 @@ describe('InvitationController', () => {
291256
292257 it ( 'throws a BadRequestError if the inviter ID and invitee ID are not found in the database' , async ( ) => {
293258 jest . spyOn ( ssoHelpers , 'isSsoCurrentAuthenticationMethod' ) . mockReturnValue ( false ) ;
259+ jest . spyOn ( ownershipService , 'hasInstanceOwner' ) . mockReturnValue ( Promise . resolve ( true ) ) ;
260+
294261 const id = uuidv4 ( ) ;
295262
296- const invitationController = new InvitationController (
297- logger ,
298- externalHooks ,
299- authService ,
300- userService ,
301- license ,
302- passwordUtility ,
303- userRepository ,
304- postHog ,
305- eventService ,
306- ) ;
263+ const invitationController = defaultInvitationController ( ) ;
307264
308265 const payload = new AcceptInvitationRequestDto ( {
309266 inviterId : id ,
@@ -332,6 +289,8 @@ describe('InvitationController', () => {
332289
333290 it ( 'throws a BadRequestError if the invitee already has a password' , async ( ) => {
334291 jest . spyOn ( ssoHelpers , 'isSsoCurrentAuthenticationMethod' ) . mockReturnValue ( false ) ;
292+ jest . spyOn ( ownershipService , 'hasInstanceOwner' ) . mockReturnValue ( Promise . resolve ( true ) ) ;
293+
335294 const invitee = mock < User > ( {
336295 id : '123' ,
337296@@ -346,17 +305,7 @@ describe('InvitationController', () => {
346305 jest . spyOn ( userRepository , 'find' ) . mockResolvedValue ( [ inviter , invitee ] ) ;
347306 const id = uuidv4 ( ) ;
348307
349- const invitationController = new InvitationController (
350- logger ,
351- externalHooks ,
352- authService ,
353- userService ,
354- license ,
355- passwordUtility ,
356- userRepository ,
357- postHog ,
358- eventService ,
359- ) ;
308+ const invitationController = defaultInvitationController ( ) ;
360309
361310 const payload = new AcceptInvitationRequestDto ( {
362311 inviterId : id ,
@@ -379,6 +328,8 @@ describe('InvitationController', () => {
379328
380329 it ( 'accepts the invitation successfully' , async ( ) => {
381330 jest . spyOn ( ssoHelpers , 'isSsoCurrentAuthenticationMethod' ) . mockReturnValue ( false ) ;
331+ jest . spyOn ( ownershipService , 'hasInstanceOwner' ) . mockReturnValue ( Promise . resolve ( true ) ) ;
332+
382333 const id = uuidv4 ( ) ;
383334 const inviter = mock < User > ( {
384335 id : '124' ,
@@ -400,17 +351,7 @@ describe('InvitationController', () => {
400351 jest . spyOn ( userService , 'toPublic' ) . mockResolvedValue ( invitee as unknown as PublicUser ) ;
401352 jest . spyOn ( externalHooks , 'run' ) . mockResolvedValue ( invitee as never ) ;
402353
403- const invitationController = new InvitationController (
404- logger ,
405- externalHooks ,
406- authService ,
407- userService ,
408- license ,
409- passwordUtility ,
410- userRepository ,
411- postHog ,
412- eventService ,
413- ) ;
354+ const invitationController = defaultInvitationController ( ) ;
414355
415356 const payload = new AcceptInvitationRequestDto ( {
416357 inviterId : id ,
0 commit comments