1package testing
2
3import (
4	"fmt"
5	"net/http"
6	"testing"
7
8	"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
9	th "github.com/gophercloud/gophercloud/testhelper"
10	"github.com/gophercloud/gophercloud/testhelper/client"
11)
12
13// ListOutput provides a single page of Project results.
14const ListOutput = `
15{
16  "projects": [
17    {
18      "is_domain": false,
19      "description": "The team that is red",
20      "domain_id": "default",
21      "enabled": true,
22      "id": "1234",
23      "name": "Red Team",
24      "parent_id": null
25    },
26    {
27      "is_domain": false,
28      "description": "The team that is blue",
29      "domain_id": "default",
30      "enabled": true,
31      "id": "9876",
32      "name": "Blue Team",
33      "parent_id": null
34    }
35  ],
36  "links": {
37    "next": null,
38    "previous": null
39  }
40}
41`
42
43// GetOutput provides a Get result.
44const GetOutput = `
45{
46  "project": {
47		"is_domain": false,
48		"description": "The team that is red",
49		"domain_id": "default",
50		"enabled": true,
51		"id": "1234",
52		"name": "Red Team",
53		"parent_id": null
54  }
55}
56`
57
58// CreateRequest provides the input to a Create request.
59const CreateRequest = `
60{
61  "project": {
62		"description": "The team that is red",
63		"name": "Red Team"
64  }
65}
66`
67
68// UpdateRequest provides the input to an Update request.
69const UpdateRequest = `
70{
71  "project": {
72		"description": "The team that is bright red",
73		"name": "Bright Red Team"
74  }
75}
76`
77
78// UpdateOutput provides an Update response.
79const UpdateOutput = `
80{
81  "project": {
82		"is_domain": false,
83		"description": "The team that is bright red",
84		"domain_id": "default",
85		"enabled": true,
86		"id": "1234",
87		"name": "Bright Red Team",
88		"parent_id": null
89  }
90}
91`
92
93// RedTeam is a Project fixture.
94var RedTeam = projects.Project{
95	IsDomain:    false,
96	Description: "The team that is red",
97	DomainID:    "default",
98	Enabled:     true,
99	ID:          "1234",
100	Name:        "Red Team",
101	ParentID:    "",
102}
103
104// BlueTeam is a Project fixture.
105var BlueTeam = projects.Project{
106	IsDomain:    false,
107	Description: "The team that is blue",
108	DomainID:    "default",
109	Enabled:     true,
110	ID:          "9876",
111	Name:        "Blue Team",
112	ParentID:    "",
113}
114
115// UpdatedRedTeam is a Project Fixture.
116var UpdatedRedTeam = projects.Project{
117	IsDomain:    false,
118	Description: "The team that is bright red",
119	DomainID:    "default",
120	Enabled:     true,
121	ID:          "1234",
122	Name:        "Bright Red Team",
123	ParentID:    "",
124}
125
126// ExpectedProjectSlice is the slice of projects expected to be returned from ListOutput.
127var ExpectedProjectSlice = []projects.Project{RedTeam, BlueTeam}
128
129// HandleListProjectsSuccessfully creates an HTTP handler at `/projects` on the
130// test handler mux that responds with a list of two tenants.
131func HandleListProjectsSuccessfully(t *testing.T) {
132	th.Mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
133		th.TestMethod(t, r, "GET")
134		th.TestHeader(t, r, "Accept", "application/json")
135		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
136
137		w.Header().Set("Content-Type", "application/json")
138		w.WriteHeader(http.StatusOK)
139		fmt.Fprintf(w, ListOutput)
140	})
141}
142
143// HandleGetProjectSuccessfully creates an HTTP handler at `/projects` on the
144// test handler mux that responds with a single project.
145func HandleGetProjectSuccessfully(t *testing.T) {
146	th.Mux.HandleFunc("/projects/1234", func(w http.ResponseWriter, r *http.Request) {
147		th.TestMethod(t, r, "GET")
148		th.TestHeader(t, r, "Accept", "application/json")
149		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
150
151		w.Header().Set("Content-Type", "application/json")
152		w.WriteHeader(http.StatusOK)
153		fmt.Fprintf(w, GetOutput)
154	})
155}
156
157// HandleCreateProjectSuccessfully creates an HTTP handler at `/projects` on the
158// test handler mux that tests project creation.
159func HandleCreateProjectSuccessfully(t *testing.T) {
160	th.Mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
161		th.TestMethod(t, r, "POST")
162		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
163		th.TestJSONRequest(t, r, CreateRequest)
164
165		w.WriteHeader(http.StatusCreated)
166		fmt.Fprintf(w, GetOutput)
167	})
168}
169
170// HandleDeleteProjectSuccessfully creates an HTTP handler at `/projects` on the
171// test handler mux that tests project deletion.
172func HandleDeleteProjectSuccessfully(t *testing.T) {
173	th.Mux.HandleFunc("/projects/1234", func(w http.ResponseWriter, r *http.Request) {
174		th.TestMethod(t, r, "DELETE")
175		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
176
177		w.WriteHeader(http.StatusNoContent)
178	})
179}
180
181// HandleUpdateProjectSuccessfully creates an HTTP handler at `/projects` on the
182// test handler mux that tests project updates.
183func HandleUpdateProjectSuccessfully(t *testing.T) {
184	th.Mux.HandleFunc("/projects/1234", func(w http.ResponseWriter, r *http.Request) {
185		th.TestMethod(t, r, "PATCH")
186		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
187		th.TestJSONRequest(t, r, UpdateRequest)
188
189		w.WriteHeader(http.StatusOK)
190		fmt.Fprintf(w, UpdateOutput)
191	})
192}
193