|
1 | 1 | package provider
|
2 | 2 |
|
3 |
| -/* |
4 | 3 | import (
|
| 4 | + "context" |
5 | 5 | "html/template"
|
| 6 | + "os" |
| 7 | + "regexp" |
6 | 8 | "strings"
|
7 | 9 | "testing"
|
8 | 10 |
|
| 11 | + "github.com/coder/coder/v2/codersdk" |
| 12 | + "github.com/coder/terraform-provider-coderd/integration" |
9 | 13 | "github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
| 14 | + "github.com/stretchr/testify/require" |
10 | 15 | )
|
11 | 16 |
|
12 | 17 | func TestAccUserDataSource(t *testing.T) {
|
13 |
| - // User by Username |
14 |
| - resource.Test(t, resource.TestCase{ |
15 |
| - PreCheck: func() { testAccPreCheck(t) }, |
16 |
| - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
17 |
| - Steps: []resource.TestStep{ |
18 |
| - { |
19 |
| - Config: testAccUserDataSourceConfig{ |
20 |
| - Username: "example", |
21 |
| - }.String(t), |
22 |
| - 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"), |
32 |
| - ), |
| 18 | + if os.Getenv("TF_ACC") == "" { |
| 19 | + t.Skip("Acceptance tests are disabled.") |
| 20 | + } |
| 21 | + ctx := context.Background() |
| 22 | + client := integration.StartCoder(ctx, t, "user_data_acc") |
| 23 | + firstUser, err := client.User(ctx, codersdk.Me) |
| 24 | + require.NoError(t, err) |
| 25 | + user, err := client.CreateUser(ctx, codersdk.CreateUserRequest{ |
| 26 | + |
| 27 | + Username: "example", |
| 28 | + Password: "SomeSecurePassword!", |
| 29 | + UserLoginType: "password", |
| 30 | + OrganizationID: firstUser.OrganizationIDs[0], |
| 31 | + }) |
| 32 | + require.NoError(t, err) |
| 33 | + _, err = client.UpdateUserRoles(ctx, user.Username, codersdk.UpdateRoles{ |
| 34 | + Roles: []string{"auditor"}, |
| 35 | + }) |
| 36 | + require.NoError(t, err) |
| 37 | + _, err = client.UpdateUserProfile(ctx, user.Username, codersdk.UpdateUserProfileRequest{ |
| 38 | + Username: user.Username, |
| 39 | + Name: "Example User", |
| 40 | + }) |
| 41 | + require.NoError(t, err) |
| 42 | + t.Run("UserByUsername", func(t *testing.T) { |
| 43 | + cfg := testAccUserDataSourceConfig{ |
| 44 | + URL: client.URL.String(), |
| 45 | + Token: client.SessionToken(), |
| 46 | + Username: user.Username, |
| 47 | + } |
| 48 | + resource.Test(t, resource.TestCase{ |
| 49 | + PreCheck: func() { testAccPreCheck(t) }, |
| 50 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 51 | + Steps: []resource.TestStep{ |
| 52 | + { |
| 53 | + Config: cfg.String(t), |
| 54 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 55 | + resource.TestCheckResourceAttr("data.coderd_user.test", "username", "example"), |
| 56 | + resource.TestCheckResourceAttr("data.coderd_user.test", "name", "Example User"), |
| 57 | + resource. TestCheckResourceAttr( "data.coderd_user.test", "email", "[email protected]"), |
| 58 | + resource.TestCheckResourceAttr("data.coderd_user.test", "roles.#", "1"), |
| 59 | + resource.TestCheckResourceAttr("data.coderd_user.test", "roles.0", "auditor"), |
| 60 | + resource.TestCheckResourceAttr("data.coderd_user.test", "login_type", "password"), |
| 61 | + resource.TestCheckResourceAttr("data.coderd_user.test", "suspended", "false"), |
| 62 | + ), |
| 63 | + }, |
33 | 64 | },
|
34 |
| - }, |
| 65 | + }) |
35 | 66 | })
|
36 |
| - resource.Test(t, resource.TestCase{ |
37 |
| - PreCheck: func() { testAccPreCheck(t) }, |
38 |
| - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
39 |
| - // User by ID |
40 |
| - Steps: []resource.TestStep{ |
41 |
| - { |
42 |
| - Config: testAccUserDataSourceConfig{ |
43 |
| - ID: "example", |
44 |
| - }.String(t), |
45 |
| - 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"), |
55 |
| - ), |
| 67 | + |
| 68 | + t.Run("UserByID", func(t *testing.T) { |
| 69 | + cfg := testAccUserDataSourceConfig{ |
| 70 | + URL: client.URL.String(), |
| 71 | + Token: client.SessionToken(), |
| 72 | + ID: user.ID.String(), |
| 73 | + } |
| 74 | + resource.Test(t, resource.TestCase{ |
| 75 | + PreCheck: func() { testAccPreCheck(t) }, |
| 76 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 77 | + // User by ID |
| 78 | + Steps: []resource.TestStep{ |
| 79 | + { |
| 80 | + Config: cfg.String(t), |
| 81 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 82 | + resource.TestCheckResourceAttr("data.coderd_user.test", "username", "example"), |
| 83 | + resource.TestCheckResourceAttr("data.coderd_user.test", "name", "Example User"), |
| 84 | + resource. TestCheckResourceAttr( "data.coderd_user.test", "email", "[email protected]"), |
| 85 | + resource.TestCheckResourceAttr("data.coderd_user.test", "roles.#", "1"), |
| 86 | + resource.TestCheckResourceAttr("data.coderd_user.test", "roles.0", "auditor"), |
| 87 | + resource.TestCheckResourceAttr("data.coderd_user.test", "login_type", "password"), |
| 88 | + resource.TestCheckResourceAttr("data.coderd_user.test", "suspended", "false"), |
| 89 | + ), |
| 90 | + }, |
| 91 | + }, |
| 92 | + }) |
| 93 | + }) |
| 94 | + t.Run("NeitherIDNorUsername", func(t *testing.T) { |
| 95 | + cfg := testAccUserDataSourceConfig{ |
| 96 | + URL: client.URL.String(), |
| 97 | + Token: client.SessionToken(), |
| 98 | + } |
| 99 | + resource.Test(t, resource.TestCase{ |
| 100 | + PreCheck: func() { testAccPreCheck(t) }, |
| 101 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 102 | + // Neither ID nor Username |
| 103 | + Steps: []resource.TestStep{ |
| 104 | + { |
| 105 | + Config: cfg.String(t), |
| 106 | + ExpectError: regexp.MustCompile(`At least one of these attributes must be configured: \[id,username\]`), |
| 107 | + }, |
56 | 108 | },
|
57 |
| - }, |
| 109 | + }) |
58 | 110 | })
|
| 111 | + |
59 | 112 | }
|
60 | 113 |
|
61 | 114 | type testAccUserDataSourceConfig struct {
|
62 |
| - URL string |
63 |
| - Token string |
| 115 | + URL string |
| 116 | + Token string |
64 | 117 |
|
65 | 118 | ID string
|
66 | 119 | Username string
|
@@ -92,4 +145,3 @@ data "coderd_user" "test" {
|
92 | 145 |
|
93 | 146 | return buf.String()
|
94 | 147 | }
|
95 |
| -*/ |
|
0 commit comments