forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_organization_integration_test.go
296 lines (235 loc) · 10.1 KB
/
admin_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
//go:build integration
// +build integration
package tfe
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAdminOrganizations_List(t *testing.T) {
skipIfCloud(t)
client := testClient(t)
ctx := context.Background()
org, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
t.Run("with no list options", func(t *testing.T) {
adminOrgList, err := client.Admin.Organizations.List(ctx, nil)
require.NoError(t, err)
// Given that org creation occurs on every test, the ordering is not
// guaranteed. It may be that the `org` created in this test does not appear
// in this list, so we want to test that the items are filled.
assert.NotEmpty(t, adminOrgList.Items)
})
t.Run("with list options", func(t *testing.T) {
// creating second org so that the query can only find the main org
_, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
adminOrgList, err := client.Admin.Organizations.List(ctx, &AdminOrganizationListOptions{
Query: org.Name,
})
require.NoError(t, err)
assert.Equal(t, true, adminOrgItemsContainsName(adminOrgList.Items, org.Name))
assert.Equal(t, 1, adminOrgList.CurrentPage)
assert.Equal(t, 1, adminOrgList.TotalCount)
})
t.Run("with list options and org name that doesn't exist", func(t *testing.T) {
randomName := "random-org-name"
adminOrgList, err := client.Admin.Organizations.List(ctx, &AdminOrganizationListOptions{
Query: randomName,
})
require.NoError(t, err)
assert.Equal(t, false, adminOrgItemsContainsName(adminOrgList.Items, org.Name))
assert.Equal(t, 1, adminOrgList.CurrentPage)
assert.Equal(t, 0, adminOrgList.TotalCount)
})
t.Run("with owners included", func(t *testing.T) {
adminOrgList, err := client.Admin.Organizations.List(ctx, &AdminOrganizationListOptions{
Include: []AdminOrgIncludeOpt{AdminOrgOwners},
})
require.NoError(t, err)
require.NotEmpty(t, adminOrgList.Items)
assert.NotNil(t, adminOrgList.Items[0].Owners)
assert.NotEmpty(t, adminOrgList.Items[0].Owners[0].Email)
})
}
func TestAdminOrganizations_Read(t *testing.T) {
skipIfCloud(t)
client := testClient(t)
ctx := context.Background()
t.Run("it fails to read an organization with an invalid id", func(t *testing.T) {
adminOrg, err := client.Admin.Organizations.Read(ctx, "")
require.Error(t, err)
assert.EqualError(t, err, ErrInvalidOrg.Error())
assert.Nil(t, adminOrg)
})
t.Run("it returns ErrResourceNotFound for an organization that doesn't exist", func(t *testing.T) {
orgName := fmt.Sprintf("non-existing-%s", randomString(t))
adminOrg, err := client.Admin.Organizations.Read(ctx, orgName)
require.Error(t, err)
assert.EqualError(t, err, ErrResourceNotFound.Error())
assert.Nil(t, adminOrg)
})
t.Run("it reads an organization successfully", func(t *testing.T) {
org, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
adminOrg, err := client.Admin.Organizations.Read(ctx, org.Name)
require.NoError(t, err)
require.NotNilf(t, adminOrg, "Organization is not nil")
assert.Equal(t, adminOrg.Name, org.Name)
// attributes part of an AdminOrganization response that are not null
assert.NotNilf(t, adminOrg.AccessBetaTools, "AccessBetaTools is not nil")
assert.NotNilf(t, adminOrg.ExternalID, "ExternalID is not nil")
assert.NotNilf(t, adminOrg.IsDisabled, "IsDisabled is not nil")
assert.NotNilf(t, adminOrg.NotificationEmail, "NotificationEmail is not nil")
assert.NotNilf(t, adminOrg.SsoEnabled, "SsoEnabled is not nil")
assert.NotNilf(t, adminOrg.TerraformWorkerSudoEnabled, "TerraformWorkerSudoEnabledis not nil")
assert.Nilf(t, adminOrg.WorkspaceLimit, "WorkspaceLimit is nil")
})
}
func TestAdminOrganizations_Delete(t *testing.T) {
skipIfCloud(t)
client := testClient(t)
ctx := context.Background()
t.Run("it fails to delete an organization with an invalid id", func(t *testing.T) {
err := client.Admin.Organizations.Delete(ctx, "")
require.Error(t, err)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
t.Run("it returns ErrResourceNotFound during an attempt to delete an organization that doesn't exist", func(t *testing.T) {
orgName := fmt.Sprintf("non-existing-%s", randomString(t))
err := client.Admin.Organizations.Delete(ctx, orgName)
require.Error(t, err)
assert.EqualError(t, err, ErrResourceNotFound.Error())
})
t.Run("it deletes an organization successfully", func(t *testing.T) {
originalOrg, _ := createOrganization(t, client)
adminOrg, err := client.Admin.Organizations.Read(ctx, originalOrg.Name)
require.NoError(t, err)
require.NotNil(t, adminOrg)
assert.Equal(t, adminOrg.Name, originalOrg.Name)
err = client.Admin.Organizations.Delete(ctx, adminOrg.Name)
require.NoError(t, err)
// Cannot find deleted org
_, err = client.Admin.Organizations.Read(ctx, originalOrg.Name)
assert.Error(t, err)
assert.EqualError(t, err, ErrResourceNotFound.Error())
})
}
func TestAdminOrganizations_ModuleConsumers(t *testing.T) {
skipIfCloud(t)
client := testClient(t)
ctx := context.Background()
t.Run("returns error if invalid org string is used", func(t *testing.T) {
org1, org1TestCleanup := createOrganization(t, client)
defer org1TestCleanup()
err := client.Admin.Organizations.UpdateModuleConsumers(ctx, org1.Name, []string{"1Hello!"})
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
t.Run("can list and update module consumers", func(t *testing.T) {
org1, org1TestCleanup := createOrganization(t, client)
defer org1TestCleanup()
org2, org2TestCleanup := createOrganization(t, client)
defer org2TestCleanup()
err := client.Admin.Organizations.UpdateModuleConsumers(ctx, org1.Name, []string{org2.Name})
require.NoError(t, err)
adminModuleConsumerList, err := client.Admin.Organizations.ListModuleConsumers(ctx, org1.Name, nil)
require.NoError(t, err)
assert.Equal(t, len(adminModuleConsumerList.Items), 1)
assert.Equal(t, adminModuleConsumerList.Items[0].Name, org2.Name)
org3, org3TestCleanup := createOrganization(t, client)
defer org3TestCleanup()
err = client.Admin.Organizations.UpdateModuleConsumers(ctx, org1.Name, []string{org3.Name})
require.NoError(t, err)
adminModuleConsumerList, err = client.Admin.Organizations.ListModuleConsumers(ctx, org1.Name, nil)
require.NoError(t, err)
assert.Equal(t, len(adminModuleConsumerList.Items), 1)
assert.Equal(t, adminModuleConsumerList.Items[0].Name, org3.Name)
})
}
func TestAdminOrganizations_Update(t *testing.T) {
skipIfCloud(t)
client := testClient(t)
ctx := context.Background()
t.Run("it fails to update an organization with an invalid id", func(t *testing.T) {
_, err := client.Admin.Organizations.Update(ctx, "", AdminOrganizationUpdateOptions{})
require.Error(t, err)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
t.Run("it returns ErrResourceNotFound for during an update on an organization that doesn't exist", func(t *testing.T) {
orgName := fmt.Sprintf("non-existing-%s", randomString(t))
_, err := client.Admin.Organizations.Update(ctx, orgName, AdminOrganizationUpdateOptions{})
require.Error(t, err)
assert.EqualError(t, err, ErrResourceNotFound.Error())
})
t.Run("fetches and updates organization", func(t *testing.T) {
org, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
adminOrg, err := client.Admin.Organizations.Read(ctx, org.Name)
require.NoError(t, err)
require.NotNilf(t, adminOrg, "Org returned as nil")
accessBetaTools := true
globalModuleSharing := false
isDisabled := false
terraformBuildWorkerApplyTimeout := "24h"
terraformBuildWorkerPlanTimeout := "24h"
terraformWorkerSudoEnabled := true
opts := AdminOrganizationUpdateOptions{
AccessBetaTools: &accessBetaTools,
GlobalModuleSharing: &globalModuleSharing,
IsDisabled: &isDisabled,
TerraformBuildWorkerApplyTimeout: &terraformBuildWorkerApplyTimeout,
TerraformBuildWorkerPlanTimeout: &terraformBuildWorkerPlanTimeout,
TerraformWorkerSudoEnabled: terraformWorkerSudoEnabled,
}
adminOrg, err = client.Admin.Organizations.Update(ctx, org.Name, opts)
require.NotNilf(t, adminOrg, "Org returned as nil when it shouldn't be.")
require.NoError(t, err)
assert.Equal(t, accessBetaTools, adminOrg.AccessBetaTools)
assert.Equal(t, adminOrg.GlobalModuleSharing, &globalModuleSharing)
assert.Equal(t, isDisabled, adminOrg.IsDisabled)
assert.Equal(t, terraformBuildWorkerApplyTimeout, adminOrg.TerraformBuildWorkerApplyTimeout)
assert.Equal(t, terraformBuildWorkerPlanTimeout, adminOrg.TerraformBuildWorkerPlanTimeout)
assert.Equal(t, terraformWorkerSudoEnabled, adminOrg.TerraformWorkerSudoEnabled)
assert.Nil(t, adminOrg.WorkspaceLimit, "default workspace limit should be nil")
isDisabled = true
globalModuleSharing = true
workspaceLimit := 42
opts = AdminOrganizationUpdateOptions{
GlobalModuleSharing: &globalModuleSharing,
IsDisabled: &isDisabled,
WorkspaceLimit: &workspaceLimit,
}
adminOrg, err = client.Admin.Organizations.Update(ctx, org.Name, opts)
require.NoError(t, err)
require.NotNilf(t, adminOrg, "Org returned as nil when it shouldn't be.")
assert.Equal(t, adminOrg.GlobalModuleSharing, &globalModuleSharing)
assert.Equal(t, adminOrg.IsDisabled, isDisabled)
assert.Equal(t, &workspaceLimit, adminOrg.WorkspaceLimit)
globalModuleSharing = false
isDisabled = false
workspaceLimit = 0
opts = AdminOrganizationUpdateOptions{
GlobalModuleSharing: &globalModuleSharing,
IsDisabled: &isDisabled,
WorkspaceLimit: &workspaceLimit,
}
adminOrg, err = client.Admin.Organizations.Update(ctx, org.Name, opts)
require.NoError(t, err)
require.NotNilf(t, adminOrg, "Org returned as nil when it shouldn't be.")
assert.Equal(t, &globalModuleSharing, adminOrg.GlobalModuleSharing)
assert.Equal(t, adminOrg.IsDisabled, isDisabled)
assert.Equal(t, &workspaceLimit, adminOrg.WorkspaceLimit)
})
}
func adminOrgItemsContainsName(items []*AdminOrganization, name string) bool {
hasName := false
for _, item := range items {
if item.Name == name {
hasName = true
break
}
}
return hasName
}