-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebhooks_test.go
182 lines (151 loc) · 5.81 KB
/
webhooks_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
// Copyright (c) David Bond, Tailscale Inc, & Contributors
// SPDX-License-Identifier: MIT
package tailscale
import (
"context"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestClient_CreateWebhook(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
req := CreateWebhookRequest{
EndpointURL: "https://example.com/my/endpoint",
ProviderType: WebhookDiscordProviderType,
Subscriptions: []WebhookSubscriptionType{WebhookNodeCreated, WebhookNodeApproved},
}
expectedSecret := "my-secret"
expectedWebhook := &Webhook{
EndpointID: "12345",
EndpointURL: req.EndpointURL,
ProviderType: req.ProviderType,
CreatorLoginName: "[email protected]",
Created: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
LastModified: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
Subscriptions: req.Subscriptions,
Secret: &expectedSecret,
}
server.ResponseBody = expectedWebhook
webhook, err := client.Webhooks().Create(context.Background(), req)
assert.NoError(t, err)
assert.Equal(t, http.MethodPost, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/webhooks", server.Path)
assert.Equal(t, expectedWebhook, webhook)
}
func TestClient_Webhooks(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
expectedWebhooks := map[string][]Webhook{
"webhooks": {
{
EndpointID: "12345",
EndpointURL: "https://example.com/my/endpoint",
ProviderType: "",
CreatorLoginName: "[email protected]",
Created: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
LastModified: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
Subscriptions: []WebhookSubscriptionType{WebhookNodeCreated, WebhookNodeApproved},
},
{
EndpointID: "54321",
EndpointURL: "https://example.com/my/endpoint/other",
ProviderType: "slack",
CreatorLoginName: "[email protected]",
Created: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
LastModified: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
Subscriptions: []WebhookSubscriptionType{WebhookNodeApproved},
},
},
}
server.ResponseBody = expectedWebhooks
actualWebhooks, err := client.Webhooks().List(context.Background())
assert.NoError(t, err)
assert.Equal(t, http.MethodGet, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/webhooks", server.Path)
assert.Equal(t, expectedWebhooks["webhooks"], actualWebhooks)
}
func TestClient_Webhook(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
expectedWebhook := &Webhook{
EndpointID: "54321",
EndpointURL: "https://example.com/my/endpoint/other",
ProviderType: "slack",
CreatorLoginName: "[email protected]",
Created: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
LastModified: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
Subscriptions: []WebhookSubscriptionType{WebhookNodeApproved},
}
server.ResponseBody = expectedWebhook
actualWebhook, err := client.Webhooks().Get(context.Background(), "54321")
assert.NoError(t, err)
assert.Equal(t, http.MethodGet, server.Method)
assert.Equal(t, "/api/v2/webhooks/54321", server.Path)
assert.Equal(t, expectedWebhook, actualWebhook)
}
func TestClient_UpdateWebhook(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
subscriptions := []WebhookSubscriptionType{WebhookNodeCreated, WebhookNodeApproved, WebhookNodeNeedsApproval}
expectedWebhook := &Webhook{
EndpointID: "54321",
EndpointURL: "https://example.com/my/endpoint/other",
ProviderType: "slack",
CreatorLoginName: "[email protected]",
Created: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
LastModified: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
Subscriptions: subscriptions,
}
server.ResponseBody = expectedWebhook
actualWebhook, err := client.Webhooks().Update(context.Background(), "54321", subscriptions)
assert.NoError(t, err)
assert.Equal(t, http.MethodPatch, server.Method)
assert.Equal(t, "/api/v2/webhooks/54321", server.Path)
assert.Equal(t, expectedWebhook, actualWebhook)
}
func TestClient_DeleteWebhook(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
err := client.Webhooks().Delete(context.Background(), "54321")
assert.NoError(t, err)
assert.Equal(t, http.MethodDelete, server.Method)
assert.Equal(t, "/api/v2/webhooks/54321", server.Path)
}
func TestClient_TestWebhook(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusAccepted
err := client.Webhooks().Test(context.Background(), "54321")
assert.NoError(t, err)
assert.Equal(t, http.MethodPost, server.Method)
assert.Equal(t, "/api/v2/webhooks/54321/test", server.Path)
}
func TestClient_RotateWebhookSecret(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
expectedSecret := "my-new-secret"
expectedWebhook := &Webhook{
EndpointID: "54321",
EndpointURL: "https://example.com/my/endpoint/other",
ProviderType: "slack",
CreatorLoginName: "[email protected]",
Created: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
LastModified: time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC),
Subscriptions: []WebhookSubscriptionType{WebhookNodeApproved},
Secret: &expectedSecret,
}
server.ResponseBody = expectedWebhook
actualWebhook, err := client.Webhooks().RotateSecret(context.Background(), "54321")
assert.NoError(t, err)
assert.Equal(t, http.MethodPost, server.Method)
assert.Equal(t, "/api/v2/webhooks/54321/rotate", server.Path)
assert.Equal(t, expectedWebhook, actualWebhook)
}