Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

fix trigger_id from being sent in update payload #210

Merged
merged 1 commit into from
Apr 24, 2020
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
21 changes: 11 additions & 10 deletions auth0/resource_auth0_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"gopkg.in/auth0.v4/management"
)

var hookNameRegexp = regexp.MustCompile("^[^\\s-][\\w -]+[^\\s-]$")

func newHook() *schema.Resource {
return &schema.Resource{

Expand All @@ -26,13 +24,10 @@ func newHook() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(
hookNameRegexp,
"Can only contain alphanumeric characters, spaces and '-'. "+
"Can neither start nor end with '-' or spaces."),
Description: "Name of this hook",
Type: schema.TypeString,
Required: true,
ValidateFunc: validateHookNameFunc(),
Description: "Name of this hook",
},
"script": {
Type: schema.TypeString,
Expand Down Expand Up @@ -122,7 +117,13 @@ func buildHook(d *schema.ResourceData) *management.Hook {
return &management.Hook{
Name: String(d, "name"),
Script: String(d, "script"),
TriggerID: String(d, "trigger_id"),
TriggerID: String(d, "trigger_id", IsNewResource()),
Enabled: Bool(d, "enabled"),
}
}

func validateHookNameFunc() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[^\\s-][\\w -]+[^\\s-]$"),
"Can only contain alphanumeric characters, spaces and '-'. Can neither start nor end with '-' or spaces.")
}
79 changes: 38 additions & 41 deletions auth0/resource_auth0_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

Expand All @@ -15,68 +14,66 @@ func TestAccHook(t *testing.T) {
"auth0": Provider(),
},
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccHook,
{
Config: testAccHookCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_hook.my_hook", "name", "pre-user-reg-hook"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "script", "function (user, context, callback) { callback(null, { user }); }"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "trigger_id", "pre-user-registration"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "enabled", "true"),
),
},
{
Config: testAccHookUpdate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_hook.my_hook", "name", "pre-user-reg-hook"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "script", "function (user, context, callback) { console.log(user); callback(null, { user }); }"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "trigger_id", "pre-user-registration"),
resource.TestCheckResourceAttr("auth0_hook.my_hook", "enabled", "false"),
),
},
},
})
}

const testAccHook = `
const testAccHookCreate = `

resource "auth0_hook" "my_hook" {
name = "pre-user-reg-hook"
script = "function (user, context, callback) { callback(null, { user }); }"
trigger_id = "pre-user-registration"
script = "function (user, context, callback) { callback(null, { user }); }"
enabled = true
}
`

const testAccHookUpdate = `

resource "auth0_hook" "my_hook" {
name = "pre-user-reg-hook"
trigger_id = "pre-user-registration"
script = "function (user, context, callback) { console.log(user); callback(null, { user }); }"
enabled = false
}
`

func TestHookNameRegexp(t *testing.T) {
testCases := []struct {
name string
valid bool
}{
{
name: "my-hook-1",
valid: true,
},
{
name: "hook 2 name with spaces",
valid: true,
},
{
name: " hook with a space prefix",
valid: false,
},
{
name: "hook with a space suffix ",
valid: false,
},
{
name: " ", // hook with only one space,
valid: false,
},
{
name: " ", // hook with only three spaces,
valid: false,
},
}
for name, valid := range map[string]bool{
"my-hook-1": true,
"hook 2 name with spaces": true,
" hook with a space prefix": false,
"hook with a space suffix ": false,
" ": false,
" ": false,
} {
fn := validateHookNameFunc()

vf := validation.StringMatch(hookNameRegexp, "invalid name")
for _, tc := range testCases {
_, errs := vf(tc.name, "name")
if errs != nil && tc.valid {
t.Fatalf("Expected %q to be valid, but got validation errors %v", tc.name, errs)
_, errs := fn(name, "name")
if errs != nil && valid {
t.Fatalf("Expected %q to be valid, but got validation errors %v", name, errs)
}
if errs == nil && !tc.valid {
t.Fatalf("Expected %q to be invalid, but got no validation errors.", tc.name)

if errs == nil && !valid {
t.Fatalf("Expected %q to be invalid, but got no validation errors.", name)
}
}
}