Joe Topjian | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 1 | package testing |
| 2 | |
| 3 | import ( |
| 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. |
| 14 | const 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 Topjian | 1c236d3 | 2017-01-09 15:33:32 -0700 | [diff] [blame] | 43 | // GetOutput provides a Get result. |
| 44 | const 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 Topjian | 8ad602c | 2017-01-11 21:01:47 -0700 | [diff] [blame] | 58 | // CreateRequest provides the input to a Create request. |
| 59 | const CreateRequest = ` |
| 60 | { |
| 61 | "project": { |
| 62 | "description": "The team that is red", |
| 63 | "name": "Red Team" |
| 64 | } |
| 65 | } |
| 66 | ` |
| 67 | |
Joe Topjian | 8baf47a | 2017-01-11 21:50:24 -0700 | [diff] [blame] | 68 | // UpdateRequest provides the input to an Update request. |
| 69 | const 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. |
| 79 | const 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 | |
Joe Topjian | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 93 | // RedTeam is a Project fixture. |
| 94 | var 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. |
| 105 | var 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 | |
Joe Topjian | 8baf47a | 2017-01-11 21:50:24 -0700 | [diff] [blame] | 115 | // UpdatedRedTeam is a Project Fixture. |
| 116 | var 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 | |
Joe Topjian | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 126 | // ExpectedProjectSlice is the slice of projects expected to be returned from ListOutput. |
| 127 | var ExpectedProjectSlice = []projects.Project{RedTeam, BlueTeam} |
| 128 | |
Joe Topjian | 1c236d3 | 2017-01-09 15:33:32 -0700 | [diff] [blame] | 129 | // HandleListProjectsSuccessfully creates an HTTP handler at `/projects` on the |
Joe Topjian | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 130 | // test handler mux that responds with a list of two tenants. |
| 131 | func 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 | } |
Joe Topjian | 1c236d3 | 2017-01-09 15:33:32 -0700 | [diff] [blame] | 142 | |
| 143 | // HandleGetProjectSuccessfully creates an HTTP handler at `/projects` on the |
| 144 | // test handler mux that responds with a single project. |
| 145 | func 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 | } |
Joe Topjian | 8ad602c | 2017-01-11 21:01:47 -0700 | [diff] [blame] | 156 | |
| 157 | // HandleCreateProjectSuccessfully creates an HTTP handler at `/projects` on the |
| 158 | // test handler mux that tests project creation. |
| 159 | func 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 | } |
Joe Topjian | d131fb8 | 2017-01-11 21:41:44 -0700 | [diff] [blame] | 169 | |
| 170 | // HandleDeleteProjectSuccessfully creates an HTTP handler at `/projects` on the |
| 171 | // test handler mux that tests project deletion. |
| 172 | func 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 | } |
Joe Topjian | 8baf47a | 2017-01-11 21:50:24 -0700 | [diff] [blame] | 180 | |
| 181 | // HandleUpdateProjectSuccessfully creates an HTTP handler at `/projects` on the |
| 182 | // test handler mux that tests project updates. |
| 183 | func 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 | } |