|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAccPasswordPolicy_create(t *testing.T) { |
| 13 | + passwordPolicy := PasswordPolicy{ |
| 14 | + MinLength: 9, |
| 15 | + MaxLength: 50, |
| 16 | + MustContainLowercase: false, |
| 17 | + MustContainUppercase: true, |
| 18 | + MustContainDigits: false, |
| 19 | + MustContainSpecialChars: true, |
| 20 | + MaxPasswordAgeInDays: 364, |
| 21 | + MinUniquePasswords: 4, |
| 22 | + AccountLockoutThreshold: 5, |
| 23 | + FailedLoginResetDurationInMins: 3, |
| 24 | + AccountLockoutDurationInMins: 30, |
| 25 | + RequireMfa: false, |
| 26 | + RememberMfa: false, |
| 27 | + } |
| 28 | + |
| 29 | + resource.Test(t, resource.TestCase{ |
| 30 | + PreCheck: func() { testAccPreCheck(t) }, |
| 31 | + Providers: testAccProviders, |
| 32 | + CheckDestroy: testAccCheckPasswordPolicyDestroy(), |
| 33 | + Steps: []resource.TestStep{ |
| 34 | + { |
| 35 | + Config: newPasswordPolicyConfig("test", &passwordPolicy), |
| 36 | + Check: resource.ComposeTestCheckFunc( |
| 37 | + testAccCheckPasswordPolicyExists("sumologic_password_policy.test"), |
| 38 | + testPasswordPolicyCheckResourceAttr("sumologic_password_policy.test", &passwordPolicy), |
| 39 | + ), |
| 40 | + }, |
| 41 | + }, |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +func TestAccPasswordPolicy_update(t *testing.T) { |
| 46 | + passwordPolicy := PasswordPolicy{ |
| 47 | + MinLength: 10, |
| 48 | + MaxLength: 51, |
| 49 | + MustContainLowercase: true, |
| 50 | + MustContainUppercase: true, |
| 51 | + MustContainDigits: false, |
| 52 | + MustContainSpecialChars: true, |
| 53 | + MaxPasswordAgeInDays: 364, |
| 54 | + MinUniquePasswords: 4, |
| 55 | + AccountLockoutThreshold: 5, |
| 56 | + FailedLoginResetDurationInMins: 3, |
| 57 | + AccountLockoutDurationInMins: 30, |
| 58 | + RequireMfa: false, |
| 59 | + RememberMfa: false, |
| 60 | + } |
| 61 | + |
| 62 | + updatedPasswordPolicy := PasswordPolicy{ |
| 63 | + MinLength: 12, |
| 64 | + MaxLength: 52, |
| 65 | + MustContainLowercase: false, |
| 66 | + MustContainUppercase: false, |
| 67 | + MustContainDigits: false, |
| 68 | + MustContainSpecialChars: true, |
| 69 | + MaxPasswordAgeInDays: 365, |
| 70 | + MinUniquePasswords: 5, |
| 71 | + AccountLockoutThreshold: 6, |
| 72 | + FailedLoginResetDurationInMins: 5, |
| 73 | + AccountLockoutDurationInMins: 31, |
| 74 | + RequireMfa: true, |
| 75 | + RememberMfa: true, |
| 76 | + } |
| 77 | + |
| 78 | + resource.Test(t, resource.TestCase{ |
| 79 | + PreCheck: func() { testAccPreCheck(t) }, |
| 80 | + Providers: testAccProviders, |
| 81 | + CheckDestroy: testAccCheckPasswordPolicyDestroy(), |
| 82 | + Steps: []resource.TestStep{ |
| 83 | + { |
| 84 | + Config: newPasswordPolicyConfig("test", &passwordPolicy), |
| 85 | + Check: resource.ComposeTestCheckFunc( |
| 86 | + testAccCheckPasswordPolicyExists("sumologic_password_policy.test"), |
| 87 | + testPasswordPolicyCheckResourceAttr("sumologic_password_policy.test", &passwordPolicy), |
| 88 | + ), |
| 89 | + }, |
| 90 | + { |
| 91 | + Config: newPasswordPolicyConfig("test", &updatedPasswordPolicy), |
| 92 | + Check: resource.ComposeTestCheckFunc( |
| 93 | + testAccCheckPasswordPolicyExists("sumologic_password_policy.test"), |
| 94 | + testPasswordPolicyCheckResourceAttr("sumologic_password_policy.test", &updatedPasswordPolicy), |
| 95 | + ), |
| 96 | + }, |
| 97 | + }, |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +func testAccCheckPasswordPolicyDestroy() resource.TestCheckFunc { |
| 102 | + // This check will break if we change the default values for password policy in the API |
| 103 | + return func(s *terraform.State) error { |
| 104 | + client := testAccProvider.Meta().(*Client) |
| 105 | + for _, rs := range s.RootModule().Resources { |
| 106 | + if rs.Type != "sumologic_password_policy" { |
| 107 | + continue |
| 108 | + } |
| 109 | + |
| 110 | + passwordPolicy, err := client.GetPasswordPolicy() |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 113 | + } |
| 114 | + |
| 115 | + if (*passwordPolicy) != DefaultPasswordPolicy { |
| 116 | + return fmt.Errorf("Password policy wasn't reset properly") |
| 117 | + } |
| 118 | + } |
| 119 | + return nil |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func testAccCheckPasswordPolicyExists(name string) resource.TestCheckFunc { |
| 124 | + return func(s *terraform.State) error { |
| 125 | + _, ok := s.RootModule().Resources[name] |
| 126 | + if !ok { |
| 127 | + return fmt.Errorf("Password policy not found: %s", name) |
| 128 | + } |
| 129 | + return nil |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func testPasswordPolicyCheckResourceAttr(resourceName string, passwordPolicy *PasswordPolicy) resource.TestCheckFunc { |
| 134 | + return func(s *terraform.State) error { |
| 135 | + f := resource.ComposeTestCheckFunc( |
| 136 | + resource.TestCheckResourceAttr(resourceName, "min_length", strconv.Itoa(passwordPolicy.MinLength)), |
| 137 | + resource.TestCheckResourceAttr(resourceName, "max_length", strconv.Itoa(passwordPolicy.MaxLength)), |
| 138 | + resource.TestCheckResourceAttr(resourceName, "must_contain_lowercase", strconv.FormatBool(passwordPolicy.MustContainLowercase)), |
| 139 | + resource.TestCheckResourceAttr(resourceName, "must_contain_uppercase", strconv.FormatBool(passwordPolicy.MustContainUppercase)), |
| 140 | + resource.TestCheckResourceAttr(resourceName, "must_contain_digits", strconv.FormatBool(passwordPolicy.MustContainDigits)), |
| 141 | + resource.TestCheckResourceAttr(resourceName, "must_contain_special_chars", strconv.FormatBool(passwordPolicy.MustContainSpecialChars)), |
| 142 | + resource.TestCheckResourceAttr(resourceName, "max_password_age_in_days", strconv.Itoa(passwordPolicy.MaxPasswordAgeInDays)), |
| 143 | + resource.TestCheckResourceAttr(resourceName, "min_unique_passwords", strconv.Itoa(passwordPolicy.MinUniquePasswords)), |
| 144 | + resource.TestCheckResourceAttr(resourceName, "account_lockout_threshold", strconv.Itoa(passwordPolicy.AccountLockoutThreshold)), |
| 145 | + resource.TestCheckResourceAttr(resourceName, "failed_login_reset_duration_in_mins", strconv.Itoa(passwordPolicy.FailedLoginResetDurationInMins)), |
| 146 | + resource.TestCheckResourceAttr(resourceName, "account_lockout_duration_in_mins", strconv.Itoa(passwordPolicy.AccountLockoutDurationInMins)), |
| 147 | + resource.TestCheckResourceAttr(resourceName, "require_mfa", strconv.FormatBool(passwordPolicy.RequireMfa)), |
| 148 | + resource.TestCheckResourceAttr(resourceName, "remember_mfa", strconv.FormatBool(passwordPolicy.RememberMfa)), |
| 149 | + ) |
| 150 | + return f(s) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func newPasswordPolicyConfig(label string, passwordPolicy *PasswordPolicy) string { |
| 155 | + return fmt.Sprintf(` |
| 156 | +resource "sumologic_password_policy" "%s" { |
| 157 | + min_length = %d |
| 158 | + max_length = %d |
| 159 | + must_contain_lowercase = %t |
| 160 | + must_contain_uppercase = %t |
| 161 | + must_contain_digits = %t |
| 162 | + must_contain_special_chars = %t |
| 163 | + max_password_age_in_days = %d |
| 164 | + min_unique_passwords = %d |
| 165 | + account_lockout_threshold = %d |
| 166 | + failed_login_reset_duration_in_mins = %d |
| 167 | + account_lockout_duration_in_mins = %d |
| 168 | + require_mfa = %t |
| 169 | + remember_mfa = %t |
| 170 | +}`, label, |
| 171 | + passwordPolicy.MinLength, |
| 172 | + passwordPolicy.MaxLength, |
| 173 | + passwordPolicy.MustContainLowercase, |
| 174 | + passwordPolicy.MustContainUppercase, |
| 175 | + passwordPolicy.MustContainDigits, |
| 176 | + passwordPolicy.MustContainSpecialChars, |
| 177 | + passwordPolicy.MaxPasswordAgeInDays, |
| 178 | + passwordPolicy.MinUniquePasswords, |
| 179 | + passwordPolicy.AccountLockoutThreshold, |
| 180 | + passwordPolicy.FailedLoginResetDurationInMins, |
| 181 | + passwordPolicy.AccountLockoutDurationInMins, |
| 182 | + passwordPolicy.RequireMfa, |
| 183 | + passwordPolicy.RememberMfa) |
| 184 | +} |
0 commit comments