1package testing
2
3import (
4	"fmt"
5	"net/http"
6	"testing"
7
8	"github.com/gophercloud/gophercloud/openstack/identity/v3/policies"
9	th "github.com/gophercloud/gophercloud/testhelper"
10	fake "github.com/gophercloud/gophercloud/testhelper/client"
11)
12
13// ListOutput provides a single page of Policy results.
14const ListOutput = `
15{
16    "links": {
17        "next": null,
18        "previous": null,
19        "self": "http://example.com/identity/v3/policies"
20    },
21    "policies": [
22        {
23            "type": "text/plain",
24            "id": "2844b2a08be147a08ef58317d6471f1f",
25            "links": {
26                "self": "http://example.com/identity/v3/policies/2844b2a08be147a08ef58317d6471f1f"
27            },
28            "blob": "'foo_user': 'role:compute-user'"
29        },
30        {
31            "type": "application/json",
32            "id": "b49884da9d31494ea02aff38d4b4e701",
33            "links": {
34                "self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701"
35            },
36            "blob": "{'bar_user': 'role:network-user'}",
37            "description": "policy for bar_user"
38        }
39    ]
40}
41`
42
43// ListWithFilterOutput provides a single page of filtered Policy results.
44const ListWithFilterOutput = `
45{
46    "links": {
47        "next": null,
48        "previous": null,
49        "self": "http://example.com/identity/v3/policies"
50    },
51    "policies": [
52        {
53            "type": "application/json",
54            "id": "b49884da9d31494ea02aff38d4b4e701",
55            "links": {
56                "self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701"
57            },
58            "blob": "{'bar_user': 'role:network-user'}",
59            "description": "policy for bar_user"
60        }
61    ]
62}
63`
64
65// GetOutput provides a Get result.
66const GetOutput = `
67{
68    "policy": {
69        "type": "application/json",
70        "id": "b49884da9d31494ea02aff38d4b4e701",
71        "links": {
72            "self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701"
73        },
74        "blob": "{'bar_user': 'role:network-user'}",
75        "description": "policy for bar_user"
76    }
77}
78`
79
80// CreateRequest provides the input to a Create request.
81const CreateRequest = `
82{
83    "policy": {
84        "blob": "{'bar_user': 'role:network-user'}",
85        "description": "policy for bar_user",
86        "type": "application/json"
87    }
88}
89`
90
91// UpdateRequest provides the input to as Update request.
92const UpdateRequest = `
93{
94    "policy": {
95        "description": "updated policy for bar_user"
96    }
97}
98`
99
100// UpdateOutput provides an update result.
101const UpdateOutput = `
102{
103    "policy": {
104        "type": "application/json",
105        "id": "b49884da9d31494ea02aff38d4b4e701",
106        "links": {
107            "self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701"
108        },
109        "blob": "{'bar_user': 'role:network-user'}",
110        "description": "updated policy for bar_user"
111    }
112}
113`
114
115// FirstPolicy is the first policy in the List request.
116var FirstPolicy = policies.Policy{
117	ID:   "2844b2a08be147a08ef58317d6471f1f",
118	Blob: "'foo_user': 'role:compute-user'",
119	Type: "text/plain",
120	Links: map[string]interface{}{
121		"self": "http://example.com/identity/v3/policies/2844b2a08be147a08ef58317d6471f1f",
122	},
123	Extra: map[string]interface{}{},
124}
125
126// SecondPolicy is the second policy in the List request.
127var SecondPolicy = policies.Policy{
128	ID:   "b49884da9d31494ea02aff38d4b4e701",
129	Blob: "{'bar_user': 'role:network-user'}",
130	Type: "application/json",
131	Links: map[string]interface{}{
132		"self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701",
133	},
134	Extra: map[string]interface{}{
135		"description": "policy for bar_user",
136	},
137}
138
139// SecondPolicyUpdated is the policy in the Update request.
140var SecondPolicyUpdated = policies.Policy{
141	ID:   "b49884da9d31494ea02aff38d4b4e701",
142	Blob: "{'bar_user': 'role:network-user'}",
143	Type: "application/json",
144	Links: map[string]interface{}{
145		"self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701",
146	},
147	Extra: map[string]interface{}{
148		"description": "updated policy for bar_user",
149	},
150}
151
152// ExpectedPoliciesSlice is the slice of policies expected to be returned from ListOutput.
153var ExpectedPoliciesSlice = []policies.Policy{FirstPolicy, SecondPolicy}
154
155// HandleListPoliciesSuccessfully creates an HTTP handler at `/policies` on the
156// test handler mux that responds with a list of two policies.
157func HandleListPoliciesSuccessfully(t *testing.T) {
158	th.Mux.HandleFunc("/policies", func(w http.ResponseWriter, r *http.Request) {
159		th.TestMethod(t, r, "GET")
160		th.TestHeader(t, r, "Accept", "application/json")
161		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
162
163		w.Header().Set("Content-Type", "application/json")
164		w.WriteHeader(http.StatusOK)
165		switch r.URL.Query().Get("type") {
166		case "":
167			fmt.Fprintf(w, ListOutput)
168		case "application/json":
169			fmt.Fprintf(w, ListWithFilterOutput)
170		default:
171			w.WriteHeader(http.StatusBadRequest)
172		}
173	})
174}
175
176// HandleCreatePolicySuccessfully creates an HTTP handler at `/policies` on the
177// test handler mux that tests policy creation.
178func HandleCreatePolicySuccessfully(t *testing.T) {
179	th.Mux.HandleFunc("/policies", func(w http.ResponseWriter, r *http.Request) {
180		th.TestMethod(t, r, "POST")
181		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
182		th.TestJSONRequest(t, r, CreateRequest)
183
184		w.WriteHeader(http.StatusCreated)
185		fmt.Fprintf(w, GetOutput)
186	})
187}
188
189// HandleGetPolicySuccessfully creates an HTTP handler at `/policies` on the
190// test handler mux that responds with a single policy.
191func HandleGetPolicySuccessfully(t *testing.T) {
192	th.Mux.HandleFunc("/policies/b49884da9d31494ea02aff38d4b4e701",
193		func(w http.ResponseWriter, r *http.Request) {
194			th.TestMethod(t, r, "GET")
195			th.TestHeader(t, r, "Accept", "application/json")
196			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
197
198			w.Header().Set("Content-Type", "application/json")
199			w.WriteHeader(http.StatusOK)
200			fmt.Fprintf(w, GetOutput)
201		},
202	)
203}
204
205// HandleUpdatePolicySuccessfully creates an HTTP handler at `/policies` on the
206// test handler mux that tests role update.
207func HandleUpdatePolicySuccessfully(t *testing.T) {
208	th.Mux.HandleFunc("/policies/b49884da9d31494ea02aff38d4b4e701",
209		func(w http.ResponseWriter, r *http.Request) {
210			th.TestMethod(t, r, "PATCH")
211			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
212			th.TestJSONRequest(t, r, UpdateRequest)
213
214			w.WriteHeader(http.StatusOK)
215			fmt.Fprintf(w, UpdateOutput)
216		},
217	)
218}
219
220// HandleDeletePolicySuccessfully creates an HTTP handler at `/policies` on the
221// test handler mux that tests policy deletion.
222func HandleDeletePolicySuccessfully(t *testing.T) {
223	th.Mux.HandleFunc("/policies/9fe1d3", func(w http.ResponseWriter, r *http.Request) {
224		th.TestMethod(t, r, "DELETE")
225		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
226
227		w.WriteHeader(http.StatusNoContent)
228	})
229}
230