@@ -28,39 +28,190 @@ make build
2828make install
2929```
3030
31- ### Running Tests
31+ ## Testing
32+
33+ ### Test Types
34+
35+ The provider has two types of tests:
36+
37+ 1 . ** Unit Tests** - Fast, isolated tests that don't require API access
38+ 2 . ** Acceptance Tests** - Integration tests that create real resources in Ory Network
39+
40+ ### Unit Tests
41+
42+ Unit tests can be run without any credentials:
43+
44+ ``` bash
45+ make test # Run all unit tests
46+ make test-short # Run unit tests in short mode
47+ ```
48+
49+ ### Acceptance Tests
3250
3351Acceptance tests are ** self-contained** - they automatically create a temporary Ory project, run tests against it, and clean up when done.
3452
53+ #### Required Environment Variables
54+
3555``` bash
36- # Unit tests (no credentials needed)
37- make test
56+ export ORY_WORKSPACE_API_KEY=" ory_wak_..." # Workspace API key
57+ export ORY_WORKSPACE_ID=" ..." # Workspace ID
58+ ```
3859
39- # Acceptance tests (requires Ory credentials)
40- export ORY_WORKSPACE_API_KEY=" ory_wak_..."
41- export ORY_WORKSPACE_ID=" ..."
60+ #### Running Acceptance Tests
61+
62+ ``` bash
63+ # Standard acceptance tests
4264make test-acc
4365
44- # Acceptance tests with debug logging
66+ # With debug logging
4567make test-acc-verbose
4668
4769# Run all tests with all features enabled
4870make test-acc-all
71+
72+ # Run specific resource tests
73+ ORY_KETO_TESTS_ENABLED=true make test-acc-keto
4974```
5075
5176#### Optional Feature Flags
5277
5378Some tests require specific Ory plan features. Enable them with environment variables:
5479
80+ | Environment Variable | Description |
81+ | ---------------------| -------------|
82+ | ` ORY_KETO_TESTS_ENABLED=true ` | Run relationship/Keto tests |
83+ | ` ORY_B2B_ENABLED=true ` | Run B2B/organization tests (requires B2B plan) |
84+ | ` ORY_SOCIAL_PROVIDER_TESTS_ENABLED=true ` | Run social provider tests |
85+ | ` ORY_SCHEMA_TESTS_ENABLED=true ` | Run identity schema tests |
86+ | ` ORY_PROJECT_TESTS_ENABLED=true ` | Run project creation/deletion tests |
87+
88+ #### API URL Overrides (for local development)
89+
5590``` bash
56- # Run relationship/Keto tests
57- ORY_KETO_TESTS_ENABLED=true make test-acc
91+ export ORY_CONSOLE_API_URL=" https://api.console.ory.sh" # Console API
92+ export ORY_PROJECT_API_URL=" https://%s.projects.oryapis.com" # Project API template
93+ ```
5894
59- # Run B2B/organization tests (requires B2B plan)
60- ORY_B2B_ENABLED=true make test-acc
95+ ### Cleaning Up Dangling Resources
6196
62- # Run social provider tests
63- ORY_SOCIAL_PROVIDER_TESTS_ENABLED=true make test-acc
97+ If tests fail and leave resources behind, use the sweepers to clean up:
98+
99+ ``` bash
100+ # Sweep all test resources
101+ go test -v ./internal/acctest -sweep=all -timeout 30m
102+
103+ # Sweep specific resource types
104+ go test -v ./internal/acctest -sweep=ory_oauth2_client -timeout 30m
105+
106+ # Enable project deletion (use with caution!)
107+ ORY_SWEEP_PROJECTS=true go test -v ./internal/acctest -sweep=ory_project -timeout 30m
108+ ```
109+
110+ Sweepers only delete resources with names starting with ` tf-acc-test ` or ` Test ` .
111+
112+ ### Writing Acceptance Tests
113+
114+ Follow these guidelines when writing acceptance tests:
115+
116+ #### 1. Use the Test Utilities
117+
118+ ``` go
119+ // go:build acceptance
120+
121+ package myresource_test
122+
123+ import (
124+ " testing"
125+ " github.com/hashicorp/terraform-plugin-testing/helper/resource"
126+ " github.com/ory/terraform-provider-orynetwork/internal/acctest"
127+ )
128+
129+ func TestAccMyResource_basic (t *testing .T ) {
130+ // Use acctest.RunTest for consistent test execution
131+ acctest.RunTest (t, resource.TestCase {
132+ PreCheck: func () { acctest.AccPreCheck (t) },
133+ ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories (),
134+ CheckDestroy: acctest.CheckDestroy (" ory_myresource" , myResourceExists),
135+ Steps: []resource.TestStep {
136+ // Create and Read
137+ {
138+ Config: testAccMyResourceConfig (),
139+ Check: resource.ComposeAggregateTestCheckFunc (
140+ resource.TestCheckResourceAttrSet (" ory_myresource.test" , " id" ),
141+ ),
142+ },
143+ // ImportState
144+ {
145+ ResourceName: " ory_myresource.test" ,
146+ ImportState: true ,
147+ ImportStateVerify: true ,
148+ },
149+ },
150+ })
151+ }
152+ ```
153+
154+ #### 2. Implement CheckDestroy
155+
156+ Every acceptance test should verify resources are properly deleted:
157+
158+ ``` go
159+ // Use existing check functions
160+ CheckDestroy: acctest.CheckDestroy (" ory_identity" , acctest.IdentityExists ),
161+ CheckDestroy: acctest.CheckDestroy (" ory_oauth2_client" , acctest.OAuth2ClientExists ),
162+ CheckDestroy: acctest.CheckDestroy (" ory_organization" , acctest.OrganizationExists ),
163+ CheckDestroy: acctest.CheckDestroy (" ory_project" , acctest.ProjectExists ),
164+
165+ // Or implement a custom existence check
166+ func myResourceExists (ctx context .Context , id string ) (bool , error ) {
167+ c , err := acctest.GetOryClient ()
168+ if err != nil {
169+ return false , err
170+ }
171+ _, err = c.GetMyResource (ctx, id)
172+ if err != nil {
173+ if strings.Contains (err.Error (), " 404" ) {
174+ return false , nil
175+ }
176+ return false , err
177+ }
178+ return true , nil
179+ }
180+ ```
181+
182+ #### 3. Test Configuration Best Practices
183+
184+ - Use ` fmt.Sprintf() ` for variable injection in HCL configs
185+ - Prefix test resource names with ` tf-acc-test ` or ` Test ` for sweeper compatibility
186+ - Include the ` provider "ory" {} ` declaration in each config
187+ - Test create, read, update, import, and delete operations
188+
189+ ``` go
190+ func testAccMyResourceConfig (name string ) string {
191+ return fmt.Sprintf (`
192+ provider "ory" {}
193+
194+ resource "ory_myresource" "test" {
195+ name = %[1]q
196+ }
197+ ` , name)
198+ }
199+ ```
200+
201+ #### 4. Feature-Gated Tests
202+
203+ For tests requiring specific Ory plan features:
204+
205+ ``` go
206+ func TestAccOrganizationResource_basic (t *testing .T ) {
207+ acctest.RequireB2BTests (t) // Skips if ORY_B2B_ENABLED != "true"
208+ // ... test implementation
209+ }
210+
211+ func TestAccRelationshipResource_basic (t *testing .T ) {
212+ acctest.RequireKetoTests (t) // Skips if ORY_KETO_TESTS_ENABLED != "true"
213+ // ... test implementation
214+ }
64215```
65216
66217### Using Local Provider
@@ -93,7 +244,18 @@ provider_installation {
932443 . Register the resource in ` internal/provider/provider.go `
942454 . Add documentation in ` docs/resources/ `
952465 . Add examples in ` examples/resources/ `
96- 6 . Write acceptance tests
247+ 6 . Write acceptance tests with CheckDestroy
248+
249+ ### Resource Contribution Checklist
250+
251+ - [ ] Resource implements all CRUD operations
252+ - [ ] Resource supports import via ` ImportState() `
253+ - [ ] Acceptance tests cover create, read, update, import, delete
254+ - [ ] Tests use ` acctest.CheckDestroy() ` to verify cleanup
255+ - [ ] Tests use ` acctest.RunTest() ` for consistent test execution
256+ - [ ] Documentation added to ` docs/resources/ `
257+ - [ ] Examples added to ` examples/resources/ `
258+ - [ ] Code passes ` make lint ` and ` make fmt `
97259
98260### Code Style
99261
@@ -110,7 +272,7 @@ Use clear, descriptive commit messages:
110272Add ory_foo resource for managing foos
111273
112274- Implement CRUD operations
113- - Add acceptance tests
275+ - Add acceptance tests with CheckDestroy
114276- Add documentation
115277```
116278
0 commit comments