Skip to content

Commit

Permalink
-ve test for notification api
Browse files Browse the repository at this point in the history
  • Loading branch information
kneerunjun committed Jun 5, 2024
1 parent 2023a16 commit 3fe4437
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ func HndlDeviceNotifics(c *gin.Context) {
byt, err := io.ReadAll(c.Request.Body)
if err != nil {
httperr.HttpErrOrOkDispatch(c, httperr.ErrBinding(err), log.WithFields(log.Fields{
"stack": "HndlDeviceNotifics/typ=cfgchange",
"stack": "HndlDeviceNotifics",
}))
return
}
err = json.Unmarshal(byt, &not)
if err != nil {
httperr.HttpErrOrOkDispatch(c, httperr.ErrBinding(err), log.WithFields(log.Fields{
"stack": "HndlDeviceNotifics/typ=cfgchange",
"stack": "HndlDeviceNotifics",
}))
return
}
Expand Down
19 changes: 19 additions & 0 deletions quick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ import (
func TestApi(t *testing.T) {
cl := &http.Client{Timeout: 5 * time.Second}
baseurl := "http://localhost:8080/api/devices/b8:27:eb:a5:be:48/notifications"

t.Run("invalid query param", func(t *testing.T) {
url := fmt.Sprintf("%s/?typ=invalidparam", baseurl)
not := models.Notification("Test aquaponics configuration", "b8:27:eb:a5:be:48", time.Now(), models.CfgChange(&aquacfg.Schedule{
Config: 1,
TickAt: "11:30",
PulseGap: 100,
Interval: 500,
}))

byt, err := json.Marshal(not)
assert.Nil(t, err, "Unexpected error when marshaling bot message")
payload := bytes.NewBuffer(byt)
req, err := http.NewRequest("POST", url, payload)
assert.Nil(t, err, "Unexpected error when forming the request")
resp, err := cl.Do(req)
assert.Nil(t, err, "unexpected error when executing the request, do you have access to the server ?")
assert.Equal(t, resp.StatusCode, http.StatusBadRequest, "Unepxected response code from server")
})
t.Run("cfg change", func(t *testing.T) {
url := fmt.Sprintf("%s/?typ=cfgchange", baseurl)
not := models.Notification("Test aquaponics configuration", "b8:27:eb:a5:be:48", time.Now(), models.CfgChange(&aquacfg.Schedule{
Expand Down

0 comments on commit 3fe4437

Please sign in to comment.