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