Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 4 | "fmt" |
| 5 | "net/http" |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 6 | "testing" |
| 7 | |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 9 | th "github.com/rackspace/gophercloud/testhelper" |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 10 | ) |
| 11 | |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 12 | func TestAuthenticatedClientV3(t *testing.T) { |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 13 | th.SetupHTTP() |
| 14 | defer th.TeardownHTTP() |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 15 | |
| 16 | const ID = "0123456789" |
| 17 | |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 18 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 19 | fmt.Fprintf(w, ` |
| 20 | { |
| 21 | "versions": { |
| 22 | "values": [ |
| 23 | { |
| 24 | "status": "stable", |
| 25 | "id": "v3.0", |
| 26 | "links": [ |
| 27 | { "href": "%s", "rel": "self" } |
| 28 | ] |
| 29 | }, |
| 30 | { |
| 31 | "status": "stable", |
| 32 | "id": "v2.0", |
| 33 | "links": [ |
| 34 | { "href": "%s", "rel": "self" } |
| 35 | ] |
| 36 | } |
| 37 | ] |
| 38 | } |
| 39 | } |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 40 | `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/") |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 41 | }) |
| 42 | |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 43 | th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 44 | w.Header().Add("X-Subject-Token", ID) |
| 45 | |
| 46 | w.WriteHeader(http.StatusCreated) |
| 47 | fmt.Fprintf(w, `{ "token": { "expires_at": "2013-02-02T18:30:59.000000Z" } }`) |
| 48 | }) |
| 49 | |
| 50 | options := gophercloud.AuthOptions{ |
| 51 | UserID: "me", |
| 52 | Password: "secret", |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 53 | IdentityEndpoint: th.Endpoint(), |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 54 | } |
Ash Wilson | 001cfa5 | 2014-09-02 14:23:23 -0400 | [diff] [blame] | 55 | client, err := AuthenticatedClient(options) |
Ash Wilson | 3431e56 | 2014-10-03 14:18:17 -0400 | [diff] [blame^] | 56 | th.AssertNoErr(t, err) |
| 57 | th.CheckEquals(t, ID, client.TokenID) |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 58 | } |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 59 | |
| 60 | func TestAuthenticatedClientV2(t *testing.T) { |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 61 | th.SetupHTTP() |
| 62 | defer th.TeardownHTTP() |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 63 | |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 64 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 65 | fmt.Fprintf(w, ` |
| 66 | { |
| 67 | "versions": { |
| 68 | "values": [ |
| 69 | { |
| 70 | "status": "experimental", |
| 71 | "id": "v3.0", |
| 72 | "links": [ |
| 73 | { "href": "%s", "rel": "self" } |
| 74 | ] |
| 75 | }, |
| 76 | { |
| 77 | "status": "stable", |
| 78 | "id": "v2.0", |
| 79 | "links": [ |
| 80 | { "href": "%s", "rel": "self" } |
| 81 | ] |
| 82 | } |
| 83 | ] |
| 84 | } |
| 85 | } |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 86 | `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/") |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 87 | }) |
| 88 | |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 89 | th.Mux.HandleFunc("/v2.0/tokens", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 90 | fmt.Fprintf(w, ` |
| 91 | { |
| 92 | "access": { |
| 93 | "token": { |
Ash Wilson | 6cc0070 | 2014-10-03 14:00:26 -0400 | [diff] [blame] | 94 | "id": "01234567890", |
| 95 | "expires": "2014-10-01T10:00:00.000000Z" |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 96 | }, |
| 97 | "serviceCatalog": [ |
| 98 | { |
| 99 | "name": "Cloud Servers", |
| 100 | "type": "compute", |
| 101 | "endpoints": [ |
| 102 | { |
| 103 | "tenantId": "t1000", |
| 104 | "publicURL": "https://compute.north.host.com/v1/t1000", |
| 105 | "internalURL": "https://compute.north.internal/v1/t1000", |
| 106 | "region": "North", |
| 107 | "versionId": "1", |
| 108 | "versionInfo": "https://compute.north.host.com/v1/", |
| 109 | "versionList": "https://compute.north.host.com/" |
| 110 | }, |
| 111 | { |
| 112 | "tenantId": "t1000", |
| 113 | "publicURL": "https://compute.north.host.com/v1.1/t1000", |
| 114 | "internalURL": "https://compute.north.internal/v1.1/t1000", |
| 115 | "region": "North", |
| 116 | "versionId": "1.1", |
| 117 | "versionInfo": "https://compute.north.host.com/v1.1/", |
| 118 | "versionList": "https://compute.north.host.com/" |
| 119 | } |
| 120 | ], |
| 121 | "endpoints_links": [] |
| 122 | }, |
| 123 | { |
| 124 | "name": "Cloud Files", |
| 125 | "type": "object-store", |
| 126 | "endpoints": [ |
| 127 | { |
| 128 | "tenantId": "t1000", |
| 129 | "publicURL": "https://storage.north.host.com/v1/t1000", |
| 130 | "internalURL": "https://storage.north.internal/v1/t1000", |
| 131 | "region": "North", |
| 132 | "versionId": "1", |
| 133 | "versionInfo": "https://storage.north.host.com/v1/", |
| 134 | "versionList": "https://storage.north.host.com/" |
| 135 | }, |
| 136 | { |
| 137 | "tenantId": "t1000", |
| 138 | "publicURL": "https://storage.south.host.com/v1/t1000", |
| 139 | "internalURL": "https://storage.south.internal/v1/t1000", |
| 140 | "region": "South", |
| 141 | "versionId": "1", |
| 142 | "versionInfo": "https://storage.south.host.com/v1/", |
| 143 | "versionList": "https://storage.south.host.com/" |
| 144 | } |
| 145 | ] |
| 146 | } |
| 147 | ] |
| 148 | } |
| 149 | } |
| 150 | `) |
| 151 | }) |
| 152 | |
| 153 | options := gophercloud.AuthOptions{ |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 154 | Username: "me", |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 155 | Password: "secret", |
Ash Wilson | db84d5d | 2014-10-03 14:07:33 -0400 | [diff] [blame] | 156 | IdentityEndpoint: th.Endpoint(), |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 157 | } |
| 158 | client, err := AuthenticatedClient(options) |
Ash Wilson | 3431e56 | 2014-10-03 14:18:17 -0400 | [diff] [blame^] | 159 | th.AssertNoErr(t, err) |
| 160 | th.CheckEquals(t, "01234567890", client.TokenID) |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 161 | } |