blob: 0c839b8cfe2696ce95bec8acaef9e2d44515f8bc [file] [log] [blame]
Joe Topjianf61691c2016-11-05 12:34:59 -06001package 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
Joe Topjian1c236d32017-01-09 15:33:32 -070043// 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
Joe Topjian8ad602c2017-01-11 21:01:47 -070058// 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
Joe Topjianf61691c2016-11-05 12:34:59 -060068// RedTeam is a Project fixture.
69var RedTeam = projects.Project{
70 IsDomain: false,
71 Description: "The team that is red",
72 DomainID: "default",
73 Enabled: true,
74 ID: "1234",
75 Name: "Red Team",
76 ParentID: "",
77}
78
79// BlueTeam is a Project fixture.
80var BlueTeam = projects.Project{
81 IsDomain: false,
82 Description: "The team that is blue",
83 DomainID: "default",
84 Enabled: true,
85 ID: "9876",
86 Name: "Blue Team",
87 ParentID: "",
88}
89
90// ExpectedProjectSlice is the slice of projects expected to be returned from ListOutput.
91var ExpectedProjectSlice = []projects.Project{RedTeam, BlueTeam}
92
Joe Topjian1c236d32017-01-09 15:33:32 -070093// HandleListProjectsSuccessfully creates an HTTP handler at `/projects` on the
Joe Topjianf61691c2016-11-05 12:34:59 -060094// test handler mux that responds with a list of two tenants.
95func HandleListProjectsSuccessfully(t *testing.T) {
96 th.Mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
97 th.TestMethod(t, r, "GET")
98 th.TestHeader(t, r, "Accept", "application/json")
99 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
100
101 w.Header().Set("Content-Type", "application/json")
102 w.WriteHeader(http.StatusOK)
103 fmt.Fprintf(w, ListOutput)
104 })
105}
Joe Topjian1c236d32017-01-09 15:33:32 -0700106
107// HandleGetProjectSuccessfully creates an HTTP handler at `/projects` on the
108// test handler mux that responds with a single project.
109func HandleGetProjectSuccessfully(t *testing.T) {
110 th.Mux.HandleFunc("/projects/1234", func(w http.ResponseWriter, r *http.Request) {
111 th.TestMethod(t, r, "GET")
112 th.TestHeader(t, r, "Accept", "application/json")
113 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
114
115 w.Header().Set("Content-Type", "application/json")
116 w.WriteHeader(http.StatusOK)
117 fmt.Fprintf(w, GetOutput)
118 })
119}
Joe Topjian8ad602c2017-01-11 21:01:47 -0700120
121// HandleCreateProjectSuccessfully creates an HTTP handler at `/projects` on the
122// test handler mux that tests project creation.
123func HandleCreateProjectSuccessfully(t *testing.T) {
124 th.Mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
125 th.TestMethod(t, r, "POST")
126 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
127 th.TestJSONRequest(t, r, CreateRequest)
128
129 w.WriteHeader(http.StatusCreated)
130 fmt.Fprintf(w, GetOutput)
131 })
132}