Skip to content

Commit

Permalink
test: added a couple more tests to test access list edits, email edit…
Browse files Browse the repository at this point in the history
…s, etc

Signed-off-by: Michal Wasilewski <[email protected]>
  • Loading branch information
mwasilew2 committed Oct 20, 2023
1 parent 04c422a commit 3ce19eb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
8 changes: 8 additions & 0 deletions spacelift/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ func resourceUserRead(ctx context.Context, d *schema.ResourceData, i interface{}
}

func resourceUserUpdate(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
// input validation
if d.HasChange("invitation_email") {
return diag.Errorf("invitation_email cannot be changed")
}
if d.HasChange("username") {
return diag.Errorf("username cannot be changed")
}

var ret diag.Diagnostics

// send an update query to the API
Expand Down
53 changes: 51 additions & 2 deletions spacelift/resource_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package spacelift

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand Down Expand Up @@ -62,8 +63,7 @@ func TestUserResource(t *testing.T) {
})
})

// Note: the api doesn't allow for the username or email to be updated
t.Run("can remove one access", func(t *testing.T) {
t.Run("can edit access list", func(t *testing.T) {
randomUsername := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
exampleEmail := fmt.Sprintf("%[email protected]", randomUsername)

Expand All @@ -90,4 +90,53 @@ func TestUserResource(t *testing.T) {

})

t.Run("cannot change email address", func(t *testing.T) {
randomUsername := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
exampleEmail := fmt.Sprintf("%[email protected]", randomUsername)

randomUsername2 := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
exampleEmail2 := fmt.Sprintf("%[email protected]", randomUsername2)

testSteps(t, []resource.TestStep{
{
Config: fmt.Sprintf(userWithOneAccess, exampleEmail, randomUsername),
Check: Resource(
resourceName,
Attribute("invitation_email", Equals(exampleEmail)),
Attribute("username", Equals(randomUsername)),
SetContains("policy", "root", "ADMIN"),
SetDoesNotContain("policy", "legacy"),
),
},
{
Config: fmt.Sprintf(userWithOneAccess, exampleEmail2, randomUsername),
ExpectError: regexp.MustCompile(`invitation_email cannot be changed`),
},
})
})

t.Run("cannot change username", func(t *testing.T) {
randomUsername := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
exampleEmail := fmt.Sprintf("%[email protected]", randomUsername)

randomUsername2 := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

testSteps(t, []resource.TestStep{
{
Config: fmt.Sprintf(userWithOneAccess, exampleEmail, randomUsername),
Check: Resource(
resourceName,
Attribute("invitation_email", Equals(exampleEmail)),
Attribute("username", Equals(randomUsername)),
SetContains("policy", "root", "ADMIN"),
SetDoesNotContain("policy", "legacy"),
),
},
{
Config: fmt.Sprintf(userWithOneAccess, exampleEmail, randomUsername2),
ExpectError: regexp.MustCompile(`username cannot be changed`),
},
})
})

}

0 comments on commit 3ce19eb

Please sign in to comment.