Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorGFM committed Nov 20, 2023
1 parent 0926430 commit b817177
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cyral/internal/tokensettings/datasource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tokensettings_test

import (
"testing"

"github.com/cyralinc/terraform-provider-cyral/cyral/internal/tokensettings"
"github.com/cyralinc/terraform-provider-cyral/cyral/provider"
"github.com/cyralinc/terraform-provider-cyral/cyral/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
completeDataSourceName = "data." + completeResourceName
)

func TestAccAccessTokenSettingsDataSource(t *testing.T) {
testSteps := []resource.TestStep{
{
Config: testAccAccessTokenSettingsDataSourceConfig(),
Check: testAccAccessTokenSettingsDataSourceCheck(),
},
}

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: provider.ProviderFactories,
Steps: testSteps,
})
}

func testAccAccessTokenSettingsDataSourceConfig() string {
return `
data "cyral_access_token_settings" "token_settings" {
}
`
}

func testAccAccessTokenSettingsDataSourceCheck() resource.TestCheckFunc {
testCheckFuncs := []resource.TestCheckFunc{
resource.TestCheckResourceAttrSet(completeDataSourceName, utils.IDKey),
resource.TestCheckResourceAttrSet(completeDataSourceName, tokensettings.MaxValidityKey),
resource.TestCheckResourceAttrSet(completeDataSourceName, tokensettings.DefaultValidityKey),
resource.TestCheckResourceAttrSet(completeDataSourceName, tokensettings.MaxNumberOfTokensPerUserKey),
resource.TestCheckResourceAttrSet(completeDataSourceName, tokensettings.OfflineTokenValidationKey),
}
return resource.ComposeTestCheckFunc(
testCheckFuncs...,
)
}
134 changes: 134 additions & 0 deletions cyral/internal/tokensettings/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package tokensettings_test

import (
"fmt"
"regexp"
"testing"

"github.com/cyralinc/terraform-provider-cyral/cyral/internal/tokensettings"
"github.com/cyralinc/terraform-provider-cyral/cyral/provider"
"github.com/cyralinc/terraform-provider-cyral/cyral/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
resourceName = "token_settings"
completeResourceName = "cyral_access_token_settings." + resourceName
)

func TestAccAccessTokenSettingsResource(t *testing.T) {
testSteps := []resource.TestStep{}
testSteps = append(testSteps, getRequiredArgumentTestSteps()...)
testSteps = append(
testSteps,
[]resource.TestStep{
{
Config: testAccAccessTokenSettingsConfig_OnlyRequiredArguments(accessTokenSettingsOnlyRequiredArguments),
Check: testAccAccessTokenSettingsCheck(accessTokenSettingsOnlyRequiredArguments),
},
{
ImportState: true,
ImportStateVerify: true,
ResourceName: completeResourceName,
},
}...,
)

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: provider.ProviderFactories,
Steps: testSteps,
})
}

func getRequiredArgumentTestSteps() []resource.TestStep {
requiredArgumentsTestSteps := []resource.TestStep{}
requiredArguments := []string{
tokensettings.MaxValidityKey,
tokensettings.DefaultValidityKey,
tokensettings.MaxNumberOfTokensPerUserKey,
tokensettings.OfflineTokenValidationKey,
}
for _, argument := range requiredArguments {
requiredArgumentsTestSteps = append(requiredArgumentsTestSteps, resource.TestStep{
Config: testAccAccessTokenSettingsConfig_EmptyConfig(),
ExpectError: regexp.MustCompile(
fmt.Sprintf(`The argument "%s" is required`, argument),
),
})
}
return requiredArgumentsTestSteps
}

func testAccAccessTokenSettingsConfig_EmptyConfig() string {
return `
resource "cyral_access_token_settings" "token_settings" {
}
`
}

type AccessTokenSettingsTestParameters struct {
settings tokensettings.AccessTokenSettings
}

var (
accessTokenSettingsOnlyRequiredArguments = AccessTokenSettingsTestParameters{
settings: tokensettings.AccessTokenSettings{
MaxValidity: "18000s",
DefaultValidity: "300s",
MaxNumberOfTokensPerUser: 5,
OfflineTokenValidation: false,
},
}
)

func testAccAccessTokenSettingsConfig_OnlyRequiredArguments(
parameters AccessTokenSettingsTestParameters,
) string {
return fmt.Sprintf(`
resource "cyral_access_token_settings" "token_settings" {
max_validity = %q
default_validity = %q
max_number_of_tokens_per_user = %d
offline_token_validation = %t
}
`,
parameters.settings.MaxValidity,
parameters.settings.DefaultValidity,
parameters.settings.MaxNumberOfTokensPerUser,
parameters.settings.OfflineTokenValidation,
)
}

func testAccAccessTokenSettingsCheck(
parameters AccessTokenSettingsTestParameters,
) resource.TestCheckFunc {
testCheckFuncs := []resource.TestCheckFunc{
resource.TestCheckResourceAttrSet(completeResourceName, utils.IDKey),
resource.TestCheckResourceAttr(
completeResourceName,
tokensettings.MaxValidityKey,
parameters.settings.MaxValidity,
),
resource.TestCheckResourceAttrSet(completeResourceName, utils.IDKey),
resource.TestCheckResourceAttr(
completeResourceName,
tokensettings.DefaultValidityKey,
parameters.settings.DefaultValidity,
),
resource.TestCheckResourceAttrSet(completeResourceName, utils.IDKey),
resource.TestCheckResourceAttr(
completeResourceName,
tokensettings.MaxNumberOfTokensPerUserKey,
fmt.Sprintf("%d", parameters.settings.MaxNumberOfTokensPerUser),
),
resource.TestCheckResourceAttrSet(completeResourceName, utils.IDKey),
resource.TestCheckResourceAttr(
completeResourceName,
tokensettings.OfflineTokenValidationKey,
fmt.Sprintf("%t", parameters.settings.OfflineTokenValidation),
),
}
return resource.ComposeTestCheckFunc(
testCheckFuncs...,
)
}

0 comments on commit b817177

Please sign in to comment.