blob: 257260c4e194fee94c39c34c3d589d277fd0da9f [file] [log] [blame]
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +02001package openstack
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 th "github.com/rackspace/gophercloud/testhelper"
10)
11
12func TestAuthenticatedClientV3(t *testing.T) {
13 th.SetupHTTP()
14 defer th.TeardownHTTP()
15
16 const ID = "0123456789"
17
18 th.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 `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/")
41 })
42
43 th.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: th.Endpoint(),
54 }
55 client, err := AuthenticatedClient(options)
56 th.AssertNoErr(t, err)
57 th.CheckEquals(t, ID, client.TokenID)
58}
59
60func TestAuthenticatedClientV2(t *testing.T) {
61 th.SetupHTTP()
62 defer th.TeardownHTTP()
63
64 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
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 }
86 `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/")
87 })
88
89 th.Mux.HandleFunc("/v2.0/tokens", func(w http.ResponseWriter, r *http.Request) {
90 fmt.Fprintf(w, `
91 {
92 "access": {
93 "token": {
94 "id": "01234567890",
95 "expires": "2014-10-01T10:00:00.000000Z"
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{
154 Username: "me",
155 Password: "secret",
156 IdentityEndpoint: th.Endpoint(),
157 }
158 client, err := AuthenticatedClient(options)
159 th.AssertNoErr(t, err)
160 th.CheckEquals(t, "01234567890", client.TokenID)
161}