Skip to content

Commit

Permalink
Improve validation for universal login template body
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Sep 8, 2023
1 parent d292300 commit dd5d469
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
4 changes: 2 additions & 2 deletions internal/auth0/branding/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/auth0/terraform-provider-auth0/internal/config"
internalError "github.com/auth0/terraform-provider-auth0/internal/error"
internalValidation "github.com/auth0/terraform-provider-auth0/internal/validation"
)

var errNoCustomDomain = fmt.Errorf(
Expand Down Expand Up @@ -94,7 +94,7 @@ func NewResource() *schema.Resource {
"body": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
ValidateFunc: internalValidation.UniversalLoginTemplateContainsCorrectTags,
Description: "The html template for the New Universal Login Experience.",
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/auth0/branding/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestAccBranding(t *testing.T) {
},
{
Config: testAccBrandingConfigThrowsAValidationErrorIfUniversalLoginBodyIsEmpty,
ExpectError: regexp.MustCompile("expected \"universal_login.0.body\" to not be an empty string"),
ExpectError: regexp.MustCompile("expected \"universal_login.0.body\" to contain a single auth0:head tag and at least one auth0:widget tag"),
},
{
Config: testAccBrandingConfigRemovesUniversalLoginTemplate,
Expand Down
18 changes: 18 additions & 0 deletions internal/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package validation
import (
"fmt"
"net/url"
"strings"
)

// IsURLWithHTTPSorEmptyString is a validation func that checks
Expand Down Expand Up @@ -40,3 +41,20 @@ func IsURLWithHTTPSorEmptyString(rawURL interface{}, key string) ([]string, []er

return nil, nil
}

// UniversalLoginTemplateContainsCorrectTags is a validation func that checks
// that the given universal login template body contains the correct tags.
func UniversalLoginTemplateContainsCorrectTags(rawBody interface{}, key string) ([]string, []error) {
v, ok := rawBody.(string)
if !ok {
return nil, []error{fmt.Errorf("expected type of %q to be string", key)}
}

if strings.Contains(v, "{%- auth0:head -%}") && strings.Contains(v, "{%- auth0:widget -%}") {
return nil, nil
}

return nil, []error{
fmt.Errorf("expected %q to contain a single auth0:head tag and at least one auth0:widget tag", key),
}
}
47 changes: 47 additions & 0 deletions internal/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,50 @@ func TestIsURLWithHTTPSorEmptyString(t *testing.T) {
})
}
}

func TestUniversalLoginTemplateContainsCorrectTags(t *testing.T) {
tests := []struct {
name string
input interface{}
key string
expectedError string
}{
{
name: "valid input",
input: `Some content {%- auth0:head -%} More content {%- auth0:widget -%}`,
key: "testKey",
expectedError: "",
},
{
name: "missing auth0:head tag",
input: `Some content More content {%- auth0:widget -%}`,
key: "testKey",
expectedError: "expected \"testKey\" to contain a single auth0:head tag and at least one auth0:widget tag",
},
{
name: "missing auth0:widget tag",
input: `Some content {%- auth0:head -%} More content`,
key: "testKey",
expectedError: "expected \"testKey\" to contain a single auth0:head tag and at least one auth0:widget tag",
},
{
name: "incorrect input type",
input: 42,
key: "testKey",
expectedError: "expected type of \"testKey\" to be string",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, errors := UniversalLoginTemplateContainsCorrectTags(test.input, test.key)

if test.expectedError != "" {
assert.EqualError(t, errors[0], test.expectedError)
return
}

assert.Len(t, errors, 0)
})
}
}

0 comments on commit dd5d469

Please sign in to comment.