Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add acceptance tests for the update of an endpoint #34

Open
wants to merge 9 commits into
base: feat/INT-4354/endpoint-management
Choose a base branch
from
155 changes: 117 additions & 38 deletions datadome/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,36 @@ func TestProviderConfigure(t *testing.T) {
})
}

/*
Resources test helpers
*/

func testAccResourcePreCheck(t *testing.T) {}

// testAccCheckResourceExists check if the given resourceName exists
func testAccCheckResourceExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("resource not found: %q", resourceName)
}

return nil
}
}

// testAccCheckCustomRuleResourceExists check if the given resourceName does not exists
func testAccCheckResourceDoesNotExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[resourceName]
if ok {
return fmt.Errorf("resource still exists: %q", resourceName)
}

return nil
}
}

/*
Resources CustomRules tests
*/
Expand Down Expand Up @@ -277,32 +307,6 @@ const testAccCustomRuleResourceConfigEmpty = `
provider "datadome" {}
`

func testAccCustomRuleResourcePreCheck(t *testing.T) {}

// testAccCheckCustomRuleResourceExists check if the given resourceName exists
func testAccCheckCustomRuleResourceExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("resource not found: %q", resourceName)
}

return nil
}
}

// testAccCheckCustomRuleResourceExists check if the given resourceName does not exists
func testAccCheckCustomRuleResourceDoesNotExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[resourceName]
if ok {
return fmt.Errorf("resource still exists: %q", resourceName)
}

return nil
}
}

// TestAccCustomRuleResource_basic test the creation and the read of a new custom rule
func TestAccCustomRuleResource_basic(t *testing.T) {
mockClient := datadome.NewMockClientCustomRule()
Expand All @@ -314,13 +318,13 @@ func TestAccCustomRuleResource_basic(t *testing.T) {
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccCustomRuleResourcePreCheck(t) },
PreCheck: func() { testAccResourcePreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCustomRuleResourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckCustomRuleResourceExists("datadome_custom_rule.accConfig"),
testAccCheckResourceExists("datadome_custom_rule.accConfig"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "name", "acc-test"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "query", "ip: 192.168.0.1"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "response", "allow"),
Expand All @@ -344,13 +348,13 @@ func TestAccCustomRuleResource_update(t *testing.T) {
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccCustomRuleResourcePreCheck(t) },
PreCheck: func() { testAccResourcePreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCustomRuleResourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckCustomRuleResourceExists("datadome_custom_rule.accConfig"),
testAccCheckResourceExists("datadome_custom_rule.accConfig"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "name", "acc-test"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "query", "ip: 192.168.0.1"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "response", "allow"),
Expand All @@ -362,7 +366,7 @@ func TestAccCustomRuleResource_update(t *testing.T) {
{
Config: testAccCustomRuleResourceConfigUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckCustomRuleResourceExists("datadome_custom_rule.accConfig"),
testAccCheckResourceExists("datadome_custom_rule.accConfig"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "name", "acc-test-updated"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "priority", "normal"),
),
Expand All @@ -382,13 +386,13 @@ func TestAccCustomRuleResource_delete(t *testing.T) {
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccCustomRuleResourcePreCheck(t) },
PreCheck: func() { testAccResourcePreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCustomRuleResourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckCustomRuleResourceExists("datadome_custom_rule.accConfig"),
testAccCheckResourceExists("datadome_custom_rule.accConfig"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "name", "acc-test"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "query", "ip: 192.168.0.1"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "response", "allow"),
Expand All @@ -400,7 +404,7 @@ func TestAccCustomRuleResource_delete(t *testing.T) {
{
Config: testAccCustomRuleResourceConfigEmpty,
Check: resource.ComposeTestCheckFunc(
testAccCheckCustomRuleResourceDoesNotExists("datadome_custom_rule.accConfig"),
testAccCheckResourceDoesNotExists("datadome_custom_rule.accConfig"),
),
},
},
Expand All @@ -418,7 +422,7 @@ func TestAccCustomRuleResource_wrongParameters(t *testing.T) {
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccCustomRuleResourcePreCheck(t) },
PreCheck: func() { testAccResourcePreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -463,7 +467,7 @@ func TestAccCustomRuleResource_createAlreadyExists(t *testing.T) {
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccCustomRuleResourcePreCheck(t) },
PreCheck: func() { testAccResourcePreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Expand All @@ -488,13 +492,13 @@ func TestAccCustomRuleResource_updateAlreadyExists(t *testing.T) {
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccCustomRuleResourcePreCheck(t) },
PreCheck: func() { testAccResourcePreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCustomRuleResourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckCustomRuleResourceExists("datadome_custom_rule.accConfig"),
testAccCheckResourceExists("datadome_custom_rule.accConfig"),
resource.TestCheckResourceAttr("datadome_custom_rule.accConfig", "name", "acc-test"),
),
},
Expand All @@ -505,3 +509,78 @@ func TestAccCustomRuleResource_updateAlreadyExists(t *testing.T) {
},
})
}

/*
Resources Endpoints tests
*/

const testAccEndpointConfig = `
provider "datadome" {}
resource "datadome_endpoint" "simple" {
cookie_same_site = "Lax"
description = "This is a test"
detection_enabled = false
name = "test-terraform"
protection_enabled = false
response_format = "auto"
source = "Web Browser"
traffic_usage = "Account Creation"
user_agent_inclusion = "TFTEST"
}
`

const testAccEndpointConfigUpdate = `
provider "datadome" {}
resource "datadome_endpoint" "simple" {
cookie_same_site = "Lax"
description = "This is a test"
detection_enabled = false
name = "test-terraform-updated"
protection_enabled = false
response_format = "auto"
source = "Mobile App"
traffic_usage = "Account Creation"
user_agent_inclusion = "TFTEST"
}
`

// TestAccEndpointResource_update test the creation of an endpoint and update it
func TestAccEndpointResource_update(t *testing.T) {
mockClient := datadome.NewMockClientEndpoint()

testAccProvider.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
return &ProviderConfig{
ClientEndpoint: mockClient,
}, nil
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccResourcePreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccEndpointConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists("datadome_endpoint.simple"),
resource.TestCheckResourceAttr("datadome_endpoint.simple", "cookie_same_site", "Lax"),
resource.TestCheckResourceAttr("datadome_endpoint.simple", "description", "This is a test"),
resource.TestCheckResourceAttr("datadome_endpoint.simple", "detection_enabled", "false"),
resource.TestCheckResourceAttr("datadome_endpoint.simple", "name", "test-terraform"),
resource.TestCheckResourceAttr("datadome_endpoint.simple", "protection_enabled", "false"),
resource.TestCheckResourceAttr("datadome_endpoint.simple", "response_format", "auto"),
resource.TestCheckResourceAttr("datadome_endpoint.simple", "source", "Web Browser"),
resource.TestCheckResourceAttr("datadome_endpoint.simple", "traffic_usage", "Account Creation"),
resource.TestCheckResourceAttr("datadome_endpoint.simple", "user_agent_inclusion", "TFTEST"),
),
},
{
Config: testAccEndpointConfigUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists("datadome_endpoint.simple"),
resource.TestCheckResourceAttr("datadome_endpoint.simple", "name", "test-terraform-updated"),
resource.TestCheckResourceAttr("datadome_endpoint.simple", "source", "Mobile App"),
),
},
},
})
}
Loading