-
Notifications
You must be signed in to change notification settings - Fork 3
/
vcs_provider_test.go
289 lines (237 loc) · 8.03 KB
/
vcs_provider_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
package scalr
import (
"context"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVCSProvidersList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
vcsTest1, vcsTest1Cleanup := createVcsProvider(t, client, nil)
defer vcsTest1Cleanup()
env, envCleanup := createEnvironment(t, client)
defer envCleanup()
envs := []*Environment{env}
vcsTest2, vcsTest2Cleanup := createVcsProvider(t, client, envs)
defer vcsTest2Cleanup()
t.Run("without list options", func(t *testing.T) {
response, err := client.VcsProviders.List(ctx, VcsProvidersListOptions{})
require.NoError(t, err)
vcsIDs := make([]string, len(response.Items))
for _, vcs := range response.Items {
vcsIDs = append(vcsIDs, vcs.ID)
}
assert.Contains(t, vcsIDs, vcsTest1.ID)
assert.Contains(t, vcsIDs, vcsTest2.ID)
assert.Equal(t, 1, response.CurrentPage)
})
t.Run("with list options", func(t *testing.T) {
response, err := client.VcsProviders.List(
ctx,
VcsProvidersListOptions{Query: &vcsTest2.Name, VcsType: &vcsTest2.VcsType, Environment: &env.ID},
)
require.NoError(t, err)
vcsIDs := make([]string, len(response.Items))
for _, vcs := range response.Items {
vcsIDs = append(vcsIDs, vcs.ID)
}
assert.Contains(t, vcsIDs, vcsTest2.ID)
assert.Equal(t, 1, response.CurrentPage)
assert.Equal(t, 1, response.TotalCount)
})
t.Run("with invalid environment filter", func(t *testing.T) {
response, err := client.VcsProviders.List(ctx, VcsProvidersListOptions{Environment: String(badIdentifier)})
assert.Len(t, response.Items, 0)
assert.NoError(t, err)
})
}
func TestVcsProvidersCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
envTest, envTestCleanup := createEnvironment(t, client)
defer envTestCleanup()
t.Run("with valid options", func(t *testing.T) {
options := VcsProviderCreateOptions{
Name: String("vcs-" + randomString(t)),
VcsType: Github,
AuthType: PersonalToken,
Token: os.Getenv("GITHUB_TOKEN"),
DraftPrRunsEnabled: Bool(true),
Environments: []*Environment{envTest},
Account: &Account{ID: defaultAccountID},
}
vcs, err := client.VcsProviders.Create(ctx, options)
require.NoError(t, err)
// Get a refreshed view from the API.
refreshed, err := client.VcsProviders.Read(ctx, vcs.ID)
require.NoError(t, err)
for _, item := range []*VcsProvider{
vcs,
refreshed,
} {
assert.NotEmpty(t, item.ID)
assert.Equal(t, *options.Name, item.Name)
assert.Equal(t, options.VcsType, item.VcsType)
assert.Equal(t, options.AuthType, item.AuthType)
assert.Equal(t, false, item.IsShared)
assert.Equal(t, true, item.DraftPrRunsEnabled)
}
})
t.Run("shared provider", func(t *testing.T) {
options := VcsProviderCreateOptions{
Name: String("vcs-" + randomString(t)),
VcsType: Github,
AuthType: PersonalToken,
Token: os.Getenv("GITHUB_TOKEN"),
IsShared: Bool(true),
Account: &Account{ID: defaultAccountID},
}
vcs, err := client.VcsProviders.Create(ctx, options)
require.NoError(t, err)
assert.NotEmpty(t, vcs.ID)
assert.Equal(t, *options.Name, vcs.Name)
assert.Equal(t, options.VcsType, vcs.VcsType)
assert.Equal(t, options.AuthType, vcs.AuthType)
assert.Equal(t, *options.IsShared, vcs.IsShared)
assert.Equal(t, false, vcs.DraftPrRunsEnabled)
})
t.Run("with agent-pool attr vcs-enabled: false", func(t *testing.T) {
ap, apCleanup := createAgentPool(t, client, false)
defer apCleanup()
options := VcsProviderCreateOptions{
Name: String("vcs-" + randomString(t)),
VcsType: Github,
AuthType: PersonalToken,
Token: os.Getenv("GITHUB_TOKEN"),
Environments: []*Environment{envTest},
Account: &Account{ID: defaultAccountID},
AgentPool: ap,
}
_, err := client.VcsProviders.Create(ctx, options)
require.Error(t, err)
})
t.Run("when options has an invalid environment", func(t *testing.T) {
_, err := client.VcsProviders.Create(ctx, VcsProviderCreateOptions{
Name: String("test-vcs"),
VcsType: Gitlab,
AuthType: PersonalToken,
Environments: []*Environment{{ID: badIdentifier}},
})
assert.Equal(
t,
ResourceNotFoundError{
Message: fmt.Sprintf(
"Invalid Relationship\n\nRelationship 'environments' with ID '%s' not found or user unauthorized.",
badIdentifier,
),
}.Error(),
err.Error(),
)
})
t.Run("when an error is returned from the api", func(t *testing.T) {
ws, err := client.VcsProviders.Create(ctx, VcsProviderCreateOptions{
Name: String("foo"),
VcsType: Github,
AuthType: PersonalToken,
Token: "invalid-token",
Environments: []*Environment{envTest},
Account: &Account{ID: defaultAccountID},
})
assert.Nil(t, ws)
assert.Error(t, err)
})
}
func TestVcsProvidersRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
envTest, envTestCleanup := createEnvironment(t, client)
defer envTestCleanup()
vcsTest, vcsTestCleanup := createVcsProvider(t, client, []*Environment{envTest})
defer vcsTestCleanup()
t.Run("when the vcs provider exists", func(t *testing.T) {
vcs, err := client.VcsProviders.Read(ctx, vcsTest.ID)
require.NoError(t, err)
assert.Equal(t, vcsTest.ID, vcs.ID)
assert.Len(t, vcs.Environments, 1)
t.Run("relationships are properly decoded", func(t *testing.T) {
assert.Equal(t, envTest.ID, vcs.Environments[0].ID)
})
})
t.Run("when the vcs provider does not exist", func(t *testing.T) {
_, err := client.VcsProviders.Read(ctx, "nonexisting")
assert.Error(t, err)
})
}
func TestVcsProvidersUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
envTest, envTestCleanup := createEnvironment(t, client)
defer envTestCleanup()
vcsTest, _ := createVcsProvider(t, client, []*Environment{envTest})
t.Run("when updating a subset of values", func(t *testing.T) {
options := VcsProviderUpdateOptions{
Name: String(randomString(t)),
IsShared: Bool(true),
}
vcsAfter, err := client.VcsProviders.Update(ctx, vcsTest.ID, options)
require.NoError(t, err)
assert.Equal(t, vcsTest.AuthType, vcsAfter.AuthType)
assert.Equal(t, vcsTest.VcsType, vcsAfter.VcsType)
assert.True(t, vcsTest.IsShared != vcsAfter.IsShared)
})
t.Run("with valid options", func(t *testing.T) {
options := VcsProviderUpdateOptions{
Name: String(randomString(t)),
}
vcs, err := client.VcsProviders.Update(ctx, vcsTest.ID, options)
require.NoError(t, err)
// Get a refreshed view of the vcs provider from the API
refreshed, err := client.VcsProviders.Read(ctx, vcsTest.ID)
require.NoError(t, err)
for _, item := range []*VcsProvider{
vcs,
refreshed,
} {
assert.Equal(t, *options.Name, item.Name)
}
})
t.Run("when an error is returned from the api", func(t *testing.T) {
vcs, err := client.VcsProviders.Update(ctx, vcsTest.ID, VcsProviderUpdateOptions{
Name: String("~invalid_name!"),
})
assert.Nil(t, vcs)
assert.Error(t, err)
})
t.Run("when options has an invalid name", func(t *testing.T) {
vcs, err := client.VcsProviders.Update(ctx, badIdentifier, VcsProviderUpdateOptions{})
assert.Nil(t, vcs)
assert.EqualError(t, err, "invalid value for vcs provider ID")
})
}
func TestVcsProvidersDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
envTest, envTestCleanup := createEnvironment(t, client)
defer envTestCleanup()
vcsTest, _ := createVcsProvider(t, client, []*Environment{envTest})
t.Run("with valid options", func(t *testing.T) {
err := client.VcsProviders.Delete(ctx, vcsTest.ID)
require.NoError(t, err)
// Try loading the vcs provider - it should fail.
_, err = client.VcsProviders.Read(ctx, vcsTest.ID)
assert.Equal(
t,
ResourceNotFoundError{
Message: fmt.Sprintf("VcsProvider with ID '%s' not found or user unauthorized.", vcsTest.ID),
}.Error(),
err.Error(),
)
})
t.Run("without a valid vcs provider ID", func(t *testing.T) {
err := client.VcsProviders.Delete(ctx, badIdentifier)
assert.EqualError(t, err, "invalid value for vcs provider ID")
})
}