Skip to content

Commit

Permalink
datasource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tujit Bora committed Oct 4, 2024
1 parent 0636ec2 commit d514d4c
Show file tree
Hide file tree
Showing 12 changed files with 431 additions and 139 deletions.
36 changes: 36 additions & 0 deletions internal/datasources/data_consent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package datasources_test

import (
"fmt"
"os"
"testing"

acctest "github.com/Cidaas/terraform-provider-cidaas/internal/test"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccDataSourceConsent_basic(t *testing.T) {
t.Parallel()
resourceName := "data.cidaas_consent.sample"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
provider "cidaas" {
base_url = "%s"
}
data "cidaas_consent" "sample" {
}
`, os.Getenv("BASE_URL")), // replace with acctest.BaseURL or have a init func to set the base URL
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "consent.#"),
resource.TestCheckResourceAttrSet(resourceName, "consent.0.id"),
resource.TestCheckResourceAttrSet(resourceName, "consent.0.consent_name"),
),
},
},
})
}
36 changes: 36 additions & 0 deletions internal/datasources/data_custom_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package datasources_test

import (
"fmt"
"os"
"testing"

acctest "github.com/Cidaas/terraform-provider-cidaas/internal/test"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccDataSourceCustomProvider_basic(t *testing.T) {
t.Parallel()
resourceName := "data.cidaas_custom_provider.sample"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
provider "cidaas" {
base_url = "%s"
}
data "cidaas_custom_provider" "sample" {
}
`, os.Getenv("BASE_URL")),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "custom_provider.#"),
resource.TestCheckResourceAttrSet(resourceName, "custom_provider.0.provider_name"),
resource.TestCheckResourceAttrSet(resourceName, "custom_provider.0.standard_type"),
),
},
},
})
}
103 changes: 42 additions & 61 deletions internal/datasources/data_group_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,67 @@ package datasources_test

import (
"fmt"
"log"
"net/http"
"os"
"testing"

"github.com/Cidaas/terraform-provider-cidaas/helpers/cidaas"
acctest "github.com/Cidaas/terraform-provider-cidaas/internal/test"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

var groupType = acctest.RandString(10)
func TestAccGroupTypeDataSource_Basic(t *testing.T) {
t.Parallel()
resourceName := "data.cidaas_group_type.sample"

func testGroupTypeConfig(groupType string) string {
return fmt.Sprintf(`
provider "cidaas" {
base_url = "https://kube-nightlybuild-dev.cidaas.de"
}
data "cidaas_group_type" "example" {
group_type = "%s"
}`, groupType)
}

func TestAccGroupTypeDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
CheckDestroy: destroyGroupTypeIfExists,
Steps: []resource.TestStep{
{
PreConfig: func() {
gt := cidaas.GroupType{
ClientConfig: cidaas.ClientConfig{
BaseURL: os.Getenv("BASE_URL"),
AccessToken: acctest.TestToken,
},
}
payload := cidaas.GroupTypeData{
RoleMode: "no_roles",
GroupType: groupType,
Description: "terraform user group type description",
}
res, _ := gt.Create(payload)
if res != nil && res.Status != http.StatusNoContent {
log.Print("failed to complete pre config")
}
},
Config: testGroupTypeConfig(groupType),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.cidaas_group_type.example", "role_mode", "no_roles"),
Config: fmt.Sprintf(`
provider "cidaas" {
base_url = "%s"
}
data "cidaas_group_type" "sample" {
}
`, os.Getenv("BASE_URL")),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "group_type.#"),
resource.TestCheckResourceAttrSet(resourceName, "group_type.0.id"),
resource.TestCheckResourceAttrSet(resourceName, "group_type.0.group_type"),
resource.TestCheckResourceAttrSet(resourceName, "group_type.0.role_mode"),
resource.TestCheckResourceAttrSet(resourceName, "group_type.0.allowed_roles.#"),
),
},
},
})
}

