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 | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/testhelper" |
| 10 | ) |
| 11 | |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 12 | func TestAuthenticatedClientV3(t *testing.T) { |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 13 | testhelper.SetupHTTP() |
| 14 | defer testhelper.TeardownHTTP() |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 15 | |
| 16 | const ID = "0123456789" |
| 17 | |
| 18 | testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 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 | } |
| 40 | `, testhelper.Endpoint()+"v3/", testhelper.Endpoint()+"v2.0/") |
| 41 | }) |
| 42 | |
| 43 | testhelper.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) { |
| 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", |
| 53 | IdentityEndpoint: testhelper.Endpoint(), |
| 54 | } |
Ash Wilson | 001cfa5 | 2014-09-02 14:23:23 -0400 | [diff] [blame] | 55 | client, err := AuthenticatedClient(options) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 56 | |
| 57 | if err != nil { |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 58 | t.Fatalf("Unexpected error from AuthenticatedClient: %s", err) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if client.TokenID != ID { |
| 62 | t.Errorf("Expected token ID to be [%s], but was [%s]", ID, client.TokenID) |
| 63 | } |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 64 | } |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 65 | |
| 66 | func TestAuthenticatedClientV2(t *testing.T) { |
| 67 | testhelper.SetupHTTP() |
| 68 | defer testhelper.TeardownHTTP() |
| 69 | |
| 70 | testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 71 | fmt.Fprintf(w, ` |
| 72 | { |
| 73 | "versions": { |
| 74 | "values": [ |
| 75 | { |
| 76 | "status": "experimental", |
| 77 | "id": "v3.0", |
| 78 | "links": [ |
| 79 | { "href": "%s", "rel": "self" } |
| 80 | ] |
| 81 | }, |
| 82 | { |
| 83 | "status": "stable", |
| 84 | "id": "v2.0", |
| 85 | "links": [ |
| 86 | { "href": "%s", "rel": "self" } |
| 87 | ] |
| 88 | } |
| 89 | ] |
| 90 | } |
| 91 | } |
| 92 | `, testhelper.Endpoint()+"v3/", testhelper.Endpoint()+"v2.0/") |
| 93 | }) |
| 94 | |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 95 | testhelper.Mux.HandleFunc("/v2.0/tokens", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 96 | w.WriteHeader(http.StatusCreated) |
| 97 | fmt.Fprintf(w, ` |
| 98 | { |
| 99 | "access": { |
| 100 | "token": { |
| 101 | "id": "01234567890" |
| 102 | }, |
| 103 | "serviceCatalog": [ |
| 104 | { |
| 105 | "name": "Cloud Servers", |
| 106 | "type": "compute", |
| 107 | "endpoints": [ |
| 108 | { |
| 109 | "tenantId": "t1000", |
| 110 | "publicURL": "https://compute.north.host.com/v1/t1000", |
| 111 | "internalURL": "https://compute.north.internal/v1/t1000", |
| 112 | "region": "North", |
| 113 | "versionId": "1", |
| 114 | "versionInfo": "https://compute.north.host.com/v1/", |
| 115 | "versionList": "https://compute.north.host.com/" |
| 116 | }, |
| 117 | { |
| 118 | "tenantId": "t1000", |
| 119 | "publicURL": "https://compute.north.host.com/v1.1/t1000", |
| 120 | "internalURL": "https://compute.north.internal/v1.1/t1000", |
| 121 | "region": "North", |
| 122 | "versionId": "1.1", |
| 123 | "versionInfo": "https://compute.north.host.com/v1.1/", |
| 124 | "versionList": "https://compute.north.host.com/" |
| 125 | } |
| 126 | ], |
| 127 | "endpoints_links": [] |
| 128 | }, |
| 129 | { |
| 130 | "name": "Cloud Files", |
| 131 | "type": "object-store", |
| 132 | "endpoints": [ |
| 133 | { |
| 134 | "tenantId": "t1000", |
| 135 | "publicURL": "https://storage.north.host.com/v1/t1000", |
| 136 | "internalURL": "https://storage.north.internal/v1/t1000", |
| 137 | "region": "North", |
| 138 | "versionId": "1", |
| 139 | "versionInfo": "https://storage.north.host.com/v1/", |
| 140 | "versionList": "https://storage.north.host.com/" |
| 141 | }, |
| 142 | { |
| 143 | "tenantId": "t1000", |
| 144 | "publicURL": "https://storage.south.host.com/v1/t1000", |
| 145 | "internalURL": "https://storage.south.internal/v1/t1000", |
| 146 | "region": "South", |
| 147 | "versionId": "1", |
| 148 | "versionInfo": "https://storage.south.host.com/v1/", |
| 149 | "versionList": "https://storage.south.host.com/" |
| 150 | } |
| 151 | ] |
| 152 | } |
| 153 | ] |
| 154 | } |
| 155 | } |
| 156 | `) |
| 157 | }) |
| 158 | |
| 159 | options := gophercloud.AuthOptions{ |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 160 | Username: "me", |
Ash Wilson | 986854a | 2014-09-08 15:51:08 -0400 | [diff] [blame] | 161 | Password: "secret", |
| 162 | IdentityEndpoint: testhelper.Endpoint(), |
| 163 | } |
| 164 | client, err := AuthenticatedClient(options) |
| 165 | |
| 166 | if err != nil { |
| 167 | t.Fatalf("Unexpected error from AuthenticatedClient: %s", err) |
| 168 | } |
| 169 | |
| 170 | if client.TokenID != "01234567890" { |
| 171 | t.Errorf("Expected token ID to be [01234567890], but was [%s]", client.TokenID) |
| 172 | } |
| 173 | } |