@@ -10,6 +10,7 @@ import (
1010
1111"github.com/coder/terraform-provider-coderd/integration"
1212"github.com/hashicorp/terraform-plugin-testing/helper/resource"
13+ "github.com/hashicorp/terraform-plugin-testing/terraform"
1314"github.com/stretchr/testify/require"
1415)
1516
@@ -35,6 +36,10 @@ func TestAccUserResource(t *testing.T) {
3536cfg2 .Username = PtrTo ("exampleNew" )
3637cfg2 .Name = PtrTo ("Example User New" )
3738
39+ cfg3 := cfg1
40+ cfg3 .Email = PtrTo ("example2@coder.com" )
41+
42+ var userId string
3843resource .Test (t , resource.TestCase {
3944PreCheck :func () {testAccPreCheck (t ) },
4045ProtoV6ProviderFactories :testAccProtoV6ProviderFactories ,
@@ -68,6 +73,15 @@ func TestAccUserResource(t *testing.T) {
6873Check :resource .ComposeAggregateTestCheckFunc (
6974resource .TestCheckResourceAttr ("coderd_user.test" ,"username" ,"exampleNew" ),
7075resource .TestCheckResourceAttr ("coderd_user.test" ,"name" ,"Example User New" ),
76+ testAccIdChanged ("coderd_user.test" ,& userId ),
77+ ),
78+ },
79+ // Replace testing
80+ {
81+ Config :cfg3 .String (t ),
82+ Check :resource .ComposeAggregateTestCheckFunc (
83+ resource .TestCheckResourceAttr ("coderd_user.test" ,"email" ,"example2@coder.com" ),
84+ testAccIdChanged ("coderd_user.test" ,& userId ),
7185),
7286},
7387// Delete testing automatically occurs in TestCase
@@ -151,3 +165,24 @@ resource "coderd_user" "test" {
151165
152166return buf .String ()
153167}
168+
169+ // Check if the id has changed since the last time this check function was run.
170+ func testAccIdChanged (resourceName string ,id * string ) resource.TestCheckFunc {
171+ return func (s * terraform.State )error {
172+ rs ,ok := s .RootModule ().Resources [resourceName ]
173+ if ! ok {
174+ return fmt .Errorf ("Resource %s not found" ,resourceName )
175+ }
176+ if rs .Primary .ID == "" {
177+ return fmt .Errorf ("No ID is set" )
178+ }
179+ if * id == "" {
180+ * id = rs .Primary .ID
181+ return nil
182+ }
183+ if rs .Primary .ID == * id {
184+ return fmt .Errorf ("ID did not change from %s" ,rs .Primary .ID )
185+ }
186+ return nil
187+ }
188+ }