-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add acceptance tests to user data source (#23)
- Loading branch information
1 parent
ee5c4e5
commit 375a205
Showing
5 changed files
with
115 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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 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 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 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(), | ||
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", "[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 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