@@ -22,6 +22,7 @@ import (
22
22
"os/exec"
23
23
"testing"
24
24
25
+ "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
25
26
"github.com/stretchr/testify/assert"
26
27
"github.com/stretchr/testify/require"
27
28
"go.mongodb.org/atlas-sdk/v20250312001/admin"
@@ -36,6 +37,7 @@ func TestAtlasOrgInvitations(t *testing.T) {
36
37
37
38
emailOrg := fmt .
Sprintf (
"test-%[email protected] " ,
n )
38
39
var orgInvitationID string
40
+ var orgInvitationIDFile string // For the file-based invite test
39
41
40
42
g .Run ("Invite" , func (t * testing.T ) { //nolint:thelper // g.Run replaces t.Run
41
43
cmd := exec .Command (cliPath ,
@@ -54,10 +56,73 @@ func TestAtlasOrgInvitations(t *testing.T) {
54
56
var invitation admin.OrganizationInvitation
55
57
require .NoError (t , json .Unmarshal (resp , & invitation ))
56
58
a .Equal (emailOrg , invitation .GetUsername ())
59
+ a .Equal ([]string {"ORG_MEMBER" }, invitation .GetRoles ())
57
60
require .NotEmpty (t , invitation .GetId ())
58
61
orgInvitationID = invitation .GetId ()
59
62
})
60
63
64
+ g .Run ("Invite with File" , func (t * testing.T ) { //nolint:thelper // g.Run replaces t.Run
65
+ a := assert .New (t )
66
+ // Create a unique email for this test
67
+ nFile := g .memoryRand ("randFile" , 1000 )
68
+ emailOrgFile := fmt .
Sprintf (
"test-file-%[email protected] " ,
nFile )
69
+
70
+ inviteData := admin.OrganizationInvitationRequest {
71
+ Username : pointer .Get (emailOrgFile ),
72
+ Roles : pointer .Get ([]string {"ORG_READ_ONLY" }),
73
+ }
74
+ inviteFilename := fmt .Sprintf ("%s/update-%s.json" , t .TempDir (), nFile )
75
+ createJSONFile (t , inviteData , inviteFilename )
76
+
77
+ cmd := exec .Command (cliPath ,
78
+ orgEntity ,
79
+ invitationsEntity ,
80
+ "invite" ,
81
+ "--file" , inviteFilename ,
82
+ "-o=json" )
83
+ cmd .Env = os .Environ ()
84
+ resp , err := RunAndGetStdOut (cmd )
85
+ require .NoError (t , err , string (resp ))
86
+
87
+ var invitation admin.OrganizationInvitation
88
+ require .NoError (t , json .Unmarshal (resp , & invitation ))
89
+ a .Equal (emailOrgFile , invitation .GetUsername ())
90
+ a .Equal (inviteData .GetRoles (), invitation .GetRoles ())
91
+ require .NotEmpty (t , invitation .GetId ())
92
+ orgInvitationIDFile = invitation .GetId () // Save ID for cleanup
93
+ })
94
+
95
+ g .Run ("Invite with File" , func (t * testing.T ) { //nolint:thelper // g.Run replaces t.Run
96
+ a := assert .New (t )
97
+ // Create a unique email for this test
98
+ nFile := g .memoryRand ("randFile" , 1000 )
99
+ emailOrgFile := fmt .
Sprintf (
"test-file-%[email protected] " ,
nFile )
100
+
101
+ inviteData := admin.OrganizationInvitationRequest {
102
+ Username : pointer .Get (emailOrgFile ),
103
+ Roles : pointer .Get ([]string {"ORG_READ_ONLY" }),
104
+ }
105
+ inviteFilename := fmt .Sprintf ("%s/update-%s.json" , t .TempDir (), nFile )
106
+ createJSONFile (t , inviteData , inviteFilename )
107
+
108
+ cmd := exec .Command (cliPath ,
109
+ orgEntity ,
110
+ invitationsEntity ,
111
+ "invite" ,
112
+ "--file" , inviteFilename ,
113
+ "-o=json" )
114
+ cmd .Env = os .Environ ()
115
+ resp , err := RunAndGetStdOut (cmd )
116
+ require .NoError (t , err , string (resp ))
117
+
118
+ var invitation admin.OrganizationInvitation
119
+ require .NoError (t , json .Unmarshal (resp , & invitation ))
120
+ a .Equal (emailOrgFile , invitation .GetUsername ())
121
+ a .Equal (inviteData .GetRoles (), invitation .GetRoles ())
122
+ require .NotEmpty (t , invitation .GetId ())
123
+ orgInvitationIDFile = invitation .GetId () // Save ID for cleanup
124
+ })
125
+
61
126
g .Run ("List" , func (t * testing.T ) { //nolint:thelper // g.Run replaces t.Run
62
127
cmd := exec .Command (cliPath ,
63
128
orgEntity ,
@@ -136,6 +201,72 @@ func TestAtlasOrgInvitations(t *testing.T) {
136
201
a .ElementsMatch ([]string {roleNameOrg }, invitation .GetRoles ())
137
202
})
138
203
204
+ const OrgGroupCreator = "ORG_GROUP_CREATOR"
205
+
206
+ g .Run ("Update with File" , func (t * testing.T ) { //nolint:thelper // g.Run replaces t.Run
207
+ require .NotEmpty (t , orgInvitationID , "orgInvitationID must be set by Invite test" )
208
+ a := assert .New (t )
209
+
210
+ nFile := g .memoryRand ("randFile" , 1000 )
211
+
212
+ // Define the update data, including GroupRoleAssignments if desired
213
+ updateRole := OrgGroupCreator
214
+ updateData := admin.OrganizationInvitationRequest {
215
+ Roles : pointer .Get ([]string {updateRole }),
216
+ }
217
+ updateFilename := fmt .Sprintf ("%s/update-%s.json" , t .TempDir (), nFile )
218
+ createJSONFile (t , updateData , updateFilename )
219
+
220
+ cmd := exec .Command (cliPath ,
221
+ orgEntity ,
222
+ invitationsEntity ,
223
+ "update" ,
224
+ orgInvitationID , // Use ID from the original Invite test
225
+ "--file" , updateFilename ,
226
+ "-o=json" )
227
+ cmd .Env = os .Environ ()
228
+ resp , err := RunAndGetStdOut (cmd )
229
+ require .NoError (t , err , string (resp ))
230
+
231
+ var invitation admin.OrganizationInvitation
232
+ require .NoError (t , json .Unmarshal (resp , & invitation ))
233
+ a .Equal (orgInvitationID , invitation .GetId ())
234
+ a .ElementsMatch (updateData .GetRoles (), invitation .GetRoles ()) // Check if roles were updated
235
+ // Add assertions for GroupRoleAssignments if included in updateData
236
+ })
237
+
238
+ g .Run ("Update with File" , func (t * testing.T ) { //nolint:thelper // g.Run replaces t.Run
239
+ require .NotEmpty (t , orgInvitationID , "orgInvitationID must be set by Invite test" )
240
+ a := assert .New (t )
241
+
242
+ nFile := g .memoryRand ("randFile" , 1000 )
243
+
244
+ // Define the update data, including GroupRoleAssignments if desired
245
+ updateRole := OrgGroupCreator
246
+ updateData := admin.OrganizationInvitationRequest {
247
+ Roles : pointer .Get ([]string {updateRole }),
248
+ }
249
+ updateFilename := fmt .Sprintf ("%s/update-%s.json" , t .TempDir (), nFile )
250
+ createJSONFile (t , updateData , updateFilename )
251
+
252
+ cmd := exec .Command (cliPath ,
253
+ orgEntity ,
254
+ invitationsEntity ,
255
+ "update" ,
256
+ orgInvitationID , // Use ID from the original Invite test
257
+ "--file" , updateFilename ,
258
+ "-o=json" )
259
+ cmd .Env = os .Environ ()
260
+ resp , err := RunAndGetStdOut (cmd )
261
+ require .NoError (t , err , string (resp ))
262
+
263
+ var invitation admin.OrganizationInvitation
264
+ require .NoError (t , json .Unmarshal (resp , & invitation ))
265
+ a .Equal (orgInvitationID , invitation .GetId ())
266
+ a .ElementsMatch (updateData .GetRoles (), invitation .GetRoles ()) // Check if roles were updated
267
+ // Add assertions for GroupRoleAssignments if included in updateData
268
+ })
269
+
139
270
g .Run ("Delete" , func (t * testing.T ) { //nolint:thelper // g.Run replaces t.Run
140
271
cmd := exec .Command (cliPath ,
141
272
orgEntity ,
@@ -150,4 +281,20 @@ func TestAtlasOrgInvitations(t *testing.T) {
150
281
expected := fmt .Sprintf ("Invitation '%s' deleted\n " , orgInvitationID )
151
282
a .Equal (expected , string (resp ))
152
283
})
284
+
285
+ g .Run ("Delete Invitation from File Test" , func (t * testing.T ) { //nolint:thelper // g.Run replaces t.Run
286
+ require .NotEmpty (t , orgInvitationIDFile , "orgInvitationIDFile must be set by Invite with File test" )
287
+ cmd := exec .Command (cliPath ,
288
+ orgEntity ,
289
+ invitationsEntity ,
290
+ "delete" ,
291
+ orgInvitationIDFile ,
292
+ "--force" )
293
+ cmd .Env = os .Environ ()
294
+ resp , err := RunAndGetStdOut (cmd )
295
+ a := assert .New (t )
296
+ require .NoError (t , err , string (resp ))
297
+ expected := fmt .Sprintf ("Invitation '%s' deleted\n " , orgInvitationIDFile )
298
+ a .Equal (expected , string (resp ))
299
+ })
153
300
}
0 commit comments