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