forked from brocaar/chirpstack-network-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_node_handler_test.go
186 lines (157 loc) · 6.08 KB
/
api_node_handler_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package loraserver
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/brocaar/loracontrol"
"github.com/gorilla/mux"
. "github.com/smartystreets/goconvey/convey"
)
func TestNodeCreateHandler(t *testing.T) {
conf := getConfig()
Convey("Given a client connected to a clean Redis database", t, func() {
c, err := loracontrol.NewClient(
loracontrol.SetStorageBackend(loracontrol.NewRedisBackend(conf.RedisServer, conf.RedisPassword)),
loracontrol.SetApplicationBackend(&loracontrol.DummyApplicationBackend{}),
loracontrol.SetGatewayBackend(&loracontrol.DummyGatewayBackend{}),
)
So(err, ShouldBeNil)
So(c.Storage().FlushAll(), ShouldBeNil)
Convey("Given a test http server serving the handler and test JSON", func() {
s := httptest.NewServer(&NodeCreateHandler{c})
node := loracontrol.Node{
DevEUI: [8]byte{8, 7, 6, 5, 4, 3, 2, 1},
AppEUI: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
}
jsonBytes, err := json.Marshal(node)
So(err, ShouldBeNil)
Convey("When posting invalid JSON", func() {
resp, err := http.Post(s.URL, "application/json", bytes.NewReader(jsonBytes[1:]))
So(err, ShouldBeNil)
Convey("Then a 401 status is returned", func() {
So(resp.StatusCode, ShouldEqual, http.StatusBadRequest)
})
})
Convey("When posting valid JSON", func() {
resp, err := http.Post(s.URL, "application/json", bytes.NewReader(jsonBytes))
So(err, ShouldBeNil)
Convey("Then the status code is 201 and the node is created", func() {
So(resp.StatusCode, ShouldEqual, http.StatusCreated)
out, err := c.Node().Get(node.DevEUI)
So(err, ShouldBeNil)
So(out, ShouldResemble, node)
Convey("Then creating the same node again fails", func() {
resp, err := http.Post(s.URL, "application/json", bytes.NewReader(jsonBytes))
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusBadRequest)
})
})
})
})
})
}
func TestNodeObjectHandler(t *testing.T) {
conf := getConfig()
Convey("Given a Client connected to a clean Redis database", t, func() {
c, err := loracontrol.NewClient(
loracontrol.SetStorageBackend(loracontrol.NewRedisBackend(conf.RedisServer, conf.RedisPassword)),
loracontrol.SetApplicationBackend(&loracontrol.DummyApplicationBackend{}),
loracontrol.SetGatewayBackend(&loracontrol.DummyGatewayBackend{}),
)
So(err, ShouldBeNil)
So(c.Storage().FlushAll(), ShouldBeNil)
Convey("Given a test server serving the handler", func() {
r := mux.NewRouter()
r.Handle("/{id}", &NodeObjectHandler{c})
s := httptest.NewServer(r)
Convey("Getting a non-existing node returns 404", func() {
resp, err := http.Get(s.URL + "/0102030405060708")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusNotFound)
})
Convey("Getting an invalid node id returns 401", func() {
resp, err := http.Get(s.URL + "/010203040506070")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusBadRequest)
})
Convey("Getting a node with an id which is too short returns 401", func() {
resp, err := http.Get(s.URL + "/01020304050607")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusBadRequest)
})
Convey("Given a node in the database", func() {
node := loracontrol.Node{
DevEUI: [8]byte{8, 7, 6, 5, 4, 3, 2, 1},
AppEUI: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
}
So(c.Node().Create(node), ShouldBeNil)
Convey("Then GET returns the node", func() {
resp, err := http.Get(s.URL + "/0807060504030201")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusOK)
out := loracontrol.Node{}
dec := json.NewDecoder(resp.Body)
So(dec.Decode(&out), ShouldBeNil)
So(out, ShouldResemble, node)
})
Convey("When DELETE-ing this node", func() {
req, err := http.NewRequest("DELETE", s.URL+"/0807060504030201", nil)
So(err, ShouldBeNil)
resp, err := http.DefaultClient.Do(req)
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusNoContent)
Convey("Then the node has been deleted", func() {
resp, err := http.Get(s.URL + "/0807060504030201")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusNotFound)
})
})
Convey("When updating the node", func() {
node.AppEUI = [8]byte{1, 1, 1, 1, 1, 1, 1, 1}
b, err := json.Marshal(node)
So(err, ShouldBeNil)
req, err := http.NewRequest("PUT", s.URL+"/0807060504030201", bytes.NewReader(b))
So(err, ShouldBeNil)
resp, err := http.DefaultClient.Do(req)
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusNoContent)
Convey("Then the node is updated", func() {
resp, err := http.Get(s.URL + "/0807060504030201")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusOK)
out := loracontrol.Node{}
dec := json.NewDecoder(resp.Body)
So(dec.Decode(&out), ShouldBeNil)
So(out, ShouldResemble, node)
})
Convey("When updating with invalid JSON then a 401 is returned", func() {
req, err := http.NewRequest("PUT", s.URL+"/0807060504030201", bytes.NewReader(b[1:]))
So(err, ShouldBeNil)
resp, err := http.DefaultClient.Do(req)
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusBadRequest)
})
Convey("When the id in the url does not match the id in the body then a 401 is returned", func() {
node.DevEUI = [8]byte{8, 7, 6, 5, 4, 3, 2, 2}
b, err := json.Marshal(node)
So(err, ShouldBeNil)
req, err := http.NewRequest("PUT", s.URL+"/0807060504030201", bytes.NewReader(b))
So(err, ShouldBeNil)
resp, err := http.DefaultClient.Do(req)
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusBadRequest)
})
})
Convey("Requesting with an invalid method returns 405", func() {
req, err := http.NewRequest("PATCH", s.URL+"/0807060504030201", nil)
So(err, ShouldBeNil)
resp, err := http.DefaultClient.Do(req)
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusMethodNotAllowed)
})
})
})
})
}