1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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"
25 // Gocheck boilerplate
26 func Test(t *testing.T) {
30 var _ = check.Suite(&HandlerSuite{})
32 type HandlerSuite struct {
33 cluster *arvados.Cluster
36 cancel context.CancelFunc
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{
44 PostgreSQL: integrationTestCluster().PostgreSQL,
45 NodeProfiles: map[string]arvados.NodeProfile{
47 Controller: arvados.SystemServiceInstance{Listen: ":"},
48 RailsAPI: arvados.SystemServiceInstance{Listen: os.Getenv("ARVADOS_TEST_API_HOST"), TLS: true, Insecure: true},
52 node := s.cluster.NodeProfiles["*"]
53 s.handler = newHandler(s.ctx, s.cluster, &node, "")
56 func (s *HandlerSuite) TearDownTest(c *check.C) {
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)
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.*`)
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{}{})
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)
105 err := json.Unmarshal(resp.Body.Bytes(), &u)
106 c.Check(err, check.IsNil)
107 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
110 func (s *HandlerSuite) TestProxyWithTokenInRequestBody(c *check.C) {
111 req := httptest.NewRequest("POST", "/arvados/v1/users/current", strings.NewReader(url.Values{
113 "api_token": {arvadostest.ActiveToken},
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)
120 err := json.Unmarshal(resp.Body.Bytes(), &u)
121 c.Check(err, check.IsNil)
122 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
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{}{})
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&?`)
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)
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)
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"})
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())