Skip to content

Commit

Permalink
added server start nohup so that api end test can be conducted
Browse files Browse the repository at this point in the history
  • Loading branch information
kneerunjun committed Jun 5, 2024
1 parent b453906 commit 6d4f9b3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ jobs:
with:
stable: 'false'
go-version: '1.22.3'
- name: local gin server
run: |
nohup go run main.go&
echo "gin server now running .."
- name: unittests
run: |
echo $DEVICEREG_URL
echo $BOT_BASEURL
echo $BOT_UNAME
echo $BOT_TOK
go clean --testcache
go test -v -timeout 30s -run TestTelegGetMe
go test -v -timeout 30s -run TestApi
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ func HndlDeviceNotifics(c *gin.Context) {
} else if typOfNotify == "gpiostat" {
log.Debug("Gpio status notification")
not = models.Notification("", "", time.Now(), models.GpioStatus(&models.Pinstat{})) // onto which the payload would be unmarshalled
} else if typOfNotify == "vitals" {
log.Debug("Vitals status notification")
not = models.Notification("", "", time.Now(), models.VitalStats("", "", "", "", "")) // onto which the payload would be unmarshalled
}
/* Reading the request body and that is agnostic of which notification it is */
byt, err := io.ReadAll(c.Request.Body)
Expand All @@ -178,7 +181,6 @@ func HndlDeviceNotifics(c *gin.Context) {
}))
return
}

/* Preparing the notification to be sent across to telegram */
msg, _ := not.ToMessageTxt()
log.WithFields(log.Fields{
Expand Down
11 changes: 5 additions & 6 deletions models/dvcdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,11 @@ func (gps *gpioStatus) ToMessageTxt() (string, error) {

/* VitalStatsData: is the object that eventually gets converted to text message in bot send */
type vitalStats struct {
DeviceData DeviceNotifcn // device details
AquaponeSrv bool // indicates if the systemctl unit is working
CfgwatchSrv bool // indicates if the systemctl unit is working
Online bool // indicates if the device is online, on internet
FreeCPU int // indicates percentage of CPU that is free
CPUUpTime string // indicates the cpu up time from uptime command
AquaponeSrv bool `json:"aquapone_service"` // indicates if the systemctl unit is working
CfgwatchSrv bool `json:"cfgwatch_service"` // indicates if the systemctl unit is working
Online bool `json:"online"` // indicates if the device is online, on internet
FreeCPU int `json:"free_cpu"` // indicates percentage of CPU that is free
CPUUpTime string `json:"cpu_uptime"` // indicates the cpu up time from uptime command
}

func (vs *vitalStats) ToMessageTxt() (string, error) {
Expand Down
26 changes: 26 additions & 0 deletions quick_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand All @@ -9,9 +10,34 @@ import (
"testing"
"time"

"github.com/eensymachines-in/patio/aquacfg"
"github.com/eensymachines-in/webpi-telegnotify/models"
"github.com/stretchr/testify/assert"
)

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("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{
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.Status, http.StatusOK, "Unepxected response code from server")
})
}

func TestTelegGetMe(t *testing.T) {
cl := &http.Client{Timeout: 5 * time.Second}
url := fmt.Sprintf("%s%s/getMe", os.Getenv("BOT_BASEURL"), os.Getenv("BOT_TOK"))
Expand Down

0 comments on commit 6d4f9b3

Please sign in to comment.