@@ -56,6 +56,75 @@ describe("moveUserToMatchingOrg", () => {
5656 ] ,
5757 } ;
5858
59+ it ( "should always invite as MEMBER role, not an elevated role" , async ( ) => {
60+ const org = {
61+ id : "org123" ,
62+ slug : "test-org" ,
63+ requestedSlug : null
64+ }
65+
66+ organizationScenarios . organizationRepository . findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail . fakeReturnOrganization (
67+ org ,
68+ { email }
69+ )
70+
71+ await moveUserToMatchingOrg ( { email } )
72+
73+ const call = vi . mocked ( inviteMembersWithNoInviterPermissionCheck ) . mock . calls [ 0 ] [ 0 ]
74+ expect ( call . invitations [ 0 ] . role ) . toBe ( MembershipRole . MEMBER )
75+ } )
76+
77+ it ( "should pass inviterName as null" , async ( ) => {
78+ const org = {
79+ id : "org123" ,
80+ slug : "test-org" ,
81+ requestedSlug : null ,
82+ } ;
83+ organizationScenarios . organizationRepository . findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail . fakeReturnOrganization (
84+ org ,
85+ { email }
86+ ) ;
87+
88+ await moveUserToMatchingOrg ( { email } ) ;
89+
90+ const call = vi . mocked ( inviteMembersWithNoInviterPermissionCheck ) . mock . calls [ 0 ] [ 0 ] ;
91+ expect ( call . inviterName ) . toBeNull ( ) ;
92+ } ) ;
93+
94+ it ( "should pass creationSource as WEBAPP" , async ( ) => {
95+ const org = {
96+ id : "org123" ,
97+ slug : "test-org" ,
98+ requestedSlug : null ,
99+ } ;
100+ organizationScenarios . organizationRepository . findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail . fakeReturnOrganization (
101+ org ,
102+ { email }
103+ ) ;
104+
105+ await moveUserToMatchingOrg ( { email } ) ;
106+
107+ const call = vi . mocked ( inviteMembersWithNoInviterPermissionCheck ) . mock . calls [ 0 ] [ 0 ] ;
108+ expect ( call . creationSource ) . toBe ( CreationSource . WEBAPP ) ;
109+ } ) ;
110+
111+ it ( "should pass exactly one invitation" , async ( ) => {
112+ const org = {
113+ id : "org123" ,
114+ slug : "test-org" ,
115+ requestedSlug : null ,
116+ } ;
117+ organizationScenarios . organizationRepository . findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail . fakeReturnOrganization (
118+ org ,
119+ { email }
120+ ) ;
121+
122+ await moveUserToMatchingOrg ( { email } ) ;
123+
124+ const call = vi . mocked ( inviteMembersWithNoInviterPermissionCheck ) . mock . calls [ 0 ] [ 0 ] ;
125+ expect ( call . invitations ) . toHaveLength ( 1 ) ;
126+ } ) ;
127+
59128 it ( "when organization has a slug and requestedSlug(slug is used)" , async ( ) => {
60129 const org = {
61130 id : "org123" ,
@@ -98,4 +167,23 @@ describe("moveUserToMatchingOrg", () => {
98167 } ) ;
99168 } ) ;
100169 } ) ;
170+
171+ describe ( "error handling" , ( ) => {
172+ it ( "should propagate errors thrown by inviteMembersWithNoInviterPermissionCheck" , async ( ) => {
173+ const org = {
174+ id : "org123" ,
175+ slug : "test-org" ,
176+ requestedSlug : null ,
177+ } ;
178+ organizationScenarios . organizationRepository . findUniqueNonPlatformOrgsByMatchingAutoAcceptEmail . fakeReturnOrganization (
179+ org ,
180+ { email }
181+ ) ;
182+
183+ const error = new Error ( "Invite failed" ) ;
184+ vi . mocked ( inviteMembersWithNoInviterPermissionCheck ) . mockRejectedValue ( error ) ;
185+
186+ await expect ( moveUserToMatchingOrg ( { email } ) ) . rejects . toThrow ( "Invite failed" ) ;
187+ } ) ;
188+ } )
101189} ) ;
0 commit comments