Skip to content

Commit 3e5d9e6

Browse files
committed
More integration tests
1 parent 52401ae commit 3e5d9e6

File tree

3 files changed

+120
-29
lines changed

3 files changed

+120
-29
lines changed

internal/handlers/handlers.go

+29-17
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,26 @@ type ApiHandler struct {
7171
}
7272

7373
type APIConfig struct {
74-
NotificationSettings NotificationSettings `json:"notification,omitempty"`
74+
NotificationSettings *NotificationSettings `json:"notification,omitempty"`
7575
}
76+
77+
func APIConfigFromFile(p string) (*APIConfig, error) {
78+
var config = &APIConfig{}
79+
d, err := os.ReadFile(path.Join(p))
80+
if err != nil {
81+
if os.IsNotExist(err) {
82+
return config, nil
83+
} else {
84+
return nil, err
85+
}
86+
}
87+
err = json.Unmarshal(d, config)
88+
if err != nil {
89+
return nil, err
90+
}
91+
return config, nil
92+
}
93+
7694
type NotificationSettings struct {
7795
VAPIDPublicKey string
7896
VAPIDPrivateKey string
@@ -82,27 +100,21 @@ func NewApiHandler(basePath string) (*ApiHandler, error) {
82100
os.MkdirAll(basePath, 0700)
83101

84102
// Check configuration
85-
var config = &APIConfig{}
86-
d, err := os.ReadFile(path.Join(basePath, "config.json"))
103+
var config, err = APIConfigFromFile(path.Join(basePath, "config.json"))
87104
if err != nil {
88-
if !os.IsNotExist(err) {
89-
return nil, err
90-
} else {
91-
privateKey, publicKey, err := webpush.GenerateVAPIDKeys()
92-
if err != nil {
93-
return nil, err
94-
}
95-
config.NotificationSettings = NotificationSettings{
96-
VAPIDPublicKey: publicKey,
97-
VAPIDPrivateKey: privateKey,
98-
}
99-
}
100-
} else {
101-
err = json.Unmarshal(d, config)
105+
return nil, err
106+
}
107+
if config.NotificationSettings == nil {
108+
privateKey, publicKey, err := webpush.GenerateVAPIDKeys()
102109
if err != nil {
103110
return nil, err
104111
}
112+
config.NotificationSettings = &NotificationSettings{
113+
VAPIDPublicKey: publicKey,
114+
VAPIDPrivateKey: privateKey,
115+
}
105116
}
117+
106118
result := &ApiHandler{
107119
BasePath: basePath,
108120
Config: *config,

internal/handlers/handlers_test.go

+90-11
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,13 @@ import (
1111
"path"
1212
"testing"
1313

14+
"github.com/Appboy/webpush-go"
1415
"github.com/davecgh/go-spew/spew"
1516
"github.com/ybizeul/ybfeed/internal/feed"
1617
)
1718

1819
const baseDir = "../../test/"
1920
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-
3121
const testFeedName = "test"
3222

3323
type APITestRequest struct {
@@ -98,6 +88,50 @@ func (t APITestRequest) performRequest() (*http.Response, error) {
9888
return w.Result(), nil
9989
}
10090

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+
101135
func TestCreateFeed(t *testing.T) {
102136
const feedName = "6ff1146b6830 #86b8f59f550189a8f91f"
103137

@@ -465,3 +499,48 @@ func TestRemoveItemNonExistentFeed(t *testing.T) {
465499
t.Errorf("Expect code 404 but got %d (%s)", res.StatusCode, string(b))
466500
}
467501
}
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+
}

test/data/test/config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"secret":"b90e516e-b256-41ff-a84e-a9e8d5b6fe30","Subscriptions":null}
1+
{"secret":"b90e516e-b256-41ff-a84e-a9e8d5b6fe30","Subscriptions":[]}

0 commit comments

Comments
 (0)