-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
153 lines (140 loc) · 4.23 KB
/
main_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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package issuesapp_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"path"
"testing"
"github.com/shurcooL/issues"
"github.com/shurcooL/issues/fs"
"github.com/shurcooL/issuesapp"
"github.com/shurcooL/reactions"
"github.com/shurcooL/users"
"github.com/shurcooL/webdavfs/vfsutil"
"golang.org/x/net/webdav"
)
func TestRoutes(t *testing.T) {
repo := issues.RepoSpec{URI: "example.org"}
issuesApp, err := mockIssuesApp(repo)
if err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
req = req.WithContext(context.WithValue(req.Context(), issuesapp.RepoSpecContextKey, repo))
req = req.WithContext(context.WithValue(req.Context(), issuesapp.BaseURIContextKey, "."))
issuesApp.ServeHTTP(w, req)
})
tests := []struct {
method, url string
wantCode int
}{
{"GET", "/assets/script.js", http.StatusOK},
{"POST", "/assets/script.js", http.StatusMethodNotAllowed},
{"GET", "/assets/gfm/gfm.css", http.StatusOK},
{"GET", "/", http.StatusOK},
{"POST", "/", http.StatusMethodNotAllowed},
{"GET", "/new", http.StatusOK},
{"PATCH", "/new", http.StatusMethodNotAllowed},
{"GET", "/1", http.StatusOK},
{"POST", "/1", http.StatusMethodNotAllowed},
{"GET", "/1/", http.StatusNotFound},
{"GET", "/1-foobar", http.StatusNotFound},
{"GET", "/2", http.StatusNotFound},
{"GET", "/foobar", http.StatusNotFound},
{"GET", "/1/edit", http.StatusMethodNotAllowed},
{"GET", "/1/comment", http.StatusMethodNotAllowed},
{"GET", "/1/foobar", http.StatusNotFound},
{"POST", "/1/comment/0", http.StatusNotFound},
{"GET", "/1/comment/foobar", http.StatusNotFound},
}
for _, tc := range tests {
req := httptest.NewRequest(tc.method, tc.url, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if got, want := w.Code, tc.wantCode; got != want {
t.Errorf("%s %q: got %v, want %v", tc.method, tc.url, http.StatusText(got), http.StatusText(want))
}
}
}
func mockIssuesApp(repo issues.RepoSpec) (http.Handler, error) {
mem := webdav.NewMemFS()
err := vfsutil.MkdirAll(context.Background(), mem, path.Join(repo.URI, "issues"), 0700)
if err != nil {
return nil, err
}
users := mockUsers{}
service, err := fs.NewService(mem, nil, nil, users)
if err != nil {
return nil, err
}
// Create a test issue with some reactions.
_, err = service.Create(context.Background(), repo, issues.Issue{
Title: "Some issue about something",
Comment: issues.Comment{
Body: "This is a test issue.",
},
Labels: []issues.Label{
{Name: "label", Color: issues.RGB{R: 224, G: 235, B: 245}},
{Name: "another", Color: issues.RGB{R: 224, G: 235, B: 245}},
},
})
if err != nil {
return nil, err
}
for _, reaction := range []reactions.EmojiID{"grinning", "+1", "construction_worker"} {
_, err = service.EditComment(context.Background(), repo, 1, issues.CommentRequest{
ID: 0,
Reaction: &reaction,
})
if err != nil {
return nil, err
}
}
for _, state := range []issues.State{issues.ClosedState, issues.OpenState} {
_, _, err = service.Edit(context.Background(), repo, 1, issues.IssueRequest{
State: &state,
})
if err != nil {
return nil, err
}
}
_, err = service.CreateComment(context.Background(), repo, 1, issues.Comment{
Body: "This is a test comment.",
})
if err != nil {
return nil, err
}
return issuesapp.New(service, users, issuesapp.Options{}), nil
}
type mockUsers struct {
users.Service
}
func (mockUsers) Get(_ context.Context, user users.UserSpec) (users.User, error) {
switch {
case user == users.UserSpec{ID: 1, Domain: "example.org"}:
return users.User{
UserSpec: user,
Login: "gopher",
Name: "Sample Gopher",
Email: "[email protected]",
AvatarURL: "https://avatars0.githubusercontent.com/u/8566911?v=4&s=32",
}, nil
default:
return users.User{}, fmt.Errorf("user %v not found", user)
}
}
func (mockUsers) GetAuthenticatedSpec(_ context.Context) (users.UserSpec, error) {
return users.UserSpec{ID: 1, Domain: "example.org"}, nil
}
func (m mockUsers) GetAuthenticated(ctx context.Context) (users.User, error) {
userSpec, err := m.GetAuthenticatedSpec(ctx)
if err != nil {
return users.User{}, err
}
if userSpec.ID == 0 {
return users.User{}, nil
}
return m.Get(ctx, userSpec)
}