File: policy_test.go

package info (click to toggle)
golang-github-denverdino-aliyungo 0.0~git20180921.13fa8aa-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,824 kB
  • sloc: xml: 1,359; makefile: 3
file content (246 lines) | stat: -rw-r--r-- 6,027 bytes parent folder | download | duplicates (3)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package ram

import (
	"encoding/json"
	//	"fmt"
	"testing"
)

var (
	policy_username  string
	policy_role_name string
	policy_name      string
	policy_document  = PolicyDocument{
		Statement: []PolicyItem{
			PolicyItem{
				Action:   "*",
				Effect:   "Allow",
				Resource: "*",
			},
		},
		Version: "1",
	}
	policy_req = PolicyRequest{
		PolicyName:  "unit_tesst_policy",
		Description: "nothing",
		PolicyType:  "Custom",
	}
	policy_group_name string
)

/*
	TODO maybe I need import ginko to my project
	BeforeEach and AfterClean is needed
*/

func TestCreatePolicy(t *testing.T) {
	var policyReq = policy_req
	document, err := json.Marshal(policy_document)
	if err != nil {
		t.Errorf("Failed to marshal document %v", err)
	}
	policyReq.PolicyDocument = string(document)
	client := NewTestClient()
	resp, err := client.CreatePolicy(policyReq)
	if err != nil {
		t.Errorf("Failed to CreatePolicy %v", err)
	}
	policy_name = resp.Policy.PolicyName
	t.Logf("pass CreatePolicy %+++v", resp)
}

func TestGetPolicy(t *testing.T) {
	var policyReq = policy_req
	policyReq.PolicyName = policy_name
	client := NewTestClient()
	resp, err := client.GetPolicy(policyReq)
	if err != nil {
		t.Errorf("Failed to GetPolicy %v", err)
	}
	t.Logf("pass GetPolicy %v", resp)
}

func TestAttachPolicyToUser(t *testing.T) {
	client := NewTestClient()
	listParams := ListUserRequest{}
	resp, err := client.ListUsers(listParams)
	if err != nil {
		t.Errorf("Failed to ListUser %v", err)
		return
	}
	policy_username = resp.Users.User[0].UserName
	attachPolicyRequest := AttachPolicyRequest{
		PolicyRequest: PolicyRequest{
			PolicyType: "Custom",
			PolicyName: policy_name,
		},
		UserName: policy_username,
	}
	attachResp, err := client.AttachPolicyToUser(attachPolicyRequest)
	if err != nil {
		t.Errorf("Failed to AttachPolicyToUser %v", err)
		return
	}
	t.Logf("pass AttachPolicyToUser %++v", attachResp)
}

func TestListPoliciesForUser(t *testing.T) {
	client := NewTestClient()
	userQuery := UserQueryRequest{
		UserName: policy_username,
	}
	resp, err := client.ListPoliciesForUser(userQuery)
	if err != nil {
		t.Errorf("Failed to ListPoliciesForUser %v", err)
		return
	}
	t.Logf("pass ListPoliciesForUser %++v", resp)
}

func TestDetachPolicyFromUser(t *testing.T) {
	client := NewTestClient()
	detachPolicyRequest := AttachPolicyRequest{
		PolicyRequest: PolicyRequest{
			PolicyType: "Custom",
			PolicyName: policy_name,
		},
		UserName: policy_username,
	}
	resp, err := client.DetachPolicyFromUser(detachPolicyRequest)
	if err != nil {
		t.Errorf("Failed to DetachPolicyFromUser %++v", err)
		return
	}
	t.Logf("pass DetachPolicyFromUser %++v", resp)
}

