|
4 | 4 | "errors"
|
5 | 5 | "fmt"
|
6 | 6 | "log"
|
7 |
| - "os" |
| 7 | + "regexp" |
8 | 8 | "testing"
|
9 | 9 |
|
10 | 10 | "github.com/aws/aws-sdk-go/aws"
|
@@ -104,49 +104,38 @@ func TestAccAWSCognitoUserPoolDomain_basic(t *testing.T) {
|
104 | 104 | }
|
105 | 105 |
|
106 | 106 | func TestAccAWSCognitoUserPoolDomain_custom(t *testing.T) {
|
| 107 | + rootDomain := testAccAwsAcmCertificateDomainFromEnv(t) |
| 108 | + domain := testAccAwsAcmCertificateRandomSubDomain(rootDomain) |
107 | 109 | poolName := fmt.Sprintf("tf-acc-test-pool-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
|
108 |
| - // This test must always run in us-east-1 |
109 |
| - // BadRequestException: Invalid certificate ARN: arn:aws:acm:us-west-2:123456789012:certificate/xxxxx. Certificate must be in 'us-east-1'. |
110 |
| - oldvar := os.Getenv("AWS_DEFAULT_REGION") |
111 |
| - os.Setenv("AWS_DEFAULT_REGION", "us-east-1") |
112 |
| - defer os.Setenv("AWS_DEFAULT_REGION", oldvar) |
113 |
| - |
114 |
| - customDomainName := os.Getenv("AWS_COGNITO_USER_POOL_DOMAIN_ROOT_DOMAIN") |
115 |
| - if customDomainName == "" { |
116 |
| - t.Skip( |
117 |
| - "Environment variable AWS_COGNITO_USER_POOL_DOMAIN_ROOT_DOMAIN is not set. " + |
118 |
| - "This environment variable must be set to the fqdn of " + |
119 |
| - "an ISSUED ACM certificate in us-east-1 to enable this test.") |
120 |
| - } |
121 | 110 |
|
122 |
| - customSubDomainName := fmt.Sprintf("%s.%s", fmt.Sprintf("tf-acc-test-domain-%d", acctest.RandInt()), customDomainName) |
123 |
| - // For now, use an environment variable to limit running this test |
124 |
| - certificateArn := os.Getenv("AWS_COGNITO_USER_POOL_DOMAIN_CERTIFICATE_ARN") |
125 |
| - if certificateArn == "" { |
126 |
| - t.Skip( |
127 |
| - "Environment variable AWS_COGNITO_USER_POOL_DOMAIN_CERTIFICATE_ARN is not set. " + |
128 |
| - "This environment variable must be set to the ARN of " + |
129 |
| - "an ISSUED ACM certificate in us-east-1 to enable this test.") |
130 |
| - } |
| 111 | + acmCertificateResourceName := "aws_acm_certificate.test" |
| 112 | + cognitoUserPoolResourceName := "aws_cognito_user_pool.test" |
| 113 | + resourceName := "aws_cognito_user_pool_domain.test" |
131 | 114 |
|
132 | 115 | resource.ParallelTest(t, resource.TestCase{
|
133 |
| - PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCognitoIdentityProvider(t) }, |
134 |
| - Providers: testAccProviders, |
135 |
| - CheckDestroy: testAccCheckAWSCognitoUserPoolDomainDestroy, |
| 116 | + PreCheck: func() { testAccPreCheck(t); testAccPreCheckCognitoUserPoolCustomDomain(t) }, |
| 117 | + ProviderFactories: testAccProviderFactories, |
| 118 | + CheckDestroy: testAccCheckAWSCognitoUserPoolDomainDestroy, |
136 | 119 | Steps: []resource.TestStep{
|
137 | 120 | {
|
138 |
| - Config: testAccAWSCognitoUserPoolDomainConfig_custom(customSubDomainName, poolName, certificateArn), |
| 121 | + Config: testAccAWSCognitoUserPoolDomainConfig_custom(rootDomain, domain, poolName), |
139 | 122 | Check: resource.ComposeAggregateTestCheckFunc(
|
140 |
| - testAccCheckAWSCognitoUserPoolDomainExists("aws_cognito_user_pool_domain.main"), |
141 |
| - resource.TestCheckResourceAttr("aws_cognito_user_pool_domain.main", "domain", customSubDomainName), |
142 |
| - resource.TestCheckResourceAttr("aws_cognito_user_pool_domain.main", "certificate_arn", certificateArn), |
143 |
| - resource.TestCheckResourceAttr("aws_cognito_user_pool.main", "name", poolName), |
144 |
| - resource.TestCheckResourceAttrSet("aws_cognito_user_pool_domain.main", "aws_account_id"), |
145 |
| - resource.TestCheckResourceAttrSet("aws_cognito_user_pool_domain.main", "cloudfront_distribution_arn"), |
146 |
| - resource.TestCheckResourceAttrSet("aws_cognito_user_pool_domain.main", "s3_bucket"), |
147 |
| - resource.TestCheckResourceAttrSet("aws_cognito_user_pool_domain.main", "version"), |
| 123 | + testAccCheckAWSCognitoUserPoolDomainExists(resourceName), |
| 124 | + testAccCheckResourceAttrAccountID(resourceName, "aws_account_id"), |
| 125 | + resource.TestCheckResourceAttrPair(resourceName, "certificate_arn", acmCertificateResourceName, "arn"), |
| 126 | + //lintignore:AWSAT001 // Reference: https://github.com/hashicorp/terraform-provider-aws/issues/11666 |
| 127 | + resource.TestMatchResourceAttr(resourceName, "cloudfront_distribution_arn", regexp.MustCompile(`[a-z0-9]+.cloudfront.net$`)), |
| 128 | + resource.TestCheckResourceAttrPair(resourceName, "domain", acmCertificateResourceName, "domain_name"), |
| 129 | + resource.TestMatchResourceAttr(resourceName, "s3_bucket", regexp.MustCompile(`^.+$`)), |
| 130 | + resource.TestCheckResourceAttrPair(resourceName, "user_pool_id", cognitoUserPoolResourceName, "id"), |
| 131 | + resource.TestMatchResourceAttr(resourceName, "version", regexp.MustCompile(`^.+$`)), |
148 | 132 | ),
|
149 | 133 | },
|
| 134 | + { |
| 135 | + ResourceName: resourceName, |
| 136 | + ImportState: true, |
| 137 | + ImportStateVerify: true, |
| 138 | + }, |
150 | 139 | },
|
151 | 140 | })
|
152 | 141 | }
|
@@ -230,16 +219,63 @@ resource "aws_cognito_user_pool" "main" {
|
230 | 219 | `, domainName, poolName)
|
231 | 220 | }
|
232 | 221 |
|
233 |
| -func testAccAWSCognitoUserPoolDomainConfig_custom(customSubDomainName, poolName, certificateArn string) string { |
234 |
| - return fmt.Sprintf(` |
235 |
| -resource "aws_cognito_user_pool_domain" "main" { |
236 |
| - domain = "%s" |
237 |
| - user_pool_id = aws_cognito_user_pool.main.id |
238 |
| - certificate_arn = "%s" |
| 222 | +func testAccAWSCognitoUserPoolDomainConfig_custom(rootDomain string, domain string, poolName string) string { |
| 223 | + return composeConfig( |
| 224 | + testAccCognitoUserPoolCustomDomainRegionProviderConfig(), |
| 225 | + fmt.Sprintf(` |
| 226 | +data "aws_route53_zone" "test" { |
| 227 | + name = %[1]q |
| 228 | + private_zone = false |
239 | 229 | }
|
240 | 230 |
|
241 |
| -resource "aws_cognito_user_pool" "main" { |
242 |
| - name = "%s" |
| 231 | +resource "aws_acm_certificate" "test" { |
| 232 | + domain_name = %[2]q |
| 233 | + validation_method = "DNS" |
| 234 | +} |
| 235 | +
|
| 236 | +# |
| 237 | +# for_each acceptance testing requires: |
| 238 | +# https://github.com/hashicorp/terraform-plugin-sdk/issues/536 |
| 239 | +# |
| 240 | +# resource "aws_route53_record" "test" { |
| 241 | +# for_each = { |
| 242 | +# for dvo in aws_acm_certificate.test.domain_validation_options: dvo.domain_name => { |
| 243 | +# name = dvo.resource_record_name |
| 244 | +# record = dvo.resource_record_value |
| 245 | +# type = dvo.resource_record_type |
| 246 | +# } |
| 247 | +# } |
| 248 | +
|
| 249 | +# allow_overwrite = true |
| 250 | +# name = each.value.name |
| 251 | +# records = [each.value.record] |
| 252 | +# ttl = 60 |
| 253 | +# type = each.value.type |
| 254 | +# zone_id = data.aws_route53_zone.test.zone_id |
| 255 | +# } |
| 256 | +
|
| 257 | +resource "aws_route53_record" "test" { |
| 258 | + allow_overwrite = true |
| 259 | + name = tolist(aws_acm_certificate.test.domain_validation_options)[0].resource_record_name |
| 260 | + records = [tolist(aws_acm_certificate.test.domain_validation_options)[0].resource_record_value] |
| 261 | + ttl = 60 |
| 262 | + type = tolist(aws_acm_certificate.test.domain_validation_options)[0].resource_record_type |
| 263 | + zone_id = data.aws_route53_zone.test.zone_id |
| 264 | +} |
| 265 | +
|
| 266 | +resource "aws_acm_certificate_validation" "test" { |
| 267 | + certificate_arn = aws_acm_certificate.test.arn |
| 268 | + validation_record_fqdns = [aws_route53_record.test.fqdn] |
| 269 | +} |
| 270 | +
|
| 271 | +resource "aws_cognito_user_pool" "test" { |
| 272 | + name = %[3]q |
| 273 | +} |
| 274 | +
|
| 275 | +resource "aws_cognito_user_pool_domain" "test" { |
| 276 | + certificate_arn = aws_acm_certificate_validation.test.certificate_arn |
| 277 | + domain = aws_acm_certificate.test.domain_name |
| 278 | + user_pool_id = aws_cognito_user_pool.test.id |
243 | 279 | }
|
244 |
| -`, customSubDomainName, certificateArn, poolName) |
| 280 | +`, rootDomain, domain, poolName)) |
245 | 281 | }
|
0 commit comments