-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebhook_integration_test.go
163 lines (135 loc) · 4.44 KB
/
webhook_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
package scalr
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWebhookIntegrationsList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
env1, deleteEnv1 := createEnvironment(t, client)
defer deleteEnv1()
env2, deleteEnv2 := createEnvironment(t, client)
defer deleteEnv2()
whTest1, whTest1Cleanup := createWebhookIntegration(t, client, true, nil)
defer whTest1Cleanup()
_, whTest2Cleanup := createWebhookIntegration(t, client, false, []*Environment{env1})
defer whTest2Cleanup()
whTest3, whTest3Cleanup := createWebhookIntegration(t, client, false, []*Environment{env2})
defer whTest3Cleanup()
t.Run("with options", func(t *testing.T) {
whl, err := client.WebhookIntegrations.List(
ctx, WebhookIntegrationListOptions{
Account: String(defaultAccountID),
Environment: &env2.ID,
},
)
require.NoError(t, err)
assert.Equal(t, 2, whl.TotalCount)
expectedIDs := []string{whTest1.ID, whTest3.ID}
actualIDs := make([]string, len(whl.Items))
for i, wh := range whl.Items {
actualIDs[i] = wh.ID
}
assert.ElementsMatch(t, expectedIDs, actualIDs)
})
}
func TestWebhookIntegrationsRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
whTest, whTestCleanup := createWebhookIntegration(t, client, true, nil)
defer whTestCleanup()
t.Run("by ID when the webhook exists", func(t *testing.T) {
wh, err := client.WebhookIntegrations.Read(ctx, whTest.ID)
require.NoError(t, err)
assert.Equal(t, whTest.ID, wh.ID)
})
t.Run("by ID when the webhook does not exist", func(t *testing.T) {
wh, err := client.WebhookIntegrations.Read(ctx, "wh-nonexisting")
assert.Nil(t, wh)
assert.Error(t, err)
})
t.Run("by ID without a valid webhook ID", func(t *testing.T) {
wh, err := client.WebhookIntegrations.Read(ctx, badIdentifier)
assert.Nil(t, wh)
assert.EqualError(t, err, "invalid value for webhook ID")
})
}
func TestWebhookIntegrationsCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("with valid options", func(t *testing.T) {
options := WebhookIntegrationCreateOptions{
Name: String("tst-" + randomString(t)),
Account: &Account{ID: defaultAccountID},
Url: String("https://example.com"),
Events: []*EventDefinition{{ID: "run:completed"}},
}
wh, err := client.WebhookIntegrations.Create(ctx, options)
require.NoError(t, err)
// Get a refreshed view from the API.
refreshed, err := client.WebhookIntegrations.Read(ctx, wh.ID)
require.NoError(t, err)
for _, item := range []*WebhookIntegration{
wh,
refreshed,
} {
assert.NotEmpty(t, item.ID)
assert.Equal(t, *options.Name, item.Name)
assert.Equal(t, options.Account, item.Account)
assert.Equal(t, *options.Url, item.Url)
assert.Equal(t, options.Events, item.Events)
}
err = client.WebhookIntegrations.Delete(ctx, wh.ID)
require.NoError(t, err)
})
}
func TestWebhookIntegrationsUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
env1, deleteEnv1 := createEnvironment(t, client)
defer deleteEnv1()
env2, deleteEnv2 := createEnvironment(t, client)
defer deleteEnv2()
whTest, whTestCleanup := createWebhookIntegration(t, client, true, nil)
defer whTestCleanup()
t.Run("with valid options", func(t *testing.T) {
options := WebhookIntegrationUpdateOptions{
Name: String(randomString(t)),
IsShared: Bool(false),
Environments: []*Environment{env1, env2},
}
wh, err := client.WebhookIntegrations.Update(ctx, whTest.ID, options)
require.NoError(t, err)
// Get a refreshed view from the API.
refreshed, err := client.WebhookIntegrations.Read(ctx, whTest.ID)
require.NoError(t, err)
for _, item := range []*WebhookIntegration{
wh,
refreshed,
} {
assert.Equal(t, *options.Name, item.Name)
assert.Equal(t, *options.IsShared, item.IsShared)
assert.Len(t, item.Environments, 2)
}
})
}
func TestWebhookIntegrationsDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
whTest, _ := createWebhookIntegration(t, client, true, nil)
t.Run("with valid options", func(t *testing.T) {
err := client.WebhookIntegrations.Delete(ctx, whTest.ID)
require.NoError(t, err)
_, err = client.WebhookIntegrations.Read(ctx, whTest.ID)
assert.Equal(
t,
ResourceNotFoundError{
Message: fmt.Sprintf("Webhook with ID '%s' not found or user unauthorized.", whTest.ID),
}.Error(),
err.Error(),
)
})
}