Merge branch '15648-centos7-upgrade-to-rh-python36'
[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.curoverse.com/arvados.git/lib/config"
14         "git.curoverse.com/arvados.git/sdk/go/arvados"
15         check "gopkg.in/check.v1"
16 )
17
18 var _ = check.Suite(&GitHandlerSuite{})
19
20 type GitHandlerSuite struct {
21         cluster *arvados.Cluster
22 }
23
24 func (s *GitHandlerSuite) SetUpTest(c *check.C) {
25         cfg, err := config.NewLoader(nil, nil).Load()
26         c.Assert(err, check.Equals, nil)
27         s.cluster, err = cfg.GetCluster("")
28         c.Assert(err, check.Equals, nil)
29
30         s.cluster.Services.GitHTTP.InternalURLs = map[arvados.URL]arvados.ServiceInstance{arvados.URL{Host: "localhost:80"}: arvados.ServiceInstance{}}
31         s.cluster.Git.GitoliteHome = "/test/ghh"
32         s.cluster.Git.Repositories = "/"
33 }
34
35 func (s *GitHandlerSuite) TestEnvVars(c *check.C) {
36         u, err := url.Parse("git.zzzzz.arvadosapi.com/test")
37         c.Check(err, check.Equals, nil)
38         resp := httptest.NewRecorder()
39         req := &http.Request{
40                 Method:     "GET",
41                 URL:        u,
42                 RemoteAddr: "[::1]:12345",
43         }
44         h := newGitHandler(s.cluster)
45         h.(*gitHandler).Path = "/bin/sh"
46         h.(*gitHandler).Args = []string{"-c", "printf 'Content-Type: text/plain\r\n\r\n'; env"}
47
48         h.ServeHTTP(resp, req)
49
50         c.Check(resp.Code, check.Equals, http.StatusOK)
51         body := resp.Body.String()
52         c.Check(body, check.Matches, `(?ms).*^PATH=.*:/test/ghh/bin$.*`)
53         c.Check(body, check.Matches, `(?ms).*^GITOLITE_HTTP_HOME=/test/ghh$.*`)
54         c.Check(body, check.Matches, `(?ms).*^GL_BYPASS_ACCESS_CHECKS=1$.*`)
55         c.Check(body, check.Matches, `(?ms).*^REMOTE_HOST=::1$.*`)
56         c.Check(body, check.Matches, `(?ms).*^REMOTE_PORT=12345$.*`)
57         c.Check(body, check.Matches, `(?ms).*^SERVER_ADDR=`+regexp.QuoteMeta("localhost:80")+`$.*`)
58 }
59
60 func (s *GitHandlerSuite) TestCGIErrorOnSplitHostPortError(c *check.C) {
61         u, err := url.Parse("git.zzzzz.arvadosapi.com/test")
62         c.Check(err, check.Equals, nil)
63         resp := httptest.NewRecorder()
64         req := &http.Request{
65                 Method:     "GET",
66                 URL:        u,
67                 RemoteAddr: "test.bad.address.missing.port",
68         }
69         h := newGitHandler(s.cluster)
70         h.ServeHTTP(resp, req)
71         c.Check(resp.Code, check.Equals, http.StatusInternalServerError)
72         c.Check(resp.Body.String(), check.Equals, "")
73 }