@@ -11,23 +11,13 @@ import (
11
11
"path"
12
12
"testing"
13
13
14
+ "github.com/Appboy/webpush-go"
14
15
"github.com/davecgh/go-spew/spew"
15
16
"github.com/ybizeul/ybfeed/internal/feed"
16
17
)
17
18
18
19
const baseDir = "../../test/"
19
20
const dataDir = "./data"
20
-
21
- func TestIndexHtml (t * testing.T ) {
22
- req := httptest .NewRequest (http .MethodGet , "/" , nil )
23
- w := httptest .NewRecorder ()
24
- RootHandlerFunc (w , req )
25
- res := w .Result ()
26
- if res .StatusCode != 200 {
27
- t .Errorf ("Expect code 200 but got %d" , res .StatusCode )
28
- }
29
- }
30
-
31
21
const testFeedName = "test"
32
22
33
23
type APITestRequest struct {
@@ -98,6 +88,50 @@ func (t APITestRequest) performRequest() (*http.Response, error) {
98
88
return w .Result (), nil
99
89
}
100
90
91
+ func TestFirstStart (t * testing.T ) {
92
+ newDir := "FOO"
93
+ t .Cleanup (func () {
94
+ os .RemoveAll (path .Join (baseDir , newDir ))
95
+ })
96
+
97
+ _ , err := NewApiHandler (path .Join (baseDir , newDir ))
98
+
99
+ if err != nil {
100
+ t .Error (err )
101
+ }
102
+
103
+ c , err := APIConfigFromFile (path .Join (baseDir , dataDir , "config.json" ))
104
+ if err != nil {
105
+ t .Error (err )
106
+ }
107
+
108
+ if c .NotificationSettings == nil ||
109
+ len (c .NotificationSettings .VAPIDPrivateKey ) == 0 ||
110
+ len (c .NotificationSettings .VAPIDPrivateKey ) == 0 {
111
+ t .Error ("Invalid config file" )
112
+ }
113
+
114
+ }
115
+ func TestIndexHtml (t * testing.T ) {
116
+ req := httptest .NewRequest (http .MethodGet , "/" , nil )
117
+ w := httptest .NewRecorder ()
118
+ RootHandlerFunc (w , req )
119
+ res := w .Result ()
120
+ if res .StatusCode != 200 {
121
+ t .Errorf ("Expect code 200 but got %d" , res .StatusCode )
122
+ }
123
+ }
124
+
125
+ func TestServiceWorker (t * testing.T ) {
126
+ req := httptest .NewRequest (http .MethodGet , "/service-worker.js" , nil )
127
+ w := httptest .NewRecorder ()
128
+ RootHandlerFunc (w , req )
129
+ res := w .Result ()
130
+ if res .StatusCode != 200 {
131
+ t .Errorf ("Expect code 200 but got %d" , res .StatusCode )
132
+ }
133
+ }
134
+
101
135
func TestCreateFeed (t * testing.T ) {
102
136
const feedName = "6ff1146b6830 #86b8f59f550189a8f91f"
103
137
@@ -465,3 +499,48 @@ func TestRemoveItemNonExistentFeed(t *testing.T) {
465
499
t .Errorf ("Expect code 404 but got %d (%s)" , res .StatusCode , string (b ))
466
500
}
467
501
}
502
+
503
+ func TestSubscribeFeedNotifications (t * testing.T ) {
504
+ body := `{"endpoint":"http://test.com","keys":{"auth":"AUTH","p256dh":"P256DH"}}`
505
+ b := bytes .NewBuffer ([]byte (body ))
506
+
507
+ t .Cleanup (func () {
508
+ f , _ := feed .GetFeed (path .Join (baseDir , dataDir , testFeedName ))
509
+ f .Config .Subscriptions = []webpush.Subscription {}
510
+ f .Config .Write ()
511
+ })
512
+
513
+ res , _ := APITestRequest {
514
+ method : http .MethodPost ,
515
+ feed : testFeedName ,
516
+ item : "subscription" ,
517
+ cookieAuthType : AuthTypeAuth ,
518
+ body : b ,
519
+ }.performRequest ()
520
+
521
+ if res .StatusCode != 200 {
522
+ t .Errorf ("Expected 200 but got %d" , res .StatusCode )
523
+ }
524
+ f , err := feed .GetFeed (path .Join (baseDir , dataDir , testFeedName ))
525
+
526
+ if err != nil {
527
+ t .Error (err )
528
+ }
529
+
530
+ if len (f .Config .Subscriptions ) == 0 {
531
+ t .Error ("No subscriptions found" )
532
+ return
533
+ }
534
+
535
+ if f .Config .Subscriptions [0 ].Endpoint != "http://test.com" {
536
+ t .Errorf ("Bad endpoint, got %s" , f .Config .Subscriptions [0 ].Endpoint )
537
+ }
538
+
539
+ if f .Config .Subscriptions [0 ].Keys .Auth != "AUTH" {
540
+ t .Errorf ("Bad auth, got %s" , f .Config .Subscriptions [0 ].Keys .Auth )
541
+ }
542
+
543
+ if f .Config .Subscriptions [0 ].Keys .P256dh != "P256DH" {
544
+ t .Errorf ("Bad p256dh, got %s" , f .Config .Subscriptions [0 ].Keys .P256dh )
545
+ }
546
+ }
0 commit comments