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" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/testhelper" |
| 10 | ) |
| 11 | |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 12 | // authTokenPost verifies that providing certain AuthOptions and Scope results in an expected JSON structure. |
| 13 | func authTokenPost(t *testing.T, options gophercloud.AuthOptions, scope *Scope, requestJSON string) { |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 14 | setup() |
| 15 | defer teardown() |
| 16 | |
| 17 | client := gophercloud.ServiceClient{ |
| 18 | Endpoint: endpoint(), |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 19 | Options: options, |
Ash Wilson | 053fcb0 | 2014-08-29 08:04:35 -0400 | [diff] [blame] | 20 | TokenID: "12345abcdef", |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | mux.HandleFunc("/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
| 24 | testhelper.TestMethod(t, r, "POST") |
| 25 | testhelper.TestHeader(t, r, "Content-Type", "application/json") |
| 26 | testhelper.TestHeader(t, r, "Accept", "application/json") |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 27 | testhelper.TestJSONRequest(t, r, requestJSON) |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 28 | |
| 29 | fmt.Fprintf(w, `{}`) |
| 30 | }) |
| 31 | |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 32 | _, err := Create(&client, scope) |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 33 | if err != nil { |
| 34 | t.Errorf("Create returned an error: %v", err) |
| 35 | } |
| 36 | } |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 37 | |
Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame^] | 38 | func authTokenPostErr(t *testing.T, options gophercloud.AuthOptions, scope *Scope, includeToken bool, expectedErr error) { |
| 39 | setup() |
| 40 | defer teardown() |
| 41 | |
| 42 | client := gophercloud.ServiceClient{ |
| 43 | Endpoint: endpoint(), |
| 44 | Options: options, |
| 45 | } |
| 46 | if includeToken { |
| 47 | client.TokenID = "abcdef123456" |
| 48 | } |
| 49 | |
| 50 | _, err := Create(&client, scope) |
| 51 | if err == nil { |
| 52 | t.Errorf("Create did NOT return an error") |
| 53 | } |
| 54 | if err != expectedErr { |
| 55 | t.Errorf("Create returned an unexpected error: wanted %v, got %v", expectedErr, err) |
| 56 | } |
| 57 | } |
| 58 | |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 59 | func TestCreateUserIDAndPassword(t *testing.T) { |
| 60 | authTokenPost(t, gophercloud.AuthOptions{UserID: "me", Password: "squirrel!"}, nil, ` |
| 61 | { |
| 62 | "auth": { |
| 63 | "identity": { |
| 64 | "methods": ["password"], |
| 65 | "password": { |
| 66 | "user": { "id": "me", "password": "squirrel!" } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | `) |
| 72 | } |
| 73 | |
| 74 | func TestCreateUsernameDomainIDPassword(t *testing.T) { |
| 75 | authTokenPost(t, gophercloud.AuthOptions{Username: "fakey", Password: "notpassword", DomainID: "abc123"}, nil, ` |
| 76 | { |
| 77 | "auth": { |
| 78 | "identity": { |
| 79 | "methods": ["password"], |
| 80 | "password": { |
| 81 | "user": { |
| 82 | "domain": { |
| 83 | "id": "abc123" |
| 84 | }, |
| 85 | "name": "fakey", |
| 86 | "password": "notpassword" |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | `) |
| 93 | } |
Ash Wilson | d8da9e4 | 2014-08-29 08:01:06 -0400 | [diff] [blame] | 94 | |
| 95 | func TestCreateUsernameDomainNamePassword(t *testing.T) { |
| 96 | authTokenPost(t, gophercloud.AuthOptions{Username: "frank", Password: "swordfish", DomainName: "spork.net"}, nil, ` |
| 97 | { |
| 98 | "auth": { |
| 99 | "identity": { |
| 100 | "methods": ["password"], |
| 101 | "password": { |
| 102 | "user": { |
| 103 | "domain": { |
| 104 | "name": "spork.net" |
| 105 | }, |
| 106 | "name": "frank", |
| 107 | "password": "swordfish" |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | `) |
| 114 | } |
Ash Wilson | 053fcb0 | 2014-08-29 08:04:35 -0400 | [diff] [blame] | 115 | |
| 116 | func TestCreateTokenID(t *testing.T) { |
| 117 | authTokenPost(t, gophercloud.AuthOptions{}, nil, ` |
| 118 | { |
| 119 | "auth": { |
| 120 | "identity": { |
| 121 | "methods": ["token"], |
| 122 | "token": { |
| 123 | "id": "12345abcdef" |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | `) |
| 129 | } |
Ash Wilson | 1fde616 | 2014-08-29 08:13:06 -0400 | [diff] [blame] | 130 | |
| 131 | func TestCreateProjectIDScope(t *testing.T) { |
| 132 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 133 | scope := &Scope{ProjectID: "123456"} |
| 134 | authTokenPost(t, options, scope, ` |
| 135 | { |
| 136 | "auth": { |
| 137 | "identity": { |
| 138 | "methods": ["password"], |
| 139 | "password": { |
| 140 | "user": { |
| 141 | "id": "fenris", |
| 142 | "password": "g0t0h311" |
| 143 | } |
| 144 | } |
| 145 | }, |
| 146 | "scope": { |
| 147 | "project": { |
| 148 | "id": "123456" |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | `) |
| 154 | } |
| 155 | |
| 156 | func TestCreateDomainIDScope(t *testing.T) { |
| 157 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 158 | scope := &Scope{DomainID: "1000"} |
| 159 | authTokenPost(t, options, scope, ` |
| 160 | { |
| 161 | "auth": { |
| 162 | "identity": { |
| 163 | "methods": ["password"], |
| 164 | "password": { |
| 165 | "user": { |
| 166 | "id": "fenris", |
| 167 | "password": "g0t0h311" |
| 168 | } |
| 169 | } |
| 170 | }, |
| 171 | "scope": { |
| 172 | "domain": { |
| 173 | "id": "1000" |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | `) |
| 179 | } |
| 180 | |
| 181 | func TestCreateProjectNameAndDomainIDScope(t *testing.T) { |
| 182 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 183 | scope := &Scope{ProjectName: "world-domination", DomainID: "1000"} |
| 184 | authTokenPost(t, options, scope, ` |
| 185 | { |
| 186 | "auth": { |
| 187 | "identity": { |
| 188 | "methods": ["password"], |
| 189 | "password": { |
| 190 | "user": { |
| 191 | "id": "fenris", |
| 192 | "password": "g0t0h311" |
| 193 | } |
| 194 | } |
| 195 | }, |
| 196 | "scope": { |
| 197 | "project": { |
| 198 | "domain": { |
| 199 | "id": "1000" |
| 200 | }, |
| 201 | "name": "world-domination" |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | `) |
| 207 | } |
| 208 | |
| 209 | func TestCreateProjectNameAndDomainNameScope(t *testing.T) { |
| 210 | options := gophercloud.AuthOptions{UserID: "fenris", Password: "g0t0h311"} |
| 211 | scope := &Scope{ProjectName: "world-domination", DomainName: "evil-plans"} |
| 212 | authTokenPost(t, options, scope, ` |
| 213 | { |
| 214 | "auth": { |
| 215 | "identity": { |
| 216 | "methods": ["password"], |
| 217 | "password": { |
| 218 | "user": { |
| 219 | "id": "fenris", |
| 220 | "password": "g0t0h311" |
| 221 | } |
| 222 | } |
| 223 | }, |
| 224 | "scope": { |
| 225 | "project": { |
| 226 | "domain": { |
| 227 | "name": "evil-plans" |
| 228 | }, |
| 229 | "name": "world-domination" |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | `) |
| 235 | } |
Ash Wilson | a8855ff | 2014-08-29 08:26:29 -0400 | [diff] [blame^] | 236 | |
| 237 | func TestCreateFailureEmptyAuth(t *testing.T) { |
| 238 | authTokenPostErr(t, gophercloud.AuthOptions{}, nil, false, ErrMissingPassword) |
| 239 | } |
| 240 | |
| 241 | func TestCreateFailureAPIKey(t *testing.T) { |
| 242 | authTokenPostErr(t, gophercloud.AuthOptions{APIKey: "something"}, nil, false, ErrAPIKeyProvided) |
| 243 | } |
| 244 | |
| 245 | func TestCreateFailureTenantID(t *testing.T) { |
| 246 | authTokenPostErr(t, gophercloud.AuthOptions{TenantID: "something"}, nil, false, ErrTenantIDProvided) |
| 247 | } |
| 248 | |
| 249 | func TestCreateFailureTenantName(t *testing.T) { |
| 250 | authTokenPostErr(t, gophercloud.AuthOptions{TenantName: "something"}, nil, false, ErrTenantNameProvided) |
| 251 | } |
| 252 | |
| 253 | func TestCreateFailureTokenIDUsername(t *testing.T) { |
| 254 | authTokenPostErr(t, gophercloud.AuthOptions{Username: "something"}, nil, true, ErrUsernameWithToken) |
| 255 | } |
| 256 | |
| 257 | func TestCreateFailureTokenIDUserID(t *testing.T) { |
| 258 | authTokenPostErr(t, gophercloud.AuthOptions{UserID: "something"}, nil, true, ErrUserIDWithToken) |
| 259 | } |
| 260 | |
| 261 | func TestCreateFailureTokenIDDomainID(t *testing.T) { |
| 262 | authTokenPostErr(t, gophercloud.AuthOptions{DomainID: "something"}, nil, true, ErrDomainIDWithToken) |
| 263 | } |
| 264 | |
| 265 | func TestCreateFailureTokenIDDomainName(t *testing.T) { |
| 266 | authTokenPostErr(t, gophercloud.AuthOptions{DomainName: "something"}, nil, true, ErrDomainNameWithToken) |
| 267 | } |