Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Webhook integration with fields: method, payloadTemplate #212

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ func verifyRequest(t *testing.T, method string, expectToken bool, status int, pa
})
}

func verifyRequestWithJsonBody(t *testing.T, method string, expectToken bool, status int, params url.Values, jsonBody string, resultPath string) func(w http.ResponseWriter, r *http.Request) {
return createResponse(t, status, resultPath, func(t *testing.T, r *http.Request) {
verifyHeaders(t, r, expectToken)
verifyParams(t, r, params)
verifyJsonBody(t, r, jsonBody)

assert.Equal(t, method, r.Method, "Incorrect HTTP method")
})
}

func verifyHeaders(t *testing.T, r *http.Request, expectToken bool) {
if val, ok := r.Header[AuthHeaderKey]; ok {
assert.Equal(t, []string{TestToken}, val, "Incorrect auth token in headers")
Expand Down Expand Up @@ -79,6 +89,16 @@ func verifyParams(t *testing.T, r *http.Request, params url.Values) {
}
}

func verifyJsonBody(t *testing.T, r *http.Request, jsonBody string) {
actualBody, err := ioutil.ReadAll(r.Body)
if err != nil {
assert.Fail(t, "Error reading request body: %v", err)
}

actualJsonBody := string(actualBody)
assert.JSONEq(t, jsonBody, actualJsonBody, "Expected body: %s, got: %s", jsonBody, actualJsonBody)
}

func createResponse(t *testing.T, status int, resultPath string, requestValidator func(t *testing.T, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if requestValidator != nil {
Expand Down
8 changes: 5 additions & 3 deletions integration/model_webhook_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ type WebhookIntegration struct {
Name string `json:"name,omitempty"`
Type Type `json:"type"`
// Webhook URL
Url string `json:"url,omitempty"`
SharedSecret string `json:"sharedSecret,omitempty"`
Headers map[string]interface{} `json:"headers,omitempty"`
Url string `json:"url,omitempty"`
SharedSecret string `json:"sharedSecret,omitempty"`
Headers map[string]interface{} `json:"headers,omitempty"`
Method string `json:"method,omitempty"`
PayloadTemplate string `json:"payloadTemplate,omitempty"`
}
4 changes: 3 additions & 1 deletion testdata/fixtures/integration/create_webhook_success.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
"headers": {},
"sharedSecret": "pa$$word",
"type": "Webhook",
"url": "https://webhook.site/<key>"
"url": "https://webhook.site/<key>",
"method": "POST",
"payloadTemplate": "{\"incidentId\": \"{{{incidentId}}}\"}"
}
34 changes: 30 additions & 4 deletions webhook_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@ func TestCreateWebhookIntegration(t *testing.T) {
teardown := setup()
defer teardown()

mux.HandleFunc("/v2/integration", verifyRequest(t, "POST", true, http.StatusOK, nil, "integration/create_webhook_success.json"))
jsonBody := `{
"type": "Webhook",
"name": "webhoooooook",
"enabled": true,
"url": "https://webhook.site/<key>",
"method": "POST",
"payloadTemplate": "{\"incidentId\": \"{{{incidentId}}}\"}"
}`
mux.HandleFunc("/v2/integration", verifyRequestWithJsonBody(t, "POST", true, http.StatusOK, nil, jsonBody, "integration/create_webhook_success.json"))

result, err := client.CreateWebhookIntegration(context.Background(), &integration.WebhookIntegration{
Type: "Webhook",
Type: "Webhook",
Name: "webhoooooook",
Enabled: true,
Url: "https://webhook.site/<key>",
Method: "POST",
PayloadTemplate: "{\"incidentId\": \"{{{incidentId}}}\"}",
})
assert.NoError(t, err, "Unexpected error creating integration")
assert.Equal(t, "webhoooooook", result.Name, "Name does not match")
Expand All @@ -37,10 +50,23 @@ func TestUpdateWebhookIntegration(t *testing.T) {
teardown := setup()
defer teardown()

mux.HandleFunc("/v2/integration/id", verifyRequest(t, "PUT", true, http.StatusOK, nil, "integration/create_webhook_success.json"))
jsonBody := `{
"type": "Webhook",
"name": "webhoooooook",
"enabled": true,
"url": "https://webhook.site/<key>",
"method": "POST",
"payloadTemplate": "{\"incidentId\": \"{{{incidentId}}}\"}"
}`
mux.HandleFunc("/v2/integration/id", verifyRequestWithJsonBody(t, "PUT", true, http.StatusOK, nil, jsonBody, "integration/create_webhook_success.json"))

result, err := client.UpdateWebhookIntegration(context.Background(), "id", &integration.WebhookIntegration{
Type: "Webhook",
Type: "Webhook",
Name: "webhoooooook",
Enabled: true,
Url: "https://webhook.site/<key>",
Method: "POST",
PayloadTemplate: "{\"incidentId\": \"{{{incidentId}}}\"}",
})
assert.NoError(t, err, "Unexpected error creating integration")
assert.Equal(t, "webhoooooook", result.Name, "Name does not match")
Expand Down
Loading