blob: 257260c4e194fee94c39c34c3d589d277fd0da9f [file] [log] [blame]
Ash Wilson6425a412014-08-29 12:30:35 -04001package openstack
2
3import (
Ash Wilson4dee1b82014-08-29 14:56:45 -04004 "fmt"
5 "net/http"
Ash Wilson6425a412014-08-29 12:30:35 -04006 "testing"
7
Ash Wilson4dee1b82014-08-29 14:56:45 -04008 "github.com/rackspace/gophercloud"
Ash Wilsondb84d5d2014-10-03 14:07:33 -04009 th "github.com/rackspace/gophercloud/testhelper"
Ash Wilson6425a412014-08-29 12:30:35 -040010)
11
Ash Wilson986854a2014-09-08 15:51:08 -040012func TestAuthenticatedClientV3(t *testing.T) {
Ash Wilsondb84d5d2014-10-03 14:07:33 -040013 th.SetupHTTP()
14 defer th.TeardownHTTP()
Ash Wilson4dee1b82014-08-29 14:56:45 -040015
16 const ID = "0123456789"
17
Ash Wilsondb84d5d2014-10-03 14:07:33 -040018 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson4dee1b82014-08-29 14:56:45 -040019 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 Wilsondb84d5d2014-10-03 14:07:33 -040040 `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/")
Ash Wilson4dee1b82014-08-29 14:56:45 -040041 })
42
Ash Wilsondb84d5d2014-10-03 14:07:33 -040043 th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson4dee1b82014-08-29 14:56:45 -040044 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 Wilsondb84d5d2014-10-03 14:07:33 -040053 IdentityEndpoint: th.Endpoint(),
Ash Wilson4dee1b82014-08-29 14:56:45 -040054 }
Ash Wilson001cfa52014-09-02 14:23:23 -040055 client, err := AuthenticatedClient(options)
Ash Wilson3431e562014-10-03 14:18:17 -040056 th.AssertNoErr(t, err)
57 th.CheckEquals(t, ID, client.TokenID)
Ash Wilson6425a412014-08-29 12:30:35 -040058}
Ash Wilson986854a2014-09-08 15:51:08 -040059
60func TestAuthenticatedClientV2(t *testing.T) {
Ash Wilsondb84d5d2014-10-03 14:07:33 -040061 th.SetupHTTP()
62 defer th.TeardownHTTP()
Ash Wilson986854a2014-09-08 15:51:08 -040063
Ash Wilsondb84d5d2014-10-03 14:07:33 -040064 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson986854a2014-09-08 15:51:08 -040065 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 Wilsondb84d5d2014-10-03 14:07:33 -040086 `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/")
Ash Wilson986854a2014-09-08 15:51:08 -040087 })
88
Ash Wilsondb84d5d2014-10-03 14:07:33 -040089 th.Mux.HandleFunc("/v2.0/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson986854a2014-09-08 15:51:08 -040090 fmt.Fprintf(w, `
91 {
92 "access": {
93 "token": {
Ash Wilson6cc00702014-10-03 14:00:26 -040094 "id": "01234567890",
95 "expires": "2014-10-01T10:00:00.000000Z"
Ash Wilson986854a2014-09-08 15:51:08 -040096 },
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 Wilson11c98282014-09-08 16:05:10 -0400154 Username: "me",
Ash Wilson986854a2014-09-08 15:51:08 -0400155 Password: "secret",
Ash Wilsondb84d5d2014-10-03 14:07:33 -0400156 IdentityEndpoint: th.Endpoint(),
Ash Wilson986854a2014-09-08 15:51:08 -0400157 }
158 client, err := AuthenticatedClient(options)
Ash Wilson3431e562014-10-03 14:18:17 -0400159 th.AssertNoErr(t, err)
160 th.CheckEquals(t, "01234567890", client.TokenID)
Ash Wilson986854a2014-09-08 15:51:08 -0400161}