1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
19 "git.curoverse.com/arvados.git/sdk/go/httpserver"
20 check "gopkg.in/check.v1"
23 // Gocheck boilerplate
24 func Test(t *testing.T) {
28 var _ = check.Suite(&HandlerSuite{})
30 type HandlerSuite struct {
31 cluster *arvados.Cluster
35 func (s *HandlerSuite) SetUpTest(c *check.C) {
36 s.cluster = &arvados.Cluster{
38 PostgreSQL: integrationTestCluster().PostgreSQL,
39 NodeProfiles: map[string]arvados.NodeProfile{
41 Controller: arvados.SystemServiceInstance{Listen: ":"},
42 RailsAPI: arvados.SystemServiceInstance{Listen: os.Getenv("ARVADOS_TEST_API_HOST"), TLS: true, Insecure: true},
46 node := s.cluster.NodeProfiles["*"]
47 s.handler = newHandler(s.cluster, &node)
50 func (s *HandlerSuite) TestProxyDiscoveryDoc(c *check.C) {
51 req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
52 resp := httptest.NewRecorder()
53 s.handler.ServeHTTP(resp, req)
54 c.Check(resp.Code, check.Equals, http.StatusOK)
55 var dd arvados.DiscoveryDocument
56 err := json.Unmarshal(resp.Body.Bytes(), &dd)
57 c.Check(err, check.IsNil)
58 c.Check(dd.BlobSignatureTTL, check.Not(check.Equals), int64(0))
59 c.Check(dd.BlobSignatureTTL > 0, check.Equals, true)
60 c.Check(len(dd.Resources), check.Not(check.Equals), 0)
61 c.Check(len(dd.Schemas), check.Not(check.Equals), 0)
64 func (s *HandlerSuite) TestRequestTimeout(c *check.C) {
65 s.cluster.HTTPRequestTimeout = arvados.Duration(time.Nanosecond)
66 req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
67 resp := httptest.NewRecorder()
68 s.handler.ServeHTTP(resp, req)
69 c.Check(resp.Code, check.Equals, http.StatusBadGateway)
70 var jresp httpserver.ErrorResponse
71 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
72 c.Check(err, check.IsNil)
73 c.Assert(len(jresp.Errors), check.Equals, 1)
74 c.Check(jresp.Errors[0], check.Matches, `.*context deadline exceeded.*`)
77 func (s *HandlerSuite) TestProxyWithoutToken(c *check.C) {
78 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
79 resp := httptest.NewRecorder()
80 s.handler.ServeHTTP(resp, req)
81 c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
82 jresp := map[string]interface{}{}
83 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
84 c.Check(err, check.IsNil)
85 c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
88 func (s *HandlerSuite) TestProxyWithToken(c *check.C) {
89 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
90 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
91 resp := httptest.NewRecorder()
92 s.handler.ServeHTTP(resp, req)
93 c.Check(resp.Code, check.Equals, http.StatusOK)
95 err := json.Unmarshal(resp.Body.Bytes(), &u)
96 c.Check(err, check.IsNil)
97 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
100 func (s *HandlerSuite) TestProxyWithTokenInRequestBody(c *check.C) {
101 req := httptest.NewRequest("POST", "/arvados/v1/users/current", strings.NewReader(url.Values{
103 "api_token": {arvadostest.ActiveToken},
105 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
106 resp := httptest.NewRecorder()
107 s.handler.ServeHTTP(resp, req)
108 c.Check(resp.Code, check.Equals, http.StatusOK)
110 err := json.Unmarshal(resp.Body.Bytes(), &u)
111 c.Check(err, check.IsNil)
112 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
115 func (s *HandlerSuite) TestProxyNotFound(c *check.C) {
116 req := httptest.NewRequest("GET", "/arvados/v1/xyzzy", nil)
117 resp := httptest.NewRecorder()
118 s.handler.ServeHTTP(resp, req)
119 c.Check(resp.Code, check.Equals, http.StatusNotFound)
120 jresp := map[string]interface{}{}
121 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
122 c.Check(err, check.IsNil)
123 c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
126 func (s *HandlerSuite) TestProxyRedirect(c *check.C) {
127 req := httptest.NewRequest("GET", "https://0.0.0.0:1/login?return_to=foo", nil)
128 resp := httptest.NewRecorder()
129 s.handler.ServeHTTP(resp, req)
130 c.Check(resp.Code, check.Equals, http.StatusFound)
131 c.Check(resp.Header().Get("Location"), check.Matches, `https://0.0.0.0:1/auth/joshid\?return_to=foo&?`)