Skip to content

Commit

Permalink
add some unit test validations (#232)
Browse files Browse the repository at this point in the history
* add some unit test validations

* add docs
  • Loading branch information
fiercegecko authored Nov 13, 2024
1 parent 57f1c0f commit 0b85b22
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/resources/corp_signal_tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ resource "sigsci_corp_signal_tag" "test" {

### Required

- `short_name` (String) The display name of the signal tag
- `short_name` (String) The display name of the signal tag. Must be 3-25 char.

### Optional

Expand Down
2 changes: 1 addition & 1 deletion docs/resources/site_signal_tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ resource "sigsci_site_signal_tag" "test" {

### Required

- `name` (String) The display name of the signal tag
- `name` (String) The display name of the signal tag. Must be 3-25 char.
- `site_short_name` (String) Site short name

### Optional
Expand Down
8 changes: 7 additions & 1 deletion provider/resource_corp_signal_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ func resourceCorpSignalTag() *schema.Resource {
Schema: map[string]*schema.Schema{
"short_name": {
Type: schema.TypeString,
Description: "The display name of the signal tag",
Description: "The display name of the signal tag. Must be 3-25 char.",
Required: true,
ForceNew: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if !validStringLength(val.(string), 3, 25) {
return nil, []error{fmt.Errorf(`received short_name "%q" is invalid. should be min len 3, max len 25`, val.(string))}
}
return nil, nil
},
},
"description": {
Type: schema.TypeString,
Expand Down
26 changes: 26 additions & 0 deletions provider/resource_corp_signal_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,29 @@ func testACCCheckCorpSignalTagDestroy(s *terraform.State) error {
}
return nil
}

func TestResourceCorpSignalTagShortNameValidation(t *testing.T) {
cases := []struct {
value string
expected bool
}{
{"s", true},
{"valid-name", false},
{"this-name-is-way-too-long-for-the-validation-rules", true},
}

resource := resourceCorpSignalTag()
nameSchema := resource.Schema["short_name"]

for _, tc := range cases {
_, errors := nameSchema.ValidateFunc(tc.value, "short_name")

if tc.expected && len(errors) == 0 {
t.Errorf("Expected an error for value '%s', but got none", tc.value)
}

if !tc.expected && len(errors) > 0 {
t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors)
}
}
}
20 changes: 20 additions & 0 deletions provider/resource_site.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package provider

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/signalsciences/go-sigsci"
)
Expand All @@ -27,17 +29,35 @@ func resourceSite() *schema.Resource {
Description: "Identifying name of the site",
Required: true,
ForceNew: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if !validStringLength(val.(string), 3, 100) {
return nil, []error{fmt.Errorf(`received short_name "%q" is invalid. should be min len 3, max len 100`, val.(string))}
}
return nil, nil
},
},
"display_name": {
Type: schema.TypeString,
Description: "Display name of the site",
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if !validStringLength(val.(string), 3, 100) {
return nil, []error{fmt.Errorf(`received display_name "%q" is invalid. should be min len 3, max len 100`, val.(string))}
}
return nil, nil
},
},
"agent_level": {
Type: schema.TypeString,
Description: "Agent action level - 'block', 'log' or 'off'",
Optional: true,
Default: "log",
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if !existsInString(val.(string), "block", "log", "off") {
return nil, []error{fmt.Errorf(`received agent_level "%q" is invalid. should be "block", "log" or "off"`, val.(string))}
}
return nil, nil
},
},
"agent_anon_mode": { // Has issues on create -- will always be default, will update just fine to the correct value
Type: schema.TypeString,
Expand Down
16 changes: 15 additions & 1 deletion provider/resource_site_signal_tag.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package provider

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/signalsciences/go-sigsci"
)
Expand All @@ -22,14 +24,26 @@ func resourceSiteSignalTag() *schema.Resource {
},
"name": {
Type: schema.TypeString,
Description: "The display name of the signal tag",
Description: "The display name of the signal tag. Must be 3-25 char.",
Required: true,
ForceNew: true, // TODO Hopefully this can be changed in the api later
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if !validStringLength(val.(string), 3, 25) {
return nil, []error{fmt.Errorf(`received name %q is invalid. should be min len 3, max len 25`, val.(string))}
}
return nil, nil
},
},
"description": {
Type: schema.TypeString,
Description: "description",
Optional: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
if !validStringLength(val.(string), 0, 140) {
return nil, []error{fmt.Errorf(`received description is invalid. should be max len 140`)}
}
return nil, nil
},
},
"configurable": {
Type: schema.TypeBool,
Expand Down
52 changes: 52 additions & 0 deletions provider/resource_site_signal_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,55 @@ func TestAccResourceSiteSignalTagCRUD(t *testing.T) {
},
})
}

