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