blob: c2899470b2dc4802b2038652ad8f856fcff7053f [file] [log] [blame]
Ash Wilson54b03822014-10-07 14:18:41 -04001package tokens
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7 "time"
8
9 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
11 os "github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
12 th "github.com/rackspace/gophercloud/testhelper"
13)
14
15var expectedToken = &os.Token{
16 ID: "aaaabbbbccccdddd",
17 ExpiresAt: time.Date(2014, time.January, 31, 15, 30, 58, 0, time.UTC),
18 Tenant: tenants.Tenant{
19 ID: "fc394f2ab2df4114bde39905f800dc57",
20 Name: "test",
21 Description: "There are many tenants. This one is yours.",
22 Enabled: true,
23 },
24}
25
26var expectedServiceCatalog = &os.ServiceCatalog{
27 Entries: []os.CatalogEntry{
28 os.CatalogEntry{
29 Name: "inscrutablewalrus",
30 Type: "something",
31 Endpoints: []os.Endpoint{
32 os.Endpoint{
33 PublicURL: "http://something0:1234/v2/",
34 Region: "region0",
35 },
36 os.Endpoint{
37 PublicURL: "http://something1:1234/v2/",
38 Region: "region1",
39 },
40 },
41 },
42 os.CatalogEntry{
43 Name: "arbitrarypenguin",
44 Type: "else",
45 Endpoints: []os.Endpoint{
46 os.Endpoint{
47 PublicURL: "http://else0:4321/v3/",
48 Region: "region0",
49 },
50 },
51 },
52 },
53}
54
55func tokenPost(t *testing.T, options gophercloud.AuthOptions, requestJSON string) os.CreateResult {
56 th.SetupHTTP()
57 defer th.TeardownHTTP()
58
59 client := gophercloud.ServiceClient{Endpoint: th.Endpoint()}
60
61 th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
62 th.TestMethod(t, r, "POST")
63 th.TestHeader(t, r, "Content-Type", "application/json")
64 th.TestHeader(t, r, "Accept", "application/json")
65 th.TestJSONRequest(t, r, requestJSON)
66
67 w.WriteHeader(http.StatusOK)
68 fmt.Fprintf(w, `
69{
70 "access": {
71 "token": {
72 "issued_at": "2014-01-30T15:30:58.000000Z",
73 "expires": "2014-01-31T15:30:58Z",
74 "id": "aaaabbbbccccdddd",
75 "tenant": {
76 "description": "There are many tenants. This one is yours.",
77 "enabled": true,
78 "id": "fc394f2ab2df4114bde39905f800dc57",
79 "name": "test"
80 }
81 },
82 "serviceCatalog": [
83 {
84 "endpoints": [
85 {
86 "publicURL": "http://something0:1234/v2/",
87 "region": "region0"
88 },
89 {
90 "publicURL": "http://something1:1234/v2/",
91 "region": "region1"
92 }
93 ],
94 "type": "something",
95 "name": "inscrutablewalrus"
96 },
97 {
98 "endpoints": [
99 {
100 "publicURL": "http://else0:4321/v3/",
101 "region": "region0"
102 }
103 ],
104 "type": "else",
105 "name": "arbitrarypenguin"
106 }
107 ]
108 }
109}
110 `)
111 })
112
113 return Create(&client, options)
114}
115
116func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) {
117 th.SetupHTTP()
118 defer th.TeardownHTTP()
119
120 client := gophercloud.ServiceClient{Endpoint: th.Endpoint()}
121
122 th.Mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
123 th.TestMethod(t, r, "POST")
124 th.TestHeader(t, r, "Content-Type", "application/json")
125 th.TestHeader(t, r, "Accept", "application/json")
126
127 w.WriteHeader(http.StatusOK)
128 fmt.Fprintf(w, `{}`)
129 })
130
131 actualErr := Create(&client, options).Err
132 th.CheckEquals(t, expectedErr, actualErr)
133}
134
135func isSuccessful(t *testing.T, result os.CreateResult) {
136 token, err := result.ExtractToken()
137 th.AssertNoErr(t, err)
138 th.CheckDeepEquals(t, expectedToken, token)
139
140 serviceCatalog, err := result.ExtractServiceCatalog()
141 th.AssertNoErr(t, err)
142 th.CheckDeepEquals(t, expectedServiceCatalog, serviceCatalog)
143}
144
145func TestCreateTokenWithAPIKey(t *testing.T) {
146 options := gophercloud.AuthOptions{
147 Username: "me",
148 APIKey: "1234567890abcdef",
149 }
150
151 isSuccessful(t, tokenPost(t, options, `
152 {
153 "auth": {
154 "RAX-KSKEY:apiKeyCredentials": {
155 "username": "me",
156 "apiKey": "1234567890abcdef"
157 }
158 }
159 }
160 `))
161}