Skip to content

Commit

Permalink
Fix custom endpoint (#72)
Browse files Browse the repository at this point in the history
* custom endpoint create unit tests

* fix custom endpoint + test

* lint

* fix integration tests

* changelog

* sample API url
  • Loading branch information
yyyogev authored Mar 14, 2021
1 parent 1b54965 commit 7f828ce
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 28 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ endpoint, err := underTest.CreateEndpoint(endpoints.Endpoint{


### Changelog
- v1.3.2
- fix client custom endpoint headers bug
- improve tests
- v1.3
- unnecessary resource updates bug fix.
- support tags in alerts
Expand Down
2 changes: 1 addition & 1 deletion endpoints/client_endpoints_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func buildCreateEndpointRequest(endpoint Endpoint) map[string]interface{} {
for k, v := range headers {
headerStrings = append(headerStrings, fmt.Sprintf("%s=%s", k, v))
}
headerString := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(headerStrings)), ","), "[]")
headerString := strings.Join(headerStrings, ",")
createEndpoint[fldEndpointHeaders] = headerString
createEndpoint[fldEndpointBodyTemplate] = endpoint.BodyTemplate
}
Expand Down
6 changes: 2 additions & 4 deletions endpoints/endpoints_bigpanda_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build integration

package endpoints_test

import (
Expand All @@ -26,7 +24,7 @@ func createBigPandaEndpoint() endpoints.Endpoint {
return endpoints.Endpoint{
Title: "bigpandaendpoint",
Description: "description",
EndpointType: "big-panda",
EndpointType: "bigpanda",
ApiToken: "api_token",
AppKey: "app_key",
}
Expand All @@ -36,7 +34,7 @@ func updateBigPandaEndpoint() endpoints.Endpoint {
return endpoints.Endpoint{
Title: "bigpandaupdatedendpoint",
Description: "updated description",
EndpointType: "big-panda",
EndpointType: "bigpanda",
ApiToken: "updated_api_token",
AppKey: "updated_app_key",
}
Expand Down
55 changes: 55 additions & 0 deletions endpoints/endpoints_create_custom_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package endpoints_test

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/logzio/logzio_terraform_client/endpoints"
"github.com/stretchr/testify/assert"
)

func TestEndpoints_CreateCustomEndpoint(t *testing.T) {
underTest, err, teardown := setupEndpointsTest()
defer teardown()

endpointId := int64(1234567)

mux.HandleFunc("/v1/endpoints/custom", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
assert.Equal(t, http.MethodPost, r.Method)
jsonBytes, _ := ioutil.ReadAll(r.Body)
var target map[string]interface{}
err = json.Unmarshal(jsonBytes, &target)
assert.NoError(t, err)
assert.Contains(t, target, "title")
assert.Contains(t, target, "description")
assert.Contains(t, target, "url")
assert.Contains(t, target, "headers")
Headers := strings.Split(fmt.Sprint(target["headers"]), ",")
assert.Equal(t, 2, len(Headers))
assert.Equal(t, strings.Split(Headers[1], "=")[1], "two words")

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, fixture("create_endpoint.json"))
})

if assert.NoError(t, err) {
endpoint, err := underTest.CreateEndpoint(endpoints.Endpoint{
Title: "testCreateCustomEndpoint",
Method: "POST",
Description: "my description",
Url: "https://jsonplaceholder.typicode.com/todos/1",
EndpointType: endpoints.EndpointTypeCustom,
Headers: map[string]string{"hello": "there", "header": "two words"},
BodyTemplate: map[string]string{"hello": "there", "header": "two"},
})
assert.NoError(t, err)
assert.NotNil(t, endpoint)
assert.Equal(t, endpointId, endpoint.Id)
}
}
10 changes: 4 additions & 6 deletions endpoints/endpoints_custom_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build integration

package endpoints_test

