-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresource_scim_group_test.go
146 lines (129 loc) · 4.22 KB
/
resource_scim_group_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
package platform_test
import (
"fmt"
"net/http"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/jfrog/terraform-provider-platform/v2/pkg/platform"
"github.com/jfrog/terraform-provider-shared/testutil"
"github.com/jfrog/terraform-provider-shared/util"
)
func TestAccSCIMGroup_full(t *testing.T) {
_, _, username := testutil.MkNames("test-scim-user", "platform_scim_user")
_, fqrn, name := testutil.MkNames("test-scim-group", "platform_scim_group")
temp := `
resource "platform_scim_user" "{{ .username }}" {
username = "{{ .email }}"
active = true
emails = [{
value = "{{ .email }}"
primary = true
}]
}
resource "platform_scim_group" "{{ .name }}" {
id = "{{ .name }}"
display_name = "{{ .name }}"
members = [{
value = platform_scim_user.{{ .username }}.username
display = platform_scim_user.{{ .username }}.username
}]
}`
testData := map[string]string{
"username": username,
"email": "test@tempurl.org",
"name": name,
}
config := util.ExecuteTemplate(name, temp, testData)
updatedTemp := `
resource "platform_scim_user" "{{ .username }}" {
username = "{{ .email }}"
active = true
emails = [{
value = "{{ .email }}"
primary = true
}]
}
resource "platform_scim_group" "{{ .name }}" {
id = "{{ .name }}"
display_name = "{{ .name }}"
members = [{
value = platform_scim_user.{{ .username }}.username
display = platform_scim_user.{{ .username }}.username
}, {
value = "anonymous"
display = "anonymous"
}]
}`
updatedTestData := map[string]string{
"username": username,
"email": "test@tempurl.org",
"name": name,
}
updatedConfig := util.ExecuteTemplate(name, updatedTemp, updatedTestData)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProviders(),
CheckDestroy: testAccSCIMGroupDestroy(fqrn),
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "id", testData["name"]),
resource.TestCheckResourceAttr(fqrn, "display_name", testData["name"]),
resource.TestCheckResourceAttr(fqrn, "members.#", "1"),
resource.TestCheckResourceAttr(fqrn, "members.0.value", testData["email"]),
resource.TestCheckResourceAttr(fqrn, "members.0.display", testData["email"]),
resource.TestCheckResourceAttr(fqrn, "meta.%", "1"),
resource.TestCheckResourceAttr(fqrn, "meta.resourceType", "Group"),
),
},
{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "id", testData["name"]),
resource.TestCheckResourceAttr(fqrn, "display_name", testData["name"]),
resource.TestCheckResourceAttr(fqrn, "members.#", "2"),
resource.TestCheckTypeSetElemNestedAttrs(fqrn, "members.*", map[string]string{
"value": testData["email"],
"display": testData["email"],
}),
resource.TestCheckTypeSetElemNestedAttrs(fqrn, "members.*", map[string]string{
"value": "anonymous",
"display": "anonymous",
}),
resource.TestCheckResourceAttr(fqrn, "meta.%", "1"),
resource.TestCheckResourceAttr(fqrn, "meta.resourceType", "Group"),
),
},
{
ResourceName: fqrn,
ImportState: true,
ImportStateVerify: true,
ImportStateId: updatedTestData["name"],
ImportStateVerifyIdentifierAttribute: "id",
},
},
})
}
func testAccSCIMGroupDestroy(id string) func(*terraform.State) error {
return func(s *terraform.State) error {
c := TestProvider.(*platform.PlatformProvider).Meta.Client
rs, ok := s.RootModule().Resources[id]
if !ok {
return fmt.Errorf("error: resource id [%s] not found", id)
}
var group platform.SCIMGroupAPIModel
resp, err := c.R().
SetPathParam("name", rs.Primary.Attributes["id"]).
SetResult(&group).
Get(platform.SCIMGroupEndpoint)
if err != nil {
return err
}
if resp != nil && resp.StatusCode() == http.StatusNotFound {
return nil
}
return fmt.Errorf("error: SCIM group %s still exists", rs.Primary.Attributes["id"])
}
}