-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapp_test.go
195 lines (181 loc) · 7.43 KB
/
app_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNoteCreation(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
expected, got, code, err := createTestNote(mock, "")
assert.Nil(t, err, "Should be no http error")
assert.Equal(t, "", got.Content, "Content should be empty, is lazy loaded")
assert.Equal(t, expected.Subject, got.Subject, "Subject should match")
assert.Equal(t, expected.Tags, got.Tags, "Tags should match")
assert.False(t, got.Encrypted, "Should not be encrypted, no password")
assert.Equal(t, http.StatusOK, code, "Response code != 200")
}
func TestNoteCreationContentFetch(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
expected, got, code, err := createTestNote(mock, "")
assert.Nil(t, err, "Should be no http error")
assert.Equal(t, http.StatusOK, code, "Response code != 200")
resp, err := http.Post(mock.server.URL+"/api/note/content/"+got.UID, "", nil)
assert.Nil(t, err, "Should be no http error")
content, _ := ioutil.ReadAll(resp.Body)
assert.Equal(t, expected.Content, string(content), "Did not get the content back")
}
func TestNoteCreationContentFetchGet(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
resp, err := http.Get(mock.server.URL + "/api/note/content/abc123")
assert.Nil(t, err, "Should be no http error")
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode, "Expected: Method Not Allowed")
}
func TestEncryptedNoteCreationContentFetch(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
password := "fancy-password"
expected, got, code, err := createTestNote(mock, password)
assert.Nil(t, err, "Should be no http error")
assert.Equal(t, http.StatusOK, code, "Response code != 200")
form := url.Values{}
form.Add("password", password)
body := strings.NewReader(form.Encode())
resp, err := http.Post(mock.server.URL+"/api/note/content/"+got.UID, urlEncoded, body)
assert.Nil(t, err, "Should be no http error")
content, err := ioutil.ReadAll(resp.Body)
assert.Equal(t, http.StatusOK, resp.StatusCode, "Response code != 200")
assert.Equal(t, expected.Content, string(content), "Did not get the content back")
}
func TestEncryptedNoteCreationExcludesPassword(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
password := "fancy-password"
_, got, _, _ := createTestNote(mock, password)
persistedNote, err := db.getNoteByUID(got.UID, password)
assert.Nil(t, err, "Should be no error fetching the note")
assert.Equal(t, "", persistedNote.Password, "Passwords should not be persisted!")
}
func TestEncryptedNoteCreationContentFetchWithWrongPassword(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
expected, got, code, err := createTestNote(mock, "foobar")
assert.Nil(t, err, "Should be no http error")
assert.Equal(t, http.StatusOK, code, "Response code != 200")
form := url.Values{}
form.Add("password", "wrong password!")
body := strings.NewReader(form.Encode())
resp, err := http.Post(mock.server.URL+"/api/note/content/"+got.UID, urlEncoded, body)
assert.Nil(t, err, "Should be no http error")
content, err := ioutil.ReadAll(resp.Body)
assert.Equal(t, http.StatusForbidden, resp.StatusCode, "Request should be forbidden!")
assert.NotEqual(t, expected.Content, string(content), "Got content back?")
}
func TestEncryptedNoteCreationWithUnicode(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
password := "fancy-password"
expected := `ŒŒŒŒŒŒŒŒŒŒ`
_, note, code, _ := createTestNote(mock, password)
assert.Equal(t, http.StatusOK, code, "Response code != 200")
note.Content = expected
note.Password = password
note, err := mock.db.update(note)
form := url.Values{}
form.Add("password", password)
body := strings.NewReader(form.Encode())
resp, err := http.Post(mock.server.URL+"/api/note/content/"+note.UID, urlEncoded, body)
assert.Nil(t, err, "Should be no http error")
content, err := ioutil.ReadAll(resp.Body)
assert.Equal(t, http.StatusOK, resp.StatusCode, "No error code expected")
assert.Equal(t, expected, string(content), "Not a match?")
}
func TestNoteDeletion(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
_, got, code, err := createTestNote(mock, "")
assert.Nil(t, err, "Should be no http error")
assert.Equal(t, http.StatusOK, code, "Response code != 200")
req, err := http.NewRequest("DELETE", mock.server.URL+"/api/note/"+got.UID, nil)
assert.Nil(t, err, "Should be no error creating a new request")
resp, err := http.DefaultClient.Do(req)
content, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err, "Should be no error reading the response")
outcome := APIResponse{}
err = json.Unmarshal(content, &outcome)
assert.Nil(t, err, "Should be no error unmarshaling the response")
assert.Equal(t, true, outcome.Success)
resp, err = http.Get(mock.server.URL + "/api/notes/list")
assert.Nil(t, err, "Should be no http error")
content, err = ioutil.ReadAll(resp.Body)
assert.Nil(t, err, "Should be not be a response error")
notes := []Note{}
json.Unmarshal(content, ¬es)
assert.Equal(t, 0, len(notes), "Should be no notes as the only one was deleted")
}
func TestNoteListing(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
expected, _, _, _ := createTestNote(mock, "")
resp, err := http.Get(mock.server.URL + "/api/notes/list")
content, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err, "Should be no http error")
notes := []Note{}
json.Unmarshal(content, ¬es)
assert.Equal(t, expected.Subject, notes[0].Subject, "Listing miissing our note")
}
func TestNoteFullTextSearch(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
_, got, _, _ := createTestNote(mock, "")
resp, err := http.Get(mock.server.URL + "/api/notes/search?q=beer")
content, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err, "Should be no http error")
uids := []string{}
json.Unmarshal(content, &uids)
if assert.Equal(t, 1, len(uids), "Full text should find 1 note") {
assert.Equal(t, got.UID, uids[0], "Full text found the note")
}
}
func TestNoteFullTextSearchDeletion(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
_, got, _, _ := createTestNote(mock, "")
req, err := http.NewRequest("DELETE", mock.server.URL+"/api/note/"+got.UID, nil)
assert.Nil(t, err, "Should be no error creating a new request")
resp, err := http.DefaultClient.Do(req)
assert.Nil(t, err, "Should be no error requesting a note deletion")
resp, err = http.Get(mock.server.URL + "/api/notes/search?q=beer")
content, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err, "Should be no http error")
uids := []string{}
json.Unmarshal(content, &uids)
assert.Equal(t, 0, len(uids), "Full text should find 0 notes")
}
func TestNoteFullTextSearchUpdate(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
_, expected, _, _ := createTestNote(mock, "")
expected.Content = "ipa only pls"
b := new(bytes.Buffer)
assert.Nil(t, json.NewEncoder(b).Encode(expected), "Should be no error encoding json")
req, err := http.NewRequest("PUT", mock.server.URL+"/api/note/"+expected.UID, b)
assert.Nil(t, err, "Should be no error creating the http request")
_, err = http.DefaultClient.Do(req)
assert.Nil(t, err, "Should be no error requesting a note update")
resp, err := http.Get(mock.server.URL + "/api/notes/search?q=ipa")
content, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err, "Should be no http error")
uids := []string{}
json.Unmarshal(content, &uids)
if assert.Equal(t, 1, len(uids), "Full text should find 1 note") {
assert.Equal(t, expected.UID, uids[0], "Full text found the note")
}
}