Skip to content

Commit

Permalink
add import test
Browse files Browse the repository at this point in the history
  • Loading branch information
davidji99 committed Mar 2, 2024
1 parent cd6c3b9 commit f1944b4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
33 changes: 33 additions & 0 deletions split/import_split_api_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package split

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"regexp"
"testing"
)

func TestAccSplitApiKey_importBasic(t *testing.T) {
workspaceID := testAccConfig.GetWorkspaceIDorSkip(t)
name := fmt.Sprintf("tftest-%s", acctest.RandString(8))
envName := fmt.Sprintf("tftest-%s", acctest.RandString(8))

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckSplitApiKey_basic(workspaceID, name, "client_side", envName),
},
{
ResourceName: "split_api_key.foobar",
ImportState: true,
ImportStateVerify: true,
ExpectError: regexp.MustCompile(`not possible to import existing API keys due to API limitations`),
},
},
})
}
56 changes: 56 additions & 0 deletions split/resource_split_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,45 @@ func TestAccSplitApiKey_ClientSide_Basic(t *testing.T) {
}

func TestAccSplitApiKey_ServerSide_Basic(t *testing.T) {
workspaceID := testAccConfig.GetWorkspaceIDorSkip(t)
name := fmt.Sprintf("tftest-%s", acctest.RandString(8))
envName := fmt.Sprintf("tftest-%s", acctest.RandString(8))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckSplitApiKey_basic(workspaceID, name, "server_side", envName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"split_environment.foobar", "id"),
),
},
},
})
}

// TestAccSplitApiKey_Admin_Basic may or may not work depending on your account's functionality. You may get the following
// error: `402 {"code":402,"message":"Forbidden by Paywalls:`
func TestAccSplitApiKey_Admin_Basic(t *testing.T) {
workspaceID := testAccConfig.GetWorkspaceIDorSkip(t)
name := fmt.Sprintf("tftest-%s", acctest.RandString(8))
envName := fmt.Sprintf("tftest-%s", acctest.RandString(8))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckSplitApiKey_basic_WithRoles(workspaceID, name, "admin", envName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"split_environment.foobar", "id"),
),
},
},
})
}

func testAccCheckSplitApiKey_basic(workspaceID, name, keyType, environmentName string) string {
Expand All @@ -55,3 +89,25 @@ resource "split_api_key" "foobar" {
}
`, workspaceID, name, keyType, environmentName)
}

func testAccCheckSplitApiKey_basic_WithRoles(workspaceID, name, keyType, environmentName string) string {
return fmt.Sprintf(`
provider "split" {
remove_environment_from_state_only = true
}
resource "split_environment" "foobar" {
workspace_id = "%[1]s"
name = "%[4]s"
production = true
}
resource "split_api_key" "foobar" {
workspace_id = "%[1]s"
name = "%[2]s"
type = "%[3]s"
environment_ids = [split_environment.foobar.id]
roles = ["API_ALL_GRANTED"]
}
`, workspaceID, name, keyType, environmentName)
}

0 comments on commit f1944b4

Please sign in to comment.