Skip to content

Commit

Permalink
test: add acceptance tests for the import of an endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lriberon committed Jan 8, 2025
1 parent cc9dd13 commit 4dda061
Showing 1 changed file with 104 additions and 30 deletions.
134 changes: 104 additions & 30 deletions datadome/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,34 @@ func TestProviderConfigure(t *testing.T) {
})
}

/*
Resources test helpers
*/

// 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 @@ -271,30 +299,6 @@ 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 @@ -312,7 +316,7 @@ func TestAccCustomRuleResource_basic(t *testing.T) {
{
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 Down Expand Up @@ -342,7 +346,7 @@ func TestAccCustomRuleResource_update(t *testing.T) {
{
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 @@ -354,7 +358,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 @@ -380,7 +384,7 @@ func TestAccCustomRuleResource_delete(t *testing.T) {
{
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 @@ -392,7 +396,7 @@ func TestAccCustomRuleResource_delete(t *testing.T) {
{
Config: testAccCustomRuleResourceConfigEmpty,
Check: resource.ComposeTestCheckFunc(
testAccCheckCustomRuleResourceDoesNotExists("datadome_custom_rule.accConfig"),
testAccCheckResourceDoesNotExists("datadome_custom_rule.accConfig"),
),
},
},
Expand Down Expand Up @@ -486,7 +490,7 @@ func TestAccCustomRuleResource_updateAlreadyExists(t *testing.T) {
{
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 @@ -497,3 +501,73 @@ 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"
}
`

func TestAccEndpointResource_import(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() { testAccCustomRuleResourcePreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccEndpointConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists("datadome_endpoint.simple"),
resource.TestCheckResourceAttrSet("datadome_endpoint.simple", "id"),
),
},
{
Config: ``,
ResourceName: "datadome_endpoint.simple",
ImportState: true,
ImportStateIdFunc: func(state *terraform.State) (string, error) {
rs, ok := state.RootModule().Resources["datadome_endpoint.simple"]
if !ok {
return "", fmt.Errorf("datadome_endpoint not found: datadome_endpoint.simple")
}
return rs.Primary.ID, nil
},
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists("datadome_endpoint.simple"),
resource.TestCheckResourceAttrSet("datadome_endpoint.simple", "id"),
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"),
),
},
},
})
}

0 comments on commit 4dda061

Please sign in to comment.