14259: Test updates & additions related to keep client HEAD requests
[arvados.git] / lib / controller / handler_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package controller
6
7 import (
8         "encoding/json"
9         "net/http"
10         "net/http/httptest"
11         "net/url"
12         "os"
13         "strings"
14         "testing"
15         "time"
16
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"
21 )
22
23 // Gocheck boilerplate
24 func Test(t *testing.T) {
25         check.TestingT(t)
26 }
27
28 var _ = check.Suite(&HandlerSuite{})
29
30 type HandlerSuite struct {
31         cluster *arvados.Cluster
32         handler http.Handler
33 }
34
35 func (s *HandlerSuite) SetUpTest(c *check.C) {
36         s.cluster = &arvados.Cluster{
37                 ClusterID:  "zzzzz",
38                 PostgreSQL: integrationTestCluster().PostgreSQL,
39                 NodeProfiles: map[string]arvados.NodeProfile{
40                         "*": {
41                                 Controller: arvados.SystemServiceInstance{Listen: ":"},
42                                 RailsAPI:   arvados.SystemServiceInstance{Listen: os.Getenv("ARVADOS_TEST_API_HOST"), TLS: true, Insecure: true},
43                         },
44                 },
45         }
46         node := s.cluster.NodeProfiles["*"]
47         s.handler = newHandler(s.cluster, &node)
48 }
49
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)
62 }
63
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.*`)
75 }
76
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{}{})
86 }
87
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)
94         var u arvados.User
95         err := json.Unmarshal(resp.Body.Bytes(), &u)
96         c.Check(err, check.IsNil)
97         c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
98 }
99
100 func (s *HandlerSuite) TestProxyWithTokenInRequestBody(c *check.C) {
101         req := httptest.NewRequest("POST", "/arvados/v1/users/current", strings.NewReader(url.Values{
102                 "_method":   {"GET"},
103                 "api_token": {arvadostest.ActiveToken},
104         }.Encode()))
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)
109         var u arvados.User
110         err := json.Unmarshal(resp.Body.Bytes(), &u)
111         c.Check(err, check.IsNil)
112         c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
113 }
114
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{}{})
124 }
125
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&?`)
132 }