| 1 |
package ram |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
|
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
var ( |
| 10 |
policy_username string |
| 11 |
policy_role_name string |
| 12 |
policy_name string |
| 13 |
policy_document = PolicyDocument{ |
| 14 |
Statement: []PolicyItem{ |
| 15 |
PolicyItem{ |
| 16 |
Action: "*", |
| 17 |
Effect: "Allow", |
| 18 |
Resource: "*", |
| 19 |
}, |
| 20 |
}, |
| 21 |
Version: "1", |
| 22 |
} |
| 23 |
policy_req = PolicyRequest{ |
| 24 |
PolicyName: "unit_tesst_policy", |
| 25 |
Description: "nothing", |
| 26 |
PolicyType: "Custom", |
| 27 |
} |
| 28 |
policy_group_name string |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
func TestCreatePolicy(t *testing.T) { |
| 37 |
var policyReq = policy_req |
| 38 |
document, err := json.Marshal(policy_document) |
| 39 |
if err != nil { |
| 40 |
t.Errorf("Failed to marshal document %v", err) |
| 41 |
} |
| 42 |
policyReq.PolicyDocument = string(document) |
| 43 |
client := NewTestClient() |
| 44 |
resp, err := client.CreatePolicy(policyReq) |
| 45 |
if err != nil { |
| 46 |
t.Errorf("Failed to CreatePolicy %v", err) |
| 47 |
} |
| 48 |
policy_name = resp.Policy.PolicyName |
| 49 |
t.Logf("pass CreatePolicy %+++v", resp) |
| 50 |
} |
| 51 |
|
| 52 |
func TestGetPolicy(t *testing.T) { |
| 53 |
var policyReq = policy_req |
| 54 |
policyReq.PolicyName = policy_name |
| 55 |
client := NewTestClient() |
| 56 |
resp, err := client.GetPolicy(policyReq) |
| 57 |
if err != nil { |
| 58 |
t.Errorf("Failed to GetPolicy %v", err) |
| 59 |
} |
| 60 |
t.Logf("pass GetPolicy %v", resp) |
| 61 |
} |
| 62 |
|
| 63 |
func TestAttachPolicyToUser(t *testing.T) { |
| 64 |
client := NewTestClient() |
| 65 |
listParams := ListUserRequest{} |
| 66 |
resp, err := client.ListUsers(listParams) |
| 67 |
if err != nil { |
| 68 |
t.Errorf("Failed to ListUser %v", err) |
| 69 |
return |
| 70 |
} |
| 71 |
policy_username = resp.Users.User[0].UserName |
| 72 |
attachPolicyRequest := AttachPolicyRequest{ |
| 73 |
PolicyRequest: PolicyRequest{ |
| 74 |
PolicyType: "Custom", |
| 75 |
PolicyName: policy_name, |
| 76 |
}, |
| 77 |
UserName: policy_username, |
| 78 |
} |
| 79 |
attachResp, err := client.AttachPolicyToUser(attachPolicyRequest) |
| 80 |
if err != nil { |
| 81 |
t.Errorf("Failed to AttachPolicyToUser %v", err) |
| 82 |
return |
| 83 |
} |
| 84 |
t.Logf("pass AttachPolicyToUser %++v", attachResp) |
| 85 |
} |
| 86 |
|
| 87 |
func TestListPoliciesForUser(t *testing.T) { |
| 88 |
client := NewTestClient() |
| 89 |
userQuery := UserQueryRequest{ |
| 90 |
UserName: policy_username, |
| 91 |
} |
| 92 |
resp, err := client.ListPoliciesForUser(userQuery) |
| 93 |
if err != nil { |
| 94 |
t.Errorf("Failed to ListPoliciesForUser %v", err) |
| 95 |
return |
| 96 |
} |
| 97 |
t.Logf("pass ListPoliciesForUser %++v", resp) |
| 98 |
} |
| 99 |
|
| 100 |
func TestDetachPolicyFromUser(t *testing.T) { |
| 101 |
client := NewTestClient() |
| 102 |
detachPolicyRequest := AttachPolicyRequest{ |
| 103 |
PolicyRequest: PolicyRequest{ |
| 104 |
PolicyType: "Custom", |
| 105 |
PolicyName: policy_name, |
| 106 |
}, |
| 107 |
UserName: policy_username, |
| 108 |
} |
| 109 |
resp, err := client.DetachPolicyFromUser(detachPolicyRequest) |
| 110 |
if err != nil { |
| 111 |
t.Errorf("Failed to DetachPolicyFromUser %++v", err) |
| 112 |
return |
| 113 |
} |
| 114 |
t.Logf("pass DetachPolicyFromUser %++v", resp) |
| 115 |
} |
| 116 |
|
| 117 |
func TestAttachPolicyToRole(t *testing.T) { |
| 118 |
client := NewTestClient() |
| 119 |
resp, err := client.ListRoles() |
| 120 |
if err != nil { |
| 121 |
t.Errorf("Failed to ListRole %v", err) |
| 122 |
return |
| 123 |
} |
| 124 |
policy_role_name = resp.Roles.Role[0].RoleName |
| 125 |
attachPolicyRequest := AttachPolicyToRoleRequest{ |
| 126 |
PolicyRequest: PolicyRequest{ |
| 127 |
PolicyType: "Custom", |
| 128 |
PolicyName: policy_name, |
| 129 |
}, |
| 130 |
RoleName: policy_role_name, |
| 131 |
} |
| 132 |
attachResp, err := client.AttachPolicyToRole(attachPolicyRequest) |
| 133 |
if err != nil { |
| 134 |
t.Errorf("Failed to AttachPolicyToRole %v", err) |
| 135 |
return |
| 136 |
} |
| 137 |
t.Logf("pass AttachPolicyToRole %++v", attachResp) |
| 138 |
} |
| 139 |
|
| 140 |
func TestListPoliciesForRole(t *testing.T) { |
| 141 |
client := NewTestClient() |
| 142 |
roleQuery := RoleQueryRequest{ |
| 143 |
RoleName: policy_role_name, |
| 144 |
} |
| 145 |
resp, err := client.ListPoliciesForRole(roleQuery) |
| 146 |
if err != nil { |
| 147 |
t.Errorf("Failed to ListPoliciesForRole %v", err) |
| 148 |
return |
| 149 |
} |
| 150 |
t.Logf("pass ListPoliciesForRole %++v", resp) |
| 151 |
} |
| 152 |
|
| 153 |
func TestDetachPolicyFromRole(t *testing.T) { |
| 154 |
client := NewTestClient() |
| 155 |
detachPolicyRequest := AttachPolicyToRoleRequest{ |
| 156 |
PolicyRequest: PolicyRequest{ |
| 157 |
PolicyType: "Custom", |
| 158 |
PolicyName: policy_name, |
| 159 |
}, |
| 160 |
RoleName: policy_role_name, |
| 161 |
} |
| 162 |
resp, err := client.DetachPolicyFromRole(detachPolicyRequest) |
| 163 |
if err != nil { |
| 164 |
t.Errorf("Failed to DetachPolicyFromRole %++v", err) |
| 165 |
return |
| 166 |
} |
| 167 |
t.Logf("pass DetachPolicyFromRole %++v", resp) |
| 168 |
} |
| 169 |
|
| 170 |
func TestAttachPolicyToGroup(t *testing.T) { |
| 171 |
client := NewTestClient() |
| 172 |
resp, err := client.ListGroup(GroupListRequest{}) |
| 173 |
if err != nil { |
| 174 |
t.Errorf("Failed to ListGroup %v", err) |
| 175 |
return |
| 176 |
} |
| 177 |
policy_group_name = resp.Groups.Group[0].GroupName |
| 178 |
attachPolicyRequest := AttachPolicyToGroupRequest{ |
| 179 |
PolicyRequest: PolicyRequest{ |
| 180 |
PolicyType: "Custom", |
| 181 |
PolicyName: policy_name, |
| 182 |
}, |
| 183 |
GroupName: policy_group_name, |
| 184 |
} |
| 185 |
attachResp, err := client.AttachPolicyToGroup(attachPolicyRequest) |
| 186 |
if err != nil { |
| 187 |
t.Errorf("Failed to AttachPolicyToGroup %v", err) |
| 188 |
return |
| 189 |
} |
| 190 |
t.Logf("pass AttachPolicyToGroup %++v", attachResp) |
| 191 |
} |
| 192 |
|
| 193 |
func TestListPoliciesForGroup(t *testing.T) { |
| 194 |
client := NewTestClient() |
| 195 |
groupQuery := GroupQueryRequest{ |
| 196 |
GroupName: policy_group_name, |
| 197 |
} |
| 198 |
resp, err := client.ListPoliciesForGroup(groupQuery) |
| 199 |
if err != nil { |
| 200 |
t.Errorf("Failed to ListPoliciesForGroup %v", err) |
| 201 |
return |
| 202 |
} |
| 203 |
t.Logf("pass ListPoliciesForGroup %++v", resp) |
| 204 |
} |
| 205 |
|
| 206 |
func TEstListEntitiesForPolicy(t *testing.T) { |
| 207 |
client := NewTestClient() |
| 208 |
policyReq := PolicyRequest{ |
| 209 |
PolicyType: "Custom", |
| 210 |
PolicyName: policy_name, |
| 211 |
} |
| 212 |
resp, err := client.ListEntitiesForPolicy(policyReq) |
| 213 |
if err != nil { |
| 214 |
t.Errorf("Failed to ListEntitiesForPolicy %++v", err) |
| 215 |
return |
| 216 |
} |
| 217 |
t.Logf("pass ListEntitiesForPolicy %++v", resp) |
| 218 |
} |
| 219 |
|
| 220 |
func TestDetachPolicyFromGroup(t *testing.T) { |
| 221 |
client := NewTestClient() |
| 222 |
detachPolicyRequest := AttachPolicyToGroupRequest{ |
| 223 |
PolicyRequest: PolicyRequest{ |
| 224 |
PolicyType: "Custom", |
| 225 |
PolicyName: policy_name, |
| 226 |
}, |
| 227 |
GroupName: policy_group_name, |
| 228 |
} |
| 229 |
resp, err := client.DetachPolicyFromGroup(detachPolicyRequest) |
| 230 |
if err != nil { |
| 231 |
t.Errorf("Failed to DetachPolicyFromGroup %++v", err) |
| 232 |
return |
| 233 |
} |
| 234 |
t.Logf("pass DetachPolicyFromGroup %++v", resp) |
| 235 |
} |
| 236 |
|
| 237 |
func TestDeletePolicy(t *testing.T) { |
| 238 |
client := NewTestClient() |
| 239 |
policyReq := policy_req |
| 240 |
resp, err := client.DeletePolicy(policyReq) |
| 241 |
if err != nil { |
| 242 |
t.Errorf("Failed to DeletePolicy %v", err) |
| 243 |
return |
| 244 |
} |
| 245 |
t.Logf("pass DeletePolicy %++v", resp) |
| 246 |
} |