Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud" |
| 10 | "github.com/rackspace/gophercloud/openstack/identity/v2/tenants" |
| 11 | th "github.com/rackspace/gophercloud/testhelper" |
| 12 | ) |
| 13 | |
| 14 | var expectedToken = &Token{ |
| 15 | ID: "aaaabbbbccccdddd", |
| 16 | ExpiresAt: time.Date(2014, time.January, 31, 15, 30, 58, 0, time.UTC), |
| 17 | Tenant: tenants.Tenant{ |
| 18 | ID: "fc394f2ab2df4114bde39905f800dc57", |
| 19 | Name: "test", |
| 20 | Description: "There are many tenants. This one is yours.", |
| 21 | Enabled: true, |
| 22 | }, |
| 23 | } |
| 24 | |
| 25 | var expectedServiceCatalog = &ServiceCatalog{ |
| 26 | Entries: []CatalogEntry{ |
| 27 | CatalogEntry{ |
| 28 | Name: "inscrutablewalrus", |
| 29 | Type: "something", |
| 30 | Endpoints: []Endpoint{ |
| 31 | Endpoint{ |
| 32 | PublicURL: "http://something0:1234/v2/", |
| 33 | Region: "region0", |
| 34 | }, |
| 35 | Endpoint{ |
| 36 | PublicURL: "http://something1:1234/v2/", |
| 37 | Region: "region1", |
| 38 | }, |
| 39 | }, |
| 40 | }, |
| 41 | CatalogEntry{ |
| 42 | Name: "arbitrarypenguin", |
| 43 | Type: "else", |
| 44 | Endpoints: []Endpoint{ |
| 45 | Endpoint{ |
| 46 | PublicURL: "http://else0:4321/v3/", |
| 47 | Region: "region0", |
| 48 | }, |
| 49 | }, |
| 50 | }, |
| 51 | }, |
| 52 | } |
| 53 | |
| 54 | func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) CreateResult { |
| 55 | th.SetupHTTP() |
| 56 | defer th.TeardownHTTP() |
| 57 | |
| 58 | client := gophercloud.ServiceClient{Endpoint: th.Endpoint()} |
| 59 | |
| 60 | th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) { |
| 61 | th.TestMethod(t, r, "POST") |
| 62 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 63 | th.TestHeader(t, r, "Accept", "application/json") |
| 64 | th.TestJSONRequest(t, r, requestJSON) |
| 65 | |
| 66 | w.WriteHeader(http.StatusOK) |
| 67 | fmt.Fprintf(w, ` |
| 68 | { |
| 69 | "access": { |
| 70 | "token": { |
| 71 | "issued_at": "2014-01-30T15:30:58.000000Z", |
| 72 | "expires": "2014-01-31T15:30:58Z", |
| 73 | "id": "aaaabbbbccccdddd", |
| 74 | "tenant": { |
| 75 | "description": "There are many tenants. This one is yours.", |
| 76 | "enabled": true, |
| 77 | "id": "fc394f2ab2df4114bde39905f800dc57", |
| 78 | "name": "test" |
| 79 | } |
| 80 | }, |
| 81 | "serviceCatalog": [ |
| 82 | { |
| 83 | "endpoints": [ |
| 84 | { |
| 85 | "publicURL": "http://something0:1234/v2/", |
| 86 | "region": "region0" |
| 87 | }, |
| 88 | { |
| 89 | "publicURL": "http://something1:1234/v2/", |
| 90 | "region": "region1" |
| 91 | } |
| 92 | ], |
| 93 | "type": "something", |
| 94 | "name": "inscrutablewalrus" |
| 95 | }, |
| 96 | { |
| 97 | "endpoints": [ |
| 98 | { |
| 99 | "publicURL": "http://else0:4321/v3/", |
| 100 | "region": "region0" |
| 101 | } |
| 102 | ], |
| 103 | "type": "else", |
| 104 | "name": "arbitrarypenguin" |
| 105 | } |
| 106 | ] |
| 107 | } |
| 108 | } |
| 109 | `) |
| 110 | }) |
| 111 | |
| 112 | return Create(&client, options) |
| 113 | } |
| 114 | |
| 115 | func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) { |
| 116 | th.SetupHTTP() |
| 117 | defer th.TeardownHTTP() |
| 118 | |
| 119 | client := gophercloud.ServiceClient{Endpoint: th.Endpoint()} |
| 120 | |
| 121 | th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) { |
| 122 | th.TestMethod(t, r, "POST") |
| 123 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 124 | th.TestHeader(t, r, "Accept", "application/json") |
| 125 | |
| 126 | w.WriteHeader(http.StatusOK) |
| 127 | fmt.Fprintf(w, `{}`) |
| 128 | }) |
| 129 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 130 | actualErr := Create(&client, options).Err |
Ash Wilson | aa197a9 | 2014-10-03 11:38:08 -0400 | [diff] [blame] | 131 | th.CheckEquals(t, expectedErr, actualErr) |
| 132 | } |
| 133 | |
| 134 | func isSuccessful(t *testing.T, result CreateResult) { |
| 135 | token, err := result.ExtractToken() |
| 136 | th.AssertNoErr(t, err) |
| 137 | th.CheckDeepEquals(t, expectedToken, token) |
| 138 | |
| 139 | serviceCatalog, err := result.ExtractServiceCatalog() |
| 140 | th.AssertNoErr(t, err) |
| 141 | th.CheckDeepEquals(t, expectedServiceCatalog, serviceCatalog) |
| 142 | } |
| 143 | |
| 144 | func TestCreateWithPassword(t *testing.T) { |
| 145 | options := gophercloud.AuthOptions{ |
| 146 | Username: "me", |
| 147 | Password: "swordfish", |
| 148 | } |
| 149 | |
| 150 | isSuccessful(t, tokenPost(t, options, ` |
| 151 | { |
| 152 | "auth": { |
| 153 | "passwordCredentials": { |
| 154 | "username": "me", |
| 155 | "password": "swordfish" |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | `)) |
| 160 | } |
| 161 | |
Ash Wilson | 29f2317 | 2014-10-03 11:45:06 -0400 | [diff] [blame] | 162 | func TestCreateTokenWithTenantID(t *testing.T) { |
| 163 | options := gophercloud.AuthOptions{ |
| 164 | Username: "me", |
| 165 | Password: "opensesame", |
| 166 | TenantID: "fc394f2ab2df4114bde39905f800dc57", |
| 167 | } |
| 168 | |
| 169 | isSuccessful(t, tokenPost(t, options, ` |
| 170 | { |
| 171 | "auth": { |
| 172 | "tenantId": "fc394f2ab2df4114bde39905f800dc57", |
| 173 | "passwordCredentials": { |
| 174 | "username": "me", |
| 175 | "password": "opensesame" |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | `)) |
| 180 | } |
| 181 | |
| 182 | func TestCreateTokenWithTenantName(t *testing.T) { |
| 183 | options := gophercloud.AuthOptions{ |
| 184 | Username: "me", |
| 185 | Password: "opensesame", |
| 186 | TenantName: "demo", |
| 187 | } |
| 188 | |
| 189 | isSuccessful(t, tokenPost(t, options, ` |
| 190 | { |
| 191 | "auth": { |
| 192 | "tenantName": "demo", |
| 193 | "passwordCredentials": { |
| 194 | "username": "me", |
| 195 | "password": "opensesame" |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | `)) |
| 200 | } |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 201 | |
| 202 | func TestProhibitUserID(t *testing.T) { |
| 203 | options := gophercloud.AuthOptions{ |
| 204 | Username: "me", |
| 205 | UserID: "1234", |
| 206 | Password: "thing", |
| 207 | } |
| 208 | tokenPostErr(t, options, ErrUserIDProvided) |
| 209 | } |
| 210 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame^] | 211 | func TestProhibitAPIKey(t *testing.T) { |
| 212 | options := gophercloud.AuthOptions{ |
| 213 | Username: "me", |
| 214 | Password: "thing", |
| 215 | APIKey: "123412341234", |
| 216 | } |
| 217 | tokenPostErr(t, options, ErrAPIKeyProvided) |
| 218 | } |
| 219 | |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 220 | func TestProhibitDomainID(t *testing.T) { |
| 221 | options := gophercloud.AuthOptions{ |
| 222 | Username: "me", |
| 223 | Password: "thing", |
| 224 | DomainID: "1234", |
| 225 | } |
| 226 | tokenPostErr(t, options, ErrDomainIDProvided) |
| 227 | } |
| 228 | |
| 229 | func TestProhibitDomainName(t *testing.T) { |
| 230 | options := gophercloud.AuthOptions{ |
| 231 | Username: "me", |
| 232 | Password: "thing", |
| 233 | DomainName: "wat", |
| 234 | } |
| 235 | tokenPostErr(t, options, ErrDomainNameProvided) |
| 236 | } |
| 237 | |
| 238 | func TestRequireUsername(t *testing.T) { |
| 239 | options := gophercloud.AuthOptions{ |
| 240 | Password: "thing", |
| 241 | } |
| 242 | tokenPostErr(t, options, ErrUsernameRequired) |
| 243 | } |
| 244 | |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame^] | 245 | func TestRequirePassword(t *testing.T) { |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 246 | options := gophercloud.AuthOptions{ |
| 247 | Username: "me", |
| 248 | } |
Ash Wilson | 1cf4d5f | 2014-10-07 14:16:18 -0400 | [diff] [blame^] | 249 | tokenPostErr(t, options, ErrPasswordRequired) |
Ash Wilson | 27d29e2 | 2014-10-03 11:57:14 -0400 | [diff] [blame] | 250 | } |