| Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 1 | package tokens | 
 | 2 |  | 
 | 3 | import ( | 
 | 4 | 	"fmt" | 
 | 5 | 	"net/http" | 
 | 6 | 	"testing" | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 7 | 	"time" | 
| Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 8 |  | 
 | 9 | 	"github.com/rackspace/gophercloud" | 
 | 10 | 	"github.com/rackspace/gophercloud/testhelper" | 
 | 11 | ) | 
 | 12 |  | 
| Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 13 | // authTokenPost verifies that providing certain AuthOptions and Scope results in an expected JSON structure. | 
 | 14 | func authTokenPost(t *testing.T, options gophercloud.AuthOptions, scope *Scope, requestJSON string) { | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 15 | 	testhelper.SetupHTTP() | 
 | 16 | 	defer testhelper.TeardownHTTP() | 
| Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 17 |  | 
| Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 18 | 	client := gophercloud.ServiceClient{ | 
| Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 19 | 		Provider: &gophercloud.ProviderClient{ | 
| Ash Wilson | 49f0f56 | 2014-09-03 08:58:20 -0400 | [diff] [blame] | 20 | 			TokenID: "12345abcdef", | 
| Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 21 | 		}, | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 22 | 		Endpoint: testhelper.Endpoint(), | 
| Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 23 | 	} | 
 | 24 |  | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 25 | 	testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { | 
| Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 26 | 		testhelper.TestMethod(t, r, "POST") | 
 | 27 | 		testhelper.TestHeader(t, r, "Content-Type", "application/json") | 
 | 28 | 		testhelper.TestHeader(t, r, "Accept", "application/json") | 
| Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 29 | 		testhelper.TestJSONRequest(t, r, requestJSON) | 
| Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 30 |  | 
| Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 31 | 		w.WriteHeader(http.StatusCreated) | 
| Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 32 | 		fmt.Fprintf(w, `{}`) | 
 | 33 | 	}) | 
 | 34 |  | 
| Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame^] | 35 | 	_, err := Create(&client, options, scope).Extract() | 
| Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 36 | 	if err != nil { | 
 | 37 | 		t.Errorf("Create returned an error: %v", err) | 
 | 38 | 	} | 
 | 39 | } | 
| Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 40 |  | 
| Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 41 | func authTokenPostErr(t *testing.T, options gophercloud.AuthOptions, scope *Scope, includeToken bool, expectedErr error) { | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 42 | 	testhelper.SetupHTTP() | 
 | 43 | 	defer testhelper.TeardownHTTP() | 
| Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 44 |  | 
| Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 45 | 	client := gophercloud.ServiceClient{ | 
| Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 46 | 		Provider: &gophercloud.ProviderClient{}, | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 47 | 		Endpoint: testhelper.Endpoint(), | 
| Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 48 | 	} | 
 | 49 | 	if includeToken { | 
| Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 50 | 		client.Provider.TokenID = "abcdef123456" | 
| Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 51 | 	} | 
 | 52 |  | 
| Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame^] | 53 | 	_, err := Create(&client, options, scope).Extract() | 
| Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 54 | 	if err == nil { | 
 | 55 | 		t.Errorf("Create did NOT return an error") | 
 | 56 | 	} | 
 | 57 | 	if err != expectedErr { | 
 | 58 | 		t.Errorf("Create returned an unexpected error: wanted %v, got %v", expectedErr, err) | 
 | 59 | 	} | 
 | 60 | } | 
 | 61 |  | 
| Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 62 | func TestCreateUserIDAndPassword(t *testing.T) { | 
 | 63 | 	authTokenPost(t, gophercloud.AuthOptions{UserID: "me", Password: "squirrel!"}, nil, ` | 
 | 64 | 		{ | 
 | 65 | 			"auth": { | 
 | 66 | 				"identity": { | 
 | 67 | 					"methods": ["password"], | 
 | 68 | 					"password": { | 
 | 69 | 						"user": { "id": "me", "password": "squirrel!" } | 
 | 70 | 					} | 
 | 71 | 				} | 
 | 72 | 			} | 
 | 73 | 		} | 
 | 74 | 	`) | 
 | 75 | } | 
 | 76 |  | 
 | 77 | func TestCreateUsernameDomainIDPassword(t *testing.T) { | 
 | 78 | 	authTokenPost(t, gophercloud.AuthOptions{Username: "fakey", Password: "notpassword", DomainID: "abc123"}, nil, ` | 
 | 79 | 		{ | 
 | 80 | 			"auth": { | 
 | 81 | 				"identity": { | 
 | 82 | 					"methods": ["password"], | 
 | 83 | 					"password": { | 
 | 84 | 						"user": { | 
 | 85 | 							"domain": { | 
 | 86 | 								"id": "abc123" | 
 | 87 | 							}, | 
 | 88 | 							"name": "fakey", | 
 | 89 | 							"password": "notpassword" | 
 | 90 | 						} | 
 | 91 | 					} | 
 | 92 | 				} | 
 | 93 | 			} | 
 | 94 | 		} | 
 | 95 | 	`) | 
 | 96 | } | 
| Ash Wilson | d8da9e4 | 2014-08-29 08:01:06 -0400 | [diff] [blame] | 97 |  | 
 | 98 | func TestCreateUsernameDomainNamePassword(t *testing.T) { | 
 | 99 | 	authTokenPost(t, gophercloud.AuthOptions{Username: "frank", Password: "swordfish", DomainName: "spork.net"}, nil, ` | 
 | 100 | 		{ | 
 | 101 | 			"auth": { | 
 | 102 | 				"identity": { | 
 | 103 | 					"methods": ["password"], | 
 | 104 | 					"password": { | 
 | 105 | 						"user": { | 
 | 106 | 							"domain": { | 
 | 107 | 								"name": "spork.net" | 
 | 108 | 							}, | 
 | 109 | 							"name": "frank", | 
 | 110 | 							"password": "swordfish" | 
 | 111 | 						} | 
 | 112 | 					} | 
 | 113 | 				} | 
 | 114 | 			} | 
 | 115 | 		} | 
 | 116 | 	`) | 
 | 117 | } | 
| Ash Wilson | 053fcb0 | 2014-08-29 08:04:35 -0400 | [diff] [blame] | 118 |  | 
 | 119 | func TestCreateTokenID(t *testing.T) { | 
 | 120 | 	authTokenPost(t, gophercloud.AuthOptions{}, nil, ` | 
 | 121 | 		{ | 
 | 122 | 			"auth": { | 
 | 123 | 				"identity": { | 
 | 124 | 					"methods": ["token"], | 
 | 125 | 					"token": { | 
 | 126 | 						"id": "12345abcdef" | 
 | 127 | 					} | 
 | 128 | 				} | 
 | 129 | 			} | 
 | 130 | 		} | 
 | 131 | 	`) | 
 | 132 | } | 
