-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_test.go
More file actions
121 lines (108 loc) · 4.26 KB
/
resource_test.go
File metadata and controls
121 lines (108 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//go:build acceptance
package customdomain_test
import (
"context"
"fmt"
"os"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/ory/terraform-provider-ory/internal/acctest"
)
func importStateCustomDomainID(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources["ory_custom_domain.test"]
if !ok {
return "", fmt.Errorf("resource not found: ory_custom_domain.test")
}
projectID := rs.Primary.Attributes["project_id"]
domainID := rs.Primary.ID
return fmt.Sprintf("%s/%s", projectID, domainID), nil
}
// cleanupStaleCustomDomains removes any existing custom domains from the test
// project. This prevents "Duplicate custom hostname found (1406)" errors when a
// previous test run failed before its destroy step could clean up.
func cleanupStaleCustomDomains(t *testing.T) {
t.Helper()
oryClient, err := acctest.GetOryClient()
if err != nil {
t.Fatalf("failed to create Ory client for custom domain cleanup: %s", err)
}
projectID := acctest.GetTestProjectID(t)
ctx := context.Background()
domains, err := oryClient.ListCustomDomains(ctx, projectID)
if err != nil {
t.Logf("warning: could not list custom domains for cleanup: %s", err)
return
}
for _, d := range domains {
t.Logf("cleaning up stale custom domain: id=%s", d.GetId())
if err := oryClient.DeleteCustomDomain(ctx, projectID, d.GetId()); err != nil {
t.Fatalf("failed to delete stale custom domain %s: %s", d.GetId(), err)
}
}
}
func testAccPreCheckCustomDomain(t *testing.T) {
acctest.AccPreCheck(t)
if os.Getenv("ORY_CUSTOM_DOMAIN_HOSTNAME") == "" {
t.Skip("ORY_CUSTOM_DOMAIN_HOSTNAME must be set for custom domain tests (e.g., test.example-e2e.orycname.dev)")
}
cleanupStaleCustomDomains(t)
}
// TestAccCustomDomainResource_basic tests the full CRUD lifecycle of a custom domain.
//
// Required environment variables:
//
// ORY_CUSTOM_DOMAIN_HOSTNAME - Hostname to use (e.g., test.example-e2e.orycname.dev)
//
// Optional environment variables:
//
// ORY_CUSTOM_DOMAIN_COOKIE_DOMAIN - Cookie domain (default: example.com)
func TestAccCustomDomainResource_basic(t *testing.T) {
hostname := os.Getenv("ORY_CUSTOM_DOMAIN_HOSTNAME")
cookieDomain := os.Getenv("ORY_CUSTOM_DOMAIN_COOKIE_DOMAIN")
if cookieDomain == "" {
cookieDomain = "example.com"
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCustomDomain(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
// Create and Read
{
Config: acctest.LoadTestConfig(t, "testdata/basic.tf.tmpl", map[string]string{
"Hostname": hostname,
"CookieDomain": cookieDomain,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("ory_custom_domain.test", "id"),
resource.TestCheckResourceAttr("ory_custom_domain.test", "hostname", hostname),
resource.TestCheckResourceAttr("ory_custom_domain.test", "cookie_domain", cookieDomain),
resource.TestCheckResourceAttrSet("ory_custom_domain.test", "verification_status"),
resource.TestCheckResourceAttrSet("ory_custom_domain.test", "created_at"),
resource.TestCheckResourceAttrSet("ory_custom_domain.test", "updated_at"),
),
},
// Update - add CORS and custom UI base URL
{
Config: acctest.LoadTestConfig(t, "testdata/updated.tf.tmpl", map[string]string{
"Hostname": hostname,
"CookieDomain": cookieDomain,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("ory_custom_domain.test", "hostname", hostname),
resource.TestCheckResourceAttr("ory_custom_domain.test", "cors_enabled", "true"),
resource.TestCheckResourceAttr("ory_custom_domain.test", "cors_allowed_origins.0", "https://app.example.com"),
resource.TestCheckResourceAttr("ory_custom_domain.test", "custom_ui_base_url", "https://"+hostname),
),
},
// ImportState
{
ResourceName: "ory_custom_domain.test",
ImportState: true,
ImportStateIdFunc: importStateCustomDomainID,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"created_at", "updated_at", "verification_status", "verification_errors", "ssl_status"},
},
},
})
}