2017-10-01 01:07:37 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux/manager"
|
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 (
|
|
|
|
client manager.Client = manager.Client{Type: "app"}
|
|
|
|
channel manager.Channel = manager.Channel{}
|
|
|
|
)
|
|
|
|
|
2017-10-01 01:07:37 +02:00
|
|
|
func TestUserReqValidation(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
cases := map[string]struct {
|
2017-10-01 01:07:37 +02:00
|
|
|
user manager.User
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"valid user request": {manager.User{"foo@example.com", "pass"}, nil},
|
|
|
|
"malformed e-mail": {manager.User{wrong, "pass"}, manager.ErrMalformedEntity},
|
|
|
|
"empty e-mail": {manager.User{"", "pass"}, manager.ErrMalformedEntity},
|
|
|
|
"empty password": {manager.User{"foo@example.com", ""}, manager.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 := userReq{tc.user}
|
|
|
|
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 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},
|
|
|
|
"empty token": {"", manager.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 {
|
2017-10-01 01:07:37 +02:00
|
|
|
client manager.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},
|
|
|
|
"missing token": {client, "", manager.ErrUnauthorizedAccess},
|
|
|
|
"wrong client type": {manager.Client{Type: wrong}, key, manager.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 {
|
2017-10-01 01:07:37 +02:00
|
|
|
client manager.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},
|
|
|
|
"non-uuid client ID": {client, wrong, key, manager.ErrNotFound},
|
|
|
|
"missing token": {client, id, "", manager.ErrUnauthorizedAccess},
|
|
|
|
"wrong client type": {manager.Client{Type: "invalid"}, id, key, manager.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 {
|
2017-10-01 01:07:37 +02:00
|
|
|
channel manager.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},
|
|
|
|
"missing token": {channel, "", manager.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 {
|
2017-10-01 01:07:37 +02:00
|
|
|
channel manager.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},
|
|
|
|
"non-uuid channel ID": {channel, wrong, key, manager.ErrNotFound},
|
|
|
|
"missing token": {channel, id, "", manager.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},
|
|
|
|
"missing token": {id, "", manager.ErrUnauthorizedAccess},
|
|
|
|
"non-uuid resource ID": {wrong, key, manager.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
|
|
|
|
size int
|
|
|
|
offset int
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"valid listing request": {key, value, value, nil},
|
|
|
|
"missing token": {"", value, value, manager.ErrUnauthorizedAccess},
|
|
|
|
"negative offset": {key, value, -value, manager.ErrMalformedEntity},
|
|
|
|
"zero size": {key, 0, value, manager.ErrMalformedEntity},
|
|
|
|
"negative size": {key, -value, value, manager.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,
|
|
|
|
size: tc.size,
|
|
|
|
offset: tc.offset,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|