| Ash Wilson | 1fde616 | 2014-08-29 08:13:06 -0400 | [diff] [blame] | 133 |  | 
 | 134 | func TestCreateProjectIDScope(t *testing.T) { | 
 | 135 | 	options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} | 
 | 136 | 	scope := &Scope{ProjectID: "123456"} | 
 | 137 | 	authTokenPost(t, options, scope, ` | 
 | 138 | 		{ | 
 | 139 | 			"auth": { | 
 | 140 | 				"identity": { | 
 | 141 | 					"methods": ["password"], | 
 | 142 | 					"password": { | 
 | 143 | 						"user": { | 
 | 144 | 							"id": "fenris", | 
 | 145 | 							"password": "g0t0h311" | 
 | 146 | 						} | 
 | 147 | 					} | 
 | 148 | 				}, | 
 | 149 | 				"scope": { | 
 | 150 | 					"project": { | 
 | 151 | 						"id": "123456" | 
 | 152 | 					} | 
 | 153 | 				} | 
 | 154 | 			} | 
 | 155 | 		} | 
 | 156 | 	`) | 
 | 157 | } | 
 | 158 |  | 
 | 159 | func TestCreateDomainIDScope(t *testing.T) { | 
 | 160 | 	options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} | 
 | 161 | 	scope := &Scope{DomainID: "1000"} | 
 | 162 | 	authTokenPost(t, options, scope, ` | 
 | 163 | 		{ | 
 | 164 | 			"auth": { | 
 | 165 | 				"identity": { | 
 | 166 | 					"methods": ["password"], | 
 | 167 | 					"password": { | 
 | 168 | 						"user": { | 
 | 169 | 							"id": "fenris", | 
 | 170 | 							"password": "g0t0h311" | 
 | 171 | 						} | 
 | 172 | 					} | 
 | 173 | 				}, | 
 | 174 | 				"scope": { | 
 | 175 | 					"domain": { | 
 | 176 | 						"id": "1000" | 
 | 177 | 					} | 
 | 178 | 				} | 
 | 179 | 			} | 
 | 180 | 		} | 
 | 181 | 	`) | 
 | 182 | } | 
 | 183 |  | 
 | 184 | func TestCreateProjectNameAndDomainIDScope(t *testing.T) { | 
 | 185 | 	options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} | 
 | 186 | 	scope := &Scope{ProjectName: "world-domination", DomainID: "1000"} | 
 | 187 | 	authTokenPost(t, options, scope, ` | 
 | 188 | 		{ | 
 | 189 | 			"auth": { | 
 | 190 | 				"identity": { | 
 | 191 | 					"methods": ["password"], | 
 | 192 | 					"password": { | 
 | 193 | 						"user": { | 
 | 194 | 							"id": "fenris", | 
 | 195 | 							"password": "g0t0h311" | 
 | 196 | 						} | 
 | 197 | 					} | 
 | 198 | 				}, | 
 | 199 | 				"scope": { | 
 | 200 | 					"project": { | 
 | 201 | 						"domain": { | 
 | 202 | 							"id": "1000" | 
 | 203 | 						}, | 
 | 204 | 						"name": "world-domination" | 
 | 205 | 					} | 
 | 206 | 				} | 
 | 207 | 			} | 
 | 208 | 		} | 
 | 209 | 	`) | 
 | 210 | } | 
 | 211 |  | 
 | 212 | func TestCreateProjectNameAndDomainNameScope(t *testing.T) { | 
 | 213 | 	options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} | 
 | 214 | 	scope := &Scope{ProjectName: "world-domination", DomainName: "evil-plans"} | 
 | 215 | 	authTokenPost(t, options, scope, ` | 
 | 216 | 		{ | 
 | 217 | 			"auth": { | 
 | 218 | 				"identity": { | 
 | 219 | 					"methods": ["password"], | 
 | 220 | 					"password": { | 
 | 221 | 						"user": { | 
 | 222 | 							"id": "fenris", | 
 | 223 | 							"password": "g0t0h311" | 
 | 224 | 						} | 
 | 225 | 					} | 
 | 226 | 				}, | 
 | 227 | 				"scope": { | 
 | 228 | 					"project": { | 
 | 229 | 						"domain": { | 
 | 230 | 							"name": "evil-plans" | 
 | 231 | 						}, | 
 | 232 | 						"name": "world-domination" | 
 | 233 | 					} | 
 | 234 | 				} | 
 | 235 | 			} | 
 | 236 | 		} | 
 | 237 | 	`) | 
 | 238 | } | 
| Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 239 |  | 
| Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 240 | func TestCreateExtractsTokenFromResponse(t *testing.T) { | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 241 | 	testhelper.SetupHTTP() | 
 | 242 | 	defer testhelper.TeardownHTTP() | 
| Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 243 |  | 
| Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 244 | 	client := gophercloud.ServiceClient{ | 
| Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 245 | 		Provider: &gophercloud.ProviderClient{}, | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 246 | 		Endpoint: testhelper.Endpoint(), | 
| Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 247 | 	} | 
 | 248 |  | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 249 | 	testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { | 
| Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 250 | 		w.Header().Add("X-Subject-Token", "aaa111") | 
 | 251 |  | 
 | 252 | 		w.WriteHeader(http.StatusCreated) | 
 | 253 | 		fmt.Fprintf(w, `{}`) | 
 | 254 | 	}) | 
 | 255 |  | 
| Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 256 | 	options := gophercloud.AuthOptions{UserID: "me", Password: "shhh"} | 
| Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame^] | 257 | 	token, err := Create(&client, options, nil).Extract() | 
| Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 258 | 	if err != nil { | 
 | 259 | 		t.Errorf("Create returned an error: %v", err) | 
 | 260 | 	} | 
 | 261 |  | 
| Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame^] | 262 | 	if token.ID != "aaa111" { | 
 | 263 | 		t.Errorf("Expected token to be aaa111, but was %s", token.ID) | 
| Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 264 | 	} | 
 | 265 | } | 
 | 266 |  | 
| Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame] | 267 | func TestCreateFailureEmptyAuth(t *testing.T) { | 
 | 268 | 	authTokenPostErr(t, gophercloud.AuthOptions{}, nil, false, ErrMissingPassword) | 
 | 269 | } | 
 | 270 |  | 
 | 271 | func TestCreateFailureAPIKey(t *testing.T) { | 
 | 272 | 	authTokenPostErr(t, gophercloud.AuthOptions{APIKey: "something"}, nil, false, ErrAPIKeyProvided) | 
 | 273 | } | 
 | 274 |  | 
 | 275 | func TestCreateFailureTenantID(t *testing.T) { | 
 | 276 | 	authTokenPostErr(t, gophercloud.AuthOptions{TenantID: "something"}, nil, false, ErrTenantIDProvided) | 
 | 277 | } | 
 | 278 |  | 
 | 279 | func TestCreateFailureTenantName(t *testing.T) { | 
 | 280 | 	authTokenPostErr(t, gophercloud.AuthOptions{TenantName: "something"}, nil, false, ErrTenantNameProvided) | 
 | 281 | } | 
 | 282 |  | 
 | 283 | func TestCreateFailureTokenIDUsername(t *testing.T) { | 
 | 284 | 	authTokenPostErr(t, gophercloud.AuthOptions{Username: "something"}, nil, true, ErrUsernameWithToken) | 
 | 285 | } | 
 | 286 |  | 
 | 287 | func TestCreateFailureTokenIDUserID(t *testing.T) { | 
 | 288 | 	authTokenPostErr(t, gophercloud.AuthOptions{UserID: "something"}, nil, true, ErrUserIDWithToken) | 
 | 289 | } | 
 | 290 |  | 
 | 291 | func TestCreateFailureTokenIDDomainID(t *testing.T) { | 
 | 292 | 	authTokenPostErr(t, gophercloud.AuthOptions{DomainID: "something"}, nil, true, ErrDomainIDWithToken) | 
 | 293 | } | 
 | 294 |  | 
 | 295 | func TestCreateFailureTokenIDDomainName(t *testing.T) { | 
 | 296 | 	authTokenPostErr(t, gophercloud.AuthOptions{DomainName: "something"}, nil, true, ErrDomainNameWithToken) | 
 | 297 | } | 
