Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brookesargent committed Nov 27, 2024
1 parent 7e8e2f5 commit 9e37fce
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
39 changes: 39 additions & 0 deletions internal/provider/webhook_recipient_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package provider
import (
"context"
"errors"
"fmt"
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand All @@ -18,6 +20,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"golang.org/x/net/http/httpguts"

"github.com/honeycombio/terraform-provider-honeycombio/client"
"github.com/honeycombio/terraform-provider-honeycombio/internal/helper"
Expand All @@ -34,6 +37,10 @@ var (

webhookTemplateTypes = []string{"trigger", "exhaustion_time", "budget_rate"}
webhookTemplateNameRegex = regexp.MustCompile(`^[a-z](?:[a-zA-Z0-9]+$)?$`)

defaultHeaderContentType = "Content-Type"
defaultHeaderUserAgent = "User-Agent"
defaultHeaderXHoneycombWebhookToken = "X-Honeycomb-Webhook-Token"
)

type webhookRecipientResource struct {
Expand Down Expand Up @@ -203,6 +210,9 @@ func (r *webhookRecipientResource) ValidateConfig(ctx context.Context, req resou
var variables []models.TemplateVariableModel
data.Variables.ElementsAs(ctx, &variables, false)

var headers []models.WebhookHeaderModel
data.Headers.ElementsAs(ctx, &headers, false)

triggerTmplExists := false
budgetRateTmplExists := false
exhaustionTimeTmplExists := false
Expand Down Expand Up @@ -261,6 +271,35 @@ func (r *webhookRecipientResource) ValidateConfig(ctx context.Context, req resou
}
duplicateMap[name] = true
}

// webhook headers cannot used reserved keys and must be valid http headers
for i, h := range headers {
header := strings.ToLower(h.Name.ValueString())
if header == strings.ToLower(defaultHeaderContentType) ||
header == strings.ToLower(defaultHeaderUserAgent) ||
header == strings.ToLower(defaultHeaderXHoneycombWebhookToken) {
resp.Diagnostics.AddAttributeError(
path.Root("header").AtListIndex(i).AtName("name"),
"Conflicting configuration arguments",
fmt.Sprintf("cannot match reserved \"name\": %s", h.Name.ValueString()),
)
}

if !httpguts.ValidHeaderFieldName(h.Name.ValueString()) {
resp.Diagnostics.AddAttributeError(
path.Root("header").AtListIndex(i).AtName("name"),
"Conflicting configuration arguments",
"invalid webhook header name",
)
}
if !httpguts.ValidHeaderFieldValue(h.Value.ValueString()) {
resp.Diagnostics.AddAttributeError(
path.Root("header").AtListIndex(i).AtName("value"),
"Conflicting configuration arguments",
"invalid webhook header value",
)
}
}
}

func (r *webhookRecipientResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Expand Down
71 changes: 71 additions & 0 deletions internal/provider/webhook_recipient_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,78 @@ resource "honeycombio_webhook_recipient" "test" {
},
})
})
}

func TestAcc_WebhookRecipientResource_validateReservedWebhookHeader(t *testing.T) {
name := test.RandomStringWithPrefix("test.", 20)
url := test.RandomURL()

resource.Test(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ProtoV5ProviderFactories: testAccProtoV5MuxServerFactory,
CheckDestroy: testAccEnsureRecipientDestroyed(t),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "honeycombio_webhook_recipient" "test" {
name = "%s"
url = "%s"
template {
type = "trigger"
body = "body"
}
header {
name = "Content-Type"
value = "xml"
}
variable {
name = "severity"
default_value = "warning"
}
}`, name, url),
ExpectError: regexp.MustCompile(`cannot match reserved "name": Content-Type`),
},
},
})
}

func TestAcc_WebhookRecipientResource_validateInvalidWebhookHeader(t *testing.T) {
name := test.RandomStringWithPrefix("test.", 20)
url := test.RandomURL()

resource.Test(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ProtoV5ProviderFactories: testAccProtoV5MuxServerFactory,
CheckDestroy: testAccEnsureRecipientDestroyed(t),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "honeycombio_webhook_recipient" "test" {
name = "%s"
url = "%s"
template {
type = "trigger"
body = "body"
}
header {
name = "é"
value = "test"
}
variable {
name = "severity"
default_value = "warning"
}
}`, name, url),
ExpectError: regexp.MustCompile(`invalid webhook header name`),
},
},
})
}

// TestAcc_WebhookRecipientResource_UpgradeFromVersion027 tests the migration case from the
Expand Down

0 comments on commit 9e37fce

Please sign in to comment.