2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
package things_test
|
2018-05-10 23:53:25 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
|
|
|
"github.com/mainflux/mainflux/things/mocks"
|
2018-05-10 23:53:25 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-05-21 12:51:46 +02:00
|
|
|
wrongID = 0
|
|
|
|
wrongValue = "wrong-value"
|
|
|
|
email = "user@example.com"
|
|
|
|
token = "token"
|
2018-05-10 23:53:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-05-15 17:13:09 +02:00
|
|
|
thing = things.Thing{Type: "app", Name: "test"}
|
|
|
|
channel = things.Channel{Name: "test", Things: []things.Thing{}}
|
2018-05-10 23:53:25 +02:00
|
|
|
)
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func newService(tokens map[string]string) things.Service {
|
2018-05-10 23:53:25 +02:00
|
|
|
users := mocks.NewUsersService(tokens)
|
2018-05-15 17:13:09 +02:00
|
|
|
thingsRepo := mocks.NewThingRepository()
|
|
|
|
channelsRepo := mocks.NewChannelRepository(thingsRepo)
|
2018-09-04 22:19:43 +02:00
|
|
|
chanCache := mocks.NewChannelCache()
|
|
|
|
thingCache := mocks.NewThingCache()
|
2018-05-10 23:53:25 +02:00
|
|
|
idp := mocks.NewIdentityProvider()
|
|
|
|
|
2018-09-04 22:19:43 +02:00
|
|
|
return things.New(users, thingsRepo, channelsRepo, chanCache, thingCache, idp)
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func TestAddThing(t *testing.T) {
|
2018-05-10 23:53:25 +02:00
|
|
|
svc := newService(map[string]string{token: email})
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2018-05-15 17:13:09 +02:00
|
|
|
thing things.Thing
|
|
|
|
key string
|
|
|
|
err error
|
2018-05-10 23:53:25 +02:00
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "add new app",
|
|
|
|
thing: things.Thing{Type: "app", Name: "a"},
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "add new device",
|
|
|
|
thing: things.Thing{Type: "device", Name: "b"},
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "add thing with wrong credentials",
|
|
|
|
thing: things.Thing{Type: "app", Name: "d"},
|
|
|
|
key: wrongValue,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "add thing with invalid type",
|
|
|
|
thing: things.Thing{Type: "invalid", Name: "d"},
|
|
|
|
key: wrongValue,
|
|
|
|
err: things.ErrMalformedEntity,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
for _, tc := range cases {
|
2018-05-15 17:13:09 +02:00
|
|
|
_, err := svc.AddThing(tc.key, tc.thing)
|
2018-10-24 11:21:03 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func TestUpdateThing(t *testing.T) {
|
2018-05-10 23:53:25 +02:00
|
|
|
svc := newService(map[string]string{token: email})
|
2018-05-16 14:28:41 +02:00
|
|
|
saved, _ := svc.AddThing(token, thing)
|
2018-05-21 12:51:46 +02:00
|
|
|
other := things.Thing{ID: wrongID, Type: "app", Key: "x"}
|
2018-05-10 23:53:25 +02:00
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2018-05-15 17:13:09 +02:00
|
|
|
thing things.Thing
|
|
|
|
key string
|
|
|
|
err error
|
2018-05-10 23:53:25 +02:00
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "update existing thing",
|
|
|
|
thing: saved,
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "update thing with wrong credentials",
|
|
|
|
thing: saved,
|
|
|
|
key: wrongValue,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "update non-existing thing",
|
|
|
|
thing: other,
|
|
|
|
key: token,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "update thing with invalid type",
|
|
|
|
thing: things.Thing{Type: "invalid", Name: "d"},
|
|
|
|
key: wrongValue,
|
|
|
|
err: things.ErrMalformedEntity,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
for _, tc := range cases {
|
2018-05-15 17:13:09 +02:00
|
|
|
err := svc.UpdateThing(tc.key, tc.thing)
|
2018-10-24 11:21:03 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func TestViewThing(t *testing.T) {
|
2018-05-10 23:53:25 +02:00
|
|
|
svc := newService(map[string]string{token: email})
|
2018-05-16 14:28:41 +02:00
|
|
|
saved, _ := svc.AddThing(token, thing)
|
2018-05-10 23:53:25 +02:00
|
|
|
|
|
|
|
cases := map[string]struct {
|
2018-05-21 12:51:46 +02:00
|
|
|
id uint64
|
2018-05-10 23:53:25 +02:00
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"view existing thing": {
|
|
|
|
id: saved.ID,
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"view thing with wrong credentials": {
|
|
|
|
id: saved.ID,
|
|
|
|
key: wrongValue,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
"view non-existing thing": {
|
|
|
|
id: wrongID,
|
|
|
|
key: token,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
2018-05-15 17:13:09 +02:00
|
|
|
_, err := svc.ViewThing(tc.key, tc.id)
|
2018-05-10 23:53:25 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func TestListThings(t *testing.T) {
|
2018-05-10 23:53:25 +02:00
|
|
|
svc := newService(map[string]string{token: email})
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
n := uint64(10)
|
|
|
|
for i := uint64(0); i < n; i++ {
|
2018-05-15 17:13:09 +02:00
|
|
|
svc.AddThing(token, thing)
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
2018-05-16 14:28:41 +02:00
|
|
|
|
2018-05-10 23:53:25 +02:00
|
|
|
cases := map[string]struct {
|
|
|
|
key string
|
2018-10-24 11:21:03 +02:00
|
|
|
offset uint64
|
|
|
|
limit uint64
|
|
|
|
size uint64
|
2018-05-10 23:53:25 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"list all things": {
|
|
|
|
key: token,
|
|
|
|
offset: 0,
|
|
|
|
limit: n,
|
|
|
|
size: n,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"list half": {
|
|
|
|
key: token,
|
|
|
|
offset: n / 2,
|
|
|
|
limit: n,
|
|
|
|
size: n / 2,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"list last thing": {
|
|
|
|
key: token,
|
|
|
|
offset: n - 1,
|
|
|
|
limit: n,
|
|
|
|
size: 1,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"list empty set": {
|
|
|
|
key: token,
|
|
|
|
offset: n + 1,
|
|
|
|
limit: n,
|
|
|
|
size: 0,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"list with zero limit": {
|
|
|
|
key: token,
|
|
|
|
offset: 1,
|
|
|
|
limit: 0,
|
|
|
|
size: 0,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"list with wrong credentials": {
|
|
|
|
key: wrongValue,
|
|
|
|
offset: 0,
|
|
|
|
limit: 0,
|
|
|
|
size: 0,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
2018-05-16 14:28:41 +02:00
|
|
|
ts, err := svc.ListThings(tc.key, tc.offset, tc.limit)
|
2018-10-24 11:21:03 +02:00
|
|
|
size := uint64(len(ts))
|
2018-05-10 23:53:25 +02:00
|
|
|
assert.Equal(t, tc.size, size, fmt.Sprintf("%s: expected %d got %d\n", desc, tc.size, size))
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func TestRemoveThing(t *testing.T) {
|
2018-05-10 23:53:25 +02:00
|
|
|
svc := newService(map[string]string{token: email})
|
2018-05-16 14:28:41 +02:00
|
|
|
saved, _ := svc.AddThing(token, thing)
|
2018-05-10 23:53:25 +02:00
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
id uint64
|
|
|
|
key string
|
|
|
|
err error
|
2018-05-10 23:53:25 +02:00
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "remove thing with wrong credentials",
|
|
|
|
id: saved.ID,
|
|
|
|
key: wrongValue,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove existing thing",
|
|
|
|
id: saved.ID,
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove removed thing",
|
|
|
|
id: saved.ID,
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove non-existing thing",
|
|
|
|
id: wrongID,
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
for _, tc := range cases {
|
2018-05-15 17:13:09 +02:00
|
|
|
err := svc.RemoveThing(tc.key, tc.id)
|
2018-10-24 11:21:03 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateChannel(t *testing.T) {
|
|
|
|
svc := newService(map[string]string{token: email})
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2018-05-15 17:13:09 +02:00
|
|
|
channel things.Channel
|
2018-05-10 23:53:25 +02:00
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "create channel",
|
|
|
|
channel: channel,
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "create channel with wrong credentials",
|
|
|
|
channel: channel,
|
|
|
|
key: wrongValue,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
for _, tc := range cases {
|
2018-05-10 23:53:25 +02:00
|
|
|
_, err := svc.CreateChannel(tc.key, tc.channel)
|
2018-10-24 11:21:03 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateChannel(t *testing.T) {
|
|
|
|
svc := newService(map[string]string{token: email})
|
2018-05-16 14:28:41 +02:00
|
|
|
saved, _ := svc.CreateChannel(token, channel)
|
2018-05-21 12:51:46 +02:00
|
|
|
other := things.Channel{ID: wrongID}
|
2018-05-10 23:53:25 +02:00
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2018-05-15 17:13:09 +02:00
|
|
|
channel things.Channel
|
2018-05-10 23:53:25 +02:00
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "update existing channel",
|
|
|
|
channel: saved,
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "update channel with wrong credentials",
|
|
|
|
channel: saved,
|
|
|
|
key: wrongValue,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "update non-existing channel",
|
|
|
|
channel: other,
|
|
|
|
key: token,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
for _, tc := range cases {
|
2018-05-10 23:53:25 +02:00
|
|
|
err := svc.UpdateChannel(tc.key, tc.channel)
|
2018-10-24 11:21:03 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestViewChannel(t *testing.T) {
|
|
|
|
svc := newService(map[string]string{token: email})
|
2018-05-16 14:28:41 +02:00
|
|
|
saved, _ := svc.CreateChannel(token, channel)
|
2018-05-10 23:53:25 +02:00
|
|
|
|
|
|
|
cases := map[string]struct {
|
2018-05-21 12:51:46 +02:00
|
|
|
id uint64
|
2018-05-10 23:53:25 +02:00
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"view existing channel": {
|
|
|
|
id: saved.ID,
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"view channel with wrong credentials": {
|
|
|
|
id: saved.ID,
|
|
|
|
key: wrongValue,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
"view non-existing channel": {
|
|
|
|
id: wrongID,
|
|
|
|
key: token,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
|
|
|
_, err := svc.ViewChannel(tc.key, tc.id)
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListChannels(t *testing.T) {
|
|
|
|
svc := newService(map[string]string{token: email})
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
n := uint64(10)
|
|
|
|
for i := uint64(0); i < n; i++ {
|
2018-05-10 23:53:25 +02:00
|
|
|
svc.CreateChannel(token, channel)
|
|
|
|
}
|
|
|
|
cases := map[string]struct {
|
|
|
|
key string
|
2018-10-24 11:21:03 +02:00
|
|
|
offset uint64
|
|
|
|
limit uint64
|
|
|
|
size uint64
|
2018-05-10 23:53:25 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"list all channels": {
|
|
|
|
key: token,
|
|
|
|
offset: 0,
|
|
|
|
limit: n,
|
|
|
|
size: n,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"list half": {
|
|
|
|
key: token,
|
|
|
|
offset: n / 2,
|
|
|
|
limit: n,
|
|
|
|
size: n / 2,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"list last channel": {
|
|
|
|
key: token,
|
|
|
|
offset: n - 1,
|
|
|
|
limit: n,
|
|
|
|
size: 1,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"list empty set": {
|
|
|
|
key: token,
|
|
|
|
offset: n + 1,
|
|
|
|
limit: n,
|
|
|
|
size: 0,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"list with zero limit": {
|
|
|
|
key: token,
|
|
|
|
offset: 1,
|
|
|
|
limit: 0,
|
|
|
|
size: 0,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"list with wrong credentials": {
|
|
|
|
key: wrongValue,
|
|
|
|
offset: 0,
|
|
|
|
limit: 0,
|
|
|
|
size: 0,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
|
|
|
ch, err := svc.ListChannels(tc.key, tc.offset, tc.limit)
|
2018-10-24 11:21:03 +02:00
|
|
|
size := uint64(len(ch))
|
2018-05-10 23:53:25 +02:00
|
|
|
assert.Equal(t, tc.size, size, fmt.Sprintf("%s: expected %d got %d\n", desc, tc.size, size))
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveChannel(t *testing.T) {
|
|
|
|
svc := newService(map[string]string{token: email})
|
2018-05-16 14:28:41 +02:00
|
|
|
saved, _ := svc.CreateChannel(token, channel)
|
2018-05-10 23:53:25 +02:00
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
id uint64
|
|
|
|
key string
|
|
|
|
err error
|
2018-05-10 23:53:25 +02:00
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "remove channel with wrong credentials",
|
|
|
|
id: saved.ID,
|
|
|
|
key: wrongValue,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove existing channel",
|
|
|
|
id: saved.ID,
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove removed channel",
|
|
|
|
id: saved.ID,
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove non-existing channel",
|
|
|
|
id: saved.ID,
|
|
|
|
key: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
for _, tc := range cases {
|
2018-05-10 23:53:25 +02:00
|
|
|
err := svc.RemoveChannel(tc.key, tc.id)
|
2018-10-24 11:21:03 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnect(t *testing.T) {
|
|
|
|
svc := newService(map[string]string{token: email})
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
sth, _ := svc.AddThing(token, thing)
|
|
|
|
sch, _ := svc.CreateChannel(token, channel)
|
2018-05-10 23:53:25 +02:00
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2018-05-15 17:13:09 +02:00
|
|
|
key string
|
2018-05-21 12:51:46 +02:00
|
|
|
chanID uint64
|
|
|
|
thingID uint64
|
2018-05-15 17:13:09 +02:00
|
|
|
err error
|
2018-05-10 23:53:25 +02:00
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "connect thing",
|
|
|
|
key: token,
|
|
|
|
chanID: sch.ID,
|
|
|
|
thingID: sth.ID,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "connect thing with wrong credentials",
|
|
|
|
key: wrongValue,
|
|
|
|
chanID: sch.ID,
|
|
|
|
thingID: sth.ID,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "connect thing to non-existing channel",
|
|
|
|
key: token,
|
|
|
|
chanID: wrongID,
|
|
|
|
thingID: sth.ID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "connect non-existing thing to channel",
|
|
|
|
key: token,
|
|
|
|
chanID: sch.ID,
|
|
|
|
thingID: wrongID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
for _, tc := range cases {
|
2018-05-15 17:13:09 +02:00
|
|
|
err := svc.Connect(tc.key, tc.chanID, tc.thingID)
|
2018-10-24 11:21:03 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDisconnect(t *testing.T) {
|
|
|
|
svc := newService(map[string]string{token: email})
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
sth, _ := svc.AddThing(token, thing)
|
|
|
|
sch, _ := svc.CreateChannel(token, channel)
|
|
|
|
svc.Connect(token, sch.ID, sth.ID)
|
2018-05-10 23:53:25 +02:00
|
|
|
|
|
|
|
cases := []struct {
|
2018-05-15 17:13:09 +02:00
|
|
|
desc string
|
|
|
|
key string
|
2018-05-21 12:51:46 +02:00
|
|
|
chanID uint64
|
|
|
|
thingID uint64
|
2018-05-15 17:13:09 +02:00
|
|
|
err error
|
2018-05-10 23:53:25 +02:00
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "disconnect connected thing",
|
|
|
|
key: token,
|
|
|
|
chanID: sch.ID,
|
|
|
|
thingID: sth.ID,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "disconnect disconnected thing",
|
|
|
|
key: token,
|
|
|
|
chanID: sch.ID,
|
|
|
|
thingID: sth.ID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "disconnect with wrong credentials",
|
|
|
|
key: wrongValue,
|
|
|
|
chanID: sch.ID,
|
|
|
|
thingID: sth.ID,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "disconnect from non-existing channel",
|
|
|
|
key: token,
|
|
|
|
chanID: wrongID,
|
|
|
|
thingID: sth.ID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "disconnect non-existing thing",
|
|
|
|
key: token,
|
|
|
|
chanID: sch.ID,
|
|
|
|
thingID: wrongID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2018-05-15 17:13:09 +02:00
|
|
|
err := svc.Disconnect(tc.key, tc.chanID, tc.thingID)
|
2018-05-10 23:53:25 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCanAccess(t *testing.T) {
|
|
|
|
svc := newService(map[string]string{token: email})
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
sth, _ := svc.AddThing(token, thing)
|
|
|
|
sch, _ := svc.CreateChannel(token, channel)
|
|
|
|
svc.Connect(token, sch.ID, sth.ID)
|
2018-05-10 23:53:25 +02:00
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
key string
|
2018-05-21 12:51:46 +02:00
|
|
|
channel uint64
|
2018-05-10 23:53:25 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"allowed access": {
|
|
|
|
key: sth.Key,
|
|
|
|
channel: sch.ID,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"not-connected cannot access": {
|
|
|
|
key: wrongValue,
|
|
|
|
channel: sch.ID,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
"access to non-existing channel": {
|
|
|
|
key: sth.Key,
|
|
|
|
channel: wrongID,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
2018-05-21 12:51:46 +02:00
|
|
|
_, err := svc.CanAccess(tc.channel, tc.key)
|
2018-05-10 23:53:25 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|
2018-05-17 20:17:02 +02:00
|
|
|
|
|
|
|
func TestIdentify(t *testing.T) {
|
|
|
|
svc := newService(map[string]string{token: email})
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
sth, _ := svc.AddThing(token, thing)
|
2018-05-17 20:17:02 +02:00
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
key string
|
2018-05-21 12:51:46 +02:00
|
|
|
id uint64
|
2018-05-17 20:17:02 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"identify existing thing": {
|
|
|
|
key: sth.Key,
|
|
|
|
id: sth.ID,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"identify non-existing thing": {
|
|
|
|
key: wrongValue,
|
|
|
|
id: wrongID,
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
2018-05-17 20:17:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
|
|
|
id, err := svc.Identify(tc.key)
|
2018-05-21 12:51:46 +02:00
|
|
|
assert.Equal(t, tc.id, id, fmt.Sprintf("%s: expected %d got %d\n", desc, tc.id, id))
|
2018-05-17 20:17:02 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|