Merge branch '15227-apiserver-properties-bugfix'
[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         "context"
9         "encoding/json"
10         "net/http"
11         "net/http/httptest"
12         "net/url"
13         "os"
14         "strings"
15         "testing"
16         "time"
17
18         "git.curoverse.com/arvados.git/sdk/go/arvados"
19         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
20         "git.curoverse.com/arvados.git/sdk/go/ctxlog"
21         "git.curoverse.com/arvados.git/sdk/go/httpserver"
22         check "gopkg.in/check.v1"
23 )
24
25 // Gocheck boilerplate
26 func Test(t *testing.T) {
27         check.TestingT(t)
28 }
29
30 var _ = check.Suite(&HandlerSuite{})
31
32 type HandlerSuite struct {
33         cluster *arvados.Cluster
34         handler http.Handler
35         ctx     context.Context
36         cancel  context.CancelFunc
37 }
38
39 func (s *HandlerSuite) SetUpTest(c *check.C) {
40         s.ctx, s.cancel = context.WithCancel(context.Background())
41         s.ctx = ctxlog.Context(s.ctx, ctxlog.New(os.Stderr, "json", "debug"))
42         s.cluster = &arvados.Cluster{
43                 ClusterID:  "zzzzz",
44                 PostgreSQL: integrationTestCluster().PostgreSQL,
45                 NodeProfiles: map[string]arvados.NodeProfile{
46                         "*": {
47                                 Controller: arvados.SystemServiceInstance{Listen: ":"},
48                                 RailsAPI:   arvados.SystemServiceInstance{Listen: os.Getenv("ARVADOS_TEST_API_HOST"), TLS: true, Insecure: true},
49                         },
50                 },
51         }
52         node := s.cluster.NodeProfiles["*"]
53         s.handler = newHandler(s.ctx, s.cluster, &node, "")
54 }
55
56 func (s *HandlerSuite) TearDownTest(c *check.C) {
57         s.cancel()
58 }
59
60 func (s *HandlerSuite) TestProxyDiscoveryDoc(c *check.C) {
61         req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
62         resp := httptest.NewRecorder()
63         s.handler.ServeHTTP(resp, req)
64         c.Check(resp.Code, check.Equals, http.StatusOK)
65         var dd arvados.DiscoveryDocument
66         err := json.Unmarshal(resp.Body.Bytes(), &dd)
67         c.Check(err, check.IsNil)
68         c.Check(dd.BlobSignatureTTL, check.Not(check.Equals), int64(0))
69         c.Check(dd.BlobSignatureTTL > 0, check.Equals, true)
70         c.Check(len(dd.Resources), check.Not(check.Equals), 0)
71         c.Check(len(dd.Schemas), check.Not(check.Equals), 0)
72 }
73
74 func (s *HandlerSuite) TestRequestTimeout(c *check.C) {
75         s.cluster.HTTPRequestTimeout = arvados.Duration(time.Nanosecond)
76         req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
77         resp := httptest.NewRecorder()
78         s.handler.ServeHTTP(resp, req)
79         c.Check(resp.Code, check.Equals, http.StatusBadGateway)
80         var jresp httpserver.ErrorResponse
81         err := json.Unmarshal(resp.Body.Bytes(), &jresp)
82         c.Check(err, check.IsNil)
83         c.Assert(len(jresp.Errors), check.Equals, 1)
84         c.Check(jresp.Errors[0], check.Matches, `.*context deadline exceeded.*`)
85 }
86
87 func (s *HandlerSuite) TestProxyWithoutToken(c *check.C) {
88         req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
89         resp := httptest.NewRecorder()
90         s.handler.ServeHTTP(resp, req)
91         c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
92         jresp := map[string]interface{}{}
93         err := json.Unmarshal(resp.Body.Bytes(), &jresp)
94         c.Check(err, check.IsNil)
95         c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
96 }
97
98 func (s *HandlerSuite) TestProxyWithToken(c *check.C) {
99         req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
100         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
101         resp := httptest.NewRecorder()
102         s.handler.ServeHTTP(resp, req)
103         c.Check(resp.Code, check.Equals, http.StatusOK)
104         var u arvados.User
105         err := json.Unmarshal(resp.Body.Bytes(), &u)
106         c.Check(err, check.IsNil)
107         c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
108 }
109
110 func (s *HandlerSuite) TestProxyWithTokenInRequestBody(c *check.C) {
111         req := httptest.NewRequest("POST", "/arvados/v1/users/current", strings.NewReader(url.Values{
112                 "_method":   {"GET"},
113                 "api_token": {arvadostest.ActiveToken},
114         }.Encode()))
115         req.Header.Set("Content-type", "application/x-www-form-urlencoded")
116         resp := httptest.NewRecorder()
117         s.handler.ServeHTTP(resp, req)
118         c.Check(resp.Code, check.Equals, http.StatusOK)
119         var u arvados.User
120         err := json.Unmarshal(resp.Body.Bytes(), &u)
121         c.Check(err, check.IsNil)
122         c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
123 }
124
125 func (s *HandlerSuite) TestProxyNotFound(c *check.C) {
126         req := httptest.NewRequest("GET", "/arvados/v1/xyzzy", nil)
127         resp := httptest.NewRecorder()
128         s.handler.ServeHTTP(resp, req)
129         c.Check(resp.Code, check.Equals, http.StatusNotFound)
130         jresp := map[string]interface{}{}
131         err := json.Unmarshal(resp.Body.Bytes(), &jresp)
132         c.Check(err, check.IsNil)
133         c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
134 }
135
136 func (s *HandlerSuite) TestProxyRedirect(c *check.C) {
137         req := httptest.NewRequest("GET", "https://0.0.0.0:1/login?return_to=foo", nil)
138         resp := httptest.NewRecorder()
139         s.handler.ServeHTTP(resp, req)
140         c.Check(resp.Code, check.Equals, http.StatusFound)
141         c.Check(resp.Header().Get("Location"), check.Matches, `https://0.0.0.0:1/auth/joshid\?return_to=%2Cfoo&?`)
142 }
143
144 func (s *HandlerSuite) TestValidateV1APIToken(c *check.C) {
145         req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
146         user, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveToken)
147         c.Assert(err, check.IsNil)
148         c.Check(user.Authorization.UUID, check.Equals, arvadostest.ActiveTokenUUID)
149         c.Check(user.Authorization.APIToken, check.Equals, arvadostest.ActiveToken)
150         c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
151         c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
152 }
153
154 func (s *HandlerSuite) TestValidateV2APIToken(c *check.C) {
155         req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
156         user, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveTokenV2)
157         c.Assert(err, check.IsNil)
158         c.Check(user.Authorization.UUID, check.Equals, arvadostest.ActiveTokenUUID)
159         c.Check(user.Authorization.APIToken, check.Equals, arvadostest.ActiveToken)
160         c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
161         c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
162         c.Check(user.Authorization.TokenV2(), check.Equals, arvadostest.ActiveTokenV2)
163 }
164
165 func (s *HandlerSuite) TestCreateAPIToken(c *check.C) {
166         req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
167         auth, err := s.handler.(*Handler).createAPItoken(req, arvadostest.ActiveUserUUID, nil)
168         c.Assert(err, check.IsNil)
169         c.Check(auth.Scopes, check.DeepEquals, []string{"all"})
170
171         user, err := s.handler.(*Handler).validateAPItoken(req, auth.TokenV2())
172         c.Assert(err, check.IsNil)
173         c.Check(user.Authorization.UUID, check.Equals, auth.UUID)
174         c.Check(user.Authorization.APIToken, check.Equals, auth.APIToken)
175         c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
176         c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
177         c.Check(user.Authorization.TokenV2(), check.Equals, auth.TokenV2())
178 }