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