-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser_resource_test.go
165 lines (150 loc) · 4.6 KB
/
user_resource_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package provider
import (
"os"
"strings"
"testing"
"text/template"
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/terraform-provider-coderd/integration"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/stretchr/testify/require"
)
func TestAccUserResource(t *testing.T) {
t.Parallel()
if os.Getenv("TF_ACC") == "" {
t.Skip("Acceptance tests are disabled.")
}
ctx := t.Context()
client := integration.StartCoder(ctx, t, "user_acc", false)
cfg1 := testAccUserResourceConfig{
URL: client.URL.String(),
Token: client.SessionToken(),
Username: ptr.Ref("example"),
Name: ptr.Ref("Example User"),
Email: ptr.Ref("[email protected]"),
Roles: ptr.Ref([]string{"owner", "auditor"}),
LoginType: ptr.Ref("password"),
Password: ptr.Ref("SomeSecurePassword!"),
}
cfg2 := cfg1
cfg2.Username = ptr.Ref("exampleNew")
cfg3 := cfg2
cfg3.Name = ptr.Ref("Example New")
cfg4 := cfg3
cfg4.LoginType = ptr.Ref("github")
cfg4.Password = nil
resource.Test(t, resource.TestCase{
IsUnitTest: true,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and Read testing
{
Config: cfg1.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.TestCheckTypeSetElemAttr("coderd_user.test", "roles.*", "auditor"),
resource.TestCheckTypeSetElemAttr("coderd_user.test", "roles.*", "owner"),
resource.TestCheckResourceAttr("coderd_user.test", "login_type", "password"),
resource.TestCheckResourceAttr("coderd_user.test", "password", "SomeSecurePassword!"),
resource.TestCheckResourceAttr("coderd_user.test", "suspended", "false"),
),
},
// Import by ID
{
ResourceName: "coderd_user.test",
ImportState: true,
ImportStateVerify: true,
// We can't pull the password from the API.
ImportStateVerifyIgnore: []string{"password"},
},
// ImportState by username
{
ResourceName: "coderd_user.test",
ImportState: true,
ImportStateVerify: true,
ImportStateId: "example",
// We can't pull the password from the API.
ImportStateVerifyIgnore: []string{"password"},
},
// Update and Read testing
{
Config: cfg2.String(t),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("coderd_user.test", "username", "exampleNew"),
resource.TestCheckResourceAttr("coderd_user.test", "name", "Example User"),
),
},
{
Config: cfg3.String(t),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("coderd_user.test", "username", "exampleNew"),
resource.TestCheckResourceAttr("coderd_user.test", "name", "Example New"),
),
},
// Replace triggered
{
Config: cfg4.String(t),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("coderd_user.test", "login_type", "github"),
),
},
// Verify config drift via deletion is handled
{
Config: cfg4.String(t),
Check: func(*terraform.State) error {
user, err := client.User(ctx, "exampleNew")
if err != nil {
return err
}
return client.DeleteUser(ctx, user.ID)
},
// The Plan should be to create the entire resource
ExpectNonEmptyPlan: true,
},
},
})
}
type testAccUserResourceConfig struct {
URL string
Token string
Username *string
Name *string
Email *string
Roles *[]string
LoginType *string
Password *string
Suspended *bool
}
func (c testAccUserResourceConfig) String(t *testing.T) string {
t.Helper()
tpl := `
provider coderd {
url = "{{.URL}}"
token = "{{.Token}}"
}
resource "coderd_user" "test" {
username = {{orNull .Username}}
name = {{orNull .Name}}
email = {{orNull .Email}}
roles = {{orNull .Roles}}
login_type = {{orNull .LoginType}}
password = {{orNull .Password}}
suspended = {{orNull .Suspended}}
}
`
// Define template functions
funcMap := template.FuncMap{
"orNull": PrintOrNull,
}
buf := strings.Builder{}
tmpl, err := template.New("test").Funcs(funcMap).Parse(tpl)
require.NoError(t, err)
err = tmpl.Execute(&buf, c)
require.NoError(t, err)
return buf.String()
}