jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 1 | package testing |
Daniel Speichert | 44e3b54 | 2015-08-26 20:55:58 -0400 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/openstack/identity/v3/roles" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/pagination" |
| 11 | "github.com/gophercloud/gophercloud/testhelper" |
| 12 | "github.com/gophercloud/gophercloud/testhelper/client" |
Daniel Speichert | 44e3b54 | 2015-08-26 20:55:58 -0400 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | func TestListSinglePage(t *testing.T) { |
| 16 | testhelper.SetupHTTP() |
| 17 | defer testhelper.TeardownHTTP() |
| 18 | |
| 19 | testhelper.Mux.HandleFunc("/role_assignments", func(w http.ResponseWriter, r *http.Request) { |
| 20 | testhelper.TestMethod(t, r, "GET") |
| 21 | testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 22 | |
| 23 | w.Header().Add("Content-Type", "application/json") |
| 24 | fmt.Fprintf(w, ` |
| 25 | { |
| 26 | "role_assignments": [ |
| 27 | { |
| 28 | "links": { |
| 29 | "assignment": "http://identity:35357/v3/domains/161718/users/313233/roles/123456" |
| 30 | }, |
| 31 | "role": { |
| 32 | "id": "123456" |
| 33 | }, |
| 34 | "scope": { |
| 35 | "domain": { |
| 36 | "id": "161718" |
| 37 | } |
| 38 | }, |
| 39 | "user": { |
| 40 | "id": "313233" |
| 41 | } |
| 42 | }, |
| 43 | { |
| 44 | "links": { |
| 45 | "assignment": "http://identity:35357/v3/projects/456789/groups/101112/roles/123456", |
| 46 | "membership": "http://identity:35357/v3/groups/101112/users/313233" |
| 47 | }, |
| 48 | "role": { |
| 49 | "id": "123456" |
| 50 | }, |
| 51 | "scope": { |
| 52 | "project": { |
| 53 | "id": "456789" |
| 54 | } |
| 55 | }, |
| 56 | "user": { |
| 57 | "id": "313233" |
| 58 | } |
| 59 | } |
| 60 | ], |
| 61 | "links": { |
| 62 | "self": "http://identity:35357/v3/role_assignments?effective", |
| 63 | "previous": null, |
| 64 | "next": null |
| 65 | } |
| 66 | } |
| 67 | `) |
| 68 | }) |
| 69 | |
| 70 | count := 0 |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 71 | err := roles.ListAssignments(client.ServiceClient(), roles.ListAssignmentsOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Daniel Speichert | 44e3b54 | 2015-08-26 20:55:58 -0400 | [diff] [blame] | 72 | count++ |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 73 | actual, err := roles.ExtractRoleAssignments(page) |
Daniel Speichert | 44e3b54 | 2015-08-26 20:55:58 -0400 | [diff] [blame] | 74 | if err != nil { |
| 75 | return false, err |
| 76 | } |
| 77 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 78 | expected := []roles.RoleAssignment{ |
| 79 | { |
| 80 | Role: roles.Role{ID: "123456"}, |
| 81 | Scope: roles.Scope{Domain: roles.Domain{ID: "161718"}}, |
| 82 | User: roles.User{ID: "313233"}, |
| 83 | Group: roles.Group{}, |
Daniel Speichert | 44e3b54 | 2015-08-26 20:55:58 -0400 | [diff] [blame] | 84 | }, |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 85 | { |
| 86 | Role: roles.Role{ID: "123456"}, |
| 87 | Scope: roles.Scope{Project: roles.Project{ID: "456789"}}, |
| 88 | User: roles.User{ID: "313233"}, |
| 89 | Group: roles.Group{}, |
Daniel Speichert | 44e3b54 | 2015-08-26 20:55:58 -0400 | [diff] [blame] | 90 | }, |
| 91 | } |
| 92 | |
| 93 | if !reflect.DeepEqual(expected, actual) { |
| 94 | t.Errorf("Expected %#v, got %#v", expected, actual) |
| 95 | } |
| 96 | |
| 97 | return true, nil |
| 98 | }) |
| 99 | if err != nil { |
| 100 | t.Errorf("Unexpected error while paging: %v", err) |
| 101 | } |
| 102 | if count != 1 { |
| 103 | t.Errorf("Expected 1 page, got %d", count) |
| 104 | } |
| 105 | } |