forked from brocaar/chirpstack-network-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_app_handler_test.go
168 lines (142 loc) · 5.42 KB
/
api_app_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
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 TestApplicationCreateHandler(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(&ApplicationCreateHandler{c})
app := loracontrol.Application{
AppEUI: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
}
jsonBytes, err := json.Marshal(app)
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 application is created", func() {
So(resp.StatusCode, ShouldEqual, http.StatusCreated)
out, err := c.Application().Get([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
So(err, ShouldBeNil)
So(out, ShouldResemble, app)
Convey("Then creating the same object again fails", func() {
resp, err := http.Post(s.URL, "application/json", bytes.NewReader(jsonBytes))
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusBadRequest)
})
})
})
})
})
}
func TestApplicationObjectHandler(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}", &ApplicationObjectHandler{c})
s := httptest.NewServer(r)
Convey("Getting a non-existing application returns 404", func() {
resp, err := http.Get(s.URL + "/0102030405060708")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusNotFound)
})
Convey("Getting an invalid application id returns 401", func() {
resp, err := http.Get(s.URL + "/010203040506070")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusBadRequest)
})
Convey("Getting an application 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 an application in the database", func() {
app := loracontrol.Application{
AppEUI: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
}
So(c.Application().Create(app), ShouldBeNil)
Convey("Then GET returns the application", func() {
resp, err := http.Get(s.URL + "/0102030405060708")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusOK)
out := loracontrol.Application{}
dec := json.NewDecoder(resp.Body)
So(dec.Decode(&out), ShouldBeNil)
So(out, ShouldResemble, app)
})
Convey("When DELETE-ing this application", func() {
req, err := http.NewRequest("DELETE", s.URL+"/0102030405060708", nil)
So(err, ShouldBeNil)
resp, err := http.DefaultClient.Do(req)
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusNoContent)
Convey("Then the application has been removed", func() {
resp, err := http.Get(s.URL + "/0102030405060708")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusNotFound)
})
})
Convey("When updating the application", func() {
app.Config = loracontrol.PropertyBag{
String: map[string]string{
"callback_url": "http://foo.bar/",
},
}
b, err := json.Marshal(app)
So(err, ShouldBeNil)
req, err := http.NewRequest("PUT", s.URL+"/0102030405060708", bytes.NewReader(b))
So(err, ShouldBeNil)
resp, err := http.DefaultClient.Do(req)
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusNoContent)
Convey("Then the application is updated", func() {
resp, err := http.Get(s.URL + "/0102030405060708")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusOK)
out := loracontrol.Application{}
dec := json.NewDecoder(resp.Body)
So(dec.Decode(&out), ShouldBeNil)
So(out, ShouldResemble, app)
})
})
Convey("Requesting with an invalid method returns 405", func() {
req, err := http.NewRequest("PATCH", s.URL+"/0102030405060708", nil)
So(err, ShouldBeNil)
resp, err := http.DefaultClient.Do(req)
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, http.StatusMethodNotAllowed)
})
})
})
})
}