func TestResourceSiteSignalTagNameValidation(t *testing.T) {
cases := []struct {
value string
expected bool
}{
{"s", true},
{"valid-name", false},
{"this-name-is-way-too-long-for-the-validation-rules", true},
}

resource := resourceSiteSignalTag()
nameSchema := resource.Schema["name"]

for _, tc := range cases {
_, errors := nameSchema.ValidateFunc(tc.value, "name")

if tc.expected && len(errors) == 0 {
t.Errorf("Expected an error for value '%s', but got none", tc.value)
}

if !tc.expected && len(errors) > 0 {
t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors)
}
}
}

func TestResourceSiteSignalTagDescriptionValidation(t *testing.T) {
cases := []struct {
value string
expected bool
}{
{"", false},
{"valid-description", false},
{"this-is-way-too-long-for-the-validation-rules-and-this-is-way-too-long-for-the-validation-rules-and-this-is-way-too-long-for-the-validation-rules", true},
}

resource := resourceSiteSignalTag()
nameSchema := resource.Schema["description"]

for _, tc := range cases {
_, errors := nameSchema.ValidateFunc(tc.value, "description")

if tc.expected && len(errors) == 0 {
t.Errorf("Expected an error for value '%s', but got none", tc.value)
}

if !tc.expected && len(errors) > 0 {
t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors)
}
}
}
80 changes: 80 additions & 0 deletions provider/resource_site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,83 @@ func testACCCheckSiteDestroy(s *terraform.State) error {
}
return nil
}

func TestResourceSiteShortNameValidation(t *testing.T) {
cases := []struct {
value string
expected bool
}{
{"s", true},
{"valid-name", false},
{"this-name-is-way-too-long-for-the-validation-rules-and-this-name-is-way-too-long-for-the-validation-rules", true},
}

resource := resourceSite()
nameSchema := resource.Schema["short_name"]

for _, tc := range cases {
_, errors := nameSchema.ValidateFunc(tc.value, "short_name")

if tc.expected && len(errors) == 0 {
t.Errorf("Expected an error for value '%s', but got none", tc.value)
}

if !tc.expected && len(errors) > 0 {
t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors)
}
}
}

func TestResourceSiteDisplayNameValidation(t *testing.T) {
cases := []struct {
value string
expected bool
}{
{"s", true},
{"valid-name", false},
{"this-name-is-way-too-long-for-the-validation-rules-and-this-name-is-way-too-long-for-the-validation-rules", true},
}

resource := resourceSite()
nameSchema := resource.Schema["display_name"]

for _, tc := range cases {
_, errors := nameSchema.ValidateFunc(tc.value, "display_name")

if tc.expected && len(errors) == 0 {
t.Errorf("Expected an error for value '%s', but got none", tc.value)
}

if !tc.expected && len(errors) > 0 {
t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors)
}
}
}

func TestResourceSiteAgentLevelValidation(t *testing.T) {
cases := []struct {
value string
expected bool
}{
{"block", false},
{"log", false},
{"off", false},
{"foobar", true},
{"", true},
}

resource := resourceSite()
nameSchema := resource.Schema["agent_level"]

for _, tc := range cases {
_, errors := nameSchema.ValidateFunc(tc.value, "agent_level")

if tc.expected && len(errors) == 0 {
t.Errorf("Expected an error for value '%s', but got none", tc.value)
}

if !tc.expected && len(errors) > 0 {
t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors)
}
}
}

0 comments on commit 0b85b22

Please sign in to comment.