| Ash Wilson | aed3db4 | 2014-08-29 08:59:56 -0400 | [diff] [blame] | 298 |  | 
 | 299 | func TestCreateFailureMissingUser(t *testing.T) { | 
 | 300 | 	options := gophercloud.AuthOptions{Password: "supersecure"} | 
 | 301 | 	authTokenPostErr(t, options, nil, false, ErrUsernameOrUserID) | 
 | 302 | } | 
 | 303 |  | 
 | 304 | func TestCreateFailureBothUser(t *testing.T) { | 
 | 305 | 	options := gophercloud.AuthOptions{ | 
 | 306 | 		Password: "supersecure", | 
 | 307 | 		Username: "oops", | 
 | 308 | 		UserID:   "redundancy", | 
 | 309 | 	} | 
 | 310 | 	authTokenPostErr(t, options, nil, false, ErrUsernameOrUserID) | 
 | 311 | } | 
 | 312 |  | 
 | 313 | func TestCreateFailureMissingDomain(t *testing.T) { | 
 | 314 | 	options := gophercloud.AuthOptions{ | 
 | 315 | 		Password: "supersecure", | 
 | 316 | 		Username: "notuniqueenough", | 
 | 317 | 	} | 
 | 318 | 	authTokenPostErr(t, options, nil, false, ErrDomainIDOrDomainName) | 
 | 319 | } | 
 | 320 |  | 
 | 321 | func TestCreateFailureBothDomain(t *testing.T) { | 
 | 322 | 	options := gophercloud.AuthOptions{ | 
 | 323 | 		Password:   "supersecure", | 
 | 324 | 		Username:   "someone", | 
 | 325 | 		DomainID:   "hurf", | 
 | 326 | 		DomainName: "durf", | 
 | 327 | 	} | 
 | 328 | 	authTokenPostErr(t, options, nil, false, ErrDomainIDOrDomainName) | 
 | 329 | } | 
 | 330 |  | 
 | 331 | func TestCreateFailureUserIDDomainID(t *testing.T) { | 
 | 332 | 	options := gophercloud.AuthOptions{ | 
 | 333 | 		UserID:   "100", | 
 | 334 | 		Password: "stuff", | 
 | 335 | 		DomainID: "oops", | 
 | 336 | 	} | 
 | 337 | 	authTokenPostErr(t, options, nil, false, ErrDomainIDWithUserID) | 
 | 338 | } | 
 | 339 |  | 
 | 340 | func TestCreateFailureUserIDDomainName(t *testing.T) { | 
 | 341 | 	options := gophercloud.AuthOptions{ | 
 | 342 | 		UserID:     "100", | 
 | 343 | 		Password:   "sssh", | 
 | 344 | 		DomainName: "oops", | 
 | 345 | 	} | 
 | 346 | 	authTokenPostErr(t, options, nil, false, ErrDomainNameWithUserID) | 
 | 347 | } | 
 | 348 |  | 
 | 349 | func TestCreateFailureScopeProjectNameAlone(t *testing.T) { | 
 | 350 | 	options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} | 
 | 351 | 	scope := &Scope{ProjectName: "notenough"} | 
 | 352 | 	authTokenPostErr(t, options, scope, false, ErrScopeDomainIDOrDomainName) | 
 | 353 | } | 
 | 354 |  | 
 | 355 | func TestCreateFailureScopeProjectNameAndID(t *testing.T) { | 
 | 356 | 	options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} | 
 | 357 | 	scope := &Scope{ProjectName: "whoops", ProjectID: "toomuch", DomainID: "1234"} | 
 | 358 | 	authTokenPostErr(t, options, scope, false, ErrScopeProjectIDOrProjectName) | 
 | 359 | } | 
 | 360 |  | 
 | 361 | func TestCreateFailureScopeProjectIDAndDomainID(t *testing.T) { | 
 | 362 | 	options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} | 
 | 363 | 	scope := &Scope{ProjectID: "toomuch", DomainID: "notneeded"} | 
 | 364 | 	authTokenPostErr(t, options, scope, false, ErrScopeProjectIDAlone) | 
 | 365 | } | 
 | 366 |  | 
 | 367 | func TestCreateFailureScopeProjectIDAndDomainNAme(t *testing.T) { | 
 | 368 | 	options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} | 
 | 369 | 	scope := &Scope{ProjectID: "toomuch", DomainName: "notneeded"} | 
 | 370 | 	authTokenPostErr(t, options, scope, false, ErrScopeProjectIDAlone) | 
 | 371 | } | 
 | 372 |  | 
 | 373 | func TestCreateFailureScopeDomainIDAndDomainName(t *testing.T) { | 
 | 374 | 	options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} | 
 | 375 | 	scope := &Scope{DomainID: "toomuch", DomainName: "notneeded"} | 
 | 376 | 	authTokenPostErr(t, options, scope, false, ErrScopeDomainIDOrDomainName) | 
 | 377 | } | 
 | 378 |  | 
 | 379 | func TestCreateFailureScopeDomainNameAlone(t *testing.T) { | 
 | 380 | 	options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} | 
 | 381 | 	scope := &Scope{DomainName: "notenough"} | 
 | 382 | 	authTokenPostErr(t, options, scope, false, ErrScopeDomainName) | 
 | 383 | } | 
 | 384 |  | 
 | 385 | func TestCreateFailureEmptyScope(t *testing.T) { | 
 | 386 | 	options := gophercloud.AuthOptions{UserID: "myself", Password: "swordfish"} | 
 | 387 | 	scope := &Scope{} | 
 | 388 | 	authTokenPostErr(t, options, scope, false, ErrScopeEmpty) | 
 | 389 | } | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 390 |  | 
