-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup_git_http_test.go
More file actions
151 lines (137 loc) · 4.76 KB
/
signup_git_http_test.go
File metadata and controls
151 lines (137 loc) · 4.76 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
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
package gobookmarks
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
"github.com/gorilla/sessions"
)
func TestGitSignupScenario(t *testing.T) {
tmp := t.TempDir()
Config.LocalGitPath = tmp
Config.SessionName = "testsession"
SessionStore = sessions.NewCookieStore([]byte("secret"))
// signup
form := url.Values{"username": {"alice"}, "password": {"secret"}}
req := httptest.NewRequest("POST", "/signup/git", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
if err := GitSignupAction(w, req); err != nil {
t.Fatalf("signup action: %v", err)
}
if _, err := os.Stat(passwordPath("alice")); err != nil {
t.Fatalf("password not created: %v", err)
}
p := GitProvider{}
if ok, err := p.RepoExists(context.Background(), "alice", nil, Config.GetRepoName()); err != nil || !ok {
t.Fatalf("repo exists: %v %v", ok, err)
}
got, _, err := p.GetBookmarks(context.Background(), "alice", "refs/heads/main", nil)
if err != nil {
t.Fatalf("get bookmarks: %v", err)
}
if got != defaultBookmarks {
t.Fatalf("bookmarks mismatch")
}
// login
req = httptest.NewRequest("POST", "/login/git", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w = httptest.NewRecorder()
if err := GitLoginAction(w, req); err != nil {
t.Fatalf("login action: %v", err)
}
cookies := w.Result().Cookies()
if len(cookies) == 0 {
t.Fatalf("no session cookie")
}
// obtain session from cookie for further actions
sessReq := httptest.NewRequest("GET", "/", nil)
sessReq.AddCookie(cookies[len(cookies)-1])
sessW := httptest.NewRecorder()
session, err := getSession(sessW, sessReq)
if err != nil {
t.Fatalf("getSession: %v", err)
}
ctx := context.WithValue(sessReq.Context(), ContextValues("session"), session)
ctx = context.WithValue(ctx, ContextValues("provider"), "git")
ctx = context.WithValue(ctx, ContextValues("coreData"), &CoreData{})
// create bookmarks on new branch
createText := "Category: New\nhttp://example.com new"
createForm := url.Values{"text": {createText}, "branch": {"feature"}}
req = httptest.NewRequest("POST", "/edit/create", strings.NewReader(createForm.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req = req.WithContext(ctx)
w = httptest.NewRecorder()
if err := BookmarksEditCreateAction(w, req); err != nil {
t.Fatalf("create action: %v", err)
}
got, _, err = p.GetBookmarks(context.Background(), "alice", "refs/heads/feature", nil)
if err != nil {
t.Fatalf("get feature bookmarks: %v", err)
}
if got != createText {
t.Fatalf("created bookmarks mismatch")
}
// update bookmarks on main branch
updated := "Category: Updated\nhttp://example.com updated"
_, sha, err := p.GetBookmarks(context.Background(), "alice", "refs/heads/main", nil)
if err != nil {
t.Fatalf("get bookmarks sha: %v", err)
}
saveForm := url.Values{"text": {updated}, "branch": {"main"}, "ref": {"refs/heads/main"}, "sha": {sha}}
req = httptest.NewRequest("POST", "/edit/save", strings.NewReader(saveForm.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req = req.WithContext(ctx)
w = httptest.NewRecorder()
if err := BookmarksEditSaveAction(w, req); err != nil {
t.Fatalf("save action: %v", err)
}
got, _, err = p.GetBookmarks(context.Background(), "alice", "refs/heads/main", nil)
if err != nil {
t.Fatalf("get updated bookmarks: %v", err)
}
if got != updated {
t.Fatalf("updated bookmarks mismatch")
}
// logout
req = httptest.NewRequest("POST", "/logout", nil)
req = req.WithContext(ctx)
w = httptest.NewRecorder()
if err := UserLogoutAction(w, req); err != nil {
t.Fatalf("logout: %v", err)
}
if _, ok := session.Values["GithubUser"]; ok {
t.Fatalf("user not cleared")
}
}
func TestGitLoginIgnoresInvalidSession(t *testing.T) {
tmp := t.TempDir()
Config.LocalGitPath = tmp
Config.SessionName = "testsession"
SessionStore = sessions.NewCookieStore([]byte("secret"))
version = "vtest"
// create user
p := GitProvider{}
ctx := context.Background()
if err := p.CreateUser(ctx, "alice", "secret"); err != nil {
t.Fatalf("create user: %v", err)
}
form := url.Values{"username": {"alice"}, "password": {"secret"}}
req := httptest.NewRequest("POST", "/login/git", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.AddCookie(&http.Cookie{Name: Config.SessionName, Value: "invalid"})
w := httptest.NewRecorder()
if err := GitLoginAction(w, req); err != nil {
t.Fatalf("login action: %v", err)
}
cookies := w.Result().Cookies()
if len(cookies) == 0 {
t.Fatalf("expected session cookie")
}
if cookies[len(cookies)-1].Value == "invalid" {
t.Fatalf("session cookie not replaced")
}
}