arche / internal/archesrv/repo_access_test.go

commit 154431fd
  1package archesrv
  2
  3import (
  4	"net/http"
  5	"testing"
  6)
  7
  8func TestForgeServer_Index_LoadsAfterSetup(t *testing.T) {
  9	s, ts := newTestServer(t)
 10	_, client := loginAsAdmin(t, s, ts)
 11
 12	resp, err := client.Get(ts.URL + "/")
 13	if err != nil {
 14		t.Fatalf("GET /: %v", err)
 15	}
 16	defer resp.Body.Close()
 17	if resp.StatusCode != http.StatusOK {
 18		t.Errorf("index after setup: want 200, got %d", resp.StatusCode)
 19	}
 20}
 21
 22func TestForgeServer_Index_RedirectsToSetupWhenEmpty(t *testing.T) {
 23	_, ts := newTestServer(t)
 24
 25	client := &http.Client{
 26		CheckRedirect: func(req *http.Request, via []*http.Request) error {
 27			return http.ErrUseLastResponse
 28		},
 29	}
 30
 31	resp, err := client.Get(ts.URL + "/")
 32	if err != nil {
 33		t.Fatalf("GET /: %v", err)
 34	}
 35	resp.Body.Close()
 36	if resp.StatusCode != http.StatusFound && resp.StatusCode != http.StatusMovedPermanently {
 37		t.Errorf("empty DB: want redirect, got %d", resp.StatusCode)
 38	}
 39	loc := resp.Header.Get("Location")
 40	if loc != "/setup" {
 41		t.Errorf("redirect target: want /setup, got %q", loc)
 42	}
 43}
 44
 45func TestForgeServer_RepoHome_RedirectsToLog(t *testing.T) {
 46	s, ts := newTestServer(t)
 47	_, client := loginAsAdmin(t, s, ts)
 48	setupRepoWithDisk(t, s, "myrepo", "private")
 49
 50	noRedir := *client
 51	noRedir.CheckRedirect = func(req *http.Request, via []*http.Request) error {
 52		return http.ErrUseLastResponse
 53	}
 54
 55	resp, err := noRedir.Get(ts.URL + "/myrepo")
 56	if err != nil {
 57		t.Fatalf("GET /myrepo: %v", err)
 58	}
 59	resp.Body.Close()
 60	if resp.StatusCode != http.StatusFound && resp.StatusCode != http.StatusMovedPermanently {
 61		t.Errorf("repo home: want redirect, got %d", resp.StatusCode)
 62	}
 63	loc := resp.Header.Get("Location")
 64	if loc != "/myrepo/log" {
 65		t.Errorf("redirect target: want /myrepo/log, got %q", loc)
 66	}
 67}
 68
 69func TestForgeServer_PrivateRepo_DeniesAnon(t *testing.T) {
 70	s, ts := newTestServer(t)
 71	_, _ = loginAsAdmin(t, s, ts)
 72	setupRepoWithDisk(t, s, "myrepo", "private")
 73
 74	anon := &http.Client{}
 75	for _, path := range []string{
 76		"/myrepo/log",
 77		"/myrepo/issues",
 78		"/myrepo/wiki",
 79	} {
 80		resp, err := anon.Get(ts.URL + path)
 81		if err != nil {
 82			t.Fatalf("GET %s: %v", path, err)
 83		}
 84		resp.Body.Close()
 85		if resp.StatusCode != http.StatusUnauthorized {
 86			t.Errorf("%s anon on private repo: want 401, got %d", path, resp.StatusCode)
 87		}
 88	}
 89}
 90
 91func TestForgeServer_PublicRepo_AllowsAnon(t *testing.T) {
 92	s, ts := newTestServer(t)
 93	_, _ = loginAsAdmin(t, s, ts)
 94	setupRepoWithDisk(t, s, "mypub", "public")
 95
 96	anon := &http.Client{}
 97	for _, path := range []string{
 98		"/mypub/issues",
 99		"/mypub/wiki",
100	} {
101		resp, err := anon.Get(ts.URL + path)
102		if err != nil {
103			t.Fatalf("GET %s: %v", path, err)
104		}
105		resp.Body.Close()
106		if resp.StatusCode != http.StatusOK {
107			t.Errorf("%s anon on public repo: want 200, got %d", path, resp.StatusCode)
108		}
109	}
110}
111
112func TestForgeServer_MissingRepo_Returns404(t *testing.T) {
113	s, ts := newTestServer(t)
114	_, client := loginAsAdmin(t, s, ts)
115
116	for _, path := range []string{
117		"/doesnotexist/issues",
118		"/doesnotexist/wiki",
119		"/doesnotexist/log",
120	} {
121		resp, err := client.Get(ts.URL + path)
122		if err != nil {
123			t.Fatalf("GET %s: %v", path, err)
124		}
125		resp.Body.Close()
126		if resp.StatusCode != http.StatusNotFound {
127			t.Errorf("%s: want 404, got %d", path, resp.StatusCode)
128		}
129	}
130}