| Ash Wilson | 5266e49 | 2014-09-09 15:44:30 -0400 | [diff] [blame] | 391 | func TestGetRequest(t *testing.T) { | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 392 | 	testhelper.SetupHTTP() | 
 | 393 | 	defer testhelper.TeardownHTTP() | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 394 |  | 
| Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 395 | 	client := gophercloud.ServiceClient{ | 
| Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 396 | 		Provider: &gophercloud.ProviderClient{ | 
| Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 397 | 			TokenID: "12345abcdef", | 
 | 398 | 		}, | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 399 | 		Endpoint: testhelper.Endpoint(), | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 400 | 	} | 
 | 401 |  | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 402 | 	testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 403 | 		testhelper.TestMethod(t, r, "GET") | 
| Jon Perritt | d8aef1b | 2014-09-11 17:50:04 -0500 | [diff] [blame] | 404 | 		testhelper.TestHeader(t, r, "Content-Type", "") | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 405 | 		testhelper.TestHeader(t, r, "Accept", "application/json") | 
 | 406 | 		testhelper.TestHeader(t, r, "X-Auth-Token", "12345abcdef") | 
 | 407 | 		testhelper.TestHeader(t, r, "X-Subject-Token", "abcdef12345") | 
 | 408 |  | 
 | 409 | 		w.WriteHeader(http.StatusOK) | 
 | 410 | 		fmt.Fprintf(w, ` | 
 | 411 | 			{ "token": { "expires_at": "2014-08-29T13:10:01.000000Z" } } | 
 | 412 | 		`) | 
 | 413 | 	}) | 
 | 414 |  | 
| Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame^] | 415 | 	token, err := Get(&client, "abcdef12345").Extract() | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 416 | 	if err != nil { | 
 | 417 | 		t.Errorf("Info returned an error: %v", err) | 
 | 418 | 	} | 
 | 419 |  | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 420 | 	expected, _ := time.Parse(time.UnixDate, "Fri Aug 29 13:10:01 UTC 2014") | 
| Ash Wilson | 3f59ade | 2014-10-02 09:22:23 -0400 | [diff] [blame^] | 421 | 	if token.ExpiresAt != expected { | 
 | 422 | 		t.Errorf("Expected expiration time %s, but was %s", expected.Format(time.UnixDate), token.ExpiresAt.Format(time.UnixDate)) | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 423 | 	} | 
 | 424 | } | 
 | 425 |  | 
| Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 426 | func prepareAuthTokenHandler(t *testing.T, expectedMethod string, status int) gophercloud.ServiceClient { | 
 | 427 | 	client := gophercloud.ServiceClient{ | 
| Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 428 | 		Provider: &gophercloud.ProviderClient{ | 
| Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 429 | 			TokenID: "12345abcdef", | 
 | 430 | 		}, | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 431 | 		Endpoint: testhelper.Endpoint(), | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 432 | 	} | 
 | 433 |  | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 434 | 	testhelper.Mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 435 | 		testhelper.TestMethod(t, r, expectedMethod) | 
| Jon Perritt | d8aef1b | 2014-09-11 17:50:04 -0500 | [diff] [blame] | 436 | 		testhelper.TestHeader(t, r, "Content-Type", "") | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 437 | 		testhelper.TestHeader(t, r, "Accept", "application/json") | 
 | 438 | 		testhelper.TestHeader(t, r, "X-Auth-Token", "12345abcdef") | 
 | 439 | 		testhelper.TestHeader(t, r, "X-Subject-Token", "abcdef12345") | 
 | 440 |  | 
 | 441 | 		w.WriteHeader(status) | 
 | 442 | 	}) | 
 | 443 |  | 
 | 444 | 	return client | 
 | 445 | } | 
 | 446 |  | 
 | 447 | func TestValidateRequestSuccessful(t *testing.T) { | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 448 | 	testhelper.SetupHTTP() | 
 | 449 | 	defer testhelper.TeardownHTTP() | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 450 | 	client := prepareAuthTokenHandler(t, "HEAD", http.StatusNoContent) | 
 | 451 |  | 
 | 452 | 	ok, err := Validate(&client, "abcdef12345") | 
 | 453 | 	if err != nil { | 
 | 454 | 		t.Errorf("Unexpected error from Validate: %v", err) | 
 | 455 | 	} | 
 | 456 |  | 
 | 457 | 	if !ok { | 
 | 458 | 		t.Errorf("Validate returned false for a valid token") | 
 | 459 | 	} | 
 | 460 | } | 
 | 461 |  | 
 | 462 | func TestValidateRequestFailure(t *testing.T) { | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 463 | 	testhelper.SetupHTTP() | 
 | 464 | 	defer testhelper.TeardownHTTP() | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 465 | 	client := prepareAuthTokenHandler(t, "HEAD", http.StatusNotFound) | 
 | 466 |  | 
 | 467 | 	ok, err := Validate(&client, "abcdef12345") | 
 | 468 | 	if err != nil { | 
 | 469 | 		t.Errorf("Unexpected error from Validate: %v", err) | 
 | 470 | 	} | 
 | 471 |  | 
 | 472 | 	if ok { | 
 | 473 | 		t.Errorf("Validate returned true for an invalid token") | 
 | 474 | 	} | 
 | 475 | } | 
 | 476 |  | 
 | 477 | func TestValidateRequestError(t *testing.T) { | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 478 | 	testhelper.SetupHTTP() | 
 | 479 | 	defer testhelper.TeardownHTTP() | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 480 | 	client := prepareAuthTokenHandler(t, "HEAD", http.StatusUnauthorized) | 
 | 481 |  | 
 | 482 | 	_, err := Validate(&client, "abcdef12345") | 
 | 483 | 	if err == nil { | 
 | 484 | 		t.Errorf("Missing expected error from Validate") | 
 | 485 | 	} | 
 | 486 | } | 
 | 487 |  | 
 | 488 | func TestRevokeRequestSuccessful(t *testing.T) { | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 489 | 	testhelper.SetupHTTP() | 
 | 490 | 	defer testhelper.TeardownHTTP() | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 491 | 	client := prepareAuthTokenHandler(t, "DELETE", http.StatusNoContent) | 
 | 492 |  | 
 | 493 | 	err := Revoke(&client, "abcdef12345") | 
 | 494 | 	if err != nil { | 
 | 495 | 		t.Errorf("Unexpected error from Revoke: %v", err) | 
 | 496 | 	} | 
 | 497 | } | 
 | 498 |  | 
 | 499 | func TestRevokeRequestError(t *testing.T) { | 
| Ash Wilson | 0ab4d61 | 2014-08-29 11:10:13 -0400 | [diff] [blame] | 500 | 	testhelper.SetupHTTP() | 
 | 501 | 	defer testhelper.TeardownHTTP() | 
| Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 502 | 	client := prepareAuthTokenHandler(t, "DELETE", http.StatusNotFound) | 
 | 503 |  | 
 | 504 | 	err := Revoke(&client, "abcdef12345") | 
 | 505 | 	if err == nil { | 
 | 506 | 		t.Errorf("Missing expected error from Revoke") | 
 | 507 | 	} | 
 | 508 | } |