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