import (
Expand All @@ -15,7 +13,7 @@ func TestIntegrationEndpoints_CustomCreateUpdate(t *testing.T) {
Title: "testCreateCustomEndpoint",
Method: "POST",
Description: "my description",
Url: "https://this.is.com/some/other/webhook",
Url: "https://jsonplaceholder.typicode.com/todos/1",
EndpointType: "custom",
Headers: map[string]string{"hello": "there", "header": "two"},
BodyTemplate: map[string]string{"hello": "there", "header": "two"},
Expand All @@ -27,7 +25,7 @@ func TestIntegrationEndpoints_CustomCreateUpdate(t *testing.T) {
Title: "testCreateUpdateCustomEndpoint",
Method: "POST",
Description: "my description update",
Url: "https://this.is.com/some/updated/webhook",
Url: "https://jsonplaceholder.typicode.com/todos/1",
EndpointType: "custom",
Headers: map[string]string{"hello": "there", "header": "two"},
BodyTemplate: map[string]string{"hello": "there", "header": "two"},
Expand All @@ -45,7 +43,7 @@ func TestIntegrationEndpoints_CustomCreateDuplicate(t *testing.T) {
Title: "testCustomDuplicateEndpoint",
Method: "POST",
Description: "my description",
Url: "https://this.is.com/some/other/webhook",
Url: "https://jsonplaceholder.typicode.com/todos/1",
EndpointType: "custom",
Headers: map[string]string{"hello": "there", "header": "two"},
BodyTemplate: map[string]string{"hello": "there", "header": "two"},
Expand All @@ -57,7 +55,7 @@ func TestIntegrationEndpoints_CustomCreateDuplicate(t *testing.T) {
Title: "testCustomDuplicateEndpoint",
Method: "POST",
Description: "my description",
Url: "https://this.is.com/some/other/webhook",
Url: "https://jsonplaceholder.typicode.com/todos/1",
EndpointType: "custom",
Headers: map[string]string{"hello": "there", "header": "two"},
BodyTemplate: map[string]string{"hello": "there", "header": "two"},
Expand Down
6 changes: 2 additions & 4 deletions endpoints/endpoints_datadog_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build integration

package endpoints_test

import (
Expand All @@ -26,7 +24,7 @@ func createDataDogEndpoint() endpoints.Endpoint {
return endpoints.Endpoint{
Title: "datadogendpoint",
Description: "description",
EndpointType: "data-dog",
EndpointType: "datadog",
ApiKey: "api_key",
}
}
Expand All @@ -35,7 +33,7 @@ func updateDataDogEndpoint() endpoints.Endpoint {
return endpoints.Endpoint{
Title: "datadogupdatedendpoint",
Description: "updated description",
EndpointType: "data-dog",
EndpointType: "datadog",
ApiKey: "updated_api_key",
}
}
4 changes: 1 addition & 3 deletions endpoints/endpoints_pagerduty_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build integration

package endpoints_test

import (
Expand Down Expand Up @@ -28,7 +26,7 @@ func createPagerDutyEndpoint() endpoints.Endpoint {
return endpoints.Endpoint{
Title: "pagerdutyvalidEndpoint",
Description: "my description",
EndpointType: "pager-duty",
EndpointType: "pagerduty",
ServiceKey: "my_service_key",
}
}
14 changes: 6 additions & 8 deletions endpoints/endpoints_slack_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build integration

package endpoints_test

import (
Expand All @@ -18,7 +16,7 @@ func TestIntegrationEndpoints_CreateDeleteGetValidEndpoint(t *testing.T) {
endpoint, err = underTest.CreateEndpoint(endpoints.Endpoint{
Title: "slackcreatedeletevalidendpoint",
Description: "my description",
Url: "https://this.is.com/some/webhook",
Url: "https://jsonplaceholder.typicode.com/todos/1",
EndpointType: "slack",
})
assert.Nil(t, err)
Expand All @@ -42,14 +40,14 @@ func TestIntegrationEndpoints_CreateDuplicateEndpoint(t *testing.T) {
endpoint, err = underTest.CreateEndpoint(endpoints.Endpoint{
Title: "slackcreateduplicateendpoint",
Description: "my description",
Url: "https://this.is.com/some/webhook",
Url: "https://jsonplaceholder.typicode.com/todos/1",
EndpointType: "slack",
})
if assert.NoError(t, err) {
duplicate, err := underTest.CreateEndpoint(endpoints.Endpoint{
Title: "slackcreateduplicateendpoint",
Description: "my description",
Url: "https://this.is.com/some/webhook",
Url: "https://jsonplaceholder.typicode.com/todos/1",
EndpointType: "slack",
})
assert.Error(t, err)
Expand All @@ -66,7 +64,7 @@ func TestIntegrationEndpoints_ListEndpoints(t *testing.T) {
endpoint, err := underTest.CreateEndpoint(endpoints.Endpoint{
Title: "slacklistendpoints",
Description: "my description",
Url: "https://this.is.com/some/webhook",
Url: "https://jsonplaceholder.typicode.com/todos/1",
EndpointType: "slack",
})
list, err := underTest.ListEndpoints()
Expand Down Expand Up @@ -101,7 +99,7 @@ func TestIntegrationEndpoints_UpdateEndpoint(t *testing.T) {
endpoint, err = underTest.CreateEndpoint(endpoints.Endpoint{
Title: "slackupdatedendpoint",
Description: "my description",
Url: "https://this.is.com/some/webhook",
Url: "https://jsonplaceholder.typicode.com/todos/1",
EndpointType: "slack",
})
assert.NoError(t, err)
Expand All @@ -110,7 +108,7 @@ func TestIntegrationEndpoints_UpdateEndpoint(t *testing.T) {
updatedEndpoint, err := underTest.UpdateEndpoint(endpoint.Id, endpoints.Endpoint{
Title: "slackupdatedupdatedendpoint",
Description: "my updated description",
Url: "https://this.is.com/some/other/webhook",
Url: "https://jsonplaceholder.typicode.com/todos/1",
EndpointType: "slack",
})
assert.NoError(t, err)
Expand Down
2 changes: 0 additions & 2 deletions endpoints/endpoints_victorops_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build integration

package endpoints_test

import (
Expand Down

0 comments on commit 7f828ce

Please sign in to comment.