-
Notifications
You must be signed in to change notification settings - Fork 961
Expand file tree
/
Copy pathresource_github_team_settings_test.go
More file actions
304 lines (266 loc) · 8.27 KB
/
resource_github_team_settings_test.go
File metadata and controls
304 lines (266 loc) · 8.27 KB
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
package github
import (
"context"
"fmt"
"regexp"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
func TestAccGithubTeamSettings(t *testing.T) {
t.Run("manages team settings can use team_id id and slug", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
teamName := fmt.Sprintf("%steam-settings-%s", testResourcePrefix, randomID)
config := fmt.Sprintf(`
resource "github_team" "test" {
name = "%s"
description = "generated by terraform provider automated testing"
}
resource "github_team_settings" "test" {
team_id = "${github_team.test.id}"
}
`, teamName)
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("github_team_settings.test", "team_id"),
)
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
{
Config: strings.Replace(config,
`github_team.test.id`,
`github_team.test.slug`, 1),
Check: check,
},
},
})
})
t.Run("manages team code review settings", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
teamName := fmt.Sprintf("%steam-settings-%s", testResourcePrefix, randomID)
config := fmt.Sprintf(`
resource "github_team" "test" {
name = "%s"
description = "generated by terraform provider automated testing"
}
resource "github_team_settings" "test" {
team_id = "${github_team.test.id}"
review_request_delegation {
algorithm = "ROUND_ROBIN"
member_count = 1
notify = true
}
}
`, teamName)
checks := map[string]resource.TestCheckFunc{
"round_robin": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_team_settings.test", "review_request_delegation.0.algorithm",
"ROUND_ROBIN",
),
),
"load_balance": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_team_settings.test", "review_request_delegation.0.algorithm",
"LOAD_BALANCE",
),
),
"review_count": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_team_settings.test", "review_request_delegation.0.member_count",
"3",
),
),
"notify": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_team_settings.test", "review_request_delegation.0.notify",
"false",
),
),
}
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: checks["round_robin"],
},
{
Config: strings.Replace(config,
`algorithm = "ROUND_ROBIN"`,
`algorithm = "LOAD_BALANCE"`, 1),
Check: checks["load_balance"],
},
{
Config: strings.Replace(config,
`member_count = 1`,
`member_count = 3`, 1),
Check: checks["review_count"],
},
{
Config: strings.Replace(config,
`notify = true`,
`notify = false`, 1),
Check: checks["notify"],
},
},
})
})
t.Run("manages team code review settings with excluded members", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
teamName := fmt.Sprintf("%steam-settings-%s", testResourcePrefix, randomID)
config := fmt.Sprintf(`
resource "github_team" "test" {
name = "%s"
description = "generated by terraform provider automated testing"
}
resource "github_team_settings" "test" {
team_id = "${github_team.test.id}"
review_request_delegation {
algorithm = "ROUND_ROBIN"
member_count = 1
notify = true
excluded_members = ["octocat", "defunkt"]
}
}
`, teamName)
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_team_settings.test", "review_request_delegation.0.algorithm",
"ROUND_ROBIN",
),
resource.TestCheckResourceAttr(
"github_team_settings.test", "review_request_delegation.0.excluded_members.#",
"2",
),
)
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
})
t.Run("cannot manage team code review settings if disabled", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
teamName := fmt.Sprintf("%steam-settings-%s", testResourcePrefix, randomID)
config := fmt.Sprintf(`
resource "github_team" "test" {
name = "%s"
description = "generated by terraform provider automated testing"
}
resource "github_team_settings" "test" {
team_id = "${github_team.test.id}"
review_request_delegation {
algorithm = "ROUND_ROBIN"
member_count = 1
notify = true
}
}
`, teamName)
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: strings.Replace(config,
`algorithm = "ROUND_ROBIN"`,
`algorithm = "invalid"`, 1),
ExpectError: regexp.MustCompile(`review request delegation algorithm must be one of \[.*\]`),
},
},
})
})
}
func TestBatchUserNodeIds(t *testing.T) {
t.Run("fetches user node IDs in batch", func(t *testing.T) {
if len(testAccConf.testExternalUser) == 0 {
t.Skip("No external user provided")
}
// Skip if not authenticated
skipUnauthenticated(t)
// Create a test owner/meta object
owner, err := getTestMeta()
if err != nil {
t.Fatalf("Failed to get test meta: %v", err)
}
ctx := context.Background()
// Test with single user
t.Run("single user", func(t *testing.T) {
usernames := []string{testAccConf.testExternalUser}
result, err := getBatchUserNodeIds(ctx, owner, usernames)
if err != nil {
t.Fatalf("getBatchUserNodeIds failed: %v", err)
}
if len(result) != 1 {
t.Errorf("Expected 1 result, got %d", len(result))
}
nodeId, exists := result[testAccConf.testExternalUser]
if !exists {
t.Errorf("Expected node ID for user %s", testAccConf.testExternalUser)
}
if nodeId == "" {
t.Errorf("Node ID should not be empty")
}
t.Logf("Successfully fetched node ID %s for user %s", nodeId, testAccConf.testExternalUser)
})
// Test with multiple users (using same user multiple times for simplicity)
t.Run("multiple users", func(t *testing.T) {
usernames := []string{testAccConf.testExternalUser, "octocat"} // octocat is a well-known GitHub user
result, err := getBatchUserNodeIds(ctx, owner, usernames)
if err != nil {
t.Fatalf("getBatchUserNodeIds failed: %v", err)
}
// We expect at least the test user to exist
if len(result) == 0 {
t.Errorf("Expected at least 1 result, got %d", len(result))
}
// Check that our test user exists
nodeId, exists := result[testAccConf.testExternalUser]
if !exists {
t.Errorf("Expected node ID for user %s", testAccConf.testExternalUser)
} else if nodeId == "" {
t.Errorf("Node ID should not be empty")
}
t.Logf("Successfully fetched %d node IDs in batch request", len(result))
for username, nodeId := range result {
t.Logf(" User: %s, Node ID: %s", username, nodeId)
}
})
// Test with empty list
t.Run("empty usernames list", func(t *testing.T) {
usernames := []string{}
result, err := getBatchUserNodeIds(ctx, owner, usernames)
if err != nil {
t.Fatalf("getBatchUserNodeIds failed: %v", err)
}
if len(result) != 0 {
t.Errorf("Expected 0 results for empty list, got %d", len(result))
}
})
// Test with non-existent user
t.Run("non-existent user", func(t *testing.T) {
nonExistentUser := "this-user-definitely-does-not-exist-12345"
usernames := []string{nonExistentUser}
result, err := getBatchUserNodeIds(ctx, owner, usernames)
if err != nil {
t.Fatalf("getBatchUserNodeIds failed: %v", err)
}
// Non-existent users should not appear in results
if len(result) != 0 {
t.Errorf("Expected 0 results for non-existent user, got %d", len(result))
}
})
})
}