func TestAttachPolicyToRole(t *testing.T) {
	client := NewTestClient()
	resp, err := client.ListRoles()
	if err != nil {
		t.Errorf("Failed to ListRole %v", err)
		return
	}
	policy_role_name = resp.Roles.Role[0].RoleName
	attachPolicyRequest := AttachPolicyToRoleRequest{
		PolicyRequest: PolicyRequest{
			PolicyType: "Custom",
			PolicyName: policy_name,
		},
		RoleName: policy_role_name,
	}
	attachResp, err := client.AttachPolicyToRole(attachPolicyRequest)
	if err != nil {
		t.Errorf("Failed to AttachPolicyToRole %v", err)
		return
	}
	t.Logf("pass AttachPolicyToRole %++v", attachResp)
}

func TestListPoliciesForRole(t *testing.T) {
	client := NewTestClient()
	roleQuery := RoleQueryRequest{
		RoleName: policy_role_name,
	}
	resp, err := client.ListPoliciesForRole(roleQuery)
	if err != nil {
		t.Errorf("Failed to ListPoliciesForRole %v", err)
		return
	}
	t.Logf("pass ListPoliciesForRole %++v", resp)
}

func TestDetachPolicyFromRole(t *testing.T) {
	client := NewTestClient()
	detachPolicyRequest := AttachPolicyToRoleRequest{
		PolicyRequest: PolicyRequest{
			PolicyType: "Custom",
			PolicyName: policy_name,
		},
		RoleName: policy_role_name,
	}
	resp, err := client.DetachPolicyFromRole(detachPolicyRequest)
	if err != nil {
		t.Errorf("Failed to DetachPolicyFromRole %++v", err)
		return
	}
	t.Logf("pass DetachPolicyFromRole %++v", resp)
}

func TestAttachPolicyToGroup(t *testing.T) {
	client := NewTestClient()
	resp, err := client.ListGroup(GroupListRequest{})
	if err != nil {
		t.Errorf("Failed to ListGroup %v", err)
		return
	}
	policy_group_name = resp.Groups.Group[0].GroupName
	attachPolicyRequest := AttachPolicyToGroupRequest{
		PolicyRequest: PolicyRequest{
			PolicyType: "Custom",
			PolicyName: policy_name,
		},
		GroupName: policy_group_name,
	}
	attachResp, err := client.AttachPolicyToGroup(attachPolicyRequest)
	if err != nil {
		t.Errorf("Failed to AttachPolicyToGroup %v", err)
		return
	}
	t.Logf("pass AttachPolicyToGroup %++v", attachResp)
}

func TestListPoliciesForGroup(t *testing.T) {
	client := NewTestClient()
	groupQuery := GroupQueryRequest{
		GroupName: policy_group_name,
	}
	resp, err := client.ListPoliciesForGroup(groupQuery)
	if err != nil {
		t.Errorf("Failed to ListPoliciesForGroup %v", err)
		return
	}
	t.Logf("pass ListPoliciesForGroup %++v", resp)
}

func TEstListEntitiesForPolicy(t *testing.T) {
	client := NewTestClient()
	policyReq := PolicyRequest{
		PolicyType: "Custom",
		PolicyName: policy_name,
	}
	resp, err := client.ListEntitiesForPolicy(policyReq)
	if err != nil {
		t.Errorf("Failed to ListEntitiesForPolicy %++v", err)
		return
	}
	t.Logf("pass ListEntitiesForPolicy %++v", resp)
}

func TestDetachPolicyFromGroup(t *testing.T) {
	client := NewTestClient()
	detachPolicyRequest := AttachPolicyToGroupRequest{
		PolicyRequest: PolicyRequest{
			PolicyType: "Custom",
			PolicyName: policy_name,
		},
		GroupName: policy_group_name,
	}
	resp, err := client.DetachPolicyFromGroup(detachPolicyRequest)
	if err != nil {
		t.Errorf("Failed to DetachPolicyFromGroup %++v", err)
		return
	}
	t.Logf("pass DetachPolicyFromGroup %++v", resp)
}

func TestDeletePolicy(t *testing.T) {
	client := NewTestClient()
	policyReq := policy_req
	resp, err := client.DeletePolicy(policyReq)
	if err != nil {
		t.Errorf("Failed to DeletePolicy %v", err)
		return
	}
	t.Logf("pass DeletePolicy %++v", resp)
}