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 | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 58 | // RedTeam is a Project fixture. |
| 59 | var RedTeam = projects.Project{ |
| 60 | IsDomain: false, |
| 61 | Description: "The team that is red", |
| 62 | DomainID: "default", |
| 63 | Enabled: true, |
| 64 | ID: "1234", |
| 65 | Name: "Red Team", |
| 66 | ParentID: "", |
| 67 | } |
| 68 | |
| 69 | // BlueTeam is a Project fixture. |
| 70 | var BlueTeam = projects.Project{ |
| 71 | IsDomain: false, |
| 72 | Description: "The team that is blue", |
| 73 | DomainID: "default", |
| 74 | Enabled: true, |
| 75 | ID: "9876", |
| 76 | Name: "Blue Team", |
| 77 | ParentID: "", |
| 78 | } |
| 79 | |
| 80 | // ExpectedProjectSlice is the slice of projects expected to be returned from ListOutput. |
| 81 | var ExpectedProjectSlice = []projects.Project{RedTeam, BlueTeam} |
| 82 | |
Joe Topjian | 1c236d3 | 2017-01-09 15:33:32 -0700 | [diff] [blame^] | 83 | // HandleListProjectsSuccessfully creates an HTTP handler at `/projects` on the |
Joe Topjian | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 84 | // test handler mux that responds with a list of two tenants. |
| 85 | func HandleListProjectsSuccessfully(t *testing.T) { |
| 86 | th.Mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) { |
| 87 | th.TestMethod(t, r, "GET") |
| 88 | th.TestHeader(t, r, "Accept", "application/json") |
| 89 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 90 | |
| 91 | w.Header().Set("Content-Type", "application/json") |
| 92 | w.WriteHeader(http.StatusOK) |
| 93 | fmt.Fprintf(w, ListOutput) |
| 94 | }) |
| 95 | } |
Joe Topjian | 1c236d3 | 2017-01-09 15:33:32 -0700 | [diff] [blame^] | 96 | |
| 97 | // HandleGetProjectSuccessfully creates an HTTP handler at `/projects` on the |
| 98 | // test handler mux that responds with a single project. |
| 99 | func HandleGetProjectSuccessfully(t *testing.T) { |
| 100 | th.Mux.HandleFunc("/projects/1234", func(w http.ResponseWriter, r *http.Request) { |
| 101 | th.TestMethod(t, r, "GET") |
| 102 | th.TestHeader(t, r, "Accept", "application/json") |
| 103 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 104 | |
| 105 | w.Header().Set("Content-Type", "application/json") |
| 106 | w.WriteHeader(http.StatusOK) |
| 107 | fmt.Fprintf(w, GetOutput) |
| 108 | }) |
| 109 | } |