Skip to content

Commit 2ee4be6

Browse files
author
Lei Pi
committed
checkin
1 parent ea4a2aa commit 2ee4be6

File tree

11 files changed

+252
-63
lines changed

11 files changed

+252
-63
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ build
22
testBuild
33
db/*.db
44
.vscode
5+
.idea

service/api_impl.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@ func newNotes(pn *hlcmsg.Pagenote) *hlcmsg.IdList {
2020
func getNotes(pn *hlcmsg.Pagenote) *hlcmsg.Pagenote {
2121
if pn.Uid > 0 {
2222
return storage.QueryPagenote(pn.Uid, pn.Pageid)
23-
} else {
24-
util.Log("error, empty uid not supported")
25-
return nil
2623
}
24+
util.Log("error, empty uid not supported")
25+
return nil
2726
}
2827

2928
func parseNewNotesRequest(r *http.Request) *hlcmsg.Pagenote {
3029
pn := &hlcmsg.Pagenote{}
3130
payload, err := readRequestPayload(r)
3231
if err != nil {
32+
util.Debug("error loading payload", err)
33+
return nil
34+
}
35+
if payload == nil || len(payload) == 0 {
36+
util.Debug("empty payload", err)
3337
return nil
3438
}
3539
if err = proto.Unmarshal(payload, pn); err != nil {

service/api_impl_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
11
package service
22

33
import "testing"
4+
import "github.com/lpimem/hlcsrv/hlcmsg"
5+
import "github.com/lpimem/hlcsrv/storage"
6+
7+
func testGetPagenote(t *testing.T, uid uint32, pid uint32, count uint32) {
8+
storage.ResetTestDb()
9+
req := &hlcmsg.Pagenote{
10+
Uid: uid,
11+
Pageid: pid,
12+
}
13+
note := getNotes(req)
14+
if count == 0 {
15+
if note != nil {
16+
t.Error("expecting nil, got", note)
17+
t.Fail()
18+
}
19+
return
20+
}
21+
if note == nil {
22+
t.Error("note should be not nil")
23+
t.Fail()
24+
return
25+
}
26+
if len(note.Highlights) != int(count) {
27+
t.Error("note should contain", count, "highlights")
28+
t.Fail()
29+
return
30+
}
31+
}
32+
33+
func TestGetNote(t *testing.T) {
34+
for _, tc := range [][]uint32{
35+
[]uint32{1, 0, 1},
36+
[]uint32{1, 1, 1},
37+
[]uint32{2, 1, 0},
38+
} {
39+
testGetPagenote(t, tc[0], tc[1], tc[2])
40+
}
41+
}
442

543
func TestCleanUrl(t *testing.T) {
644
tcs := [][]string{

service/api_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package service
2+
3+
import (
4+
"net/http/httptest"
5+
"testing"
6+
7+
"net/http"
8+
9+
"io/ioutil"
10+
11+
"bytes"
12+
13+
"github.com/golang/protobuf/proto"
14+
"github.com/lpimem/hlcsrv/hlcmsg"
15+
"github.com/lpimem/hlcsrv/storage"
16+
)
17+
18+
func TestNewPagenote(t *testing.T) {
19+
const url = "/pagenote/new"
20+
var w *httptest.ResponseRecorder
21+
// post only
22+
get := httptest.NewRequest("GET", url, nil)
23+
w = httptest.NewRecorder()
24+
savePagenote(w, get)
25+
if w.Code != http.StatusBadRequest {
26+
t.Error("Expecting", http.StatusBadRequest, "got", w.Code, w.Body.String())
27+
}
28+
29+
// post empty
30+
var post *http.Request
31+
post = httptest.NewRequest("POST", url, nil)
32+
w = httptest.NewRecorder()
33+
savePagenote(w, post)
34+
if w.Code == http.StatusOK {
35+
t.Error("null request body shouldn't get accepted")
36+
t.Fail()
37+
}
38+
// post normal
39+
reqPn := &hlcmsg.Pagenote{
40+
Uid: 1,
41+
Pageid: 1,
42+
Url: "http://example.com",
43+
Highlights: []*hlcmsg.RangeMeta{
44+
&hlcmsg.RangeMeta{
45+
Anchor: "/",
46+
Start: "/1",
47+
StartOffset: 0,
48+
End: "/2",
49+
EndOffset: 3,
50+
},
51+
},
52+
}
53+
buf, _ := proto.Marshal(reqPn)
54+
reader := bytes.NewReader(buf)
55+
post = httptest.NewRequest("POST", url, reader)
56+
w = httptest.NewRecorder()
57+
savePagenote(w, post)
58+
if w.Code != http.StatusOK {
59+
t.Error("valid new pn request failed", w.Body.String())
60+
t.Fail()
61+
return
62+
}
63+
respBody := w.Body.Bytes()
64+
pnResp := &hlcmsg.HlcResp{}
65+
err := proto.Unmarshal(respBody, pnResp)
66+
if err != nil {
67+
t.Error("cannot parse response body")
68+
t.Fail()
69+
}
70+
if pnResp.Code != hlcmsg.HlcResp_SUC {
71+
t.Error("valid new pn request failed", pnResp.Msg)
72+
t.Fail()
73+
}
74+
if pnResp.IdList == nil || len(pnResp.IdList.Arr) != 1 {
75+
t.Error("Failed to get ", 1, "created id")
76+
t.Fail()
77+
}
78+
}
79+
80+
func TestGetPageNote(t *testing.T) {
81+
req := httptest.NewRequest("GET", "/pagenote?uid=1", nil)
82+
recorder := httptest.NewRecorder()
83+
getPagenote(recorder, req)
84+
httpResp := recorder.Result()
85+
if httpResp.StatusCode != http.StatusOK {
86+
t.Error("response code should be ", http.StatusOK, "got", httpResp.StatusCode, httpResp.Status)
87+
t.Fail()
88+
}
89+
if recorder.Body == nil {
90+
t.Error("response body shouldn't be nil ")
91+
t.Fail()
92+
}
93+
buf, err := ioutil.ReadAll(httpResp.Body)
94+
if err != nil {
95+
t.Error("cannot read resp body", err)
96+
t.Fail()
97+
return
98+
}
99+
if len(buf) < 1 {
100+
t.Error("response body buf shouldn't be empty ")
101+
t.Fail()
102+
}
103+
resp := &hlcmsg.HlcResp{}
104+
proto.Unmarshal(buf, resp)
105+
if resp.Code != hlcmsg.HlcResp_SUC {
106+
t.Error("response code should be ", hlcmsg.HlcResp_SUC, "got", resp.Code)
107+
t.Fail()
108+
}
109+
if len(resp.PagenoteList) < 1 || len(resp.PagenoteList[0].Highlights) < 1 {
110+
t.Error("parsed response page list should contain 1 range meta")
111+
t.Fail()
112+
}
113+
}
114+
115+
func init() {
116+
storage.ResetTestDb()
117+
}

service/checker.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package service
22

33
import "net/http"
44

5-
type ReqCookieCheckerBuilder []string
5+
type ReqCookieCheckerBuilder struct {
6+
headers []string
7+
}
68

7-
func (builder ReqCookieCheckerBuilder) Require(key string) {
8-
builder = append(builder, key)
9+
func (builder *ReqCookieCheckerBuilder) Require(key string) {
10+
builder.headers = append(builder.headers, key)
911
}
1012

1113
func (builder ReqCookieCheckerBuilder) Build() RequestInterceptor {
1214
return func(req *http.Request) error {
13-
for _, expect := range builder {
15+
for _, expect := range builder.headers {
1416
if _, err := req.Cookie(expect); err != nil {
1517
return err
1618
}
@@ -22,8 +24,7 @@ func (builder ReqCookieCheckerBuilder) Build() RequestInterceptor {
2224
func RequirePost(w http.ResponseWriter, r *http.Request) bool {
2325
if r.Method == http.MethodPost {
2426
return true
25-
} else {
26-
http.Error(w, "only post accepted", http.StatusBadRequest)
27-
return false
2827
}
28+
http.Error(w, "only post accepted", http.StatusBadRequest)
29+
return false
2930
}

storage/pagenote_dict.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (d *PagenoteDict) AddPagenote(uid uint32, note *hlcmsg.Pagenote) {
3535
func (d *PagenoteDict) GetPagenote(uid uint32, pid uint32) *hlcmsg.Pagenote {
3636
notes := d.getOrCreatePagenoteList(uid)
3737
for _, n := range notes {
38-
if n.Pageid == pid {
38+
if pid == 0 || n.Pageid == pid {
3939
return n
4040
}
4141
}

storage/storage.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
var storage *SqliteStorage = nil
99

1010
func InitStorage(path string) {
11+
if storage != nil {
12+
util.Log("WARN:", path, "is not inited as storage as storage was already registered at ", storage.path)
13+
return
14+
}
1115
storage = NewSqliteStorage(path)
1216
}
1317

storage/storage_impl_test.go

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ import (
55

66
"github.com/golang/protobuf/proto"
77
"github.com/lpimem/hlcsrv/hlcmsg"
8-
"github.com/lpimem/hlcsrv/util"
98
)
109

1110
func TestQueryNotesByUID(t *testing.T) {
12-
resetDb()
11+
ResetTestDb()
1312
testQueryNotes(1, 0, "uid", t)
1413
}
1514

1615
func TestQueryNotesByPid(t *testing.T) {
17-
resetDb()
16+
ResetTestDb()
1817
testQueryNotes(0, 1, "pid", t)
1918
}
2019

2120
func TestQueryNotesByUidAndPid(t *testing.T) {
22-
resetDb()
21+
ResetTestDb()
2322
testQueryNotes(1, 1, "uid and pid", t)
2423
}
2524

@@ -57,7 +56,7 @@ func testQueryNotes(uid, pid uint32, msg string, t *testing.T) (notes []*hlcmsg.
5756
}
5857

5958
func TestNewRangeMeta(t *testing.T) {
60-
resetDb()
59+
ResetTestDb()
6160
var err error
6261
metas := storage.QueryMetaList(1, 1)
6362
if len(metas) < 1 {
@@ -89,7 +88,7 @@ func TestNewRangeMeta(t *testing.T) {
8988
}
9089

9190
func TestDeleteRangeMeta(t *testing.T) {
92-
resetDb()
91+
ResetTestDb()
9392
metas := storage.QueryMetaList(1, 1)
9493
count := len(metas)
9594
if count < 1 {
@@ -112,7 +111,7 @@ func TestDeleteRangeMeta(t *testing.T) {
112111
}
113112

114113
func TestQueryPageId(t *testing.T) {
115-
resetDb()
114+
ResetTestDb()
116115
pid := storage.QueryPageId("example.com")
117116
if pid != 1 {
118117
t.Error("Should get 1 for page id, but got", pid)
@@ -126,7 +125,7 @@ func TestQueryPageId(t *testing.T) {
126125
}
127126

128127
func TestNewPage(t *testing.T) {
129-
resetDb()
128+
ResetTestDb()
130129
url := "http://new.example.com"
131130
pid := storage.NewPage("test", url)
132131
if pid < 1 {
@@ -141,7 +140,7 @@ func TestNewPage(t *testing.T) {
141140
}
142141

143142
func TestQueryUser(t *testing.T) {
144-
resetDb()
143+
ResetTestDb()
145144
uname := "Bob"
146145
uemail := "[email protected]"
147146
passwd := "unsafe"
@@ -158,7 +157,7 @@ func TestQueryUser(t *testing.T) {
158157
}
159158

160159
func TestNewUser(t *testing.T) {
161-
resetDb()
160+
ResetTestDb()
162161
uname := "Alice"
163162
email := "[email protected]"
164163
passwd := "unsafe"
@@ -179,47 +178,6 @@ func TestNewUser(t *testing.T) {
179178
}
180179
}
181180

182-
func InitTestDb() {
183-
InitStorage(util.GetAbsRunDirPath() + "/db/test.db")
184-
err := CleanDb()
185-
if err != nil {
186-
panic(err)
187-
}
188-
err = SeedTestDb()
189-
if err != nil {
190-
panic(err)
191-
}
192-
}
193-
194-
func CleanDb() error {
195-
_, err := storage.DB.Exec(`
196-
delete from hlc_range;
197-
delete from hlc_user;
198-
delete from hlc_page;
199-
delete from hlc_comments;
200-
`)
201-
return err
202-
}
203-
204-
func SeedTestDb() error {
205-
_, err := storage.DB.Exec(`
206-
insert into hlc_user(id, name, email, password, _slt)
207-
values (1, "Bob", "[email protected]", "unsafe", "unsafe");
208-
209-
insert into hlc_page(id, title, url)
210-
values (1, "example", "example.com");
211-
212-
insert into hlc_range(id, anchor, start, startOffset, end, endOffset, text, page, author)
213-
values (1, "#c", "#c/1", 0, "#c/12", 32, "This is the selected text", 1, 1);
214-
`)
215-
return err
216-
}
217-
218-
func resetDb() {
219-
CleanDb()
220-
SeedTestDb()
221-
}
222-
223181
func init() {
224-
InitTestDb()
182+
ResetTestDb()
225183
}

0 commit comments

Comments
 (0)