Skip to content

Commit

Permalink
[10/X] DXCDT-455: Fix auth0_trigger_action delete logic and add tests…
Browse files Browse the repository at this point in the history
… for import (#639)
  • Loading branch information
sergiught authored Jun 14, 2023
1 parent a130e1c commit a5db686
Show file tree
Hide file tree
Showing 8 changed files with 3,732 additions and 2,367 deletions.
8 changes: 2 additions & 6 deletions internal/auth0/action/resource_trigger_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func deleteTriggerAction(_ context.Context, d *schema.ResourceData, m interface{
return diag.FromErr(err)
}

var updatedBindings []*management.ActionBinding
updatedBindings := make([]*management.ActionBinding, 0)
for _, binding := range triggerBindings.Bindings {
if binding.Action.GetID() == actionID {
continue
Expand All @@ -219,9 +219,5 @@ func deleteTriggerAction(_ context.Context, d *schema.ResourceData, m interface{
})
}

if err := api.Action.UpdateBindings(trigger, updatedBindings); err != nil {
return diag.FromErr(err)
}

return nil
return diag.FromErr(api.Action.UpdateBindings(trigger, updatedBindings))
}
192 changes: 100 additions & 92 deletions internal/auth0/action/resource_trigger_action_test.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,88 @@
package action_test

import (
"os"
"strings"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/stretchr/testify/assert"

"github.com/auth0/terraform-provider-auth0/internal/acctest"
)

const givenAnAction = ` resource auth0_action my_action {
name = "Test Action {{.testName}}"
code = "exports.onExecutePostLogin = async (event, api) => {};"
const testAccCreateTriggerAction = testAccGivenTwoActions + `
resource "auth0_trigger_action" "my_action_post_login" {
depends_on = [ auth0_action.action_bar ]
supported_triggers {
id = "post-login"
version = "v3"
}
deploy = true
action_id = auth0_action.action_foo.id
trigger = auth0_action.action_foo.supported_triggers[0].id
}
`

const testAccCreateTriggerAction = givenAnAction + `
resource auth0_trigger_action my_action_post_login {
action_id = auth0_action.my_action.id
trigger = auth0_action.my_action.supported_triggers[0].id
}
const testAccUpdateTriggerAction = testAccGivenTwoActions + `
resource "auth0_trigger_action" "my_action_post_login" {
depends_on = [ auth0_action.action_bar ]
action_id = auth0_action.action_foo.id
trigger = auth0_action.action_foo.supported_triggers[0].id
display_name = format("%s %s", auth0_action.action_foo.name,"(new display name)")
}
`

const testAccUpdateTriggerAction = givenAnAction + `
resource auth0_trigger_action my_action_post_login {
action_id = auth0_action.my_action.id
trigger = auth0_action.my_action.supported_triggers[0].id
display_name = format("%s %s", auth0_action.my_action.name,"(new display name)")
}
const testAccCreateAnotherTriggerAction = testAccGivenTwoActions + `
resource "auth0_trigger_action" "my_action_post_login" {
depends_on = [ auth0_action.action_bar ]
action_id = auth0_action.action_foo.id
trigger = auth0_action.action_foo.supported_triggers[0].id
display_name = format("%s %s", auth0_action.action_foo.name,"(new display name)")
}
resource "auth0_trigger_action" "another_action_post_login" {
depends_on = [ auth0_trigger_action.my_action_post_login ]
action_id = auth0_action.action_bar.id
trigger = auth0_action.action_bar.supported_triggers[0].id
display_name = auth0_action.action_bar.name
}
`

const testAccCreateAnotherTriggerAction = `
resource auth0_action another_action {
name = "Test Action {{.testName}} (another)"
code = "exports.onExecutePostLogin = async (event, api) => {};"
const testAccTriggerActionImportSetup = testAccGivenTwoActions + `
resource "auth0_trigger_actions" "login_flow" {
depends_on = [ auth0_action.action_bar ]
trigger = "post-login"
supported_triggers {
id = "post-login"
version = "v3"
}
deploy = true
actions {
id = auth0_action.action_foo.id
display_name = auth0_action.action_foo.name
}
resource auth0_trigger_action another_action_post_login {
action_id = auth0_action.another_action.id
trigger = auth0_action.another_action.supported_triggers[0].id
display_name = auth0_action.another_action.name
actions {
id = auth0_action.action_bar.id
display_name = auth0_action.action_bar.name
}
}
`

const testAccTriggerActionImportCheck = testAccTriggerActionImportSetup + `
resource "auth0_trigger_action" "action_1" {
depends_on = [ auth0_trigger_actions.login_flow ]
action_id = auth0_action.action_foo.id
trigger = auth0_action.action_foo.supported_triggers[0].id
display_name = auth0_action.action_foo.name
}
resource "auth0_trigger_action" "action_2" {
depends_on = [ auth0_trigger_action.action_1 ]
action_id = auth0_action.action_bar.id
trigger = auth0_action.action_bar.supported_triggers[0].id
display_name = auth0_action.action_bar.name
}
`

func TestAccTriggerAction(t *testing.T) {
Expand All @@ -64,86 +92,66 @@ func TestAccTriggerAction(t *testing.T) {
Config: acctest.ParseTestName(testAccCreateTriggerAction, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_trigger_action.my_action_post_login", "trigger", "post-login"),
resource.TestCheckResourceAttrSet("auth0_trigger_action.my_action_post_login", "action_id"),
resource.TestCheckResourceAttr("auth0_trigger_action.my_action_post_login", "display_name", "Test Action TestAccTriggerAction"),
resource.TestCheckTypeSetElemAttrPair("auth0_trigger_action.my_action_post_login", "action_id", "auth0_action.action_foo", "id"),
resource.TestCheckResourceAttr("auth0_trigger_action.my_action_post_login", "display_name", fmt.Sprintf("Test Trigger Binding Foo %s", t.Name())),
),
},
{
Config: acctest.ParseTestName(testAccUpdateTriggerAction, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_trigger_action.my_action_post_login", "trigger", "post-login"),
resource.TestCheckResourceAttr("auth0_trigger_action.my_action_post_login", "display_name", "Test Action TestAccTriggerAction (new display name)"),
resource.TestCheckResourceAttrSet("auth0_trigger_action.my_action_post_login", "action_id"),
resource.TestCheckTypeSetElemAttrPair("auth0_trigger_action.my_action_post_login", "action_id", "auth0_action.action_foo", "id"),
resource.TestCheckResourceAttr("auth0_trigger_action.my_action_post_login", "display_name", fmt.Sprintf("Test Trigger Binding Foo %s (new display name)", t.Name())),
),
},
{
Config: acctest.ParseTestName(testAccCreateAnotherTriggerAction, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_trigger_action.my_action_post_login", "trigger", "post-login"),
resource.TestCheckTypeSetElemAttrPair("auth0_trigger_action.my_action_post_login", "action_id", "auth0_action.action_foo", "id"),
resource.TestCheckResourceAttr("auth0_trigger_action.my_action_post_login", "display_name", fmt.Sprintf("Test Trigger Binding Foo %s (new display name)", t.Name())),
resource.TestCheckResourceAttr("auth0_trigger_action.another_action_post_login", "trigger", "post-login"),
resource.TestCheckResourceAttrSet("auth0_trigger_action.another_action_post_login", "action_id"),
resource.TestCheckTypeSetElemAttrPair("auth0_trigger_action.another_action_post_login", "action_id", "auth0_action.action_bar", "id"),
resource.TestCheckResourceAttr("auth0_trigger_action.another_action_post_login", "display_name", fmt.Sprintf("Test Trigger Binding Bar %s", t.Name())),
),
},
{
Config: acctest.ParseTestName(testAccCreateTriggerAction, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_trigger_action.my_action_post_login", "trigger", "post-login"),
resource.TestCheckResourceAttrSet("auth0_trigger_action.my_action_post_login", "action_id"),
),
Config: acctest.ParseTestName(testAccGivenTwoActions, t.Name()),
},
},
})
}

