diff --git a/.github/workflows/unittest.yml b/.github/workflows/unittest.yml index f4c5b9a..d95b501 100644 --- a/.github/workflows/unittest.yml +++ b/.github/workflows/unittest.yml @@ -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 diff --git a/main.go b/main.go index c40bdd7..6e76940 100644 --- a/main.go +++ b/main.go @@ -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) @@ -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{ diff --git a/models/dvcdata.go b/models/dvcdata.go index 4fe0996..30b6f6a 100644 --- a/models/dvcdata.go +++ b/models/dvcdata.go @@ -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) { diff --git a/quick_test.go b/quick_test.go index 65ac829..a35c3b0 100644 --- a/quick_test.go +++ b/quick_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "encoding/json" "fmt" "io" @@ -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"))