func destroyGroupTypeIfExists(s *terraform.State) error {
var groupTypeInState string
for _, rs := range s.RootModule().Resources {
if rs.Type != "cidaas_group_type" {
continue
}
groupTypeInState = rs.Primary.Attributes["group_type"]
}

if groupTypeInState != groupType {
return fmt.Errorf("resource not found with by the role created in preconfig step")
}
func TestAccGroupTypeDataSource_RoleModeFilter(t *testing.T) {
t.Parallel()
resourceName := "data.cidaas_group_type.sample"

// resource found and destroy from remote
client := cidaas.GroupType{
ClientConfig: cidaas.ClientConfig{
BaseURL: os.Getenv("BASE_URL"),
AccessToken: acctest.TestToken,
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
provider "cidaas" {
base_url = "%s"
}
data "cidaas_group_type" "sample" {
filter {
name = "role_mode"
values = ["allowed_roles"]
}
}
`, os.Getenv("BASE_URL")),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "group_type.#"),
resource.TestCheckResourceAttr(resourceName, "group_type.0.role_mode", "allowed_roles"),
resource.TestCheckResourceAttrSet(resourceName, "group_type.0.allowed_roles.#"),
),
},
},
}

err := client.Delete(groupTypeInState)
if err != nil {
return fmt.Errorf("failed to destroy resource in remote created in the preconfig step")
}
return nil
})
}
3 changes: 2 additions & 1 deletion internal/datasources/data_registration_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type RegistrationField struct {
IsGroup types.Bool `tfsdk:"is_group"`
ParentGroupID types.String `tfsdk:"parent_group_id"`
FieldType types.String `tfsdk:"field_type"`
Order types.Int32 `tfsdk:"order"`
Order types.Int64 `tfsdk:"order"`
}

var registrationFieldsFilter = FilterConfig{
Expand Down Expand Up @@ -165,5 +165,6 @@ func parseRegistrationField(rf cidaas.RegistrationFieldConfig) (result Registrat
result.ReadOnly = types.BoolValue(rf.ReadOnly)
result.Internal = types.BoolValue(rf.Internal)
result.ParentGroupID = types.StringValue(rf.ParentGroupID)
result.Order = types.Int64Value(rf.Order)
return result
}
73 changes: 73 additions & 0 deletions internal/datasources/data_registration_field_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package datasources_test

import (
"fmt"
"os"
"testing"

acctest "github.com/Cidaas/terraform-provider-cidaas/internal/test"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccRegFieldDataSource_Basic(t *testing.T) {
t.Parallel()
resourceName := "data.cidaas_registration_field.sample"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
provider "cidaas" {
base_url = "%s"
}
data "cidaas_registration_field" "sample" {
}
`, os.Getenv("BASE_URL")),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "registration_field.#"),
resource.TestCheckResourceAttrSet(resourceName, "registration_field.0.id"),
resource.TestCheckResourceAttrSet(resourceName, "registration_field.0.parent_group_id"),
resource.TestCheckResourceAttrSet(resourceName, "registration_field.0.field_type"),
resource.TestCheckResourceAttrSet(resourceName, "registration_field.0.data_type"),
resource.TestCheckResourceAttrSet(resourceName, "registration_field.0.field_key"),
resource.TestCheckResourceAttrSet(resourceName, "registration_field.0.required"),
resource.TestCheckResourceAttrSet(resourceName, "registration_field.0.internal"),
resource.TestCheckResourceAttrSet(resourceName, "registration_field.0.enabled"),
resource.TestCheckResourceAttrSet(resourceName, "registration_field.0.read_only"),
resource.TestCheckResourceAttrSet(resourceName, "registration_field.0.is_group"),
resource.TestCheckResourceAttrSet(resourceName, "registration_field.0.order"),
),
},
},
})
}

func TestAccRegFieldDataSource_FieldTypeFilter(t *testing.T) {
t.Parallel()
resourceName := "data.cidaas_registration_field.sample"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
provider "cidaas" {
base_url = "%s"
}
data "cidaas_registration_field" "sample" {
filter {
name = "field_type"
values = ["CUSTOM"]
}
}
`, os.Getenv("BASE_URL")),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "registration_field.0.field_type", "CUSTOM"),
),
},
},
})
}
Loading

0 comments on commit d514d4c

Please sign in to comment.