-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjournal_test.go
305 lines (230 loc) · 8.22 KB
/
journal_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/jamiefdhurst/journal/pkg/adapter/giphy"
"github.com/jamiefdhurst/journal/pkg/adapter/json"
"github.com/jamiefdhurst/journal/internal/app"
"github.com/jamiefdhurst/journal/internal/app/model"
"github.com/jamiefdhurst/journal/internal/app/router"
"github.com/jamiefdhurst/journal/pkg/database"
pkgrouter "github.com/jamiefdhurst/journal/pkg/router"
)
var (
rtr *pkgrouter.Router
server *httptest.Server
)
func init() {
rtr = router.NewRouter(nil)
server = httptest.NewServer(rtr)
log.Println("Serving on " + server.URL)
}
func fixtures(t *testing.T) {
container := &app.Container{Configuration: app.DefaultConfiguration()}
adapter := giphy.Client{Client: &json.Client{}}
db := &database.Sqlite{}
if err := db.Connect("test/data/test.db"); err != nil {
t.Error("Could not open test database for writing...")
}
// Setup container
container.Db = db
container.Giphy = adapter
rtr.Container = container
js := model.Journals{Container: container}
db.Exec("DROP TABLE journal")
js.CreateTable()
// Set up data
db.Exec("INSERT INTO journal (slug, title, content, date) VALUES (?, ?, ?, ?)", "test", "Test", "<p>Test!</p>", "2018-01-01")
db.Exec("INSERT INTO journal (slug, title, content, date) VALUES (?, ?, ?, ?)", "test-2", "Another Test", "<p>Test again!</p>", "2018-02-01")
db.Exec("INSERT INTO journal (slug, title, content, date) VALUES (?, ?, ?, ?)", "test-3", "A Final Test", "<p>Test finally!</p>", "2018-03-01")
}
func TestConfig(t *testing.T) {
os.Setenv("J_TITLE", "A Test Title")
configuration := config()
if configuration.Title != "A Test Title" {
t.Error("Expected title to be set through environment")
}
if configuration.Port != "3000" {
t.Errorf("Expected default port to be set, got %s", configuration.Port)
}
}
func TestLoadDatabase(t *testing.T) {
container.Configuration.DatabasePath = "test/data/test.db"
closeFunc := loadDatabase()
closeFunc()
}
func TestLoadGiphy(t *testing.T) {
existing := os.Getenv("J_GIPHY_API_KEY")
os.Setenv("J_GIPHY_API_KEY", "foobar")
loadGiphy()
os.Setenv("J_GIPHY_API_KEY", existing)
if container.Giphy == nil {
t.Error("Expected Giphy adapter to be setup")
}
}
func TestApiv1List(t *testing.T) {
fixtures(t)
request, _ := http.NewRequest("GET", server.URL+"/api/v1/post", nil)
res, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 200 {
t.Error("Expected 200 status code")
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
expected := `[{"id":3,"slug":"test-3","title":"A Final Test","date":"2018-03-01T00:00:00Z","content":"<p>Test finally!</p>"},{"id":2,"slug":"test-2","title":"Another Test","date":"2018-02-01T00:00:00Z","content":"<p>Test again!</p>"},{"id":1,"slug":"test","title":"Test","date":"2018-01-01T00:00:00Z","content":"<p>Test!</p>"}]`
// Use contains to get rid of any extra whitespace that we can discount
if !strings.Contains(string(body[:]), expected) {
t.Errorf("Expected:\n\t%s\nGot:\n\t%s", expected, string(body[:]))
}
}
func TestApiV1Single(t *testing.T) {
fixtures(t)
request, _ := http.NewRequest("GET", server.URL+"/api/v1/post/test", nil)
res, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 200 {
t.Error("Expected 200 status code")
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
expected := `{"id":1,"slug":"test","title":"Test","date":"2018-01-01T00:00:00Z","content":"<p>Test!</p>"}`
// Use contains to get rid of any extra whitespace that we can discount
if !strings.Contains(string(body[:]), expected) {
t.Errorf("Expected:\n\t%s\nGot:\n\t%s", expected, string(body[:]))
}
}
func TestApiV1Single_NotFound(t *testing.T) {
fixtures(t)
request, _ := http.NewRequest("GET", server.URL+"/api/v1/post/random", nil)
res, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 404 {
t.Error("Expected 404 status code")
}
}
func TestApiV1Create(t *testing.T) {
fixtures(t)
request, _ := http.NewRequest("PUT", server.URL+"/api/v1/post", strings.NewReader(`{"title":"Test 4","date":"2018-06-01T00:00:00Z","content":"<p>Test 4!</p>"}`))
res, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 201 {
t.Error("Expected 201 status code")
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
expected := `{"id":4,"slug":"test-4","title":"Test 4","date":"2018-06-01T00:00:00Z","content":"<p>Test 4!</p>"}`
// Use contains to get rid of any extra whitespace that we can discount
if !strings.Contains(string(body[:]), expected) {
t.Errorf("Expected:\n\t%s\nGot:\n\t%s", expected, string(body[:]))
}
}
func TestApiV1Create_InvalidRequest(t *testing.T) {
fixtures(t)
request, _ := http.NewRequest("PUT", server.URL+"/api/v1/post", nil)
res, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 400 {
t.Error("Expected 400 status code")
}
}
func TestApiV1Create_MissingData(t *testing.T) {
fixtures(t)
request, _ := http.NewRequest("PUT", server.URL+"/api/v1/post", strings.NewReader(`{"title":"Test 4"}`))
res, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 400 {
t.Error("Expected 400 status code")
}
}
func TestApiV1Create_RepeatTitles(t *testing.T) {
fixtures(t)
request, _ := http.NewRequest("PUT", server.URL+"/api/v1/post", strings.NewReader(`{"title":"Repeated","date":"2018-02-01T00:00:00Z","content":"<p>Repeated content test!</p>"}`))
res, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 201 {
t.Error("Expected 201 status code")
}
request, _ = http.NewRequest("PUT", server.URL+"/api/v1/post", strings.NewReader(`{"title":"Repeated","date":"2019-02-01T00:00:00Z","content":"<p>Repeated content test again!</p>"}`))
res, err = http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 201 {
t.Error("Expected 201 status code")
}
request, _ = http.NewRequest("GET", server.URL+"/api/v1/post/repeated-1", nil)
res, err = http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 200 {
t.Error("Expected 200 status code")
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
expected := `{"id":5,"slug":"repeated-1","title":"Repeated","date":"2019-02-01T00:00:00Z","content":"<p>Repeated content test again!</p>"}`
// Use contains to get rid of any extra whitespace that we can discount
if !strings.Contains(string(body[:]), expected) {
t.Errorf("Expected:\n\t%s\nGot:\n\t%s", expected, string(body[:]))
}
}
func TestApiV1Update(t *testing.T) {
fixtures(t)
request, _ := http.NewRequest("POST", server.URL+"/api/v1/post/test", strings.NewReader(`{"title":"A different title"}`))
res, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 200 {
t.Error("Expected 200 status code")
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
expected := `{"id":1,"slug":"test","title":"A different title","date":"2018-01-01T00:00:00Z","content":"<p>Test!</p>"}`
// Use contains to get rid of any extra whitespace that we can discount
if !strings.Contains(string(body[:]), expected) {
t.Errorf("Expected:\n\t%s\nGot:\n\t%s", expected, string(body[:]))
}
}
func TestApiV1Update_NotFound(t *testing.T) {
fixtures(t)
request, _ := http.NewRequest("POST", server.URL+"/api/v1/post/random", strings.NewReader(`{"title":"A different title"}`))
res, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 404 {
t.Error("Expected 404 status code")
}
}
func TestApiV1Update_InvalidRequest(t *testing.T) {
fixtures(t)
request, _ := http.NewRequest("POST", server.URL+"/api/v1/post/test", nil)
res, err := http.DefaultClient.Do(request)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if res.StatusCode != 400 {
t.Error("Expected 400 status code")
}
}