-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategory_edit_http_test.go
More file actions
94 lines (88 loc) · 3.42 KB
/
category_edit_http_test.go
File metadata and controls
94 lines (88 loc) · 3.42 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
package gobookmarks
import (
"context"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/gorilla/sessions"
"golang.org/x/oauth2"
)
func setupCategoryEditTest(t *testing.T) (GitProvider, string, *sessions.Session, context.Context) {
tmp := t.TempDir()
Config.LocalGitPath = tmp
p := GitProvider{}
user := "alice"
if err := p.CreateRepo(context.Background(), user, nil, Config.GetRepoName()); err != nil {
t.Fatalf("CreateRepo: %v", err)
}
Config.SessionName = "testsession"
SessionStore = sessions.NewCookieStore([]byte("secret"))
sessReq := httptest.NewRequest("GET", "/", nil)
sess, err := getSession(httptest.NewRecorder(), sessReq)
if err != nil {
t.Fatalf("getSession: %v", err)
}
sess.Values["GithubUser"] = &User{Login: user}
sess.Values["Token"] = &oauth2.Token{}
ctx := context.WithValue(sessReq.Context(), ContextValues("session"), sess)
ctx = context.WithValue(ctx, ContextValues("provider"), "git")
ctx = context.WithValue(ctx, ContextValues("coreData"), &CoreData{})
return p, user, sess, ctx
}
func TestCategoryEditSaveAction(t *testing.T) {
p, user, _, ctx := setupCategoryEditTest(t)
original := "Category: First\nhttp://one.com one\nCategory: Second\nhttp://two.com two\n"
if err := p.CreateBookmarks(context.Background(), user, nil, "main", original); err != nil {
t.Fatalf("CreateBookmarks: %v", err)
}
_, sha, err := p.GetBookmarks(context.Background(), user, "refs/heads/main", nil)
if err != nil {
t.Fatalf("GetBookmarks: %v", err)
}
newSection := "Category: Second\nhttp://changed.com x"
form := url.Values{"text": {newSection}, "branch": {"main"}, "ref": {"refs/heads/main"}, "sha": {sha}}
req := httptest.NewRequest("POST", "/editCategory?index=1", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req = req.WithContext(ctx)
w := httptest.NewRecorder()
if err := CategoryEditSaveAction(w, req); err != nil {
t.Fatalf("CategoryEditSaveAction: %v", err)
}
got, _, err := p.GetBookmarks(context.Background(), user, "refs/heads/main", nil)
if err != nil {
t.Fatalf("GetBookmarks after: %v", err)
}
expected := "Category: First\nhttp://one.com one\n" + newSection
if got != expected {
t.Fatalf("expected %q got %q", expected, got)
}
}
func TestCategoryEditSaveActionAnonymous(t *testing.T) {
p, user, _, ctx := setupCategoryEditTest(t)
original := "Category:\nhttp://one.com\nCategory: Named\nhttp://two.com\n"
if err := p.CreateBookmarks(context.Background(), user, nil, "main", original); err != nil {
t.Fatalf("CreateBookmarks: %v", err)
}
_, sha, err := p.GetBookmarks(context.Background(), user, "refs/heads/main", nil)
if err != nil {
t.Fatalf("GetBookmarks: %v", err)
}
newSection := "Category:\nhttp://changed.com"
form := url.Values{"text": {newSection}, "branch": {"main"}, "ref": {"refs/heads/main"}, "sha": {sha}}
req := httptest.NewRequest("POST", "/editCategory?index=0", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req = req.WithContext(ctx)
w := httptest.NewRecorder()
if err := CategoryEditSaveAction(w, req); err != nil {
t.Fatalf("CategoryEditSaveAction: %v", err)
}
got, _, err := p.GetBookmarks(context.Background(), user, "refs/heads/main", nil)
if err != nil {
t.Fatalf("GetBookmarks after: %v", err)
}
expected := newSection + "\nCategory: Named\nhttp://two.com\n"
if got != expected {
t.Fatalf("expected %q got %q", expected, got)
}
}