-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_test.go
138 lines (123 loc) · 5.59 KB
/
quick_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"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("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")
})
// NOTE: there isnt a possibility of a bad configuration - since the update is tightly guarded by cfgwatch
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.StatusCode, http.StatusOK, "Unepxected response code from server")
})
t.Run("GPIO_report_status", func(t *testing.T) {
url := fmt.Sprintf("%s/?typ=gpiostat", baseurl)
not := models.Notification("Test aquaponics configuration", "b8:27:eb:a5:be:48", time.Now(), models.GpioStatus(models.PinStatus("Motor control relay", models.ACTUATOR, 33, models.DIGIPIN_HIGH)))
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.StatusOK, "Unepxected response code from server")
})
t.Run("vital_status", func(t *testing.T) {
url := fmt.Sprintf("%s/?typ=vitals", baseurl)
not := models.Notification("Test aquaponics configuration", "b8:27:eb:a5:be:48", time.Now(), models.VitalStats("active", "active", "HTTP/2 200", "16 7", "4d 8h"))
t.Log("Now logging the vital stats notification")
t.Log(not.ToMessageTxt())
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.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"))
req, err := http.NewRequest("GET", url, nil)
assert.Nil(t, err, "failed to create new request")
assert.NotNil(t, req, "Unexpected nil request object")
resp, err := cl.Do(req)
assert.Nil(t, err, "Unexpected error when sending request")
assert.NotNil(t, resp, "Unexpected nil response")
byt, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
assert.Nil(t, err, "Unexpected error when reading response body")
result := map[string]interface{}{}
err = json.Unmarshal(byt, &result)
assert.Nil(t, err, "Unexpected error when unmarshalling")
t.Log(result)
}
// func TestTelegSendMessage(t *testing.T) {
// cl := &http.Client{Timeout: 5 * time.Second}
// url := fmt.Sprintf("%s%s/sendMessage", os.Getenv("BOT_BASEURL"), os.Getenv("BOT_TOK"))
// msg, _ := models.Notification("Aquaponics pump-III", "D5-5C-1C-04-81-29", time.Now(), models.CfgChange(&aquacfg.Schedule{
// Config: 2,
// TickAt: "14:00",
// PulseGap: 100,
// Interval: 500,
// })).ToMessageTxt()
// bm := BotMessage{ChatID: GRP_ID, Txt: msg, ParseMode: "markdown"}
// // byt, _ := json.Marshal(map[string]string{"chat_id": GRP_ID, "text": "This is hi from inside the bot"})
// byt, _ := json.Marshal(bm)
// req, err := http.NewRequest("POST", url, bytes.NewBuffer(byt))
// req.Header.Set("Content-Type", "application/json")
// assert.Nil(t, err, "failed to create new request")
// assert.NotNil(t, req, "Unexpected nil request object")
// resp, err := cl.Do(req)
// assert.Nil(t, err, "Unexpected error when sending request")
// assert.NotNil(t, resp, "Unexpected nil response")
// byt, err = io.ReadAll(resp.Body)
// defer resp.Body.Close()
// assert.Nil(t, err, "Unexpected error when reading response body")
// result := map[string]interface{}{}
// err = json.Unmarshal(byt, &result)
// assert.Nil(t, err, "Unexpected error when unmarshalling")
// t.Log(result)
// }