2018-05-10 23:53:25 +02:00
|
|
|
package http
|
2017-10-01 01:07:37 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-05-10 23:53:25 +02:00
|
|
|
"github.com/mainflux/mainflux/clients"
|
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-10 23:53:25 +02:00
|
|
|
client = clients.Client{Type: "app"}
|
|
|
|
channel = clients.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-10 23:53:25 +02:00
|
|
|
"empty token": {"", clients.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddClientReqValidation(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-10 23:53:25 +02:00
|
|
|
client clients.Client
|
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 client addition request": {client, key, nil},
|
2018-05-10 23:53:25 +02:00
|
|
|
"missing token": {client, "", clients.ErrUnauthorizedAccess},
|
|
|
|
"wrong client type": {clients.Client{Type: wrong}, key, clients.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 := addClientReq{
|
|
|
|
key: tc.key,
|
|
|
|
client: tc.client,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 TestUpdateClientReqValidation(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-10 23:53:25 +02:00
|
|
|
client clients.Client
|
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 client update request": {client, id, key, nil},
|
2018-05-10 23:53:25 +02:00
|
|
|
"non-uuid client ID": {client, wrong, key, clients.ErrNotFound},
|
|
|
|
"missing token": {client, id, "", clients.ErrUnauthorizedAccess},
|
|
|
|
"wrong client type": {clients.Client{Type: "invalid"}, id, key, clients.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 := updateClientReq{
|
|
|
|
key: tc.key,
|
|
|
|
id: tc.id,
|
|
|
|
client: tc.client,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10 23:53:25 +02:00
|
|
|
channel clients.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-10 23:53:25 +02:00
|
|
|
"missing token": {channel, "", clients.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-10 23:53:25 +02:00
|
|
|
channel clients.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-10 23:53:25 +02:00
|
|
|
"non-uuid channel ID": {channel, wrong, key, clients.ErrNotFound},
|
|
|
|
"missing token": {channel, id, "", clients.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-10 23:53:25 +02:00
|
|
|
"missing token": {id, "", clients.ErrUnauthorizedAccess},
|
|
|
|
"non-uuid resource ID": {wrong, key, clients.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-10 23:53:25 +02:00
|
|
|
"missing token": {"", value, value, clients.ErrUnauthorizedAccess},
|
|
|
|
"negative offset": {key, -value, value, clients.ErrMalformedEntity},
|
|
|
|
"zero limit": {key, value, 0, clients.ErrMalformedEntity},
|
|
|
|
"negative limit": {key, value, -value, clients.ErrMalformedEntity},
|
|
|
|
"too big limit": {key, value, 20 * value, clients.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
|
|
|
}
|
|
|
|
}
|