- Notifications
You must be signed in to change notification settings - Fork4
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
e6eb0123485170bd799f30f716edeece858f149934c524e54034e1c2b6972368832347File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||
| if os.Getenv("TF_ACC") == "" { | ||
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This is what MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 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: "example@coder.com", | ||
| 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", "example@coder.com"), | ||
| 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("UserByID", func(t *testing.T) { | ||
| cfg := testAccUserDataSourceConfig{ | ||
| URL: client.URL.String(), | ||
| Token: client.SessionToken(), | ||
| ID: user.ID.String(), | ||
| } | ||
| 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", "example@coder.com"), | ||
| 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 | ||
| ID string | ||
| Username string | ||
| @@ -92,4 +145,3 @@ data "coderd_user" "test" { | ||
| return buf.String() | ||
| } | ||