-
Notifications
You must be signed in to change notification settings - Fork 3
/
team_test.go
295 lines (248 loc) · 7.83 KB
/
team_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
package scalr
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
defaultTeamID = "team-t67mjtnokncjpd8"
)
func TestTeamsList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
teamTest1, teamTest1Cleanup := createTeam(t, client, nil)
defer teamTest1Cleanup()
teamTest2, teamTest2Cleanup := createTeam(t, client, nil)
defer teamTest2Cleanup()
t.Run("with empty options", func(t *testing.T) {
tl, err := client.Teams.List(ctx, TeamListOptions{})
require.NoError(t, err)
var tIDs []string
for _, t := range tl.Items {
tIDs = append(tIDs, t.ID)
}
assert.Equal(t, 1, tl.CurrentPage)
assert.Contains(t, tIDs, teamTest1.ID)
assert.Contains(t, tIDs, teamTest2.ID)
})
t.Run("with team filter", func(t *testing.T) {
tl, err := client.Teams.List(ctx, TeamListOptions{
Team: String(teamTest1.ID),
})
require.NoError(t, err)
assert.Equal(t, 1, tl.CurrentPage)
assert.Equal(t, 1, tl.TotalCount)
assert.Equal(t, teamTest1.ID, tl.Items[0].ID)
})
t.Run("with name filter", func(t *testing.T) {
tl, err := client.Teams.List(ctx, TeamListOptions{
Name: String(teamTest1.Name),
})
require.NoError(t, err)
var tIDs []string
// Set of team names
tNames := make(map[string]bool)
for _, t := range tl.Items {
tIDs = append(tIDs, t.ID)
if !tNames[t.Name] {
tNames[t.Name] = true
}
}
assert.Equal(t, 1, tl.CurrentPage)
assert.True(t, tl.TotalCount >= 1)
assert.Contains(t, tIDs, teamTest1.ID)
assert.Equal(t, 1, len(tNames))
assert.Contains(t, tNames, teamTest1.Name)
})
t.Run("with identity provider filter", func(t *testing.T) {
tl, err := client.Teams.List(ctx, TeamListOptions{
IdentityProvider: String(defaultIdentityProviderScalrID),
})
require.NoError(t, err)
var tIDs []string
// Set of IDP IDs
idpIDs := make(map[string]bool)
for _, team := range tl.Items {
tIDs = append(tIDs, team.ID)
if !idpIDs[team.IdentityProvider.ID] {
idpIDs[team.IdentityProvider.ID] = true
}
}
assert.Equal(t, 1, tl.CurrentPage)
assert.True(t, tl.TotalCount >= 1)
assert.Contains(t, tIDs, teamTest1.ID)
assert.Contains(t, tIDs, teamTest2.ID)
assert.Equal(t, 1, len(idpIDs))
assert.Contains(t, idpIDs, defaultIdentityProviderScalrID)
})
t.Run("with account filter", func(t *testing.T) {
tl, err := client.Teams.List(ctx, TeamListOptions{
Account: String(defaultAccountID),
})
require.NoError(t, err)
var tIDs []string
accIDs := make(map[string]bool)
for _, t := range tl.Items {
tIDs = append(tIDs, t.ID)
if !accIDs[t.Account.ID] {
accIDs[t.Account.ID] = true
}
}
assert.Equal(t, 1, tl.CurrentPage)
assert.True(t, tl.TotalCount >= 1)
assert.Contains(t, tIDs, teamTest1.ID)
assert.Contains(t, tIDs, teamTest2.ID)
assert.Equal(t, 1, len(accIDs))
assert.Contains(t, accIDs, defaultAccountID)
})
t.Run("without a valid identity provider", func(t *testing.T) {
tl, err := client.Teams.List(ctx, TeamListOptions{
IdentityProvider: String(badIdentifier),
})
assert.NoError(t, err)
assert.Len(t, tl.Items, 0)
})
t.Run("with list options", func(t *testing.T) {
tl, err := client.Teams.List(ctx, TeamListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Empty(t, tl.Items)
assert.Equal(t, 999, tl.CurrentPage)
assert.True(t, tl.TotalCount >= 1)
})
}
func TestTeamsCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("with valid options", func(t *testing.T) {
options := TeamCreateOptions{
Account: &Account{ID: defaultAccountID},
Name: String("foo" + randomString(t)),
Description: String("bar"),
Users: []*User{{ID: defaultUserID}},
}
team, err := client.Teams.Create(ctx, options)
require.NoError(t, err)
// Get a refreshed view from the API.
refreshed, err := client.Teams.Read(ctx, team.ID)
require.NoError(t, err)
for _, item := range []*Team{
team,
refreshed,
} {
assert.NotEmpty(t, item.ID)
assert.Equal(t, *options.Name, item.Name)
assert.Equal(t, *options.Description, item.Description)
assert.Equal(t, options.Account, item.Account)
assert.Equal(t, options.Users, item.Users)
}
err = client.Teams.Delete(ctx, team.ID)
require.NoError(t, err)
})
t.Run("with empty options", func(t *testing.T) {
team, err := client.Teams.Create(ctx, TeamCreateOptions{})
assert.Nil(t, team)
assert.EqualError(t, err, "name is required")
})
t.Run("when options has an invalid account", func(t *testing.T) {
team, err := client.Teams.Create(ctx, TeamCreateOptions{
Name: String("foo"),
Account: &Account{ID: badIdentifier},
})
assert.Nil(t, team)
assert.EqualError(t, err, "invalid value for account ID")
})
t.Run("when options has an invalid identity provider", func(t *testing.T) {
team, err := client.Teams.Create(ctx, TeamCreateOptions{
Name: String("foo"),
IdentityProvider: &IdentityProvider{ID: badIdentifier},
})
assert.Nil(t, team)
assert.EqualError(t, err, "invalid value for identity provider ID")
})
}
func TestTeamsRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
testTeam, testTeamCleanup := createTeam(t, client, []*User{{ID: defaultUserID}})
defer testTeamCleanup()
t.Run("when the team exists", func(t *testing.T) {
team, err := client.Teams.Read(ctx, testTeam.ID)
require.NoError(t, err)
assert.Equal(t, testTeam.ID, team.ID)
t.Run("relationships are properly decoded", func(t *testing.T) {
assert.Equal(t, team.Account.ID, defaultAccountID)
assert.Equal(t, team.IdentityProvider.ID, defaultIdentityProviderScalrID)
assert.Equal(t, team.Users[0].ID, defaultUserID)
})
})
t.Run("when the team does not exist", func(t *testing.T) {
team, err := client.Teams.Read(ctx, "team-nonexisting")
assert.Nil(t, team)
assert.Error(t, err)
})
t.Run("without a valid team ID", func(t *testing.T) {
team, err := client.Teams.Read(ctx, badIdentifier)
assert.Nil(t, team)
assert.EqualError(t, err, "invalid value for team ID")
})
}
func TestTeamsUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
testTeam, testTeamCleanup := createTeam(t, client, nil)
defer testTeamCleanup()
t.Run("when updating a subset of values", func(t *testing.T) {
options := TeamUpdateOptions{
Name: String("tst-" + randomString(t)),
}
teamAfter, err := client.Teams.Update(ctx, testTeam.ID, options)
require.NoError(t, err)
assert.Equal(t, *options.Name, teamAfter.Name)
assert.Equal(t, testTeam.Description, teamAfter.Description)
})
t.Run("when updating a users list", func(t *testing.T) {
users := []*User{{ID: defaultUserID}}
options := TeamUpdateOptions{
Name: String(testTeam.Name),
Users: users,
}
teamAfter, err := client.Teams.Update(ctx, testTeam.ID, options)
require.NoError(t, err)
assert.Equal(t, testTeam.Name, teamAfter.Name)
assert.Equal(t, users, teamAfter.Users)
})
t.Run("without a valid team ID", func(t *testing.T) {
team, err := client.Teams.Update(ctx, badIdentifier, TeamUpdateOptions{})
assert.Nil(t, team)
assert.EqualError(t, err, "invalid value for team ID")
})
}
func TestTeamsDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
testTeam, _ := createTeam(t, client, nil)
t.Run("with valid options", func(t *testing.T) {
err := client.Teams.Delete(ctx, testTeam.ID)
require.NoError(t, err)
// Try loading team - it should fail.
_, err = client.Teams.Read(ctx, testTeam.ID)
assert.Equal(
t,
ResourceNotFoundError{
Message: fmt.Sprintf("Team with ID '%s' not found or user unauthorized.", testTeam.ID),
}.Error(),
err.Error(),
)
})
t.Run("without a valid team ID", func(t *testing.T) {
err := client.Teams.Delete(ctx, badIdentifier)
assert.EqualError(t, err, "invalid value for team ID")
})
}