-
Notifications
You must be signed in to change notification settings - Fork 24
/
main_test.go
1379 lines (1164 loc) · 47.5 KB
/
main_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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strconv"
"strings"
"testing"
"time"
. "github.com/toldjuuso/vertigo/databases/sqlx"
"github.com/PuerkitoBio/goquery"
"github.com/russross/blackfriday"
slug "github.com/shurcooL/sanitized_anchor_name"
. "github.com/smartystreets/goconvey/convey"
"github.com/toldjuuso/excerpt"
)
var server = NewServer()
var settings Vertigo
var user User
var post Post
var userid string
var postslug string
var recovery string
var sessioncookie string
var secondusersessioncookie string
var malformedsessioncookie = "MTQxNDc2NzAyOXxEdi1CQkFFQ180SUFBUkFCRUFBQUhmLUNBQUVHYzNSeWFXNW5EQVlBQkhWelpYSUZhVzUwTmpRRUFnQUN8Y2PFc-lZ8aEMWypbKXTD-LWg6o9DtJaMzd8NMc8m87A="
func TestInstallationWizard(t *testing.T) {
Convey("Opening homepage", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/", nil)
Convey("it should display installation wizard", func() {
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
doc, _ := goquery.NewDocumentFromReader(recorder.Body)
sel := doc.Find("h1").First().Text()
So(sel, ShouldEqual, "Your settings file seems to be missing some fields. Lets fix that.")
})
})
}
func TestStaticPages(t *testing.T) {
Convey("All static pages should return 200 OK", t, func() {
var recorder = httptest.NewRecorder()
Convey("Loading API index page", func() {
request, _ := http.NewRequest("GET", "/api", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
})
Convey("Loading user register page", func() {
request, _ := http.NewRequest("GET", "/user/register", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
})
Convey("Loading user login page", func() {
request, _ := http.NewRequest("GET", "/user/login", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
})
Convey("Loading user recovery page", func() {
request, _ := http.NewRequest("GET", "/user/recover", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
})
Convey("Loading user reset page", func() {
request, _ := http.NewRequest("GET", "/user/reset/1/foobar", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
})
Convey("Loading non-existent page", func() {
request, _ := http.NewRequest("GET", "/foobar", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
})
})
}
func TestEmptyAPIRoutes(t *testing.T) {
Convey("API routes should return empty JSON responses", t, func() {
var recorder = httptest.NewRecorder()
Convey("posts page should return []", func() {
request, _ := http.NewRequest("GET", "/api/posts", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
So(recorder.Body.String(), ShouldEqual, "[]")
})
Convey("users page should return []", func() {
request, _ := http.NewRequest("GET", "/api/users", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
So(recorder.Body.String(), ShouldEqual, "[]")
})
Convey("get post should return 404", func() {
request, _ := http.NewRequest("GET", "/api/user/0", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
So(recorder.Body.String(), ShouldEqual, `{"error":"Not found"}`)
})
Convey("get user should return 404", func() {
request, _ := http.NewRequest("GET", "/api/post/0", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
So(recorder.Body.String(), ShouldEqual, `{"error":"Not found"}`)
})
})
}
func TestCreatingSettings(t *testing.T) {
Convey("after creating", t, func() {
Convey("settings.Firstrun should equal true", func() {
settings := VertigoSettings()
So(settings.Firstrun, ShouldBeTrue)
})
})
}
func TestSavingSettingsViaInstallationWizard(t *testing.T) {
Convey("by submitting data in JSON", t, func() {
var recorder = httptest.NewRecorder()
Convey("it should return 200 OK", func() {
settings.Hostname = "http://example.com"
settings.Name = "Foo's blog"
settings.Description = "Foo's test blog"
settings.MailerLogin = os.Getenv("SMTP_LOGIN")
settings.MailerPassword = os.Getenv("SMTP_PASSWORD")
port, _ := strconv.Atoi(os.Getenv("SMTP_PORT"))
settings.MailerPort = port
settings.MailerHostname = os.Getenv("SMTP_SERVER")
payload, _ := json.Marshal(settings)
request, _ := http.NewRequest("POST", "/api/installation", bytes.NewReader(payload))
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
})
})
}
func TestSettingValues(t *testing.T) {
Convey("Settings object should be the same as settings object", t, func() {
So(Settings.Hostname, ShouldEqual, settings.Hostname)
So(Settings.Name, ShouldEqual, settings.Name)
So(Settings.Description, ShouldEqual, settings.Description)
So(Settings.MailerLogin, ShouldEqual, settings.MailerLogin)
So(Settings.MailerPassword, ShouldEqual, settings.MailerPassword)
So(Settings.MailerPort, ShouldEqual, settings.MailerPort)
So(Settings.MailerHostname, ShouldEqual, settings.MailerHostname)
So(Settings.AllowRegistrations, ShouldBeTrue)
})
}
func TestManipulatingSettings(t *testing.T) {
Convey("when manipulating the global Settings variable", t, func() {
Convey("should save the changes to disk", func() {
settings = *Settings
settings.Name = "Juuso's Blog"
s, err := settings.Update()
if err != nil {
panic(err)
}
Settings = s
})
Convey("frontpage's <title> should now be 'Juuso's Blog'", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/", nil)
server.ServeHTTP(recorder, request)
doc, _ := goquery.NewDocumentFromReader(recorder.Body)
sel := doc.Find("title").Text()
So(sel, ShouldEqual, Settings.Name)
})
})
TestSettingValues(t)
}
func TestCreateFirstUser(t *testing.T) {
user.Name = "Juuso"
user.Password = "foo"
user.Email = "[email protected]"
user.Location = "Europe/Helsinki"
testCreateUser(t, user.Name, user.Password, user.Email, user.Location)
}
func testCreateUser(t *testing.T, name string, password string, email string, location string) {
// Convey("using frontend", t, func() {
// Convey("with valid input it should return 200 OK", func() {
// var recorder = httptest.NewRecorder()
// payload := fmt.Sprintf(`name=%s&password=%s&email=%s`, name, password, email)
// request, _ := http.NewRequest("POST", "/user/register", strings.NewReader(payload))
// request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// server.ServeHTTP(recorder, request)
// So(recorder.Code, ShouldEqual, 302)
// })
// })
Convey("using API", t, func() {
payload := fmt.Sprintf(`{"name":"%s", "password":"%s", "email":"%s", "location":"%s"}`, name, password, email, location)
badpayload := fmt.Sprintf(`{"name":"%s", "password":"%s", "email":"%s", "location":"EU/FI"}`, name, password, email)
Convey("with bad location it should should return 422", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("POST", "/api/user", strings.NewReader(badpayload))
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 422)
So(recorder.Body.String(), ShouldEqual, `{"error":"Location invalid. Please use IANA timezone database compatible locations."}`)
})
Convey("with valid input it should return 200 OK", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("POST", "/api/user", strings.NewReader(payload))
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
json.Unmarshal(recorder.Body.Bytes(), &user)
So(user.Name, ShouldEqual, name)
So(user.Email, ShouldEqual, email)
So(user.Posts, ShouldBeEmpty)
So(user.Location, ShouldEqual, location)
})
Convey("with the same email should return 422", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("POST", "/api/user", strings.NewReader(payload))
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 422)
So(recorder.Body.String(), ShouldEqual, `{"error":"Email already in use"}`)
})
})
}
func TestReadUser(t *testing.T) {
Convey("reading the latest user by ID should return 200 OK", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/user/%d", user.ID), nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
So(recorder.Body.String(), ShouldEqual, `{"id":1,"name":"Juuso","email":"[email protected]","posts":[],"location":"Europe/Helsinki"}`)
})
}
func TestReadUsers(t *testing.T) {
Convey("reading all users return 200 OK", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/users/", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
So(recorder.Body.String(), ShouldEqual, `[{"id":1,"name":"Juuso","email":"[email protected]","posts":[],"location":"Europe/Helsinki"}]`)
})
}
func TestReadUserSpecialCases(t *testing.T) {
Convey("when user does not exist, it should return not found", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/user/3", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
So(recorder.Body.String(), ShouldEqual, `{"error":"Not found"}`)
})
Convey("when user ID is malformed (eg. string), it should return 400", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/user/foobar", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 400)
So(recorder.Body.String(), ShouldEqual, `{"error":"The user ID could not be parsed from the request URL."}`)
})
}
func TestUserSignin(t *testing.T) {
Convey("on frontend", t, func() {
var recorder = httptest.NewRecorder()
Convey("should return 401 with wrong password", func() {
request, _ := http.NewRequest("POST", "/user/login", strings.NewReader(`password=foobar&[email protected]`))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
})
Convey("should return 404 with non-existent email", func() {
request, _ := http.NewRequest("POST", "/user/login", strings.NewReader(`password=Juuso&[email protected]`))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
})
Convey("should return 302 with valid data", func() {
request, _ := http.NewRequest("POST", "/user/login", strings.NewReader(fmt.Sprintf(`password=%s&email=%s`, user.Password, user.Email)))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 302)
})
})
TestUserLogout(t)
Convey("on JSON API", t, func() {
var recorder = httptest.NewRecorder()
Convey("should return 401 with wrong password", func() {
request, _ := http.NewRequest("POST", "/api/user/login", strings.NewReader(`{"password": "Juuso", "email": "[email protected]"}`))
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
})
Convey("should return 404 with wrong non-existent email", func() {
request, _ := http.NewRequest("POST", "/api/user/login", strings.NewReader(`{"password": "Juuso", "email": "[email protected]"}`))
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
})
Convey("should return 200 with valid data", func() {
request, _ := http.NewRequest("POST", "/api/user/login", strings.NewReader(fmt.Sprintf(`{"password":"%s", "email":"%s"}`, user.Password, user.Email)))
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
// i assure, nothing else worked
sessioncookie = strings.Split(strings.TrimLeft(recorder.HeaderMap["Set-Cookie"][0], "id="), ";")[0]
})
})
}
func TestUserControlPanel(t *testing.T) {
Convey("without authentication, it should fail", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/user", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
})
Convey("with authentication, it should succeed", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/user", nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
})
}
func TestSessionRedirect(t *testing.T) {
Convey("without authentication, it should not redirect", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/user/login", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
})
Convey("with authentication, it should redirect", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/user/login", nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 302)
})
}
func TestPostCreationPage(t *testing.T) {
Convey("without authentication, it should 401", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/posts/new", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
})
Convey("with authentication, it should 200 OK", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/posts/new", nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
})
}
func TestCreateFirstPost(t *testing.T) {
testCreatePost(t, 1, "First post", "This is example post with HTML elements like **bold** and *italics* in place.")
}
func testCreatePostRequest(t *testing.T, payload []byte, p Post) {
Convey("with authentication and valid data, it should 200 OK", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("POST", "/api/post", bytes.NewReader(payload))
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
json.Unmarshal(recorder.Body.Bytes(), &post)
So(post.ID, ShouldEqual, p.ID)
So(post.Title, ShouldEqual, p.Title)
So(post.Content, ShouldEqual, p.Content)
So(post.Markdown, ShouldEqual, p.Markdown)
So(post.Slug, ShouldEqual, p.Slug)
So(post.Author, ShouldEqual, p.Author)
So(post.Created, ShouldBeGreaterThan, int64(1400000000))
if post.Updated != post.Created {
So(post.Updated, ShouldAlmostEqual, post.Created, 5)
}
So(post.Excerpt, ShouldEqual, p.Excerpt)
So(post.Viewcount, ShouldEqual, p.Viewcount)
})
}
func TestReadPost(t *testing.T) {
Convey("using API", t, func() {
Convey("with the latest post's slug, it should return 200 OK", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/post/%s", post.Slug), nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
var p Post
json.Unmarshal(recorder.Body.Bytes(), &p)
So(post.ID, ShouldEqual, p.ID)
So(post.Title, ShouldEqual, p.Title)
So(post.Content, ShouldEqual, p.Content)
So(post.Markdown, ShouldEqual, p.Markdown)
So(post.Slug, ShouldEqual, p.Slug)
So(post.Author, ShouldEqual, p.Author)
So(post.Created, ShouldBeGreaterThan, int64(1400000000))
if post.Updated != post.Created {
So(post.Updated, ShouldAlmostEqual, post.Created, 5)
}
So(post.Excerpt, ShouldEqual, p.Excerpt)
So(post.Viewcount, ShouldEqual, p.Viewcount)
post.Viewcount += 1
time.Sleep(1 * time.Second)
})
})
Convey("using frontend", t, func() {
Convey("with the latest post's slug, it should return 200 OK", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/post/%s", post.Slug), nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
post.Viewcount += 1
time.Sleep(1 * time.Second)
})
})
}
// func TestReadPostSpecialCases(t *testing.T) {
// Convey("should return error when accessing slug called `new`", t, func() {
// var recorder = httptest.NewRecorder()
// request, _ := http.NewRequest("GET", "/api/post/new", nil)
// server.ServeHTTP(recorder, request)
// So(recorder.Code, ShouldEqual, 400)
// So(recorder.Body.String(), ShouldEqual, `{"error":"There can't be a post called 'new'."}`)
// })
// }
func TestPublishPost(t *testing.T) {
Convey("publishing post which does not exist", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/post/foobar/publish", nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
})
Convey("without session data should return HTTP 401", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/post/%s/publish", post.Slug), nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
})
Convey("with session data should return HTTP 200", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/post/%s/publish", post.Slug), nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Body.String(), ShouldEqual, `{"success":"Post published"}`)
So(recorder.Code, ShouldEqual, 200)
})
}
func TestUnpublishPost(t *testing.T) {
Convey("unpublishing post which does not exist", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/post/foobar/unpublish", nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
})
Convey("without session data should return HTTP 401", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/post/%s/unpublish", post.Slug), nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
})
Convey("with session data should return HTTP 200", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/post/%s/unpublish", post.Slug), nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
So(recorder.Body.String(), ShouldEqual, `{"success":"Post unpublished"}`)
})
Convey("after unpublishing, the post should be hidden", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/posts", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
So(recorder.Body.String(), ShouldEqual, `[]`)
})
TestPublishPost(t)
}
func TestPostListing(t *testing.T) {
Convey("using API", t, func() {
Convey("after a post is published, it should be listed on /api/posts", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/posts", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
var posts []Post
json.Unmarshal(recorder.Body.Bytes(), &posts)
for i, p := range posts {
So(i, ShouldEqual, 0)
So(post.ID, ShouldEqual, p.ID)
So(post.Title, ShouldEqual, p.Title)
So(post.Content, ShouldEqual, p.Content)
So(post.Markdown, ShouldEqual, p.Markdown)
So(post.Slug, ShouldEqual, p.Slug)
So(post.Author, ShouldEqual, p.Author)
So(post.Created, ShouldBeGreaterThan, int64(1400000000))
if post.Updated != post.Created {
So(post.Updated, ShouldAlmostEqual, post.Created, 5)
}
So(post.Excerpt, ShouldEqual, p.Excerpt)
So(post.Viewcount, ShouldEqual, p.Viewcount)
}
})
})
Convey("using frontend", t, func() {
Convey("after a post is published, it should be listed on frontpage", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
doc, _ := goquery.NewDocumentFromReader(recorder.Body)
sel := doc.Find("article .title").Text()
So(sel, ShouldEqual, post.Title)
})
})
}
func TestPostOwner(t *testing.T) {
Convey("using API", t, func() {
Convey("post owner should have data linked to their profile", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/user/1", nil)
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
var u User
json.Unmarshal(recorder.Body.Bytes(), &u)
So(u.ID, ShouldEqual, user.ID)
So(u.Name, ShouldEqual, user.Name)
So(u.Email, ShouldEqual, user.Email)
p := u.Posts[0]
So(post.ID, ShouldEqual, p.ID)
So(post.Title, ShouldEqual, p.Title)
So(post.Content, ShouldEqual, p.Content)
So(post.Markdown, ShouldEqual, p.Markdown)
So(post.Slug, ShouldEqual, p.Slug)
So(post.Author, ShouldEqual, p.Author)
So(post.Created, ShouldBeGreaterThan, int64(1400000000))
if post.Updated != post.Created {
So(post.Updated, ShouldAlmostEqual, post.Created, 5)
}
So(post.Excerpt, ShouldEqual, p.Excerpt)
So(post.Viewcount, ShouldEqual, p.Viewcount)
})
})
}
func TestPostEditPage(t *testing.T) {
Convey("should return 200 OK with authorization", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/post/%s/edit", post.Slug), nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
})
Convey("should return 401 without authorization", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/post/%s/edit", post.Slug), nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
})
}
func TestUpdateFirstPost(t *testing.T) {
testUpdatePost(t, "First post edited", "This is API edited example post with HTML elements like **bold** and *italics* in place.")
}
func testUpdatePostAPI(t *testing.T, payload []byte) {
Convey("should return 401 without authorization", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("POST", fmt.Sprintf("/api/post/%s/edit", post.Slug), bytes.NewReader(payload))
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
So(recorder.Body.String(), ShouldEqual, `{"error":"Unauthorized"}`)
})
Convey("should return 401 with bad authorization", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("POST", fmt.Sprintf("/api/post/%s/edit", post.Slug), bytes.NewReader(payload))
cookie := &http.Cookie{Name: "id", Value: malformedsessioncookie}
request.AddCookie(cookie)
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
So(recorder.Body.String(), ShouldEqual, `{"error":"Unauthorized"}`)
})
Convey("should return 404 with non-existent post", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("POST", "/api/post/foobar/edit", bytes.NewReader(payload))
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
So(recorder.Body.String(), ShouldEqual, `{"error":"Not found"}`)
})
Convey("should return 200 with successful authorization", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("POST", fmt.Sprintf("/api/post/%s/edit", post.Slug), bytes.NewReader(payload))
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
var p Post
json.Unmarshal(recorder.Body.Bytes(), &p)
post.Slug = p.Slug
So(post.ID, ShouldEqual, p.ID)
So(post.Title, ShouldEqual, p.Title)
So(post.Content, ShouldEqual, p.Content)
So(post.Markdown, ShouldEqual, p.Markdown)
So(post.Slug, ShouldEqual, p.Slug)
So(post.Author, ShouldEqual, p.Author)
So(post.Created, ShouldBeGreaterThan, int64(1400000000))
if post.Updated != post.Created {
So(post.Updated, ShouldAlmostEqual, post.Created, 5)
}
So(post.Excerpt, ShouldEqual, p.Excerpt)
So(post.Viewcount, ShouldEqual, p.Viewcount)
})
}
func TestPostAfterUpdating(t *testing.T) {
Convey("the post should not be displayed on frontpage", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
doc, _ := goquery.NewDocumentFromReader(recorder.Body)
sel := doc.Find("article h1").Text()
So(sel, ShouldBeEmpty)
})
Convey("update should return HTTP 200", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/post/%s/publish", post.Slug), nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Body.String(), ShouldEqual, `{"success":"Post published"}`)
So(recorder.Code, ShouldEqual, 200)
})
Convey("after updating, post should be displayed on frontpage", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
doc, _ := goquery.NewDocumentFromReader(recorder.Body)
sel := doc.Find("article .title").Text()
So(sel, ShouldEqual, post.Title)
})
Convey("the post should not be displayed trough API", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/posts", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
var posts []Post
json.Unmarshal(recorder.Body.Bytes(), &posts)
for i, p := range posts {
So(i, ShouldEqual, 0)
So(post.ID, ShouldEqual, p.ID)
So(post.Title, ShouldEqual, p.Title)
So(post.Content, ShouldEqual, p.Content)
So(post.Markdown, ShouldEqual, p.Markdown)
So(post.Slug, ShouldEqual, p.Slug)
So(post.Author, ShouldEqual, p.Author)
So(post.Created, ShouldBeGreaterThan, int64(1400000000))
if post.Updated != post.Created {
So(post.Updated, ShouldAlmostEqual, post.Created, 5)
}
So(post.Excerpt, ShouldEqual, p.Excerpt)
}
})
}
func testUpdatePostFrontend(t *testing.T, payload string) {
Convey("should return 302 with successful authorization", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("POST", fmt.Sprintf("/post/%s/edit", post.Slug), strings.NewReader(payload))
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 302)
})
}
func testCreatePost(t *testing.T, owner int64, title string, markdown string) {
var u User
u.ID = owner
var p Post
p.ID = post.ID + 1
p.Title = title
p.Markdown = markdown
p.Content = string(blackfriday.MarkdownCommon([]byte(markdown)))
p.Slug = slug.Create(p.Title)
p.Author = u.ID
p.Excerpt = excerpt.Make(p.Content, 15)
p.Viewcount = 0
payload, err := json.Marshal(p)
if err != nil {
panic(err)
}
testCreatePostRequest(t, payload, p)
}
func TestCreateSecondPost(t *testing.T) {
testCreatePost(t, 1, "Second post", "This is second post")
}
func testUpdatePost(t *testing.T, title string, markdown string) {
// create JSON payload
var p Post
p.Title = title
p.Markdown = markdown
p.Content = string(blackfriday.MarkdownCommon([]byte(markdown)))
apiPayload, _ := json.Marshal(p)
// save changes to global object for further testing comparison
post.Title = p.Title
post.Content = string(blackfriday.MarkdownCommon([]byte(markdown)))
post.Markdown = markdown
post.Excerpt = excerpt.Make(p.Content, 15)
testUpdatePostAPI(t, apiPayload)
// creates form-encoded payload
p2 := url.Values{}
p2.Set("title", title)
p2.Add("markdown", markdown)
frontendPayload := p2.Encode()
// save changes to global object for further testing comparison
post.Title = p2.Get("title")
post.Markdown = p2.Get("markdown")
post.Excerpt = excerpt.Make(post.Content, 15)
testUpdatePostFrontend(t, frontendPayload)
TestReadPost(t)
}
func TestUpdateSecondPost(t *testing.T) {
testUpdatePost(t, "Second post edited", "This is edited second post")
}
func TestControlPanelListing(t *testing.T) {
Convey("should list both posts", t, func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/user", nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
doc, _ := goquery.NewDocumentFromReader(recorder.Body)
doc.Find("ul").Each(func(i int, s *goquery.Selection) {
So(i, ShouldBeLessThanOrEqualTo, post.ID)
})
})
}
func TestCreateThirdPost(t *testing.T) {
testUpdatePost(t, "Third post", "This is third post")
TestPublishPost(t)
TestReadPost(t)
TestControlPanelListing(t)
}
func TestDeletePost(t *testing.T) {
Convey("using API", t, func() {
Convey("it should return 401 without sessioncookies", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/post/%s/delete", post.Slug), nil)
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
So(recorder.Body.String(), ShouldEqual, `{"error":"Unauthorized"}`)
})
Convey("it should return 401 with malformed sessioncookie", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/post/%s/delete", post.Slug), nil)
cookie := &http.Cookie{Name: "id", Value: malformedsessioncookie}
request.AddCookie(cookie)
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
So(recorder.Body.String(), ShouldEqual, `{"error":"Unauthorized"}`)
})
Convey("it should return 404 when trying to delete non-existent post", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/post/foobar/delete", nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
So(recorder.Body.String(), ShouldEqual, `{"error":"Not found"}`)
})
Convey("it should return 200 with successful sessioncookies", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/post/%s/delete", post.Slug), nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
request.Header.Set("Content-Type", "application/json")
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
So(recorder.Body.String(), ShouldEqual, `{"success":"Post deleted"}`)
if *Driver == "sqlite3" {
// SQLite's re-assigns ID if one is removed
post.ID--
}
})
})
TestControlPanelListing(t)
}
func TestReadSettings(t *testing.T) {
Convey("using API", t, func() {
Convey("it should return 401 without sessioncookies", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/settings", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
So(recorder.Body.String(), ShouldEqual, `{"error":"Unauthorized"}`)
})
Convey("reading with malformed sessioncookies it should return 401", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/settings", nil)
cookie := &http.Cookie{Name: "id", Value: malformedsessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
So(recorder.Body.String(), ShouldEqual, `{"error":"Unauthorized"}`)
})
Convey("reading with sessioncookies it should return 200", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/api/settings", nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
var returnedSettings Vertigo
json.Unmarshal(recorder.Body.Bytes(), &returnedSettings)
var settingsWithoutCookieHash = settings
settingsWithoutCookieHash.CookieHash = ""
So(returnedSettings, ShouldResemble, settingsWithoutCookieHash)
})
})
Convey("using frontend", t, func() {
Convey("without sessioncookies it should return 401", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/user/settings", nil)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
})
Convey("with malformed sessioncookies it should return 401", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/user/settings", nil)
cookie := &http.Cookie{Name: "id", Value: malformedsessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 401)
})
Convey("with successful sessioncookies it should return 200", func() {
var recorder = httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/user/settings", nil)
cookie := &http.Cookie{Name: "id", Value: sessioncookie}
request.AddCookie(cookie)
server.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 200)
})
})