github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/identity/v3/users/testing/fixtures.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/opentelekomcloud/gophertelekomcloud/openstack/identity/v3/groups" 9 "github.com/opentelekomcloud/gophertelekomcloud/openstack/identity/v3/projects" 10 "github.com/opentelekomcloud/gophertelekomcloud/openstack/identity/v3/users" 11 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 12 "github.com/opentelekomcloud/gophertelekomcloud/testhelper/client" 13 ) 14 15 // ListOutput provides a single page of User results. 16 const ListOutput = ` 17 { 18 "links": { 19 "next": null, 20 "previous": null, 21 "self": "http://example.com/identity/v3/users" 22 }, 23 "users": [ 24 { 25 "domain_id": "default", 26 "enabled": true, 27 "id": "2844b2a08be147a08ef58317d6471f1f", 28 "name": "glance" 29 }, 30 { 31 "default_project_id": "263fd9", 32 "domain_id": "1789d1", 33 "enabled": true, 34 "id": "9fe1d3", 35 "name": "jsmith" 36 } 37 ] 38 } 39 ` 40 41 // GetOutput provides a Get result. 42 const GetOutput = ` 43 { 44 "user": { 45 "default_project_id": "263fd9", 46 "domain_id": "1789d1", 47 "enabled": true, 48 "id": "9fe1d3", 49 "name": "jsmith" 50 } 51 } 52 ` 53 54 // GetOutputNoOptions provides a Get result of a user with no options. 55 const GetOutputNoOptions = ` 56 { 57 "user": { 58 "default_project_id": "263fd9", 59 "domain_id": "1789d1", 60 "enabled": true, 61 "id": "9fe1d3", 62 "name": "jsmith" 63 } 64 } 65 ` 66 67 // CreateRequest provides the input to a Create request. 68 const CreateRequest = ` 69 { 70 "user": { 71 "default_project_id": "263fd9", 72 "domain_id": "1789d1", 73 "enabled": true, 74 "name": "jsmith", 75 "password": "secretsecret" 76 } 77 } 78 ` 79 80 // CreateNoOptionsRequest provides the input to a Create request with no Options. 81 const CreateNoOptionsRequest = ` 82 { 83 "user": { 84 "default_project_id": "263fd9", 85 "domain_id": "1789d1", 86 "enabled": true, 87 "name": "jsmith", 88 "password": "secretsecret" 89 } 90 } 91 ` 92 93 // UpdateRequest provides the input to as Update request. 94 const UpdateRequest = ` 95 { 96 "user": { 97 "enabled": false 98 } 99 } 100 ` 101 102 // UpdateOutput provides an update result. 103 const UpdateOutput = ` 104 { 105 "user": { 106 "default_project_id": "263fd9", 107 "domain_id": "1789d1", 108 "enabled": false, 109 "id": "9fe1d3", 110 "name": "jsmith" 111 } 112 } 113 ` 114 115 // UpdateExtendedRequest provides the input to as Update request. 116 const UpdateExtendedRequest = ` 117 { 118 "user": { 119 "enabled": false, 120 "email": "email@generic.otc" 121 } 122 } 123 ` 124 125 // UpdateExtendedOutput provides an update result. 126 const UpdateExtendedOutput = ` 127 { 128 "user": { 129 "default_project_id": "263fd9", 130 "domain_id": "1789d1", 131 "enabled": false, 132 "id": "9fe1d3", 133 "name": "jsmith", 134 "pwd_status": false, 135 "email": "email@generic.otc" 136 } 137 } 138 ` 139 140 // ListGroupsOutput provides a ListGroups result. 141 const ListGroupsOutput = ` 142 { 143 "groups": [ 144 { 145 "description": "Developers cleared for work on all general projects", 146 "domain_id": "1789d1", 147 "id": "ea167b", 148 "links": { 149 "self": "https://example.com/identity/v3/groups/ea167b" 150 }, 151 "building": "Hilltop A", 152 "name": "Developers" 153 }, 154 { 155 "description": "Developers cleared for work on secret projects", 156 "domain_id": "1789d1", 157 "id": "a62db1", 158 "links": { 159 "self": "https://example.com/identity/v3/groups/a62db1" 160 }, 161 "name": "Secure Developers" 162 } 163 ], 164 "links": { 165 "self": "http://example.com/identity/v3/users/9fe1d3/groups", 166 "previous": null, 167 "next": null 168 } 169 } 170 ` 171 172 // ListProjectsOutput provides a ListProjects result. 173 const ListProjectsOutput = ` 174 { 175 "links": { 176 "next": null, 177 "previous": null, 178 "self": "http://localhost:5000/identity/v3/users/foobar/projects" 179 }, 180 "projects": [ 181 { 182 "description": "my first project", 183 "domain_id": "11111", 184 "enabled": true, 185 "id": "abcde", 186 "links": { 187 "self": "http://localhost:5000/identity/v3/projects/abcde" 188 }, 189 "name": "project 1", 190 "parent_id": "11111" 191 }, 192 { 193 "description": "my second project", 194 "domain_id": "22222", 195 "enabled": true, 196 "id": "bcdef", 197 "links": { 198 "self": "http://localhost:5000/identity/v3/projects/bcdef" 199 }, 200 "name": "project 2", 201 "parent_id": "22222" 202 } 203 ] 204 } 205 ` 206 207 // FirstUser is the first user in the List request. 208 var FirstUser = users.User{ 209 DomainID: "default", 210 Enabled: true, 211 ID: "2844b2a08be147a08ef58317d6471f1f", 212 Name: "glance", 213 } 214 215 // SecondUser is the second user in the List request. 216 var SecondUser = users.User{ 217 DefaultProjectID: "263fd9", 218 DomainID: "1789d1", 219 Enabled: true, 220 ID: "9fe1d3", 221 Name: "jsmith", 222 } 223 224 var SecondUserNoOptions = users.User{ 225 DefaultProjectID: "263fd9", 226 DomainID: "1789d1", 227 Enabled: true, 228 ID: "9fe1d3", 229 Name: "jsmith", 230 } 231 232 // SecondUserUpdated is how SecondUser should look after an Update. 233 var SecondUserUpdated = users.User{ 234 DefaultProjectID: "263fd9", 235 DomainID: "1789d1", 236 Enabled: false, 237 ID: "9fe1d3", 238 Name: "jsmith", 239 } 240 241 // SecondUserUpdated is how SecondUser should look after an Update. 242 var ThirdUserUpdated = users.User{ 243 DefaultProjectID: "263fd9", 244 DomainID: "1789d1", 245 Enabled: false, 246 ID: "9fe1d3", 247 Name: "jsmith", 248 Email: "email@generic.otc", 249 } 250 251 // ExpectedUsersSlice is the slice of users expected to be returned from ListOutput. 252 var ExpectedUsersSlice = []users.User{FirstUser, SecondUser} 253 254 var FirstGroup = groups.Group{ 255 Description: "Developers cleared for work on all general projects", 256 DomainID: "1789d1", 257 ID: "ea167b", 258 Links: map[string]interface{}{ 259 "self": "https://example.com/identity/v3/groups/ea167b", 260 }, 261 Extra: map[string]interface{}{ 262 "building": "Hilltop A", 263 }, 264 Name: "Developers", 265 } 266 267 var SecondGroup = groups.Group{ 268 Description: "Developers cleared for work on secret projects", 269 DomainID: "1789d1", 270 ID: "a62db1", 271 Links: map[string]interface{}{ 272 "self": "https://example.com/identity/v3/groups/a62db1", 273 }, 274 Extra: map[string]interface{}{}, 275 Name: "Secure Developers", 276 } 277 278 var ExpectedGroupsSlice = []groups.Group{FirstGroup, SecondGroup} 279 280 var FirstProject = projects.Project{ 281 Description: "my first project", 282 DomainID: "11111", 283 Enabled: true, 284 ID: "abcde", 285 Name: "project 1", 286 ParentID: "11111", 287 } 288 289 var SecondProject = projects.Project{ 290 Description: "my second project", 291 DomainID: "22222", 292 Enabled: true, 293 ID: "bcdef", 294 Name: "project 2", 295 ParentID: "22222", 296 } 297 298 var ExpectedProjectsSlice = []projects.Project{FirstProject, SecondProject} 299 300 // HandleListUsersSuccessfully creates an HTTP handler at `/users` on the 301 // test handler mux that responds with a list of two users. 302 func HandleListUsersSuccessfully(t *testing.T) { 303 th.Mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { 304 th.TestMethod(t, r, "GET") 305 th.TestHeader(t, r, "Accept", "application/json") 306 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 307 308 w.Header().Set("Content-Type", "application/json") 309 w.WriteHeader(http.StatusOK) 310 _, _ = fmt.Fprint(w, ListOutput) 311 }) 312 } 313 314 // HandleGetUserSuccessfully creates an HTTP handler at `/users` on the 315 // test handler mux that responds with a single user. 316 func HandleGetUserSuccessfully(t *testing.T) { 317 th.Mux.HandleFunc("/users/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 318 th.TestMethod(t, r, "GET") 319 th.TestHeader(t, r, "Accept", "application/json") 320 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 321 322 w.Header().Set("Content-Type", "application/json") 323 w.WriteHeader(http.StatusOK) 324 _, _ = fmt.Fprint(w, GetOutput) 325 }) 326 } 327 328 // HandleCreateUserSuccessfully creates an HTTP handler at `/users` on the 329 // test handler mux that tests user creation. 330 func HandleCreateUserSuccessfully(t *testing.T) { 331 th.Mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { 332 th.TestMethod(t, r, "POST") 333 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 334 th.TestJSONRequest(t, r, CreateRequest) 335 336 w.WriteHeader(http.StatusCreated) 337 _, _ = fmt.Fprint(w, GetOutput) 338 }) 339 } 340 341 // HandleCreateNoOptionsUserSuccessfully creates an HTTP handler at `/users` on the 342 // test handler mux that tests user creation. 343 func HandleCreateNoOptionsUserSuccessfully(t *testing.T) { 344 th.Mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { 345 th.TestMethod(t, r, "POST") 346 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 347 th.TestJSONRequest(t, r, CreateNoOptionsRequest) 348 349 w.WriteHeader(http.StatusCreated) 350 _, _ = fmt.Fprint(w, GetOutputNoOptions) 351 }) 352 } 353 354 // HandleUpdateUserSuccessfully creates an HTTP handler at `/users` on the 355 // test handler mux that tests user update. 356 func HandleUpdateUserSuccessfully(t *testing.T) { 357 th.Mux.HandleFunc("/users/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 358 th.TestMethod(t, r, "PATCH") 359 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 360 th.TestJSONRequest(t, r, UpdateRequest) 361 362 w.WriteHeader(http.StatusOK) 363 _, _ = fmt.Fprint(w, UpdateOutput) 364 }) 365 } 366 367 // HandleUpdateUserSuccessfully creates an HTTP handler at `/users` on the 368 // test handler mux that tests user update. 369 func HandleExtendedUpdateUserSuccessfully(t *testing.T) { 370 th.Mux.HandleFunc("/OS-USER/users/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 371 th.TestMethod(t, r, "PUT") 372 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 373 th.TestJSONRequest(t, r, UpdateExtendedRequest) 374 375 w.WriteHeader(http.StatusOK) 376 _, _ = fmt.Fprint(w, UpdateExtendedOutput) 377 }) 378 } 379 380 // HandleDeleteUserSuccessfully creates an HTTP handler at `/users` on the 381 // test handler mux that tests user deletion. 382 func HandleDeleteUserSuccessfully(t *testing.T) { 383 th.Mux.HandleFunc("/users/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 384 th.TestMethod(t, r, "DELETE") 385 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 386 387 w.WriteHeader(http.StatusNoContent) 388 }) 389 } 390 391 // HandleListUserGroupsSuccessfully creates an HTTP handler at /users/{userID}/groups 392 // on the test handler mux that respons with a list of two groups 393 func HandleListUserGroupsSuccessfully(t *testing.T) { 394 th.Mux.HandleFunc("/users/9fe1d3/groups", func(w http.ResponseWriter, r *http.Request) { 395 th.TestMethod(t, r, "GET") 396 th.TestHeader(t, r, "Accept", "application/json") 397 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 398 399 w.Header().Set("Content-Type", "application/json") 400 w.WriteHeader(http.StatusOK) 401 _, _ = fmt.Fprint(w, ListGroupsOutput) 402 }) 403 } 404 405 // HandleListUserProjectsSuccessfully creates an HTTP handler at /users/{userID}/projects 406 // on the test handler mux that respons wit a list of two projects 407 func HandleListUserProjectsSuccessfully(t *testing.T) { 408 th.Mux.HandleFunc("/users/9fe1d3/projects", func(w http.ResponseWriter, r *http.Request) { 409 th.TestMethod(t, r, "GET") 410 th.TestHeader(t, r, "Accept", "application/json") 411 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 412 413 w.Header().Set("Content-Type", "application/json") 414 w.WriteHeader(http.StatusOK) 415 _, _ = fmt.Fprint(w, ListProjectsOutput) 416 }) 417 } 418 419 // HandleListInGroupSuccessfully creates an HTTP handler at /groups/{groupID}/users 420 // on the test handler mux that response with a list of two users 421 func HandleListInGroupSuccessfully(t *testing.T) { 422 th.Mux.HandleFunc("/groups/ea167b/users", func(w http.ResponseWriter, r *http.Request) { 423 th.TestMethod(t, r, "GET") 424 th.TestHeader(t, r, "Accept", "application/json") 425 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 426 427 w.Header().Set("Content-Type", "application/json") 428 w.WriteHeader(http.StatusOK) 429 _, _ = fmt.Fprint(w, ListOutput) 430 }) 431 }