/
organization_tags_integration_test.go
187 lines (142 loc) · 4.7 KB
/
organization_tags_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
package tfe
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOrganizationTagsList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
assert.NotNil(t, orgTest)
workspaceTest, workspaceTestCleanup := createWorkspace(t, client, orgTest)
defer workspaceTestCleanup()
assert.NotNil(t, workspaceTest)
var tags []*Tag
for i := 0; i < 10; i++ {
tags = append(tags, &Tag{
Name: fmt.Sprintf("tag%d", i),
})
}
err := client.Workspaces.AddTags(ctx, workspaceTest.ID, WorkspaceAddTagsOptions{
Tags: tags,
})
require.NoError(t, err)
// this is a tag id we'll use in the filter param of the second test
var testTagID string
t.Run("with no query params", func(t *testing.T) {
tags, err := client.OrganizationTags.List(ctx, orgTest.Name, nil)
require.NoError(t, err)
assert.Equal(t, 10, len(tags.Items))
testTagID = tags.Items[0].ID
for _, tag := range tags.Items {
assert.NotNil(t, tag.ID)
assert.NotNil(t, tag.Name)
assert.GreaterOrEqual(t, tag.InstanceCount, 1)
t.Run("ensure org relation is properly decoded", func(t *testing.T) {
assert.NotNil(t, tag.Organization)
})
}
})
t.Run("with query params", func(t *testing.T) {
tags, err := client.OrganizationTags.List(ctx, orgTest.Name, &OrganizationTagsListOptions{
ListOptions: ListOptions{
PageNumber: 1,
PageSize: 5,
},
Filter: testTagID,
})
require.NoError(t, err)
assert.Equal(t, 5, len(tags.Items))
for _, tag := range tags.Items {
// ensure tag specified in filter param was omitted from results
assert.NotNil(t, tag.ID, testTagID)
t.Run("ensure org relation is properly decoded", func(t *testing.T) {
assert.NotNil(t, tag.Organization)
})
}
})
}
func TestOrganizationTagsDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
assert.NotNil(t, orgTest)
workspaceTest, workspaceTestCleanup := createWorkspace(t, client, orgTest)
defer workspaceTestCleanup()
assert.NotNil(t, workspaceTest)
var tags []*Tag
for i := 0; i < 10; i++ {
tags = append(tags, &Tag{
Name: fmt.Sprintf("tag%d", i),
})
}
err := client.Workspaces.AddTags(ctx, workspaceTest.ID, WorkspaceAddTagsOptions{
Tags: tags,
})
require.NoError(t, err)
t.Run("delete tags by id", func(t *testing.T) {
tags, err := client.OrganizationTags.List(ctx, orgTest.Name, nil)
require.NoError(t, err)
var tagIds []string
// since we added 10 tags to the org, grab a subset
for i := 0; i < 5; i++ {
assert.NotNil(t, tags.Items[i].ID)
tagIds = append(tagIds, tags.Items[i].ID)
}
err = client.OrganizationTags.Delete(ctx, orgTest.Name, OrganizationTagsDeleteOptions{
IDs: tagIds,
})
require.NoError(t, err)
//sanity check ensure tags were deleted from the organization
tags, err = client.OrganizationTags.List(ctx, orgTest.Name, nil)
require.NoError(t, err)
assert.Equal(t, 5, len(tags.Items))
})
}
func TestOrganizationTagsAddWorkspace(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
assert.NotNil(t, orgTest)
workspaceTest, workspaceTestCleanup := createWorkspace(t, client, orgTest)
defer workspaceTestCleanup()
assert.NotNil(t, workspaceTest)
var tags []*Tag
for i := 0; i < 2; i++ {
tags = append(tags, &Tag{
Name: fmt.Sprintf("tag%d", i),
})
}
err := client.Workspaces.AddTags(ctx, workspaceTest.ID, WorkspaceAddTagsOptions{
Tags: tags,
})
require.NoError(t, err)
t.Run("add tags to new workspaces", func(t *testing.T) {
// fetch tag ids to associate to workspace
tags, err := client.OrganizationTags.List(ctx, orgTest.Name, nil)
require.NoError(t, err)
tagID := tags.Items[0].ID
// create the workspaces we'll use to associate tags
workspaceToAdd1, workspaceToAdd1Cleanup := createWorkspace(t, client, orgTest)
defer workspaceToAdd1Cleanup()
workspaceToAdd2, workspaceToAdd2Cleanup := createWorkspace(t, client, orgTest)
defer workspaceToAdd2Cleanup()
err = client.OrganizationTags.AddWorkspaces(ctx, tagID, AddWorkspacesToTagOptions{
WorkspaceIDs: []string{workspaceToAdd1.ID, workspaceToAdd2.ID},
})
require.NoError(t, err)
//Ensure the tag was properly associated with the workspaces
fetched, err := client.Workspaces.Tags(ctx, workspaceToAdd1.ID, nil)
require.NoError(t, err)
assert.Equal(t, fetched.Items[0].ID, tagID)
fetched, err = client.Workspaces.Tags(ctx, workspaceToAdd2.ID, nil)
require.NoError(t, err)
assert.Equal(t, fetched.Items[0].ID, tagID)
})
}