const testAccImportTriggerAction = `
resource auth0_action my_action {
name = "Test Action {{.testName}}"
code = "exports.onExecutePostLogin = async (event, api) => {};"
supported_triggers {
id = "post-login"
version = "v3"
}
}
resource auth0_trigger_action my_action_post_login {
action_id = auth0_action.my_action.id
trigger = tolist(auth0_action.my_action.supported_triggers)[0].id
}
`

func TestAccTriggerActionImport(t *testing.T) {
if os.Getenv("AUTH0_DOMAIN") != acctest.RecordingsDomain {
// The test runs only with recordings as it requires an initial setup.
t.Skip()
}

testName := strings.ToLower(t.Name())

importedActionID := "a35e5b88-7a3e-4e7a-9a3d-4e0f3b4d95d2"

acctest.Test(t, resource.TestCase{
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: acctest.ParseTestName(testAccImportTriggerAction, testName),
ResourceName: "auth0_action.my_action",
ImportState: true,
ImportStateId: importedActionID,
Config: acctest.ParseTestName(testAccTriggerActionImportSetup, t.Name()),
},
{
Config: acctest.ParseTestName(testAccTriggerActionImportCheck, t.Name()),
ResourceName: "auth0_trigger_action.action_1",
ImportState: true,
ImportStateIdFunc: func(state *terraform.State) (string, error) {
actionID, err := acctest.ExtractResourceAttributeFromState(state, "auth0_action.action_foo", "id")
assert.NoError(t, err)

return "post-login::" + actionID, nil
},
ImportStatePersist: true,
},
{
Config: acctest.ParseTestName(testAccImportTriggerAction, testName),
ResourceName: "auth0_trigger_action.my_action_post_login",
ImportState: true,
ImportStateId: "post-login::" + importedActionID,
Config: acctest.ParseTestName(testAccTriggerActionImportCheck, t.Name()),
ResourceName: "auth0_trigger_action.action_2",
ImportState: true,
ImportStateIdFunc: func(state *terraform.State) (string, error) {
actionID, err := acctest.ExtractResourceAttributeFromState(state, "auth0_action.action_bar", "id")
assert.NoError(t, err)

return "post-login::" + actionID, nil
},
ImportStatePersist: true,
},
{
Config: acctest.ParseTestName(testAccImportTriggerAction, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_trigger_action.my_action_post_login", "trigger", "post-login"),
resource.TestCheckResourceAttrSet("auth0_trigger_action.my_action_post_login", "action_id"),
),
Config: acctest.ParseTestName(testAccTriggerActionImportCheck, t.Name()),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
},
},
})
Expand Down
9 changes: 3 additions & 6 deletions internal/auth0/action/resource_trigger_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ func readTriggerBinding(_ context.Context, d *schema.ResourceData, m interface{}
}

func updateTriggerBinding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
triggerBindings := expandTriggerBindings(d.GetRawConfig().GetAttr("actions"))
api := m.(*config.Config).GetAPI()

triggerBindings := expandTriggerBindings(d.GetRawConfig().GetAttr("actions"))
if err := api.Action.UpdateBindings(d.Id(), triggerBindings); err != nil {
return diag.FromErr(err)
}
Expand All @@ -111,9 +112,5 @@ func updateTriggerBinding(ctx context.Context, d *schema.ResourceData, m interfa

func deleteTriggerBinding(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*config.Config).GetAPI()
if err := api.Action.UpdateBindings(d.Id(), []*management.ActionBinding{}); err != nil {
return diag.FromErr(err)
}

return nil
return diag.FromErr(api.Action.UpdateBindings(d.Id(), []*management.ActionBinding{}))
}
Loading

0 comments on commit a5db686

Please sign in to comment.