blob: f75ab6c1b371e16fa32d1854a3cdad976bdd99ed [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Ash Wilson6425a412014-08-29 12:30:35 -04002
3import (
Ash Wilson4dee1b82014-08-29 14:56:45 -04004 "fmt"
5 "net/http"
Ash Wilson6425a412014-08-29 12:30:35 -04006 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
jrperritt3d966162016-06-06 14:08:54 -05009 "github.com/gophercloud/gophercloud/openstack"
Jon Perritt27249f42016-02-18 10:35:59 -060010 th "github.com/gophercloud/gophercloud/testhelper"
Ash Wilson6425a412014-08-29 12:30:35 -040011)
12
Ash Wilson986854a2014-09-08 15:51:08 -040013func TestAuthenticatedClientV3(t *testing.T) {
Ash Wilsondb84d5d2014-10-03 14:07:33 -040014 th.SetupHTTP()
15 defer th.TeardownHTTP()
Ash Wilson4dee1b82014-08-29 14:56:45 -040016
17 const ID = "0123456789"
18
Ash Wilsondb84d5d2014-10-03 14:07:33 -040019 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson4dee1b82014-08-29 14:56:45 -040020 fmt.Fprintf(w, `
21 {
22 "versions": {
23 "values": [
24 {
25 "status": "stable",
26 "id": "v3.0",
27 "links": [
28 { "href": "%s", "rel": "self" }
29 ]
30 },
31 {
32 "status": "stable",
33 "id": "v2.0",
34 "links": [
35 { "href": "%s", "rel": "self" }
36 ]
37 }
38 ]
39 }
40 }
Ash Wilsondb84d5d2014-10-03 14:07:33 -040041 `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/")
Ash Wilson4dee1b82014-08-29 14:56:45 -040042 })
43
Ash Wilsondb84d5d2014-10-03 14:07:33 -040044 th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson4dee1b82014-08-29 14:56:45 -040045 w.Header().Add("X-Subject-Token", ID)
46
47 w.WriteHeader(http.StatusCreated)
48 fmt.Fprintf(w, `{ "token": { "expires_at": "2013-02-02T18:30:59.000000Z" } }`)
49 })
50
51 options := gophercloud.AuthOptions{
Carolyn Van Slyck9977e512016-07-20 14:34:52 -050052 Username: "me",
Ash Wilson4dee1b82014-08-29 14:56:45 -040053 Password: "secret",
Carolyn Van Slyck9977e512016-07-20 14:34:52 -050054 DomainName: "default",
55 TenantName: "project",
Ash Wilsondb84d5d2014-10-03 14:07:33 -040056 IdentityEndpoint: th.Endpoint(),
Ash Wilson4dee1b82014-08-29 14:56:45 -040057 }
jrperritt3d966162016-06-06 14:08:54 -050058 client, err := openstack.AuthenticatedClient(options)
Ash Wilson3431e562014-10-03 14:18:17 -040059 th.AssertNoErr(t, err)
60 th.CheckEquals(t, ID, client.TokenID)
Ash Wilson6425a412014-08-29 12:30:35 -040061}
Ash Wilson986854a2014-09-08 15:51:08 -040062
63func TestAuthenticatedClientV2(t *testing.T) {
Ash Wilsondb84d5d2014-10-03 14:07:33 -040064 th.SetupHTTP()
65 defer th.TeardownHTTP()
Ash Wilson986854a2014-09-08 15:51:08 -040066
Ash Wilsondb84d5d2014-10-03 14:07:33 -040067 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson986854a2014-09-08 15:51:08 -040068 fmt.Fprintf(w, `
69 {
70 "versions": {
71 "values": [
72 {
73 "status": "experimental",
74 "id": "v3.0",
75 "links": [
76 { "href": "%s", "rel": "self" }
77 ]
78 },
79 {
80 "status": "stable",
81 "id": "v2.0",
82 "links": [
83 { "href": "%s", "rel": "self" }
84 ]
85 }
86 ]
87 }
88 }
Ash Wilsondb84d5d2014-10-03 14:07:33 -040089 `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/")
Ash Wilson986854a2014-09-08 15:51:08 -040090 })
91
Ash Wilsondb84d5d2014-10-03 14:07:33 -040092 th.Mux.HandleFunc("/v2.0/tokens", func(w http.ResponseWriter, r *http.Request) {
Ash Wilson986854a2014-09-08 15:51:08 -040093 fmt.Fprintf(w, `
94 {
95 "access": {
96 "token": {
Ash Wilson6cc00702014-10-03 14:00:26 -040097 "id": "01234567890",
98 "expires": "2014-10-01T10:00:00.000000Z"
Ash Wilson986854a2014-09-08 15:51:08 -040099 },
100 "serviceCatalog": [
101 {
102 "name": "Cloud Servers",
103 "type": "compute",
104 "endpoints": [
105 {
106 "tenantId": "t1000",
107 "publicURL": "https://compute.north.host.com/v1/t1000",
108 "internalURL": "https://compute.north.internal/v1/t1000",
109 "region": "North",
110 "versionId": "1",
111 "versionInfo": "https://compute.north.host.com/v1/",
112 "versionList": "https://compute.north.host.com/"
113 },
114 {
115 "tenantId": "t1000",
116 "publicURL": "https://compute.north.host.com/v1.1/t1000",
117 "internalURL": "https://compute.north.internal/v1.1/t1000",
118 "region": "North",
119 "versionId": "1.1",
120 "versionInfo": "https://compute.north.host.com/v1.1/",
121 "versionList": "https://compute.north.host.com/"
122 }
123 ],
124 "endpoints_links": []
125 },
126 {
127 "name": "Cloud Files",
128 "type": "object-store",
129 "endpoints": [
130 {
131 "tenantId": "t1000",
132 "publicURL": "https://storage.north.host.com/v1/t1000",
133 "internalURL": "https://storage.north.internal/v1/t1000",
134 "region": "North",
135 "versionId": "1",
136 "versionInfo": "https://storage.north.host.com/v1/",
137 "versionList": "https://storage.north.host.com/"
138 },
139 {
140 "tenantId": "t1000",
141 "publicURL": "https://storage.south.host.com/v1/t1000",
142 "internalURL": "https://storage.south.internal/v1/t1000",
143 "region": "South",
144 "versionId": "1",
145 "versionInfo": "https://storage.south.host.com/v1/",
146 "versionList": "https://storage.south.host.com/"
147 }
148 ]
149 }
150 ]
151 }
152 }
153 `)
154 })
155
156 options := gophercloud.AuthOptions{
Ash Wilson11c98282014-09-08 16:05:10 -0400157 Username: "me",
Ash Wilson986854a2014-09-08 15:51:08 -0400158 Password: "secret",
Ash Wilsondb84d5d2014-10-03 14:07:33 -0400159 IdentityEndpoint: th.Endpoint(),
Ash Wilson986854a2014-09-08 15:51:08 -0400160 }
jrperritt3d966162016-06-06 14:08:54 -0500161 client, err := openstack.AuthenticatedClient(options)
Ash Wilson3431e562014-10-03 14:18:17 -0400162 th.AssertNoErr(t, err)
163 th.CheckEquals(t, "01234567890", client.TokenID)
Ash Wilson986854a2014-09-08 15:51:08 -0400164}