blob: dd9b704d8d7400927630aa887e1b9ca7cd2b03d5 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Daniel Speichert44e3b542015-08-26 20:55:58 -04002
3import (
4 "fmt"
5 "net/http"
6 "reflect"
7 "testing"
8
jrperritt3d966162016-06-06 14:08:54 -05009 "github.com/gophercloud/gophercloud/openstack/identity/v3/roles"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/pagination"
11 "github.com/gophercloud/gophercloud/testhelper"
12 "github.com/gophercloud/gophercloud/testhelper/client"
Daniel Speichert44e3b542015-08-26 20:55:58 -040013)
14
15func 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
jrperritt3d966162016-06-06 14:08:54 -050071 err := roles.ListAssignments(client.ServiceClient(), roles.ListAssignmentsOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Daniel Speichert44e3b542015-08-26 20:55:58 -040072 count++
jrperritt3d966162016-06-06 14:08:54 -050073 actual, err := roles.ExtractRoleAssignments(page)
Daniel Speichert44e3b542015-08-26 20:55:58 -040074 if err != nil {
75 return false, err
76 }
77
jrperritt3d966162016-06-06 14:08:54 -050078 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 Speichert44e3b542015-08-26 20:55:58 -040084 },
jrperritt3d966162016-06-06 14:08:54 -050085 {
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 Speichert44e3b542015-08-26 20:55:58 -040090 },
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}