-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
106 lines (91 loc) · 3.62 KB
/
Copy pathserver_test.go
File metadata and controls
106 lines (91 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"html/template"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/go-git/go-git/v5"
)
func TestRequireWriteAuth(t *testing.T) {
sc := NewSmithy(t.TempDir())
handler := sc.RequireWriteAuth(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) })
response := httptest.NewRecorder()
handler(response, httptest.NewRequest(http.MethodPost, "/new", nil))
if response.Code != http.StatusServiceUnavailable {
t.Fatalf("without configured token: status = %d, want %d", response.Code, http.StatusServiceUnavailable)
}
sc.WriteToken = "secret"
response = httptest.NewRecorder()
handler(response, httptest.NewRequest(http.MethodPost, "/new", nil))
if response.Code != http.StatusUnauthorized || response.Header().Get("WWW-Authenticate") == "" {
t.Fatalf("without credentials: status = %d, challenge = %q", response.Code, response.Header().Get("WWW-Authenticate"))
}
request := httptest.NewRequest(http.MethodPost, "/new", nil)
request.SetBasicAuth("git", "secret")
response = httptest.NewRecorder()
handler(response, request)
if response.Code != http.StatusNoContent {
t.Fatalf("with valid credentials: status = %d, want %d", response.Code, http.StatusNoContent)
}
}
func TestTemplatesLoadWithQueryEscapingHelpers(t *testing.T) {
sc := NewSmithy(t.TempDir())
if err := sc.LoadTemplates(); err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
sc.IndexView(response, httptest.NewRequest(http.MethodGet, "/", nil))
if response.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", response.Code, http.StatusOK)
}
if got := response.Header().Get("Content-Type"); got != "text/html; charset=utf-8" {
t.Fatalf("Content-Type = %q", got)
}
}
func TestRenderDoesNotWritePartialTemplateOutput(t *testing.T) {
sc := NewSmithy(t.TempDir())
sc.template = template.Must(template.New("broken.html").Parse(`partial {{.Missing}}`))
response := httptest.NewRecorder()
sc.Render(response, "broken", struct{}{})
if response.Code != http.StatusInternalServerError {
t.Fatalf("status = %d, want %d", response.Code, http.StatusInternalServerError)
}
if strings.Contains(response.Body.String(), "partial") {
t.Fatalf("response contains partial template output: %q", response.Body.String())
}
}
func TestInfoRefsRejectsUnknownServiceBeforeStartingGit(t *testing.T) {
sc := NewSmithy(t.TempDir())
repoPath := filepath.Join(sc.Root, "repo.git")
repo, err := git.PlainInit(repoPath, true)
if err != nil {
t.Fatal(err)
}
sc.AddRepository(RepositoryWithName{Name: "repo.git", Path: repoPath, Repository: repo})
request := httptest.NewRequest(http.MethodGet, "/repo.git/info/refs?service=git-shell", nil)
request.SetPathValue("repo", "repo.git")
response := httptest.NewRecorder()
sc.getInfoRefs(response, request)
if response.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d", response.Code, http.StatusBadRequest)
}
}
func TestInfoRefsReturnsServerErrorWhenGitCannotStart(t *testing.T) {
sc := NewSmithy(t.TempDir())
sc.GitExecutable = filepath.Join(t.TempDir(), "missing-git")
repoPath := filepath.Join(sc.Root, "repo.git")
repo, err := git.PlainInit(repoPath, true)
if err != nil {
t.Fatal(err)
}
sc.AddRepository(RepositoryWithName{Name: "repo.git", Path: repoPath, Repository: repo})
request := httptest.NewRequest(http.MethodGet, "/repo.git/info/refs?service=git-upload-pack", nil)
request.SetPathValue("repo", "repo.git")
response := httptest.NewRecorder()
sc.getInfoRefs(response, request)
if response.Code != http.StatusInternalServerError {
t.Fatalf("status = %d, want %d", response.Code, http.StatusInternalServerError)
}
}