Skip to content

Commit

Permalink
feat(testsetup): automate authentication for test fixture recording
Browse files Browse the repository at this point in the history
The test setup automatically detects if a fixture will be
(re-)recorded. For recording the user from env is used and
for playback a redacted test user is used.
Furthermore, re-recording can now be enforced without prior
deletion of the fixture by setting the environment variable
TEST_FORCE_REC to 'true'.

Resolves #294

Also: Improve redaction of custom idp related values by always
      using a custom idp and a test issuer.

Resolves #277
  • Loading branch information
kuntzed committed Aug 1, 2023
1 parent 2359dd1 commit 0caa62c
Show file tree
Hide file tree
Showing 183 changed files with 16,899 additions and 16,187 deletions.
10 changes: 5 additions & 5 deletions internal/provider/datasource_directory_entitlements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
func TestDataSourceDirectoryEntitlements(t *testing.T) {
t.Parallel()
t.Run("happy path", func(t *testing.T) {
rec := setupVCR(t, "fixtures/datasource_directory_entitlements")
rec, user := setupVCR(t, "fixtures/datasource_directory_entitlements")
defer stopQuietly(rec)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryEntitlements("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
Config: hclProviderFor(user) + hclDatasourceDirectoryEntitlements("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.btp_directory_entitlements.uut", "id", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
resource.TestCheckResourceAttr("data.btp_directory_entitlements.uut", "directory_id", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
Expand All @@ -38,7 +38,7 @@ func TestDataSourceDirectoryEntitlements(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryEntitlements("uut", "this-is-not-a-uuid"),
Config: hclDatasourceDirectoryEntitlements("uut", "this-is-not-a-uuid"),
ExpectError: regexp.MustCompile(`Attribute directory_id value must be a valid UUID, got: this-is-not-a-uuid`),
},
},
Expand All @@ -50,7 +50,7 @@ func TestDataSourceDirectoryEntitlements(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + `data "btp_directory_entitlements" "uut" {}`,
Config: `data "btp_directory_entitlements" "uut" {}`,
ExpectError: regexp.MustCompile(`The argument "directory_id" is required, but no definition was found`),
},
},
Expand All @@ -71,7 +71,7 @@ func TestDataSourceDirectoryEntitlements(t *testing.T) {
ProtoV6ProviderFactories: getProviders(srv.Client()),
Steps: []resource.TestStep{
{
Config: hclProviderWithCLIServerURL(srv.URL) + hclDatasourceDirectoryEntitlements("uut", "5357bda0-8651-4eab-a69d-12d282bc3247"),
Config: hclProviderForCLIServerAt(srv.URL) + hclDatasourceDirectoryEntitlements("uut", "5357bda0-8651-4eab-a69d-12d282bc3247"),
ExpectError: regexp.MustCompile(`Received response with unexpected status \[Status: 404; Correlation ID:\s+[a-f0-9\-]+\]`),
},
},
Expand Down
10 changes: 5 additions & 5 deletions internal/provider/datasource_directory_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
func TestDataSourceDirectoryLabels(t *testing.T) {
t.Parallel()
t.Run("happy path", func(t *testing.T) {
rec := setupVCR(t, "fixtures/datasource_directory_labels")
rec, user := setupVCR(t, "fixtures/datasource_directory_labels")
defer stopQuietly(rec)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryLabels("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
Config: hclProviderFor(user) + hclDatasourceDirectoryLabels("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.btp_directory_labels.uut", "directory_id", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
resource.TestCheckResourceAttr("data.btp_directory_labels.uut", "values.%", "2"),
Expand All @@ -37,7 +37,7 @@ func TestDataSourceDirectoryLabels(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryLabels("uut", "this-is-not-a-uuid"),
Config: hclDatasourceDirectoryLabels("uut", "this-is-not-a-uuid"),
ExpectError: regexp.MustCompile(`Attribute directory_id value must be a valid UUID, got: this-is-not-a-uuid`),
},
},
Expand All @@ -49,7 +49,7 @@ func TestDataSourceDirectoryLabels(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + `data "btp_directory_labels" "uut" {}`,
Config: `data "btp_directory_labels" "uut" {}`,
ExpectError: regexp.MustCompile(`The argument "directory_id" is required, but no definition was found`),
},
},
Expand All @@ -70,7 +70,7 @@ func TestDataSourceDirectoryLabels(t *testing.T) {
ProtoV6ProviderFactories: getProviders(srv.Client()),
Steps: []resource.TestStep{
{
Config: hclProviderWithCLIServerURL(srv.URL) + hclDatasourceDirectoryLabels("uut", "5357bda0-8651-4eab-a69d-12d282bc3247"),
Config: hclProviderForCLIServerAt(srv.URL) + hclDatasourceDirectoryLabels("uut", "5357bda0-8651-4eab-a69d-12d282bc3247"),
ExpectError: regexp.MustCompile(`Received response with unexpected status \[Status: 404; Correlation ID:\s+[a-f0-9\-]+\]`),
},
},
Expand Down
16 changes: 8 additions & 8 deletions internal/provider/datasource_directory_role_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
func TestDataSourceDirectoryRoleCollection(t *testing.T) {
t.Parallel()
t.Run("happy path", func(t *testing.T) {
rec := setupVCR(t, "fixtures/datasource_directory_role_collection")
rec, user := setupVCR(t, "fixtures/datasource_directory_role_collection")
defer stopQuietly(rec)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRoleCollection("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "Directory Viewer"),
Config: hclProviderFor(user) + hclDatasourceDirectoryRoleCollection("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "Directory Viewer"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.btp_directory_role_collection.uut", "directory_id", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
resource.TestCheckResourceAttr("data.btp_directory_role_collection.uut", "name", "Directory Viewer"),
Expand All @@ -35,15 +35,15 @@ func TestDataSourceDirectoryRoleCollection(t *testing.T) {
})
})
t.Run("error path - directory not security enabled", func(t *testing.T) {
rec := setupVCR(t, "fixtures/datasource_directory_role_collection.not_security_enabled")
rec, user := setupVCR(t, "fixtures/datasource_directory_role_collection.not_security_enabled")
defer stopQuietly(rec)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRoleCollection("uut", "5357bda0-8651-4eab-a69d-12d282bc3247", "Directory Viewer"),
Config: hclProviderFor(user) + hclDatasourceDirectoryRoleCollection("uut", "5357bda0-8651-4eab-a69d-12d282bc3247", "Directory Viewer"),
ExpectError: regexp.MustCompile(`Received response with unexpected status \[Status: 400; Correlation ID:\s+[a-f0-9\-]+\]`),
},
},
Expand All @@ -55,7 +55,7 @@ func TestDataSourceDirectoryRoleCollection(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + `data "btp_directory_role_collections" "uut" {}`,
Config: `data "btp_directory_role_collections" "uut" {}`,
ExpectError: regexp.MustCompile(`The argument "directory_id" is required, but no definition was found`),
},
},
Expand All @@ -67,7 +67,7 @@ func TestDataSourceDirectoryRoleCollection(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRoleCollection("uut", "this-is-not-a-uuid", "Directory Viewer"),
Config: hclDatasourceDirectoryRoleCollection("uut", "this-is-not-a-uuid", "Directory Viewer"),
ExpectError: regexp.MustCompile(`Attribute directory_id value must be a valid UUID, got: this-is-not-a-uuid`),
},
},
Expand All @@ -79,7 +79,7 @@ func TestDataSourceDirectoryRoleCollection(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRoleCollection("uut", "5357bda0-8651-4eab-a69d-12d282bc3247", ""),
Config: hclDatasourceDirectoryRoleCollection("uut", "5357bda0-8651-4eab-a69d-12d282bc3247", ""),
ExpectError: regexp.MustCompile(`Attribute name string length must be at least 1, got: 0`),
},
},
Expand All @@ -100,7 +100,7 @@ func TestDataSourceDirectoryRoleCollection(t *testing.T) {
ProtoV6ProviderFactories: getProviders(srv.Client()),
Steps: []resource.TestStep{
{
Config: hclProviderWithCLIServerURL(srv.URL) + hclDatasourceDirectoryRoleCollection("uut", "5357bda0-8651-4eab-a69d-12d282bc3247", "Directory Viewer"),
Config: hclProviderForCLIServerAt(srv.URL) + hclDatasourceDirectoryRoleCollection("uut", "5357bda0-8651-4eab-a69d-12d282bc3247", "Directory Viewer"),
ExpectError: regexp.MustCompile(`Received response with unexpected status \[Status: 404; Correlation ID:\s+[a-f0-9\-]+\]`),
},
},
Expand Down
14 changes: 7 additions & 7 deletions internal/provider/datasource_directory_role_collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
func TestDataSourceDirectoryRoleCollections(t *testing.T) {
t.Parallel()
t.Run("happy path", func(t *testing.T) {
rec := setupVCR(t, "fixtures/datasource_directory_role_collections")
rec, user := setupVCR(t, "fixtures/datasource_directory_role_collections")
defer stopQuietly(rec)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRoleCollections("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
Config: hclProviderFor(user) + hclDatasourceDirectoryRoleCollections("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.btp_directory_role_collections.uut", "directory_id", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
resource.TestCheckResourceAttr("data.btp_directory_role_collections.uut", "values.#", "2"),
Expand All @@ -32,15 +32,15 @@ func TestDataSourceDirectoryRoleCollections(t *testing.T) {
})
})
t.Run("error path - directory not security enabled", func(t *testing.T) {
rec := setupVCR(t, "fixtures/datasource_directory_role_collections.not_security_enabled")
rec, user := setupVCR(t, "fixtures/datasource_directory_role_collections.not_security_enabled")
defer stopQuietly(rec)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRoleCollections("uut", "5357bda0-8651-4eab-a69d-12d282bc3247"),
Config: hclProviderFor(user) + hclDatasourceDirectoryRoleCollections("uut", "5357bda0-8651-4eab-a69d-12d282bc3247"),
ExpectError: regexp.MustCompile(`Received response with unexpected status \[Status: 400; Correlation ID:\s+[a-f0-9\-]+\]`),
},
},
Expand All @@ -52,7 +52,7 @@ func TestDataSourceDirectoryRoleCollections(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + `data "btp_directory_role_collections" "uut" {}`,
Config: `data "btp_directory_role_collections" "uut" {}`,
ExpectError: regexp.MustCompile(`The argument "directory_id" is required, but no definition was found`),
},
},
Expand All @@ -64,7 +64,7 @@ func TestDataSourceDirectoryRoleCollections(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRoleCollections("uut", "this-is-not-a-uuid"),
Config: hclDatasourceDirectoryRoleCollections("uut", "this-is-not-a-uuid"),
ExpectError: regexp.MustCompile(`Attribute directory_id value must be a valid UUID, got: this-is-not-a-uuid`),
},
},
Expand All @@ -85,7 +85,7 @@ func TestDataSourceDirectoryRoleCollections(t *testing.T) {
ProtoV6ProviderFactories: getProviders(srv.Client()),
Steps: []resource.TestStep{
{
Config: hclProviderWithCLIServerURL(srv.URL) + hclDatasourceDirectoryRoleCollections("uut", "5357bda0-8651-4eab-a69d-12d282bc3247"),
Config: hclProviderForCLIServerAt(srv.URL) + hclDatasourceDirectoryRoleCollections("uut", "5357bda0-8651-4eab-a69d-12d282bc3247"),
ExpectError: regexp.MustCompile(`Received response with unexpected status \[Status: 404; Correlation ID:\s+[a-f0-9\-]+\]`),
},
},
Expand Down
20 changes: 10 additions & 10 deletions internal/provider/datasource_directory_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
func TestDataSourceDirectoryRole(t *testing.T) {
t.Parallel()
t.Run("happy path", func(t *testing.T) {
rec := setupVCR(t, "fixtures/datasource_directory_role")
rec, user := setupVCR(t, "fixtures/datasource_directory_role")
defer stopQuietly(rec)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRole("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "Directory Viewer", "Directory_Viewer", "cis-central!b13"),
Config: hclProviderFor(user) + hclDatasourceDirectoryRole("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "Directory Viewer", "Directory_Viewer", "cis-central!b13"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.btp_directory_role.uut", "directory_id", "05368777-4934-41e8-9f3c-6ec5f4d564b9"),
resource.TestCheckResourceAttr("data.btp_directory_role.uut", "name", "Directory Viewer"),
Expand All @@ -37,15 +37,15 @@ func TestDataSourceDirectoryRole(t *testing.T) {
})
})
t.Run("error path - directory not security enabled", func(t *testing.T) {
rec := setupVCR(t, "fixtures/datasource_directory_role.not_security_enabled")
rec, user := setupVCR(t, "fixtures/datasource_directory_role.not_security_enabled")
defer stopQuietly(rec)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: getProviders(rec.GetDefaultClient()),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRole("uut", "5357bda0-8651-4eab-a69d-12d282bc3247", "Directory Viewer", "Directory_Viewer", "cis-central!b13"),
Config: hclProviderFor(user) + hclDatasourceDirectoryRole("uut", "5357bda0-8651-4eab-a69d-12d282bc3247", "Directory Viewer", "Directory_Viewer", "cis-central!b13"),
ExpectError: regexp.MustCompile(`Received response with unexpected status \[Status: 400; Correlation ID:\s+[a-f0-9\-]+\]`),
},
},
Expand All @@ -57,7 +57,7 @@ func TestDataSourceDirectoryRole(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + `data "btp_directory_role" "uut" {}`,
Config: `data "btp_directory_role" "uut" {}`,
ExpectError: regexp.MustCompile(`The argument "(directory_id|name|role_template_name|app_id)" is required, but no definition was found.`),
},
},
Expand All @@ -69,7 +69,7 @@ func TestDataSourceDirectoryRole(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRole("uut", "this-is-not-a-uuid", "a", "b", "c"),
Config: hclDatasourceDirectoryRole("uut", "this-is-not-a-uuid", "a", "b", "c"),
ExpectError: regexp.MustCompile(`Attribute directory_id value must be a valid UUID, got: this-is-not-a-uuid`),
},
},
Expand All @@ -81,7 +81,7 @@ func TestDataSourceDirectoryRole(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRole("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "", "b", "c"),
Config: hclDatasourceDirectoryRole("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "", "b", "c"),
ExpectError: regexp.MustCompile(`Attribute name string length must be at least 1, got: 0`),
},
},
Expand All @@ -93,7 +93,7 @@ func TestDataSourceDirectoryRole(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRole("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "a", "", "c"),
Config: hclDatasourceDirectoryRole("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "a", "", "c"),
ExpectError: regexp.MustCompile(`Attribute role_template_name string length must be at least 1, got: 0`),
},
},
Expand All @@ -105,7 +105,7 @@ func TestDataSourceDirectoryRole(t *testing.T) {
ProtoV6ProviderFactories: getProviders(nil),
Steps: []resource.TestStep{
{
Config: hclProvider() + hclDatasourceDirectoryRole("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "a", "b", ""),
Config: hclDatasourceDirectoryRole("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "a", "b", ""),
ExpectError: regexp.MustCompile(`Attribute app_id string length must be at least 1, got: 0`),
},
},
Expand All @@ -126,7 +126,7 @@ func TestDataSourceDirectoryRole(t *testing.T) {
ProtoV6ProviderFactories: getProviders(srv.Client()),
Steps: []resource.TestStep{
{
Config: hclProviderWithCLIServerURL(srv.URL) + hclDatasourceDirectoryRole("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "Directory Viewer", "Directory_Viewer", "cis-central!b13"),
Config: hclProviderForCLIServerAt(srv.URL) + hclDatasourceDirectoryRole("uut", "05368777-4934-41e8-9f3c-6ec5f4d564b9", "Directory Viewer", "Directory_Viewer", "cis-central!b13"),
ExpectError: regexp.MustCompile(`Received response with unexpected status \[Status: 404; Correlation ID:\s+[a-f0-9\-]+\]`),
},
},
Expand Down
Loading

0 comments on commit 0caa62c

Please sign in to comment.