blob: dd39e775b713b61d509d18e032b2e7979445a37b [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 Wilson6425a412014-08-29 12:30:35 -04009 "github.com/rackspace/gophercloud/testhelper"
10)
11
Ash Wilson986854a2014-09-08 15:51:08 -040012func TestAuthenticatedClientV3(t *testing.T) {
Ash Wilson6425a412014-08-29 12:30:35 -040013 testhelper.SetupHTTP()
14 defer testhelper.TeardownHTTP()
Ash Wilson4dee1b82014-08-29 14:56:45 -040015
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 Wilson001cfa52014-09-02 14:23:23 -040055 client, err := AuthenticatedClient(options)
Ash Wilson4dee1b82014-08-29 14:56:45 -040056
57 if err != nil {
Ash Wilson986854a2014-09-08 15:51:08 -040058 t.Fatalf("Unexpected error from AuthenticatedClient: %s", err)
Ash Wilson4dee1b82014-08-29 14:56:45 -040059 }
60
61 if client.TokenID != ID {
62 t.Errorf("Expected token ID to be [%s], but was [%s]", ID, client.TokenID)
63 }
Ash Wilson6425a412014-08-29 12:30:35 -040064}
Ash Wilson986854a2014-09-08 15:51:08 -040065
66func 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 Wilson11c98282014-09-08 16:05:10 -040095 testhelper.Mux.HandleFunc("/v2.0/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson986854a2014-09-08 15:51:08 -040096 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 Wilson11c98282014-09-08 16:05:10 -0400160 Username: "me",
Ash Wilson986854a2014-09-08 15:51:08 -0400161 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}