-
Notifications
You must be signed in to change notification settings - Fork 3
chore: add acceptance tests to user data source #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e6eb012
chore: add acceptance tests to user data source
ethanndickson 3485170
error test
ethanndickson bd799f3
review
ethanndickson 0f716ed
fixup
ethanndickson eece858
gen + fmt
ethanndickson f149934
add skip
ethanndickson c524e54
validate retrieved id/username against provided
ethanndickson 034e1c2
fixup
ethanndickson b697236
fixup2
ethanndickson 8832347
Merge branch 'main' into ethan/user-data-acc
ethanndickson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,119 @@ | ||
package provider | ||
|
||
/* | ||
import ( | ||
"context" | ||
"html/template" | ||
"os" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/terraform-provider-coderd/integration" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAccUserDataSource(t *testing.T) { | ||
// User by Username | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccUserDataSourceConfig{ | ||
Username: "example", | ||
}.String(t), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("coderd_user.test", "username", "example"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "name", "Example User"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "email", "[email protected]"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "roles.#", "2"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "roles.0", "auditor"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "roles.1", "owner"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "login_type", "password"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "password", "SomeSecurePassword!"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "suspended", "false"), | ||
), | ||
if os.Getenv("TF_ACC") == "" { | ||
t.Skip("Acceptance tests are disabled.") | ||
} | ||
ctx := context.Background() | ||
client := integration.StartCoder(ctx, t, "user_data_acc") | ||
firstUser, err := client.User(ctx, codersdk.Me) | ||
require.NoError(t, err) | ||
user, err := client.CreateUser(ctx, codersdk.CreateUserRequest{ | ||
Email: "[email protected]", | ||
Username: "example", | ||
Password: "SomeSecurePassword!", | ||
UserLoginType: "password", | ||
OrganizationID: firstUser.OrganizationIDs[0], | ||
}) | ||
require.NoError(t, err) | ||
_, err = client.UpdateUserRoles(ctx, user.Username, codersdk.UpdateRoles{ | ||
Roles: []string{"auditor"}, | ||
}) | ||
require.NoError(t, err) | ||
_, err = client.UpdateUserProfile(ctx, user.Username, codersdk.UpdateUserProfileRequest{ | ||
Username: user.Username, | ||
Name: "Example User", | ||
}) | ||
require.NoError(t, err) | ||
t.Run("UserByUsername", func(t *testing.T) { | ||
cfg := testAccUserDataSourceConfig{ | ||
URL: client.URL.String(), | ||
Token: client.SessionToken(), | ||
Username: user.Username, | ||
} | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: cfg.String(t), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "username", "example"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "name", "Example User"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "email", "[email protected]"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "roles.#", "1"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "roles.0", "auditor"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "login_type", "password"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "suspended", "false"), | ||
), | ||
}, | ||
}, | ||
}, | ||
}) | ||
}) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
// User by ID | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccUserDataSourceConfig{ | ||
ID: "example", | ||
}.String(t), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("coderd_user.test", "username", "example"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "name", "Example User"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "email", "[email protected]"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "roles.#", "2"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "roles.0", "auditor"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "roles.1", "owner"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "login_type", "password"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "password", "SomeSecurePassword!"), | ||
resource.TestCheckResourceAttr("coderd_user.test", "suspended", "false"), | ||
), | ||
|
||
t.Run("UserByID", func(t *testing.T) { | ||
cfg := testAccUserDataSourceConfig{ | ||
URL: client.URL.String(), | ||
Token: client.SessionToken(), | ||
Username: user.ID.String(), | ||
ethanndickson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
// User by ID | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: cfg.String(t), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "username", "example"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "name", "Example User"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "email", "[email protected]"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "roles.#", "1"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "roles.0", "auditor"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "login_type", "password"), | ||
resource.TestCheckResourceAttr("data.coderd_user.test", "suspended", "false"), | ||
), | ||
}, | ||
}, | ||
}) | ||
}) | ||
t.Run("NeitherIDNorUsername", func(t *testing.T) { | ||
cfg := testAccUserDataSourceConfig{ | ||
URL: client.URL.String(), | ||
Token: client.SessionToken(), | ||
} | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
// Neither ID nor Username | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: cfg.String(t), | ||
ExpectError: regexp.MustCompile(`At least one of these attributes must be configured: \[id,username\]`), | ||
}, | ||
}, | ||
}, | ||
}) | ||
}) | ||
|
||
} | ||
|
||
type testAccUserDataSourceConfig struct { | ||
URL string | ||
Token string | ||
URL string | ||
Token string | ||
|
||
ID string | ||
Username string | ||
|
@@ -92,4 +145,3 @@ data "coderd_user" "test" { | |
|
||
return buf.String() | ||
} | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what
resource.Test
does when it's run, so we should skip the setup if there's nothing to run.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can write all these tests a bit nicer as several steps inside a
resource.TestCase
, but we're not really doing seperate steps, so subtests are fine.