16387: Allow batch update to set is_active=false for a remote user.
[arvados.git] / services / arv-git-httpd / git_handler_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "net/http"
9         "net/http/httptest"
10         "net/url"
11         "regexp"
12
13         "git.arvados.org/arvados.git/lib/config"
14         "git.arvados.org/arvados.git/sdk/go/arvados"
15         "git.arvados.org/arvados.git/sdk/go/ctxlog"
16         check "gopkg.in/check.v1"
17 )
18
19 var _ = check.Suite(&GitHandlerSuite{})
20
21 type GitHandlerSuite struct {
22         cluster *arvados.Cluster
23 }
24
25 func (s *GitHandlerSuite) SetUpTest(c *check.C) {
26         cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
27         c.Assert(err, check.Equals, nil)
28         s.cluster, err = cfg.GetCluster("")
29         c.Assert(err, check.Equals, nil)
30
31         s.cluster.Services.GitHTTP.InternalURLs = map[arvados.URL]arvados.ServiceInstance{arvados.URL{Host: "localhost:80"}: arvados.ServiceInstance{}}
32         s.cluster.Git.GitoliteHome = "/test/ghh"
33         s.cluster.Git.Repositories = "/"
34 }
35
36 func (s *GitHandlerSuite) TestEnvVars(c *check.C) {
37         u, err := url.Parse("git.zzzzz.arvadosapi.com/test")
38         c.Check(err, check.Equals, nil)
39         resp := httptest.NewRecorder()
40         req := &http.Request{
41                 Method:     "GET",
42                 URL:        u,
43                 RemoteAddr: "[::1]:12345",
44         }
45         h := newGitHandler(s.cluster)
46         h.(*gitHandler).Path = "/bin/sh"
47         h.(*gitHandler).Args = []string{"-c", "printf 'Content-Type: text/plain\r\n\r\n'; env"}
48
49         h.ServeHTTP(resp, req)
50
51         c.Check(resp.Code, check.Equals, http.StatusOK)
52         body := resp.Body.String()
53         c.Check(body, check.Matches, `(?ms).*^PATH=.*:/test/ghh/bin$.*`)
54         c.Check(body, check.Matches, `(?ms).*^GITOLITE_HTTP_HOME=/test/ghh$.*`)
55         c.Check(body, check.Matches, `(?ms).*^GL_BYPASS_ACCESS_CHECKS=1$.*`)
56         c.Check(body, check.Matches, `(?ms).*^REMOTE_HOST=::1$.*`)
57         c.Check(body, check.Matches, `(?ms).*^REMOTE_PORT=12345$.*`)
58         c.Check(body, check.Matches, `(?ms).*^SERVER_ADDR=`+regexp.QuoteMeta("localhost:80")+`$.*`)
59 }
60
61 func (s *GitHandlerSuite) TestCGIErrorOnSplitHostPortError(c *check.C) {
62         u, err := url.Parse("git.zzzzz.arvadosapi.com/test")
63         c.Check(err, check.Equals, nil)
64         resp := httptest.NewRecorder()
65         req := &http.Request{
66                 Method:     "GET",
67                 URL:        u,
68                 RemoteAddr: "test.bad.address.missing.port",
69         }
70         h := newGitHandler(s.cluster)
71         h.ServeHTTP(resp, req)
72         c.Check(resp.Code, check.Equals, http.StatusInternalServerError)
73         c.Check(resp.Body.String(), check.Equals, "")
74 }