-
Notifications
You must be signed in to change notification settings - Fork 65
Add password generation test #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
171fa70
Add test for password generation
JillRegan 0cb6a54
Move heleprs to a validate package
JillRegan 979a473
Fix merge conflicts
JillRegan 237ccd7
Remove config maps
JillRegan 49fa008
Remvoe extra check
JillRegan 71264af
Create new uuid and checks package
JillRegan bb48703
Fix syntax error
JillRegan 89da259
Add error handeling for uuid checks
JillRegan 973d71e
Update checks array
JillRegan c49cc60
Update checks step for readability
JillRegan fbf2df0
Adjust variable name
JillRegan 02a9267
Refactor test cases
JillRegan b8de34c
Update comment
JillRegan 586727f
Update password recipe struct
JillRegan 9eb1d17
Update BuildPasswordRecipeChecks to use PasswordRecipe struct
JillRegan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package checks | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| // BuildItemChecks creates a list of test assertions to verify item attributes | ||
| func BuildItemChecks(resourceName string, attrs map[string]any) []resource.TestCheckFunc { | ||
| checks := []resource.TestCheckFunc{ | ||
| resource.TestCheckResourceAttrSet(resourceName, "uuid"), | ||
| resource.TestCheckResourceAttrSet(resourceName, "id"), | ||
| } | ||
|
|
||
| for attr, expectedValue := range attrs { | ||
| // Check if the value is a slice and iterate over it | ||
| if slice, ok := expectedValue.([]string); ok { | ||
| checks = append(checks, resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("%s.#", attr), fmt.Sprintf("%d", len(slice)))) | ||
|
|
||
| for i, val := range slice { | ||
| checks = append(checks, resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("%s.%d", attr, i), val)) | ||
| } | ||
| } else { | ||
| checks = append(checks, resource.TestCheckResourceAttr(resourceName, attr, fmt.Sprintf("%v", expectedValue))) | ||
| } | ||
| } | ||
|
|
||
| return checks | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package checks | ||
volodymyrZotov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-testing/terraform" | ||
| ) | ||
|
|
||
| // BuildPasswordRecipeChecks creates a list of test assertions to verify password recipe attributes | ||
| func BuildPasswordRecipeChecks(resourceName string, recipe map[string]any) []resource.TestCheckFunc { | ||
| checks := []resource.TestCheckFunc{ | ||
| resource.TestCheckResourceAttr(resourceName, "password_recipe.#", "1"), | ||
| } | ||
|
|
||
| length, _ := recipe["length"].(int) | ||
| symbols, _ := recipe["symbols"].(bool) | ||
| digits, _ := recipe["digits"].(bool) | ||
| letters, _ := recipe["letters"].(bool) | ||
|
|
||
| if length > 0 { | ||
| checks = append(checks, checkPasswordPattern(resourceName, fmt.Sprintf("^.{%d}$", length), "length")) | ||
| } | ||
|
|
||
| if symbols { | ||
| checks = append(checks, checkPasswordPattern(resourceName, `[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~`+"`"+`]`, "symbols")) | ||
| } else { | ||
| checks = append(checks, checkPasswordPattern(resourceName, `^[^!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~\`+"`"+`]+$`, "symbols")) | ||
| } | ||
|
|
||
| if digits { | ||
| checks = append(checks, checkPasswordPattern(resourceName, `[0-9]`, "digits")) | ||
| } else { | ||
| checks = append(checks, checkPasswordPattern(resourceName, `^[^0-9]+$`, "digits")) | ||
| } | ||
|
|
||
| if letters { | ||
| checks = append(checks, checkPasswordPattern(resourceName, `[a-zA-Z]`, "letters")) | ||
| } else { | ||
| checks = append(checks, checkPasswordPattern(resourceName, `^[^a-zA-Z]+$`, "letters")) | ||
| } | ||
|
|
||
| return checks | ||
| } | ||
|
|
||
| // checkPasswordPattern creates a test assertion to verify password pattern with regex | ||
| func checkPasswordPattern(resourceName, pattern, description string) resource.TestCheckFunc { | ||
| return func(s *terraform.State) error { | ||
| rs, ok := s.RootModule().Resources[resourceName] | ||
| if !ok { | ||
| return fmt.Errorf("resource not found: %s", resourceName) | ||
| } | ||
|
|
||
| password := rs.Primary.Attributes["password"] | ||
| matched, _ := regexp.MatchString(pattern, password) | ||
|
|
||
| if !matched { | ||
| return fmt.Errorf("password does not match expected pattern: %s", description) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package uuid | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-testing/terraform" | ||
| ) | ||
|
|
||
| // CaptureItemUUID captures the UUID of a resource item | ||
| func CaptureItemUUID(t *testing.T, resourceName string, uuidPtr *string) resource.TestCheckFunc { | ||
| return func(s *terraform.State) error { | ||
| rs, ok := s.RootModule().Resources[resourceName] | ||
|
|
||
| if !ok { | ||
| return fmt.Errorf("resource not found: %s", resourceName) | ||
| } | ||
|
|
||
| *uuidPtr = rs.Primary.Attributes["uuid"] | ||
|
|
||
| if *uuidPtr == "" { | ||
| return fmt.Errorf("UUID is empty") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // VerifyItemUUIDUnchanged verifies that the resource UUID matches the expected UUID | ||
| func VerifyItemUUIDUnchanged(t *testing.T, resourceName string, expectedUUID *string) resource.TestCheckFunc { | ||
| return func(s *terraform.State) error { | ||
| rs, ok := s.RootModule().Resources[resourceName] | ||
|
|
||
| if !ok { | ||
| return fmt.Errorf("resource not found: %s", resourceName) | ||
| } | ||
|
|
||
| currentUUID := rs.Primary.Attributes["uuid"] | ||
|
|
||
| if currentUUID == "" { | ||
| return fmt.Errorf("UUID is empty") | ||
| } | ||
|
|
||
| if currentUUID != *expectedUUID { | ||
| return fmt.Errorf("UUID changed from %s to %s - resource was replaced instead of updated", *expectedUUID, currentUUID) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.