github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v3/roles/testing/fixtures.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/huaweicloud/golangsdk/openstack/identity/v3/roles" 9 th "github.com/huaweicloud/golangsdk/testhelper" 10 fake "github.com/huaweicloud/golangsdk/testhelper/client" 11 ) 12 13 // ListOutput provides a single page of Role results. 14 const ListOutput = ` 15 { 16 "links": { 17 "next": null, 18 "previous": null, 19 "self": "http://example.com/identity/v3/roles" 20 }, 21 "roles": [ 22 { 23 "domain_id": "default", 24 "id": "2844b2a08be147a08ef58317d6471f1f", 25 "links": { 26 "self": "http://example.com/identity/v3/roles/2844b2a08be147a08ef58317d6471f1f" 27 }, 28 "name": "admin-read-only" 29 }, 30 { 31 "domain_id": "1789d1", 32 "id": "9fe1d3", 33 "links": { 34 "self": "https://example.com/identity/v3/roles/9fe1d3" 35 }, 36 "name": "support", 37 "extra": { 38 "description": "read-only support role" 39 } 40 } 41 ] 42 } 43 ` 44 45 // GetOutput provides a Get result. 46 const GetOutput = ` 47 { 48 "role": { 49 "domain_id": "1789d1", 50 "id": "9fe1d3", 51 "links": { 52 "self": "https://example.com/identity/v3/roles/9fe1d3" 53 }, 54 "name": "support", 55 "extra": { 56 "description": "read-only support role" 57 } 58 } 59 } 60 ` 61 62 // CreateRequest provides the input to a Create request. 63 const CreateRequest = ` 64 { 65 "role": { 66 "domain_id": "1789d1", 67 "name": "support", 68 "description": "read-only support role" 69 } 70 } 71 ` 72 73 // UpdateRequest provides the input to as Update request. 74 const UpdateRequest = ` 75 { 76 "role": { 77 "description": "admin read-only support role" 78 } 79 } 80 ` 81 82 // UpdateOutput provides an update result. 83 const UpdateOutput = ` 84 { 85 "role": { 86 "domain_id": "1789d1", 87 "id": "9fe1d3", 88 "links": { 89 "self": "https://example.com/identity/v3/roles/9fe1d3" 90 }, 91 "name": "support", 92 "extra": { 93 "description": "admin read-only support role" 94 } 95 } 96 } 97 ` 98 99 const ListAssignmentOutput = ` 100 { 101 "role_assignments": [ 102 { 103 "links": { 104 "assignment": "http://identity:35357/v3/domains/161718/users/313233/roles/123456" 105 }, 106 "role": { 107 "id": "123456" 108 }, 109 "scope": { 110 "domain": { 111 "id": "161718" 112 } 113 }, 114 "user": { 115 "id": "313233" 116 } 117 }, 118 { 119 "links": { 120 "assignment": "http://identity:35357/v3/projects/456789/groups/101112/roles/123456", 121 "membership": "http://identity:35357/v3/groups/101112/users/313233" 122 }, 123 "role": { 124 "id": "123456" 125 }, 126 "scope": { 127 "project": { 128 "id": "456789" 129 } 130 }, 131 "user": { 132 "id": "313233" 133 } 134 } 135 ], 136 "links": { 137 "self": "http://identity:35357/v3/role_assignments?effective", 138 "previous": null, 139 "next": null 140 } 141 } 142 ` 143 144 // FirstRole is the first role in the List request. 145 var FirstRole = roles.Role{ 146 DomainID: "default", 147 ID: "2844b2a08be147a08ef58317d6471f1f", 148 Links: map[string]interface{}{ 149 "self": "http://example.com/identity/v3/roles/2844b2a08be147a08ef58317d6471f1f", 150 }, 151 Name: "admin-read-only", 152 Extra: map[string]interface{}{}, 153 } 154 155 // SecondRole is the second role in the List request. 156 var SecondRole = roles.Role{ 157 DomainID: "1789d1", 158 ID: "9fe1d3", 159 Links: map[string]interface{}{ 160 "self": "https://example.com/identity/v3/roles/9fe1d3", 161 }, 162 Name: "support", 163 Extra: map[string]interface{}{ 164 "description": "read-only support role", 165 }, 166 } 167 168 // SecondRoleUpdated is how SecondRole should look after an Update. 169 var SecondRoleUpdated = roles.Role{ 170 DomainID: "1789d1", 171 ID: "9fe1d3", 172 Links: map[string]interface{}{ 173 "self": "https://example.com/identity/v3/roles/9fe1d3", 174 }, 175 Name: "support", 176 Extra: map[string]interface{}{ 177 "description": "admin read-only support role", 178 }, 179 } 180 181 // ExpectedRolesSlice is the slice of roles expected to be returned from ListOutput. 182 var ExpectedRolesSlice = []roles.Role{FirstRole, SecondRole} 183 184 // HandleListRolesSuccessfully creates an HTTP handler at `/roles` on the 185 // test handler mux that responds with a list of two roles. 186 func HandleListRolesSuccessfully(t *testing.T) { 187 th.Mux.HandleFunc("/roles", func(w http.ResponseWriter, r *http.Request) { 188 th.TestMethod(t, r, "GET") 189 th.TestHeader(t, r, "Accept", "application/json") 190 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 191 192 w.Header().Set("Content-Type", "application/json") 193 w.WriteHeader(http.StatusOK) 194 fmt.Fprintf(w, ListOutput) 195 }) 196 } 197 198 // HandleGetRoleSuccessfully creates an HTTP handler at `/roles` on the 199 // test handler mux that responds with a single role. 200 func HandleGetRoleSuccessfully(t *testing.T) { 201 th.Mux.HandleFunc("/roles/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 202 th.TestMethod(t, r, "GET") 203 th.TestHeader(t, r, "Accept", "application/json") 204 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 205 206 w.Header().Set("Content-Type", "application/json") 207 w.WriteHeader(http.StatusOK) 208 fmt.Fprintf(w, GetOutput) 209 }) 210 } 211 212 // HandleCreateRoleSuccessfully creates an HTTP handler at `/roles` on the 213 // test handler mux that tests role creation. 214 func HandleCreateRoleSuccessfully(t *testing.T) { 215 th.Mux.HandleFunc("/roles", func(w http.ResponseWriter, r *http.Request) { 216 th.TestMethod(t, r, "POST") 217 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 218 th.TestJSONRequest(t, r, CreateRequest) 219 220 w.WriteHeader(http.StatusCreated) 221 fmt.Fprintf(w, GetOutput) 222 }) 223 } 224 225 // HandleUpdateRoleSuccessfully creates an HTTP handler at `/roles` on the 226 // test handler mux that tests role update. 227 func HandleUpdateRoleSuccessfully(t *testing.T) { 228 th.Mux.HandleFunc("/roles/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 229 th.TestMethod(t, r, "PATCH") 230 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 231 th.TestJSONRequest(t, r, UpdateRequest) 232 233 w.WriteHeader(http.StatusOK) 234 fmt.Fprintf(w, UpdateOutput) 235 }) 236 } 237 238 // HandleDeleteRoleSuccessfully creates an HTTP handler at `/roles` on the 239 // test handler mux that tests role deletion. 240 func HandleDeleteRoleSuccessfully(t *testing.T) { 241 th.Mux.HandleFunc("/roles/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 242 th.TestMethod(t, r, "DELETE") 243 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 244 245 w.WriteHeader(http.StatusNoContent) 246 }) 247 } 248 249 func HandleAssignSuccessfully(t *testing.T) { 250 th.Mux.HandleFunc("/projects/{project_id}/users/{user_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) { 251 th.TestMethod(t, r, "PUT") 252 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 253 w.WriteHeader(http.StatusNoContent) 254 }) 255 256 th.Mux.HandleFunc("/projects/{project_id}/groups/{group_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) { 257 th.TestMethod(t, r, "PUT") 258 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 259 w.WriteHeader(http.StatusNoContent) 260 }) 261 262 th.Mux.HandleFunc("/domains/{domain_id}/users/{user_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) { 263 th.TestMethod(t, r, "PUT") 264 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 265 w.WriteHeader(http.StatusNoContent) 266 }) 267 268 th.Mux.HandleFunc("/domains/{domain_id}/groups/{group_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) { 269 th.TestMethod(t, r, "PUT") 270 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 271 w.WriteHeader(http.StatusNoContent) 272 }) 273 } 274 275 func HandleUnassignSuccessfully(t *testing.T) { 276 th.Mux.HandleFunc("/projects/{project_id}/users/{user_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) { 277 th.TestMethod(t, r, "DELETE") 278 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 279 w.WriteHeader(http.StatusNoContent) 280 }) 281 282 th.Mux.HandleFunc("/projects/{project_id}/groups/{group_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) { 283 th.TestMethod(t, r, "DELETE") 284 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 285 w.WriteHeader(http.StatusNoContent) 286 }) 287 288 th.Mux.HandleFunc("/domains/{domain_id}/users/{user_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) { 289 th.TestMethod(t, r, "DELETE") 290 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 291 w.WriteHeader(http.StatusNoContent) 292 }) 293 294 th.Mux.HandleFunc("/domains/{domain_id}/groups/{group_id}/roles/{role_id}", func(w http.ResponseWriter, r *http.Request) { 295 th.TestMethod(t, r, "DELETE") 296 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 297 w.WriteHeader(http.StatusNoContent) 298 }) 299 } 300 301 // FirstRoleAssignment is the first role assignment in the List request. 302 var FirstRoleAssignment = roles.RoleAssignment{ 303 Catalog: "BASE", 304 Description: "Tenant Administrator", 305 DisplayName: "Tenant Administrator", 306 ID: "699bd62cda304d2cad03fd2fb190b8cf", 307 Name: "te_admin", 308 Type: "AA", 309 Policy: roles.Policy{ 310 Statement: []roles.Statement{}, 311 Version: "v1", 312 }, 313 } 314 315 // SecondRoleAssignemnt is the second role assignemnt in the List request. 316 var SecondRoleAssignment = roles.RoleAssignment{ 317 Catalog: "BASE", 318 Description: "Security Administrator", 319 DisplayName: "Security Administrator", 320 ID: "699bd62cda304d2cad03fd2fb190b8ce", 321 Name: "secu_admin", 322 Type: "AA", 323 Policy: roles.Policy{ 324 Statement: []roles.Statement{}, 325 Version: "v1", 326 }, 327 } 328 329 // ExpectedRoleAssignmentsSlice is the slice of role assignments expected to be 330 // returned from ListAssignmentOutput. 331 var ExpectedRoleAssignmentsSlice = []roles.RoleAssignment{FirstRoleAssignment, SecondRoleAssignment} 332 333 // HandleListRoleAssignmentsSuccessfully creates an HTTP handler at `/role_assignments` on the 334 // test handler mux that responds with a list of two role assignments. 335 func HandleListRoleAssignmentsSuccessfully(t *testing.T) { 336 th.Mux.HandleFunc("/domains/{domain_id}/groups/{group_id}/roles", func(w http.ResponseWriter, r *http.Request) { 337 th.TestMethod(t, r, "GET") 338 th.TestHeader(t, r, "Accept", "application/json") 339 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 340 341 w.Header().Set("Content-Type", "application/json") 342 w.WriteHeader(http.StatusOK) 343 fmt.Fprintf(w, ListAssignmentOutput) 344 }) 345 346 th.Mux.HandleFunc("/projects/{project_id}/groups/{group_id}/roles", func(w http.ResponseWriter, r *http.Request) { 347 th.TestMethod(t, r, "GET") 348 th.TestHeader(t, r, "Accept", "application/json") 349 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 350 351 w.Header().Set("Content-Type", "application/json") 352 w.WriteHeader(http.StatusOK) 353 fmt.Fprintf(w, ListAssignmentOutput) 354 }) 355 356 th.Mux.HandleFunc("/projects/{project_id}/users/{user_id}/roles", func(w http.ResponseWriter, r *http.Request) { 357 th.TestMethod(t, r, "GET") 358 th.TestHeader(t, r, "Accept", "application/json") 359 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 360 361 w.Header().Set("Content-Type", "application/json") 362 w.WriteHeader(http.StatusOK) 363 fmt.Fprintf(w, ListAssignmentOutput) 364 }) 365 366 th.Mux.HandleFunc("/domains/{domain_id}/users/{user_id}/roles", func(w http.ResponseWriter, r *http.Request) { 367 th.TestMethod(t, r, "GET") 368 th.TestHeader(t, r, "Accept", "application/json") 369 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 370 371 w.Header().Set("Content-Type", "application/json") 372 w.WriteHeader(http.StatusOK) 373 fmt.Fprintf(w, ListAssignmentOutput) 374 }) 375 }