2018-05-10 23:53:25 +02:00
|
|
|
package http
|
2017-10-01 01:07:37 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2018-03-11 18:06:01 +01:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2017-10-01 01:07:37 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
const wrong string = "?"
|
|
|
|
|
|
|
|
var (
|
2018-05-15 17:13:09 +02:00
|
|
|
thing = things.Thing{Type: "app"}
|
|
|
|
channel = things.Channel{}
|
2018-03-11 18:06:01 +01:00
|
|
|
)
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
func TestIdentityReqValidation(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
cases := map[string]struct {
|
2017-10-01 01:07:37 +02:00
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"non-empty token": {uuid.NewV4().String(), nil},
|
2018-05-15 17:13:09 +02:00
|
|
|
"empty token": {"", things.ErrUnauthorizedAccess},
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-10-01 01:07:37 +02:00
|
|
|
req := identityReq{tc.key}
|
|
|
|
err := req.validate()
|
2018-03-11 18:06:01 +01:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func TestAddThingReqValidation(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
key := uuid.NewV4().String()
|
2017-10-01 01:07:37 +02:00
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
cases := map[string]struct {
|
2018-05-15 17:13:09 +02:00
|
|
|
thing things.Thing
|
|
|
|
key string
|
|
|
|
err error
|
2017-10-01 01:07:37 +02:00
|
|
|
}{
|
2018-05-15 17:13:09 +02:00
|
|
|
"valid thing addition request": {thing, key, nil},
|
|
|
|
"missing token": {thing, "", things.ErrUnauthorizedAccess},
|
|
|
|
"wrong thing type": {things.Thing{Type: wrong}, key, things.ErrMalformedEntity},
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2018-05-15 17:13:09 +02:00
|
|
|
req := addThingReq{
|
|
|
|
key: tc.key,
|
|
|
|
thing: tc.thing,
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := req.validate()
|
2018-03-11 18:06:01 +01:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func TestUpdateThingReqValidation(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
key := uuid.NewV4().String()
|
|
|
|
id := uuid.NewV4().String()
|
2017-10-01 01:07:37 +02:00
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
cases := map[string]struct {
|
2018-05-15 17:13:09 +02:00
|
|
|
thing things.Thing
|
|
|
|
id string
|
|
|
|
key string
|
|
|
|
err error
|
2017-10-01 01:07:37 +02:00
|
|
|
}{
|
2018-05-15 17:13:09 +02:00
|
|
|
"valid thing update request": {thing, id, key, nil},
|
|
|
|
"non-uuid thing ID": {thing, wrong, key, things.ErrNotFound},
|
|
|
|
"missing token": {thing, id, "", things.ErrUnauthorizedAccess},
|
|
|
|
"wrong thing type": {things.Thing{Type: "invalid"}, id, key, things.ErrMalformedEntity},
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2018-05-15 17:13:09 +02:00
|
|
|
req := updateThingReq{
|
|
|
|
key: tc.key,
|
|
|
|
id: tc.id,
|
|
|
|
thing: tc.thing,
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := req.validate()
|
2018-03-11 18:06:01 +01:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateChannelReqValidation(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
key := uuid.NewV4().String()
|
2017-10-01 01:07:37 +02:00
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
cases := map[string]struct {
|
2018-05-15 17:13:09 +02:00
|
|
|
channel things.Channel
|
2018-03-11 18:06:01 +01:00
|
|
|
key string
|
2017-10-01 01:07:37 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"valid channel creation request": {channel, key, nil},
|
2018-05-15 17:13:09 +02:00
|
|
|
"missing token": {channel, "", things.ErrUnauthorizedAccess},
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-10-01 01:07:37 +02:00
|
|
|
req := createChannelReq{
|
|
|
|
key: tc.key,
|
|
|
|
channel: tc.channel,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := req.validate()
|
2018-03-11 18:06:01 +01:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateChannelReqValidation(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
key := uuid.NewV4().String()
|
|
|
|
id := uuid.NewV4().String()
|
2017-10-01 01:07:37 +02:00
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
cases := map[string]struct {
|
2018-05-15 17:13:09 +02:00
|
|
|
channel things.Channel
|
2018-03-11 18:06:01 +01:00
|
|
|
id string
|
|
|
|
key string
|
2017-10-01 01:07:37 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"valid channel update request": {channel, id, key, nil},
|
2018-05-15 17:13:09 +02:00
|
|
|
"non-uuid channel ID": {channel, wrong, key, things.ErrNotFound},
|
|
|
|
"missing token": {channel, id, "", things.ErrUnauthorizedAccess},
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-10-01 01:07:37 +02:00
|
|
|
req := updateChannelReq{
|
|
|
|
key: tc.key,
|
|
|
|
id: tc.id,
|
|
|
|
channel: tc.channel,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := req.validate()
|
2018-03-11 18:06:01 +01:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestViewResourceReqValidation(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
key := uuid.NewV4().String()
|
|
|
|
id := uuid.NewV4().String()
|
2017-10-01 01:07:37 +02:00
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
cases := map[string]struct {
|
2017-10-01 01:07:37 +02:00
|
|
|
id string
|
2018-03-11 18:06:01 +01:00
|
|
|
key string
|
2017-10-01 01:07:37 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"valid resource viewing request": {id, key, nil},
|
2018-05-15 17:13:09 +02:00
|
|
|
"missing token": {id, "", things.ErrUnauthorizedAccess},
|
|
|
|
"non-uuid resource ID": {wrong, key, things.ErrNotFound},
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-10-01 01:07:37 +02:00
|
|
|
req := viewResourceReq{tc.key, tc.id}
|
|
|
|
err := req.validate()
|
2018-03-11 18:06:01 +01:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListResourcesReqValidation(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
key := uuid.NewV4().String()
|
|
|
|
value := 10
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-10-01 01:07:37 +02:00
|
|
|
key string
|
|
|
|
offset int
|
2018-04-18 22:36:24 +02:00
|
|
|
limit int
|
2017-10-01 01:07:37 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"valid listing request": {key, value, value, nil},
|
2018-05-15 17:13:09 +02:00
|
|
|
"missing token": {"", value, value, things.ErrUnauthorizedAccess},
|
|
|
|
"negative offset": {key, -value, value, things.ErrMalformedEntity},
|
|
|
|
"zero limit": {key, value, 0, things.ErrMalformedEntity},
|
|
|
|
"negative limit": {key, value, -value, things.ErrMalformedEntity},
|
|
|
|
"too big limit": {key, value, 20 * value, things.ErrMalformedEntity},
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-10-01 01:07:37 +02:00
|
|
|
req := listResourcesReq{
|
|
|
|
key: tc.key,
|
|
|
|
offset: tc.offset,
|
2018-04-18 22:36:24 +02:00
|
|
|
limit: tc.limit,
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := req.validate()
|
2018-03-11 18:06:01 +01:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
}
|