1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00
Mainflux.mainflux/readers/api/endpoint_test.go
Aleksandar Novaković b9bf63e377 MF-475 - Replace increment ID with UUID (#490)
* Update increment ID to UUID in things service

Update increment ID to UUID for things and channels in things
service and proto files. Also, update ID type from uint to string.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update increment ID to UUID in http adapter

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update increment ID to UUID in ws adapter

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update increment ID to UUID in CoAP adapter

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update increment ID to UUID in normalizer service

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update increment ID to UUID in writer services

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update increment ID to UUID in reader services

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update increment ID to UUID in SDK

Update increment ID to UUID in SDK. Update id type to string.
Update tests.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update increment ID to UUID in mqtt adapter

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Remove unnecessary case from influxdb reader

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update tests in order to increase code coverage

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update lora adapter to use string ID instead of unsigned int

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00

178 lines
4.6 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package api_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/readers"
"github.com/mainflux/mainflux/readers/api"
"github.com/mainflux/mainflux/readers/mocks"
"github.com/stretchr/testify/assert"
)
const (
svcName = "test-service"
token = "1"
invalid = "invalid"
numOfMessages = 42
chanID = "1"
valueFields = 6
)
func newService() readers.MessageRepository {
messages := []mainflux.Message{}
for i := 0; i < numOfMessages; i++ {
msg := mainflux.Message{
Channel: chanID,
Publisher: "1",
Protocol: "mqtt",
}
// Mix possible values as well as value sum.
count := i % valueFields
switch count {
case 0:
msg.Value = &mainflux.Message_FloatValue{FloatValue: 5}
case 1:
msg.Value = &mainflux.Message_BoolValue{BoolValue: false}
case 2:
msg.Value = &mainflux.Message_StringValue{StringValue: "value"}
case 3:
msg.Value = &mainflux.Message_DataValue{DataValue: "base64data"}
case 4:
msg.ValueSum = nil
case 5:
msg.ValueSum = &mainflux.SumValue{Value: 45}
}
messages = append(messages, msg)
}
return mocks.NewMessageRepository(map[string][]mainflux.Message{
chanID: messages,
})
}
func newServer(repo readers.MessageRepository, tc mainflux.ThingsServiceClient) *httptest.Server {
mux := api.MakeHandler(repo, tc, svcName)
return httptest.NewServer(mux)
}
type testRequest struct {
client *http.Client
method string
url string
token string
}
func (tr testRequest) make() (*http.Response, error) {
req, err := http.NewRequest(tr.method, tr.url, nil)
if err != nil {
return nil, err
}
if tr.token != "" {
req.Header.Set("Authorization", tr.token)
}
return tr.client.Do(req)
}
func TestReadAll(t *testing.T) {
svc := newService()
tc := mocks.NewThingsService()
ts := newServer(svc, tc)
defer ts.Close()
cases := map[string]struct {
url string
token string
status int
}{
"read page with valid offset and limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=10", ts.URL, chanID),
token: token,
status: http.StatusOK,
},
"read page with negative offset": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=-1&limit=10", ts.URL, chanID),
token: token,
status: http.StatusBadRequest,
},
"read page with negative limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=-10", ts.URL, chanID),
token: token,
status: http.StatusBadRequest,
},
"read page with zero limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=0", ts.URL, chanID),
token: token,
status: http.StatusBadRequest,
},
"read page with non-integer offset": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=abc&limit=10", ts.URL, chanID),
token: token,
status: http.StatusBadRequest,
},
"read page with non-integer limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=abc", ts.URL, chanID),
token: token,
status: http.StatusBadRequest,
},
"read page with invalid channel id": {
url: fmt.Sprintf("%s/channels//messages?offset=0&limit=10", ts.URL),
token: token,
status: http.StatusBadRequest,
},
"read page with invalid token": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=10", ts.URL, chanID),
token: invalid,
status: http.StatusForbidden,
},
"read page with multiple offset": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&offset=1&limit=10", ts.URL, chanID),
token: token,
status: http.StatusBadRequest,
},
"read page with multiple limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=20&limit=10", ts.URL, chanID),
token: token,
status: http.StatusBadRequest,
},
"read page with empty token": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=10", ts.URL, chanID),
token: "",
status: http.StatusForbidden,
},
"read page with default offset": {
url: fmt.Sprintf("%s/channels/%s/messages?limit=10", ts.URL, chanID),
token: token,
status: http.StatusOK,
},
"read page with default limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0", ts.URL, chanID),
token: token,
status: http.StatusOK,
},
}
for desc, tc := range cases {
req := testRequest{
client: ts.Client(),
method: http.MethodGet,
url: tc.url,
token: tc.token,
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected %d got %d", desc, tc.status, res.StatusCode))
}
}