forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganization_integration_test.go
620 lines (508 loc) · 17.9 KB
/
organization_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"bytes"
"context"
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOrganizationsList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest1, orgTest1Cleanup := createOrganization(t, client)
t.Cleanup(orgTest1Cleanup)
orgTest2, orgTest2Cleanup := createOrganization(t, client)
t.Cleanup(orgTest2Cleanup)
t.Run("with no list options", func(t *testing.T) {
orgl, err := client.Organizations.List(ctx, nil)
require.NoError(t, err)
assert.Contains(t, orgl.Items, orgTest1)
assert.Contains(t, orgl.Items, orgTest2)
t.Skip("paging not supported yet in API")
assert.Equal(t, 1, orgl.CurrentPage)
assert.Equal(t, 2, orgl.TotalCount)
})
t.Run("with list options", func(t *testing.T) {
t.Skip("paging not supported yet in API")
// Request a page number which is out of range. The result should
// be successful, but return no results if the paging options are
// properly passed along.
orgl, err := client.Organizations.List(ctx, &OrganizationListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Empty(t, orgl)
assert.Equal(t, 999, orgl.CurrentPage)
assert.Equal(t, 2, orgl.TotalCount)
})
t.Run("when querying on a valid org name", func(t *testing.T) {
org, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
orgList, err := client.Organizations.List(ctx, &OrganizationListOptions{
Query: org.Name,
})
require.NoError(t, err)
assert.Equal(t, true, orgItemsContainsName(orgList.Items, org.Name))
})
t.Run("when querying on a valid email", func(t *testing.T) {
org, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
orgList, err := client.Organizations.List(ctx, &OrganizationListOptions{
Query: org.Email,
})
require.NoError(t, err)
assert.Equal(t, true, orgItemsContainsEmail(orgList.Items, org.Email))
})
t.Run("with invalid query name", func(t *testing.T) {
org, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
orgList, err := client.Organizations.List(ctx, &OrganizationListOptions{
Query: org.Name,
})
require.NoError(t, err)
assert.NotEqual(t, orgList.Items, orgTest1.Name)
})
t.Run("with invalid query email", func(t *testing.T) {
org, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
orgList, err := client.Organizations.List(ctx, &OrganizationListOptions{
Query: org.Email,
})
require.NoError(t, err)
assert.NotEqual(t, orgList.Items, orgTest1.Email)
})
}
func TestOrganizationsCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("with valid options", func(t *testing.T) {
options := OrganizationCreateOptions{
Name: String(randomString(t)),
Email: String(randomString(t) + "@tfe.local"),
}
org, err := client.Organizations.Create(ctx, options)
require.NoError(t, err)
t.Cleanup(func() {
err := client.Organizations.Delete(ctx, org.Name)
if err != nil {
t.Logf("error deleting organization (%s): %s", org.Name, err)
}
})
assert.Equal(t, *options.Name, org.Name)
assert.Equal(t, *options.Email, org.Email)
})
t.Run("when no email is provided", func(t *testing.T) {
org, err := client.Organizations.Create(ctx, OrganizationCreateOptions{
Name: String("foo"),
})
assert.Nil(t, org)
assert.Equal(t, err, ErrRequiredEmail)
})
t.Run("when no name is provided", func(t *testing.T) {
_, err := client.Organizations.Create(ctx, OrganizationCreateOptions{
Email: String("foo@bar.com"),
})
assert.EqualError(t, err, ErrRequiredName.Error())
})
t.Run("with invalid name", func(t *testing.T) {
org, err := client.Organizations.Create(ctx, OrganizationCreateOptions{
Name: String(badIdentifier),
Email: String("foo@bar.com"),
})
assert.Nil(t, org)
assert.EqualError(t, err, ErrInvalidName.Error())
})
}
func TestOrganizationsRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
t.Run("when the org exists", func(t *testing.T) {
org, err := client.Organizations.Read(ctx, orgTest.Name)
require.NoError(t, err)
assert.Equal(t, orgTest, org)
assert.NotEmpty(t, org.Permissions)
t.Run("permissions are properly decoded", func(t *testing.T) {
assert.True(t, org.Permissions.CanDestroy)
})
t.Run("timestamps are populated", func(t *testing.T) {
assert.NotEmpty(t, org.CreatedAt)
// By default accounts are in the free tier and are not in a trial
assert.Empty(t, org.TrialExpiresAt)
})
})
t.Run("with invalid name", func(t *testing.T) {
org, err := client.Organizations.Read(ctx, badIdentifier)
assert.Nil(t, org)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
t.Run("when the org does not exist", func(t *testing.T) {
_, err := client.Organizations.Read(ctx, randomString(t))
assert.Error(t, err)
})
t.Run("reads default project", func(t *testing.T) {
skipUnlessBeta(t)
org, err := client.Organizations.ReadWithOptions(ctx, orgTest.Name, OrganizationReadOptions{Include: []OrganizationIncludeOpt{OrganizationDefaultProject}})
require.NoError(t, err)
assert.Equal(t, orgTest.Name, org.Name)
require.NotNil(t, org.DefaultProject)
assert.NotNil(t, org.DefaultProject.Name)
})
}
func TestOrganizationsUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("with TFC-only options", func(t *testing.T) {
skipIfEnterprise(t)
orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
options := OrganizationUpdateOptions{
SendPassingStatusesForUntriggeredSpeculativePlans: Bool(false),
}
org, err := client.Organizations.Update(ctx, orgTest.Name, options)
require.NoError(t, err)
assert.Equal(t, false, org.SendPassingStatusesForUntriggeredSpeculativePlans)
})
t.Run("with valid options", func(t *testing.T) {
orgTest, orgTestCleanup := createOrganization(t, client)
options := OrganizationUpdateOptions{
Name: String(randomString(t)),
Email: String(randomString(t) + "@tfe.local"),
SessionTimeout: Int(3600),
SessionRemember: Int(3600),
}
org, err := client.Organizations.Update(ctx, orgTest.Name, options)
if err != nil {
orgTestCleanup()
}
require.NoError(t, err)
// Make sure we clean up the renamed org.
defer func() {
err := client.Organizations.Delete(ctx, org.Name)
if err != nil {
t.Logf("Error deleting organization (%s): %s", org.Name, err)
}
}()
// Also get a fresh result from the API to ensure we get the
// expected values back.
refreshed, err := client.Organizations.Read(ctx, *options.Name)
require.NoError(t, err)
for _, item := range []*Organization{
org,
refreshed,
} {
assert.Equal(t, *options.Name, item.Name)
assert.Equal(t, *options.Email, item.Email)
assert.Equal(t, *options.SessionTimeout, item.SessionTimeout)
assert.Equal(t, *options.SessionRemember, item.SessionRemember)
}
})
t.Run("with invalid name", func(t *testing.T) {
org, err := client.Organizations.Update(ctx, badIdentifier, OrganizationUpdateOptions{})
assert.Nil(t, org)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
t.Run("when only updating a subset of fields", func(t *testing.T) {
orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
org, err := client.Organizations.Update(ctx, orgTest.Name, OrganizationUpdateOptions{})
require.NoError(t, err)
assert.Equal(t, orgTest.Name, org.Name)
assert.Equal(t, orgTest.Email, org.Email)
})
}
func TestOrganizationsDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("with valid options", func(t *testing.T) {
orgTest, _ := createOrganization(t, client)
err := client.Organizations.Delete(ctx, orgTest.Name)
require.NoError(t, err)
// Try fetching the org again - it should error.
_, err = client.Organizations.Read(ctx, orgTest.Name)
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("with invalid name", func(t *testing.T) {
err := client.Organizations.Delete(ctx, badIdentifier)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
}
func TestOrganizationsReadCapacity(t *testing.T) {
t.Skip("Capacity queues are not available in the API")
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
wTest1, wTestCleanup1 := createWorkspace(t, client, orgTest)
t.Cleanup(wTestCleanup1)
wTest2, wTestCleanup2 := createWorkspace(t, client, orgTest)
t.Cleanup(wTestCleanup2)
wTest3, wTestCleanup3 := createWorkspace(t, client, orgTest)
t.Cleanup(wTestCleanup3)
wTest4, wTestCleanup4 := createWorkspace(t, client, orgTest)
t.Cleanup(wTestCleanup4)
t.Run("without queued runs", func(t *testing.T) {
c, err := client.Organizations.ReadCapacity(ctx, orgTest.Name)
require.NoError(t, err)
assert.Equal(t, 0, c.Pending)
assert.Equal(t, 0, c.Running)
})
// For this test FRQ should be enabled and have a
// limit of 2 concurrent runs per organization.
t.Run("with queued runs", func(t *testing.T) {
_, runCleanup1 := createRun(t, client, wTest1)
t.Cleanup(runCleanup1)
_, runCleanup2 := createRun(t, client, wTest2)
t.Cleanup(runCleanup2)
_, runCleanup3 := createRun(t, client, wTest3)
t.Cleanup(runCleanup3)
_, runCleanup4 := createRun(t, client, wTest4)
t.Cleanup(runCleanup4)
c, err := client.Organizations.ReadCapacity(ctx, orgTest.Name)
require.NoError(t, err)
assert.Equal(t, 2, c.Pending)
assert.Equal(t, 2, c.Running)
})
t.Run("with invalid name", func(t *testing.T) {
org, err := client.Organizations.Read(ctx, badIdentifier)
assert.Nil(t, org)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
t.Run("when the org does not exist", func(t *testing.T) {
_, err := client.Organizations.Read(ctx, randomString(t))
assert.Error(t, err)
})
}
func TestOrganizationsReadEntitlements(t *testing.T) {
skipIfEnterprise(t)
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
t.Run("when the org exists", func(t *testing.T) {
entitlements, err := client.Organizations.ReadEntitlements(ctx, orgTest.Name)
require.NoError(t, err)
assert.NotEmpty(t, entitlements.ID)
assert.True(t, entitlements.Agents)
assert.True(t, entitlements.AuditLogging)
assert.True(t, entitlements.CostEstimation)
assert.True(t, entitlements.Operations)
assert.True(t, entitlements.PrivateModuleRegistry)
assert.True(t, entitlements.SSO)
assert.True(t, entitlements.Sentinel)
assert.True(t, entitlements.StateStorage)
assert.True(t, entitlements.Teams)
assert.True(t, entitlements.VCSIntegrations)
})
t.Run("with invalid name", func(t *testing.T) {
entitlements, err := client.Organizations.ReadEntitlements(ctx, badIdentifier)
assert.Nil(t, entitlements)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
t.Run("when the org does not exist", func(t *testing.T) {
_, err := client.Organizations.ReadEntitlements(ctx, randomString(t))
assert.Equal(t, ErrResourceNotFound, err)
})
}
func TestOrganizationsReadRunQueue(t *testing.T) {
t.Skip("Capacity queues are not available in the API")
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
wTest1, wTestCleanup1 := createWorkspace(t, client, orgTest)
t.Cleanup(wTestCleanup1)
wTest2, wTestCleanup2 := createWorkspace(t, client, orgTest)
t.Cleanup(wTestCleanup2)
wTest3, wTestCleanup3 := createWorkspace(t, client, orgTest)
t.Cleanup(wTestCleanup3)
wTest4, wTestCleanup4 := createWorkspace(t, client, orgTest)
t.Cleanup(wTestCleanup4)
t.Run("without queued runs", func(t *testing.T) {
rq, err := client.Organizations.ReadRunQueue(ctx, orgTest.Name, ReadRunQueueOptions{})
require.NoError(t, err)
assert.Equal(t, 0, len(rq.Items))
})
// Create a couple or runs to fill the queue.
rTest1, rTestCleanup1 := createRun(t, client, wTest1)
t.Cleanup(rTestCleanup1)
rTest2, rTestCleanup2 := createRun(t, client, wTest2)
t.Cleanup(rTestCleanup2)
rTest3, rTestCleanup3 := createRun(t, client, wTest3)
t.Cleanup(rTestCleanup3)
rTest4, rTestCleanup4 := createRun(t, client, wTest4)
t.Cleanup(rTestCleanup4)
// For this test FRQ should be enabled and have a
// limit of 2 concurrent runs per organization.
t.Run("with queued runs", func(t *testing.T) {
rq, err := client.Organizations.ReadRunQueue(ctx, orgTest.Name, ReadRunQueueOptions{})
require.NoError(t, err)
found := []string{}
for _, r := range rq.Items {
found = append(found, r.ID)
}
assert.Contains(t, found, rTest1.ID)
assert.Contains(t, found, rTest2.ID)
assert.Contains(t, found, rTest3.ID)
assert.Contains(t, found, rTest4.ID)
})
t.Run("without queue options", func(t *testing.T) {
rq, err := client.Organizations.ReadRunQueue(ctx, orgTest.Name, ReadRunQueueOptions{})
require.NoError(t, err)
found := []string{}
for _, r := range rq.Items {
found = append(found, r.ID)
}
assert.Contains(t, found, rTest1.ID)
assert.Contains(t, found, rTest2.ID)
assert.Contains(t, found, rTest3.ID)
assert.Contains(t, found, rTest4.ID)
assert.Equal(t, 1, rq.CurrentPage)
assert.Equal(t, 4, rq.TotalCount)
})
t.Run("with queue options", func(t *testing.T) {
// Request a page number which is out of range. The result should
// be successful, but return no results if the paging options are
// properly passed along.
rq, err := client.Organizations.ReadRunQueue(ctx, orgTest.Name, ReadRunQueueOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Empty(t, rq.Items)
assert.Equal(t, 999, rq.CurrentPage)
assert.Equal(t, 4, rq.TotalCount)
})
t.Run("with invalid name", func(t *testing.T) {
org, err := client.Organizations.Read(ctx, badIdentifier)
assert.Nil(t, org)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
t.Run("when the org does not exist", func(t *testing.T) {
_, err := client.Organizations.Read(ctx, randomString(t))
assert.Error(t, err)
})
}
func TestOrganization_Unmarshal(t *testing.T) {
data := map[string]interface{}{
"data": map[string]interface{}{
"type": "organizations",
"id": "org-name",
"attributes": map[string]interface{}{
"assessments-enforced": true,
"collaborator-auth-policy": AuthPolicyPassword,
"cost-estimation-enabled": true,
"created-at": "2018-03-02T23:42:06.651Z",
"email": "test@hashicorp.com",
"permissions": map[string]interface{}{
"can-create-team": true,
},
},
},
}
byteData, err := json.Marshal(data)
require.NoError(t, err)
responseBody := bytes.NewReader(byteData)
org := &Organization{}
err = unmarshalResponse(responseBody, org)
require.NoError(t, err)
iso8601TimeFormat := "2006-01-02T15:04:05Z"
parsedTime, err := time.Parse(iso8601TimeFormat, "2018-03-02T23:42:06.651Z")
require.NoError(t, err)
assert.Equal(t, org.Name, "org-name")
assert.Equal(t, org.AssessmentsEnforced, true)
assert.Equal(t, org.CreatedAt, parsedTime)
assert.Equal(t, org.CollaboratorAuthPolicy, AuthPolicyPassword)
assert.Equal(t, org.CostEstimationEnabled, true)
assert.Equal(t, org.Email, "test@hashicorp.com")
assert.NotEmpty(t, org.Permissions)
assert.Equal(t, org.Permissions.CanCreateTeam, true)
}
func TestOrganizationsReadRunTasksPermission(t *testing.T) {
skipUnlessBeta(t)
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
t.Run("when the org exists", func(t *testing.T) {
org, err := client.Organizations.Read(ctx, orgTest.Name)
require.NoError(t, err)
assert.Equal(t, orgTest, org)
assert.NotEmpty(t, org.Permissions)
t.Run("permissions are properly decoded", func(t *testing.T) {
assert.True(t, org.Permissions.CanManageRunTasks)
})
})
}
func TestOrganizationsReadRunTasksEntitlement(t *testing.T) {
skipIfEnterprise(t)
skipUnlessBeta(t)
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
t.Run("when the org exists", func(t *testing.T) {
entitlements, err := client.Organizations.ReadEntitlements(ctx, orgTest.Name)
require.NoError(t, err)
assert.NotEmpty(t, entitlements.ID)
assert.True(t, entitlements.RunTasks)
})
}
func TestOrganizationsAllowForceDeleteSetting(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("creates and updates allow force delete", func(t *testing.T) {
options := OrganizationCreateOptions{
Name: String(randomString(t)),
Email: String(randomString(t) + "@tfe.local"),
AllowForceDeleteWorkspaces: Bool(true),
}
org, err := client.Organizations.Create(ctx, options)
require.NoError(t, err)
t.Cleanup(func() {
err := client.Organizations.Delete(ctx, org.Name)
if err != nil {
t.Errorf("error deleting organization (%s): %s", org.Name, err)
}
})
assert.Equal(t, *options.Name, org.Name)
assert.Equal(t, *options.Email, org.Email)
assert.True(t, org.AllowForceDeleteWorkspaces)
org, err = client.Organizations.Update(ctx, org.Name, OrganizationUpdateOptions{AllowForceDeleteWorkspaces: Bool(false)})
require.NoError(t, err)
assert.False(t, org.AllowForceDeleteWorkspaces)
org, err = client.Organizations.Read(ctx, org.Name)
require.NoError(t, err)
assert.False(t, org.AllowForceDeleteWorkspaces)
})
}
func orgItemsContainsName(items []*Organization, name string) bool {
hasName := false
for _, item := range items {
if item.Name == name {
hasName = true
break
}
}
return hasName
}
func orgItemsContainsEmail(items []*Organization, email string) bool {
hasEmail := false
for _, item := range items {
if item.Email == email {
hasEmail = true
break
}
}
return hasEmail
}