Skip to content

Commit e6eb012

Browse files
committed
chore: add acceptance tests to user data source
1 parent 8416e35 commit e6eb012

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

internal/provider/user_data_source_test.go

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,89 @@
11
package provider
22

3-
/*
43
import (
4+
"context"
55
"html/template"
66
"strings"
77
"testing"
88

9+
"github.com/coder/coder/v2/codersdk"
10+
"github.com/coder/terraform-provider-coderd/integration"
911
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/stretchr/testify/require"
1013
)
1114

1215
func TestAccUserDataSource(t *testing.T) {
16+
ctx := context.Background()
17+
client := integration.StartCoder(ctx, t, "user_acc")
18+
firstUser, err := client.User(ctx, codersdk.Me)
19+
require.NoError(t, err)
20+
user, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
21+
22+
Username: "example",
23+
Password: "SomeSecurePassword!",
24+
UserLoginType: "password",
25+
OrganizationID: firstUser.OrganizationIDs[0],
26+
})
27+
require.NoError(t, err)
28+
_, err = client.UpdateUserRoles(ctx, user.Username, codersdk.UpdateRoles{
29+
Roles: []string{"auditor"},
30+
})
31+
require.NoError(t, err)
32+
_, err = client.UpdateUserProfile(ctx, user.Username, codersdk.UpdateUserProfileRequest{
33+
Username: user.Username,
34+
Name: "Example User",
35+
})
36+
require.NoError(t, err)
37+
cfg := testAccUserDataSourceConfig{
38+
URL: client.URL.String(),
39+
Token: client.SessionToken(),
40+
}
1341
// User by Username
42+
cfg.Username = user.Username
1443
resource.Test(t, resource.TestCase{
1544
PreCheck: func() { testAccPreCheck(t) },
1645
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
1746
Steps: []resource.TestStep{
1847
{
19-
Config: testAccUserDataSourceConfig{
20-
Username: "example",
21-
}.String(t),
48+
Config: cfg.String(t),
2249
Check: resource.ComposeAggregateTestCheckFunc(
23-
resource.TestCheckResourceAttr("coderd_user.test", "username", "example"),
24-
resource.TestCheckResourceAttr("coderd_user.test", "name", "Example User"),
25-
resource.TestCheckResourceAttr("coderd_user.test", "email", "[email protected]"),
26-
resource.TestCheckResourceAttr("coderd_user.test", "roles.#", "2"),
27-
resource.TestCheckResourceAttr("coderd_user.test", "roles.0", "auditor"),
28-
resource.TestCheckResourceAttr("coderd_user.test", "roles.1", "owner"),
29-
resource.TestCheckResourceAttr("coderd_user.test", "login_type", "password"),
30-
resource.TestCheckResourceAttr("coderd_user.test", "password", "SomeSecurePassword!"),
31-
resource.TestCheckResourceAttr("coderd_user.test", "suspended", "false"),
50+
resource.TestCheckResourceAttr("data.coderd_user.test", "username", "example"),
51+
resource.TestCheckResourceAttr("data.coderd_user.test", "name", "Example User"),
52+
resource.TestCheckResourceAttr("data.coderd_user.test", "email", "[email protected]"),
53+
resource.TestCheckResourceAttr("data.coderd_user.test", "roles.#", "1"),
54+
resource.TestCheckResourceAttr("data.coderd_user.test", "roles.0", "auditor"),
55+
resource.TestCheckResourceAttr("data.coderd_user.test", "login_type", "password"),
56+
resource.TestCheckResourceAttr("data.coderd_user.test", "suspended", "false"),
3257
),
3358
},
3459
},
3560
})
61+
cfg.Username = ""
62+
cfg.ID = user.ID.String()
3663
resource.Test(t, resource.TestCase{
3764
PreCheck: func() { testAccPreCheck(t) },
3865
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
3966
// User by ID
4067
Steps: []resource.TestStep{
4168
{
42-
Config: testAccUserDataSourceConfig{
43-
ID: "example",
44-
}.String(t),
69+
Config: cfg.String(t),
4570
Check: resource.ComposeAggregateTestCheckFunc(
46-
resource.TestCheckResourceAttr("coderd_user.test", "username", "example"),
47-
resource.TestCheckResourceAttr("coderd_user.test", "name", "Example User"),
48-
resource.TestCheckResourceAttr("coderd_user.test", "email", "[email protected]"),
49-
resource.TestCheckResourceAttr("coderd_user.test", "roles.#", "2"),
50-
resource.TestCheckResourceAttr("coderd_user.test", "roles.0", "auditor"),
51-
resource.TestCheckResourceAttr("coderd_user.test", "roles.1", "owner"),
52-
resource.TestCheckResourceAttr("coderd_user.test", "login_type", "password"),
53-
resource.TestCheckResourceAttr("coderd_user.test", "password", "SomeSecurePassword!"),
54-
resource.TestCheckResourceAttr("coderd_user.test", "suspended", "false"),
71+
resource.TestCheckResourceAttr("data.coderd_user.test", "username", "example"),
72+
resource.TestCheckResourceAttr("data.coderd_user.test", "name", "Example User"),
73+
resource.TestCheckResourceAttr("data.coderd_user.test", "email", "[email protected]"),
74+
resource.TestCheckResourceAttr("data.coderd_user.test", "roles.#", "1"),
75+
resource.TestCheckResourceAttr("data.coderd_user.test", "roles.0", "auditor"),
76+
resource.TestCheckResourceAttr("data.coderd_user.test", "login_type", "password"),
77+
resource.TestCheckResourceAttr("data.coderd_user.test", "suspended", "false"),
5578
),
5679
},
5780
},
5881
})
5982
}
6083

6184
type testAccUserDataSourceConfig struct {
62-
URL string
63-
Token string
85+
URL string
86+
Token string
6487

6588
ID string
6689
Username string
@@ -92,4 +115,3 @@ data "coderd_user" "test" {
92115

93116
return buf.String()
94117
}
95-
*/

0 commit comments

Comments
 (0)