client_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package testing
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud"
  7. "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack"
  8. th "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper"
  9. )
  10. const ID = "0123456789"
  11. func TestAuthenticatedClientV3(t *testing.T) {
  12. th.SetupHTTP()
  13. defer th.TeardownHTTP()
  14. th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  15. fmt.Fprintf(w, `
  16. {
  17. "versions": {
  18. "values": [
  19. {
  20. "status": "stable",
  21. "id": "v3.0",
  22. "links": [
  23. { "href": "%s", "rel": "self" }
  24. ]
  25. },
  26. {
  27. "status": "stable",
  28. "id": "v2.0",
  29. "links": [
  30. { "href": "%s", "rel": "self" }
  31. ]
  32. }
  33. ]
  34. }
  35. }
  36. `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/")
  37. })
  38. th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
  39. w.Header().Add("X-Subject-Token", ID)
  40. w.WriteHeader(http.StatusCreated)
  41. fmt.Fprintf(w, `{ "token": { "expires_at": "2013-02-02T18:30:59.000000Z" } }`)
  42. })
  43. options := gophercloud.AuthOptions{
  44. Username: "me",
  45. Password: "secret",
  46. DomainName: "default",
  47. TenantName: "project",
  48. IdentityEndpoint: th.Endpoint(),
  49. }
  50. client, err := openstack.AuthenticatedClient(options)
  51. th.AssertNoErr(t, err)
  52. th.CheckEquals(t, ID, client.TokenID)
  53. }
  54. func TestAuthenticatedClientV2(t *testing.T) {
  55. th.SetupHTTP()
  56. defer th.TeardownHTTP()
  57. th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  58. fmt.Fprintf(w, `
  59. {
  60. "versions": {
  61. "values": [
  62. {
  63. "status": "experimental",
  64. "id": "v3.0",
  65. "links": [
  66. { "href": "%s", "rel": "self" }
  67. ]
  68. },
  69. {
  70. "status": "stable",
  71. "id": "v2.0",
  72. "links": [
  73. { "href": "%s", "rel": "self" }
  74. ]
  75. }
  76. ]
  77. }
  78. }
  79. `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/")
  80. })
  81. th.Mux.HandleFunc("/v2.0/tokens", func(w http.ResponseWriter, r *http.Request) {
  82. fmt.Fprintf(w, `
  83. {
  84. "access": {
  85. "token": {
  86. "id": "01234567890",
  87. "expires": "2014-10-01T10:00:00.000000Z"
  88. },
  89. "serviceCatalog": [
  90. {
  91. "name": "Cloud Servers",
  92. "type": "compute",
  93. "endpoints": [
  94. {
  95. "tenantId": "t1000",
  96. "publicURL": "https://compute.north.host.com/v1/t1000",
  97. "internalURL": "https://compute.north.internal/v1/t1000",
  98. "region": "North",
  99. "versionId": "1",
  100. "versionInfo": "https://compute.north.host.com/v1/",
  101. "versionList": "https://compute.north.host.com/"
  102. },
  103. {
  104. "tenantId": "t1000",
  105. "publicURL": "https://compute.north.host.com/v1.1/t1000",
  106. "internalURL": "https://compute.north.internal/v1.1/t1000",
  107. "region": "North",
  108. "versionId": "1.1",
  109. "versionInfo": "https://compute.north.host.com/v1.1/",
  110. "versionList": "https://compute.north.host.com/"
  111. }
  112. ],
  113. "endpoints_links": []
  114. },
  115. {
  116. "name": "Cloud Files",
  117. "type": "object-store",
  118. "endpoints": [
  119. {
  120. "tenantId": "t1000",
  121. "publicURL": "https://storage.north.host.com/v1/t1000",
  122. "internalURL": "https://storage.north.internal/v1/t1000",
  123. "region": "North",
  124. "versionId": "1",
  125. "versionInfo": "https://storage.north.host.com/v1/",
  126. "versionList": "https://storage.north.host.com/"
  127. },
  128. {
  129. "tenantId": "t1000",
  130. "publicURL": "https://storage.south.host.com/v1/t1000",
  131. "internalURL": "https://storage.south.internal/v1/t1000",
  132. "region": "South",
  133. "versionId": "1",
  134. "versionInfo": "https://storage.south.host.com/v1/",
  135. "versionList": "https://storage.south.host.com/"
  136. }
  137. ]
  138. }
  139. ]
  140. }
  141. }
  142. `)
  143. })
  144. options := gophercloud.AuthOptions{
  145. Username: "me",
  146. Password: "secret",
  147. IdentityEndpoint: th.Endpoint(),
  148. }
  149. client, err := openstack.AuthenticatedClient(options)
  150. th.AssertNoErr(t, err)
  151. th.CheckEquals(t, "01234567890", client.TokenID)
  152. }
  153. func TestIdentityAdminV3Client(t *testing.T) {
  154. th.SetupHTTP()
  155. defer th.TeardownHTTP()
  156. th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  157. fmt.Fprintf(w, `
  158. {
  159. "versions": {
  160. "values": [
  161. {
  162. "status": "stable",
  163. "id": "v3.0",
  164. "links": [
  165. { "href": "%s", "rel": "self" }
  166. ]
  167. },
  168. {
  169. "status": "stable",
  170. "id": "v2.0",
  171. "links": [
  172. { "href": "%s", "rel": "self" }
  173. ]
  174. }
  175. ]
  176. }
  177. }
  178. `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/")
  179. })
  180. th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
  181. w.Header().Add("X-Subject-Token", ID)
  182. w.WriteHeader(http.StatusCreated)
  183. fmt.Fprintf(w, `
  184. {
  185. "token": {
  186. "audit_ids": ["VcxU2JYqT8OzfUVvrjEITQ", "qNUTIJntTzO1-XUk5STybw"],
  187. "catalog": [
  188. {
  189. "endpoints": [
  190. {
  191. "id": "39dc322ce86c4111b4f06c2eeae0841b",
  192. "interface": "public",
  193. "region": "RegionOne",
  194. "url": "http://localhost:5000"
  195. },
  196. {
  197. "id": "ec642f27474842e78bf059f6c48f4e99",
  198. "interface": "internal",
  199. "region": "RegionOne",
  200. "url": "http://localhost:5000"
  201. },
  202. {
  203. "id": "c609fc430175452290b62a4242e8a7e8",
  204. "interface": "admin",
  205. "region": "RegionOne",
  206. "url": "http://localhost:35357"
  207. }
  208. ],
  209. "id": "4363ae44bdf34a3981fde3b823cb9aa2",
  210. "type": "identity",
  211. "name": "keystone"
  212. }
  213. ],
  214. "expires_at": "2013-02-27T18:30:59.999999Z",
  215. "is_domain": false,
  216. "issued_at": "2013-02-27T16:30:59.999999Z",
  217. "methods": [
  218. "password"
  219. ],
  220. "project": {
  221. "domain": {
  222. "id": "1789d1",
  223. "name": "example.com"
  224. },
  225. "id": "263fd9",
  226. "name": "project-x"
  227. },
  228. "roles": [
  229. {
  230. "id": "76e72a",
  231. "name": "admin"
  232. },
  233. {
  234. "id": "f4f392",
  235. "name": "member"
  236. }
  237. ],
  238. "service_providers": [
  239. {
  240. "auth_url":"https://example.com:5000/v3/OS-FEDERATION/identity_providers/acme/protocols/saml2/auth",
  241. "id": "sp1",
  242. "sp_url": "https://example.com:5000/Shibboleth.sso/SAML2/ECP"
  243. },
  244. {
  245. "auth_url":"https://other.example.com:5000/v3/OS-FEDERATION/identity_providers/acme/protocols/saml2/auth",
  246. "id": "sp2",
  247. "sp_url": "https://other.example.com:5000/Shibboleth.sso/SAML2/ECP"
  248. }
  249. ],
  250. "user": {
  251. "domain": {
  252. "id": "1789d1",
  253. "name": "example.com"
  254. },
  255. "id": "0ca8f6",
  256. "name": "Joe",
  257. "password_expires_at": "2016-11-06T15:32:17.000000"
  258. }
  259. }
  260. }
  261. `)
  262. })
  263. options := gophercloud.AuthOptions{
  264. Username: "me",
  265. Password: "secret",
  266. DomainID: "12345",
  267. IdentityEndpoint: th.Endpoint(),
  268. }
  269. pc, err := openstack.AuthenticatedClient(options)
  270. th.AssertNoErr(t, err)
  271. sc, err := openstack.NewIdentityV3(pc, gophercloud.EndpointOpts{
  272. Availability: gophercloud.AvailabilityAdmin,
  273. })
  274. th.AssertNoErr(t, err)
  275. th.CheckEquals(t, "http://localhost:35357/v3/", sc.Endpoint)
  276. }
  277. func testAuthenticatedClientFails(t *testing.T, endpoint string) {
  278. options := gophercloud.AuthOptions{
  279. Username: "me",
  280. Password: "secret",
  281. DomainName: "default",
  282. TenantName: "project",
  283. IdentityEndpoint: endpoint,
  284. }
  285. _, err := openstack.AuthenticatedClient(options)
  286. if err == nil {
  287. t.Fatal("expected error but call succeeded")
  288. }
  289. }
  290. func TestAuthenticatedClientV3Fails(t *testing.T) {
  291. testAuthenticatedClientFails(t, "http://bad-address.example.com/v3")
  292. }
  293. func TestAuthenticatedClientV2Fails(t *testing.T) {
  294. testAuthenticatedClientFails(t, "http://bad-address.example.com/v2.0")
  295. }