github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/extensions/federation/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/gophercloud/gophercloud/openstack/identity/v3/extensions/federation" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 "github.com/gophercloud/gophercloud/testhelper/client" 11 ) 12 13 const ListOutput = ` 14 { 15 "links": { 16 "next": null, 17 "previous": null, 18 "self": "http://example.com/identity/v3/OS-FEDERATION/mappings" 19 }, 20 "mappings": [ 21 { 22 "id": "ACME", 23 "links": { 24 "self": "http://example.com/identity/v3/OS-FEDERATION/mappings/ACME" 25 }, 26 "rules": [ 27 { 28 "local": [ 29 { 30 "user": { 31 "name": "{0}" 32 } 33 }, 34 { 35 "group": { 36 "id": "0cd5e9" 37 } 38 } 39 ], 40 "remote": [ 41 { 42 "type": "UserName" 43 }, 44 { 45 "type": "orgPersonType", 46 "not_any_of": [ 47 "Contractor", 48 "Guest" 49 ] 50 } 51 ] 52 } 53 ] 54 } 55 ] 56 } 57 ` 58 59 const CreateRequest = ` 60 { 61 "mapping": { 62 "rules": [ 63 { 64 "local": [ 65 { 66 "user": { 67 "name": "{0}" 68 } 69 }, 70 { 71 "group": { 72 "id": "0cd5e9" 73 } 74 } 75 ], 76 "remote": [ 77 { 78 "type": "UserName" 79 }, 80 { 81 "type": "orgPersonType", 82 "not_any_of": [ 83 "Contractor", 84 "Guest" 85 ] 86 } 87 ] 88 } 89 ] 90 } 91 } 92 ` 93 94 const CreateOutput = ` 95 { 96 "mapping": { 97 "id": "ACME", 98 "links": { 99 "self": "http://example.com/identity/v3/OS-FEDERATION/mappings/ACME" 100 }, 101 "rules": [ 102 { 103 "local": [ 104 { 105 "user": { 106 "name": "{0}" 107 } 108 }, 109 { 110 "group": { 111 "id": "0cd5e9" 112 } 113 } 114 ], 115 "remote": [ 116 { 117 "type": "UserName" 118 }, 119 { 120 "type": "orgPersonType", 121 "not_any_of": [ 122 "Contractor", 123 "Guest" 124 ] 125 } 126 ] 127 } 128 ] 129 } 130 } 131 ` 132 133 const GetOutput = CreateOutput 134 135 const UpdateRequest = ` 136 { 137 "mapping": { 138 "rules": [ 139 { 140 "local": [ 141 { 142 "user": { 143 "name": "{0}" 144 } 145 }, 146 { 147 "group": { 148 "id": "0cd5e9" 149 } 150 } 151 ], 152 "remote": [ 153 { 154 "type": "UserName" 155 }, 156 { 157 "type": "orgPersonType", 158 "any_one_of": [ 159 "Contractor", 160 "SubContractor" 161 ] 162 } 163 ] 164 } 165 ] 166 } 167 } 168 ` 169 170 const UpdateOutput = ` 171 { 172 "mapping": { 173 "id": "ACME", 174 "links": { 175 "self": "http://example.com/identity/v3/OS-FEDERATION/mappings/ACME" 176 }, 177 "rules": [ 178 { 179 "local": [ 180 { 181 "user": { 182 "name": "{0}" 183 } 184 }, 185 { 186 "group": { 187 "id": "0cd5e9" 188 } 189 } 190 ], 191 "remote": [ 192 { 193 "type": "UserName" 194 }, 195 { 196 "type": "orgPersonType", 197 "any_one_of": [ 198 "Contractor", 199 "SubContractor" 200 ] 201 } 202 ] 203 } 204 ] 205 } 206 } 207 ` 208 209 var MappingACME = federation.Mapping{ 210 ID: "ACME", 211 Links: map[string]interface{}{ 212 "self": "http://example.com/identity/v3/OS-FEDERATION/mappings/ACME", 213 }, 214 Rules: []federation.MappingRule{ 215 { 216 Local: []federation.RuleLocal{ 217 { 218 User: &federation.RuleUser{ 219 Name: "{0}", 220 }, 221 }, 222 { 223 Group: &federation.Group{ 224 ID: "0cd5e9", 225 }, 226 }, 227 }, 228 Remote: []federation.RuleRemote{ 229 { 230 Type: "UserName", 231 }, 232 { 233 Type: "orgPersonType", 234 NotAnyOf: []string{ 235 "Contractor", 236 "Guest", 237 }, 238 }, 239 }, 240 }, 241 }, 242 } 243 244 var MappingUpdated = federation.Mapping{ 245 ID: "ACME", 246 Links: map[string]interface{}{ 247 "self": "http://example.com/identity/v3/OS-FEDERATION/mappings/ACME", 248 }, 249 Rules: []federation.MappingRule{ 250 { 251 Local: []federation.RuleLocal{ 252 { 253 User: &federation.RuleUser{ 254 Name: "{0}", 255 }, 256 }, 257 { 258 Group: &federation.Group{ 259 ID: "0cd5e9", 260 }, 261 }, 262 }, 263 Remote: []federation.RuleRemote{ 264 { 265 Type: "UserName", 266 }, 267 { 268 Type: "orgPersonType", 269 AnyOneOf: []string{ 270 "Contractor", 271 "SubContractor", 272 }, 273 }, 274 }, 275 }, 276 }, 277 } 278 279 // ExpectedMappingsSlice is the slice of mappings expected to be returned from ListOutput. 280 var ExpectedMappingsSlice = []federation.Mapping{MappingACME} 281 282 // HandleListMappingsSuccessfully creates an HTTP handler at `/mappings` on the 283 // test handler mux that responds with a list of two mappings. 284 func HandleListMappingsSuccessfully(t *testing.T) { 285 th.Mux.HandleFunc("/OS-FEDERATION/mappings", func(w http.ResponseWriter, r *http.Request) { 286 th.TestMethod(t, r, "GET") 287 th.TestHeader(t, r, "Accept", "application/json") 288 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 289 290 w.Header().Set("Content-Type", "application/json") 291 w.WriteHeader(http.StatusOK) 292 fmt.Fprintf(w, ListOutput) 293 }) 294 } 295 296 // HandleCreateMappingSuccessfully creates an HTTP handler at `/mappings` on the 297 // test handler mux that tests mapping creation. 298 func HandleCreateMappingSuccessfully(t *testing.T) { 299 th.Mux.HandleFunc("/OS-FEDERATION/mappings/ACME", func(w http.ResponseWriter, r *http.Request) { 300 th.TestMethod(t, r, "PUT") 301 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 302 th.TestJSONRequest(t, r, CreateRequest) 303 304 w.WriteHeader(http.StatusCreated) 305 fmt.Fprintf(w, CreateOutput) 306 }) 307 } 308 309 // HandleGetMappingSuccessfully creates an HTTP handler at `/mappings` on the 310 // test handler mux that responds with a single mapping. 311 func HandleGetMappingSuccessfully(t *testing.T) { 312 th.Mux.HandleFunc("/OS-FEDERATION/mappings/ACME", func(w http.ResponseWriter, r *http.Request) { 313 th.TestMethod(t, r, "GET") 314 th.TestHeader(t, r, "Accept", "application/json") 315 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 316 317 w.Header().Set("Content-Type", "application/json") 318 w.WriteHeader(http.StatusOK) 319 fmt.Fprintf(w, GetOutput) 320 }) 321 } 322 323 // HandleUpdateMappingSuccessfully creates an HTTP handler at `/mappings` on the 324 // test handler mux that tests mapping update. 325 func HandleUpdateMappingSuccessfully(t *testing.T) { 326 th.Mux.HandleFunc("/OS-FEDERATION/mappings/ACME", func(w http.ResponseWriter, r *http.Request) { 327 th.TestMethod(t, r, "PATCH") 328 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 329 th.TestJSONRequest(t, r, UpdateRequest) 330 331 w.WriteHeader(http.StatusOK) 332 fmt.Fprintf(w, UpdateOutput) 333 }) 334 } 335 336 // HandleDeleteMappingSuccessfully creates an HTTP handler at `/mappings` on the 337 // test handler mux that tests mapping deletion. 338 func HandleDeleteMappingSuccessfully(t *testing.T) { 339 th.Mux.HandleFunc("/OS-FEDERATION/mappings/ACME", func(w http.ResponseWriter, r *http.Request) { 340 th.TestMethod(t, r, "DELETE") 341 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 342 343 w.WriteHeader(http.StatusNoContent) 344 }) 345 }