Skip to content

Commit

Permalink
Use single regex and add valid resource configs in validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jukie committed Apr 15, 2019
1 parent 0714b28 commit 7345bd8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
36 changes: 30 additions & 6 deletions aws/resource_aws_transfer_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,19 @@ func TestAccAWSTransferUserName_validation(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccAWSTransferUserName_validation("!@#$%^"),
ExpectError: regexp.MustCompile(`"user_name" can only contain alphanumeric characters, underscores, and hyphens`),
ExpectError: regexp.MustCompile(`Invalid "user_name": must be between 3 and 32 alphanumeric or special characters hyphen and underscore. However, "user_name" cannot begin with a hyphen`),
},
{
Config: testAccAWSTransferUserName_validation(acctest.RandString(2)),
ExpectError: regexp.MustCompile(`"user_name" must be at least 3 characters`),
ExpectError: regexp.MustCompile(`Invalid "user_name": must be between 3 and 32 alphanumeric or special characters hyphen and underscore. However, "user_name" cannot begin with a hyphen`),
},
{
Config: testAccAWSTransferUserName_validation(acctest.RandString(33)),
ExpectError: regexp.MustCompile(`"user_name" cannot be more than 32 characters`),
ExpectError: regexp.MustCompile(`Invalid "user_name": must be between 3 and 32 alphanumeric or special characters hyphen and underscore. However, "user_name" cannot begin with a hyphen`),
},
{
Config: testAccAWSTransferUserName_validation("-abcdef"),
ExpectError: regexp.MustCompile(`"user_name" cannot begin with a hyphen`),
ExpectError: regexp.MustCompile(`Invalid "user_name": must be between 3 and 32 alphanumeric or special characters hyphen and underscore. However, "user_name" cannot begin with a hyphen`),
},
{
Config: testAccAWSTransferUserName_validation("valid_username"),
Expand Down Expand Up @@ -293,10 +293,34 @@ resource "aws_transfer_user" "foo" {

func testAccAWSTransferUserName_validation(rName string) string {
return fmt.Sprintf(`
resource "aws_transfer_server" "foo" {
identity_provider_type = "SERVICE_MANAGED"
tags = {
NAME = "tf-acc-test-transfer-server"
}
}
resource "aws_transfer_user" "foo" {
server_id = "s-123456abcdeffffff"
server_id = "${aws_transfer_server.foo.id}"
user_name = "%s"
role = "arn:aws:iam::123456789012:role/foo"
role = "${aws_iam_role.foo.arn}"
}
resource "aws_iam_role" "foo" {
name = "tf-test-transfer-user-iam-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "transfer.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
`, rName)
}
Expand Down
13 changes: 2 additions & 11 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,8 @@ func validateTransferServerID(v interface{}, k string) (ws []string, errors []er
func validateTransferUserName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
// https://docs.aws.amazon.com/transfer/latest/userguide/API_CreateUser.html
if regexp.MustCompile(`[^0-9a-zA-Z_-]`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q can only contain alphanumeric characters, underscores, and hyphens", k))
}
if len(value) < 3 {
errors = append(errors, fmt.Errorf("%q must be at least 3 characters", k))
}
if len(value) > 32 {
errors = append(errors, fmt.Errorf("%q cannot be more than 32 characters", k))
}
if regexp.MustCompile(`^-`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q cannot begin with a hyphen", k))
if !regexp.MustCompile(`^[a-zA-Z0-9_][a-zA-Z0-9_-]{2,31}$`).MatchString(value) {
errors = append(errors, fmt.Errorf("Invalid %q: must be between 3 and 32 alphanumeric or special characters hyphen and underscore. However, %q cannot begin with a hyphen", k, k))
}
return
}
Expand Down

0 comments on commit 7345bd8

Please sign in to comment.