b8622ddd60a831ec5e82333e8c47f4706d65253e / internal/web/commits_test.go · 2955 bytes · raw
package web
import (
"net/http"
"strings"
"testing"
)
func TestCommitsListAndPrivateGate(t *testing.T) {
httpServer, _, dataDir := newTestServer(t)
loggedIn := newLoggedInClient(t, httpServer)
createRepo(t, loggedIn, httpServer, "pub", "public")
createRepo(t, loggedIn, httpServer, "sec", "private")
seedFiles(t, dataDir, "pub")
seedFiles(t, dataDir, "sec")
resp, err := (&http.Client{}).Get(httpServer.URL + "/josie/pub/commits")
if err != nil {
t.Fatalf("GET commits: %v", err)
}
body := readAll(t, resp)
if resp.StatusCode != http.StatusOK || !strings.Contains(body, "seed") {
t.Fatalf("commits page = %d, want 200 with subject: %q", resp.StatusCode, body)
}
if !strings.Contains(body, "/josie/pub/commit/") {
t.Error("no commit links")
}
resp, err = noFollowClient().Get(httpServer.URL + "/josie/sec/commits")
if err != nil {
t.Fatalf("GET private commits: %v", err)
}
readAll(t, resp)
if resp.StatusCode != http.StatusNotFound {
t.Errorf("anonymous private commits = %d, want 404 (no existence oracle)", resp.StatusCode)
}
resp, err = (&http.Client{}).Get(httpServer.URL + "/josie/pub/commits/nosuchbranch")
if err != nil {
t.Fatalf("GET bad ref: %v", err)
}
readAll(t, resp)
if resp.StatusCode != http.StatusNotFound {
t.Errorf("bad ref = %d, want 404", resp.StatusCode)
}
}
func TestCommitView(t *testing.T) {
httpServer, _, dataDir := newTestServer(t)
loggedIn := newLoggedInClient(t, httpServer)
createRepo(t, loggedIn, httpServer, "pub", "public")
seedFiles(t, dataDir, "pub")
resp, err := (&http.Client{}).Get(httpServer.URL + "/josie/pub/commits")
if err != nil {
t.Fatalf("GET commits: %v", err)
}
listing := readAll(t, resp)
start := strings.Index(listing, "/josie/pub/commit/")
if start < 0 {
t.Fatal("no commit link to follow")
}
rest := listing[start:]
sha := rest[len("/josie/pub/commit/"):]
sha = sha[:strings.IndexByte(sha, '"')]
resp, err = (&http.Client{}).Get(httpServer.URL + sha2href(sha))
if err != nil {
t.Fatalf("GET commit: %v", err)
}
body := readAll(t, resp)
if resp.StatusCode != http.StatusOK {
t.Fatalf("commit view = %d", resp.StatusCode)
}
for _, want := range []string{"seed", sha, "README", "browse files at this commit"} {
if !strings.Contains(body, want) {
t.Errorf("commit view lacks %q", want)
}
}
resp, err = (&http.Client{}).Get(httpServer.URL + "/josie/pub/commit/notahex!")
if err != nil {
t.Fatalf("GET bad sha: %v", err)
}
readAll(t, resp)
if resp.StatusCode != http.StatusNotFound {
t.Errorf("bad sha = %d, want 404", resp.StatusCode)
}
resp, err = (&http.Client{}).Get(httpServer.URL + "/josie/pub/commit/0000000000000000000000000000000000000000")
if err != nil {
t.Fatalf("GET missing sha: %v", err)
}
readAll(t, resp)
if resp.StatusCode != http.StatusNotFound {
t.Errorf("missing sha = %d, want 404", resp.StatusCode)
}
}
func sha2href(sha string) string { return "/josie/pub/commit/" + sha }