Skip to content

Commit

Permalink
Adding search by source, updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Danzabar committed Feb 15, 2017
1 parent d1c1a8c commit 5ea09dd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ func FindNotification(w http.ResponseWriter, r *http.Request) {
func GetNotification(w http.ResponseWriter, r *http.Request) {
var n []Notification
hasRead, err := strconv.ParseBool(r.FormValue("read"))
source := r.FormValue("source")

s := Notification{Read: hasRead}

if source != "" {
s.Source = source
}

if err != nil {
hasRead = false
Expand All @@ -164,7 +171,7 @@ func GetNotification(w http.ResponseWriter, r *http.Request) {
App.db.Preload("Tags").
Limit(p.Limit).
Offset(p.Offset).
Where(&Notification{Read: hasRead}).
Where(&s).
Find(&n)

jsonStr, _ := json.Marshal(&n)
Expand Down
20 changes: 20 additions & 0 deletions notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,23 @@ func TestPostReadFail(t *testing.T) {

assert.Equal(t, 404, resp.StatusCode)
}

func TestGetNotificationBySource(t *testing.T) {
App.db.Create(&Notification{Message: "Test1", Source: "source1"})
App.db.Create(&Notification{Message: "Test2", Source: "source2"})

req, _ := http.NewRequest("GET", fmt.Sprintf("%s/api/v1/notification?source=source2", server.URL), nil)
req.SetBasicAuth("test", "test")
resp, err := http.DefaultClient.Do(req)

if err != nil {
t.Fatal(err)
}

var n []Notification
json.NewDecoder(resp.Body).Decode(&n)

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, 1, len(n))
assert.Equal(t, "Test2", n[0].Message)
}

0 comments on commit 5ea09dd

Please sign in to comment.