Skip to content

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 10 commits into from
Jul 16, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 65 additions & 28 deletions internal/provider/user_data_source_test.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,104 @@
package provider

/*
import (
"context"
"html/template"
"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) {
ctx := context.Background()
client := integration.StartCoder(ctx, t, "user_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)
cfg := testAccUserDataSourceConfig{
URL: client.URL.String(),
Token: client.SessionToken(),
}
// User by Username
cfg.Username = user.Username
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccUserDataSourceConfig{
Username: "example",
}.String(t),
Config: cfg.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"),
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"),
),
},
},
})
cfg.Username = ""
cfg.ID = user.ID.String()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
// User by ID
Steps: []resource.TestStep{
{
Config: testAccUserDataSourceConfig{
ID: "example",
}.String(t),
Config: cfg.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"),
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"),
),
},
},
})
cfg.ID = ""
resource.Test(t, resource.TestCase{
IsUnitTest: true,
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
Expand Down Expand Up @@ -92,4 +130,3 @@ data "coderd_user" "test" {

return buf.String()
}
*/
Loading