File: requests_test.go

package info (click to toggle)
golang-github-rackspace-gophercloud 1.0.0%2Bgit20161013.1012.e00690e8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,148 kB
  • ctags: 6,414
  • sloc: sh: 16; makefile: 6
file content (104 lines) | stat: -rw-r--r-- 3,023 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package roles

import (
	"fmt"
	"net/http"
	"reflect"
	"testing"

	"github.com/rackspace/gophercloud/pagination"
	"github.com/rackspace/gophercloud/testhelper"
	"github.com/rackspace/gophercloud/testhelper/client"
)

func TestListSinglePage(t *testing.T) {
	testhelper.SetupHTTP()
	defer testhelper.TeardownHTTP()

	testhelper.Mux.HandleFunc("/role_assignments", func(w http.ResponseWriter, r *http.Request) {
		testhelper.TestMethod(t, r, "GET")
		testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)

		w.Header().Add("Content-Type", "application/json")
		fmt.Fprintf(w, `
			{
                "role_assignments": [
                    {
                        "links": {
                            "assignment": "http://identity:35357/v3/domains/161718/users/313233/roles/123456"
                        },
                        "role": {
                            "id": "123456"
                        },
                        "scope": {
                            "domain": {
                                "id": "161718"
                            }
                        },
                        "user": {
                            "id": "313233"
                        }
                    },
                    {
                        "links": {
                            "assignment": "http://identity:35357/v3/projects/456789/groups/101112/roles/123456",
                            "membership": "http://identity:35357/v3/groups/101112/users/313233"
                        },
                        "role": {
                            "id": "123456"
                        },
                        "scope": {
                            "project": {
                                "id": "456789"
                            }
                        },
                        "user": {
                            "id": "313233"
                        }
                    }
                ],
                "links": {
                    "self": "http://identity:35357/v3/role_assignments?effective",
                    "previous": null,
                    "next": null
                }
            }
		`)
	})

	count := 0
	err := ListAssignments(client.ServiceClient(), ListAssignmentsOpts{}).EachPage(func(page pagination.Page) (bool, error) {
		count++
		actual, err := ExtractRoleAssignments(page)
		if err != nil {
			return false, err
		}

		expected := []RoleAssignment{
			RoleAssignment{
				Role:  Role{ID: "123456"},
				Scope: Scope{Domain: Domain{ID: "161718"}},
				User:  User{ID: "313233"},
				Group: Group{},
			},
			RoleAssignment{
				Role:  Role{ID: "123456"},
				Scope: Scope{Project: Project{ID: "456789"}},
				User:  User{ID: "313233"},
				Group: Group{},
			},
		}

		if !reflect.DeepEqual(expected, actual) {
			t.Errorf("Expected %#v, got %#v", expected, actual)
		}

		return true, nil
	})
	if err != nil {
		t.Errorf("Unexpected error while paging: %v", err)
	}
	if count != 1 {
		t.Errorf("Expected 1 page, got %d", count)
	}
}