2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-05-10 23:53:25 +02:00
|
|
|
package http
|
2017-10-01 01:07:37 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-24 11:21:03 +02:00
|
|
|
"strconv"
|
2017-10-01 01:07:37 +02:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-10-24 11:21:03 +02:00
|
|
|
"non-empty token": {
|
|
|
|
key: uuid.NewV4().String(),
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"empty token": {
|
|
|
|
key: "",
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
2017-10-01 01:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2018-05-21 12:51:46 +02:00
|
|
|
req := identityReq{key: tc.key}
|
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 TestAddThingReqValidation(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
key := uuid.NewV4().String()
|
2018-05-21 12:51:46 +02:00
|
|
|
valid := things.Thing{Type: "app"}
|
2018-12-05 13:09:25 +01:00
|
|
|
invalid := things.Thing{ID: "0", Type: ""}
|
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-10-24 11:21:03 +02:00
|
|
|
"valid thing addition request": {
|
|
|
|
thing: valid,
|
|
|
|
key: key,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"missing token": {
|
|
|
|
thing: valid,
|
|
|
|
key: "", err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
"empty thing type": {
|
|
|
|
thing: invalid,
|
|
|
|
key: key,
|
|
|
|
err: 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{
|
2018-10-24 11:21:03 +02:00
|
|
|
key: tc.key,
|
|
|
|
Name: tc.thing.Name,
|
|
|
|
Type: tc.thing.Type,
|
|
|
|
Metadata: tc.thing.Metadata,
|
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()
|
2018-12-05 13:09:25 +01:00
|
|
|
valid := things.Thing{ID: "1", Type: "app"}
|
|
|
|
invalid := things.Thing{ID: "0", Type: ""}
|
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
|
2018-10-24 11:21:03 +02:00
|
|
|
id string
|
2018-05-15 17:13:09 +02:00
|
|
|
key string
|
|
|
|
err error
|
2017-10-01 01:07:37 +02:00
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"valid thing update request": {
|
|
|
|
thing: valid,
|
2018-12-05 13:09:25 +01:00
|
|
|
id: valid.ID,
|
2018-10-24 11:21:03 +02:00
|
|
|
key: key,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"missing token": {
|
|
|
|
thing: valid,
|
2018-12-05 13:09:25 +01:00
|
|
|
id: valid.ID,
|
2018-10-24 11:21:03 +02:00
|
|
|
key: "",
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
"empty thing type": {
|
|
|
|
thing: invalid,
|
2018-12-05 13:09:25 +01:00
|
|
|
id: valid.ID,
|
|
|
|
key: key,
|
|
|
|
err: things.ErrMalformedEntity,
|
|
|
|
},
|
|
|
|
"empty thing id": {
|
|
|
|
thing: valid,
|
|
|
|
id: "",
|
2018-10-24 11:21:03 +02:00
|
|
|
key: key,
|
|
|
|
err: 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{
|
2018-10-24 11:21:03 +02:00
|
|
|
key: tc.key,
|
|
|
|
id: tc.id,
|
|
|
|
Name: tc.thing.Name,
|
|
|
|
Type: tc.thing.Type,
|
|
|
|
Metadata: tc.thing.Metadata,
|
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-05-21 12:51:46 +02:00
|
|
|
channel := things.Channel{}
|
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-10-24 11:21:03 +02:00
|
|
|
"valid channel creation request": {
|
|
|
|
channel: channel,
|
|
|
|
key: key,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"missing token": {
|
|
|
|
channel: channel,
|
|
|
|
key: "",
|
|
|
|
err: 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{
|
2018-10-24 11:21:03 +02:00
|
|
|
key: tc.key,
|
|
|
|
Name: tc.channel.Name,
|
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 TestUpdateChannelReqValidation(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
key := uuid.NewV4().String()
|
2018-12-05 13:09:25 +01:00
|
|
|
channel := things.Channel{ID: "1"}
|
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-10-24 11:21:03 +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-10-24 11:21:03 +02:00
|
|
|
"valid channel update request": {
|
|
|
|
channel: channel,
|
2018-12-05 13:09:25 +01:00
|
|
|
id: channel.ID,
|
2018-10-24 11:21:03 +02:00
|
|
|
key: key,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"missing token": {
|
|
|
|
channel: channel,
|
2018-12-05 13:09:25 +01:00
|
|
|
id: channel.ID,
|
2018-10-24 11:21:03 +02:00
|
|
|
key: "",
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
2018-12-05 13:09:25 +01:00
|
|
|
"empty channel id": {
|
|
|
|
channel: channel,
|
|
|
|
id: "",
|
|
|
|
key: key,
|
|
|
|
err: 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 := updateChannelReq{
|
2018-10-24 11:21:03 +02:00
|
|
|
key: tc.key,
|
|
|
|
id: tc.id,
|
|
|
|
Name: tc.channel.Name,
|
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 TestViewResourceReqValidation(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
key := uuid.NewV4().String()
|
2018-05-21 12:51:46 +02:00
|
|
|
id := uint64(1)
|
2017-10-01 01:07:37 +02:00
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
cases := map[string]struct {
|
2018-10-24 11:21:03 +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-10-24 11:21:03 +02:00
|
|
|
"valid resource viewing request": {
|
|
|
|
id: strconv.FormatUint(id, 10),
|
|
|
|
key: key,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"missing token": {
|
|
|
|
id: strconv.FormatUint(id, 10),
|
|
|
|
key: "",
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
2018-12-05 13:09:25 +01:00
|
|
|
"empty resource id": {
|
|
|
|
id: "",
|
|
|
|
key: key,
|
|
|
|
err: 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 := 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()
|
2018-10-24 11:21:03 +02:00
|
|
|
value := uint64(10)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-10-01 01:07:37 +02:00
|
|
|
key string
|
2018-10-24 11:21:03 +02:00
|
|
|
offset uint64
|
|
|
|
limit uint64
|
2017-10-01 01:07:37 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"valid listing request": {
|
|
|
|
key: key,
|
|
|
|
offset: value,
|
|
|
|
limit: value,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"missing token": {
|
|
|
|
key: "",
|
|
|
|
offset: value,
|
|
|
|
limit: value,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
"zero limit": {
|
|
|
|
key: key,
|
|
|
|
offset: value,
|
|
|
|
limit: 0,
|
|
|
|
err: things.ErrMalformedEntity,
|
|
|
|
},
|
|
|
|
"too big limit": {
|
|
|
|
key: key,
|
|
|
|
offset: value,
|
|
|
|
limit: 20 * value,
|
|
|
|
err: 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
|
|
|
}
|
|
|
|
}
|
2018-10-24 11:21:03 +02:00
|
|
|
|
|
|
|
func TestConnectionReqValidation(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
key string
|
|
|
|
chanID string
|
|
|
|
thingID string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
"valid key": {
|
|
|
|
key: "valid-key",
|
|
|
|
chanID: "1",
|
|
|
|
thingID: "1",
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"empty key": {
|
|
|
|
key: "",
|
|
|
|
chanID: "1",
|
|
|
|
thingID: "1",
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
2018-12-05 13:09:25 +01:00
|
|
|
"empty channel id": {
|
|
|
|
key: "valid-key",
|
|
|
|
chanID: "",
|
|
|
|
thingID: "1",
|
|
|
|
err: things.ErrMalformedEntity,
|
|
|
|
},
|
|
|
|
"empty thing id": {
|
|
|
|
key: "valid-key",
|
|
|
|
chanID: "1",
|
|
|
|
thingID: "",
|
|
|
|
err: things.ErrMalformedEntity,
|
|
|
|
},
|
2018-10-24 11:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
|
|
|
req := connectionReq{
|
|
|
|
key: tc.key,
|
|
|
|
chanID: tc.chanID,
|
|
|
|
thingID: tc.thingID,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := req.validate()
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|