diff --git a/cyral/internal/tokensettings/datasource_test.go b/cyral/internal/tokensettings/datasource_test.go new file mode 100644 index 00000000..b741f944 --- /dev/null +++ b/cyral/internal/tokensettings/datasource_test.go @@ -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..., + ) +} diff --git a/cyral/internal/tokensettings/resource_test.go b/cyral/internal/tokensettings/resource_test.go new file mode 100644 index 00000000..d61811ab --- /dev/null +++ b/cyral/internal/tokensettings/resource_test.go @@ -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: "72000s", + DefaultValidity: "36000s", + MaxNumberOfTokensPerUser: 5, + OfflineTokenValidation: true, + }, + } +) + +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..., + ) +}