2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
package postgres_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-06-16 02:30:46 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
|
|
|
"github.com/mainflux/mainflux/things/postgres"
|
2018-05-16 14:28:41 +02:00
|
|
|
"github.com/mainflux/mainflux/things/uuid"
|
2018-03-11 18:06:01 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestChannelSave(t *testing.T) {
|
|
|
|
email := "channel-save@example.com"
|
2018-05-11 01:00:10 +02:00
|
|
|
channelRepo := postgres.NewChannelRepository(db, testLog)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
channel := things.Channel{Owner: email}
|
2018-05-16 14:28:41 +02:00
|
|
|
|
2018-11-28 15:58:48 +01:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
channel things.Channel
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "create valid channel",
|
|
|
|
channel: channel,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "create invalid channel",
|
|
|
|
channel: things.Channel{Owner: email, Metadata: "invalid"},
|
|
|
|
err: things.ErrMalformedEntity,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
_, err := channelRepo.Save(tc.channel)
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
|
|
}
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChannelUpdate(t *testing.T) {
|
|
|
|
email := "channel-update@example.com"
|
2018-05-11 01:00:10 +02:00
|
|
|
chanRepo := postgres.NewChannelRepository(db, testLog)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
c := things.Channel{Owner: email}
|
|
|
|
id, _ := chanRepo.Save(c)
|
|
|
|
c.ID = id
|
2018-03-11 18:06:01 +01: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-03-11 18:06:01 +01:00
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "update existing channel",
|
|
|
|
channel: c,
|
|
|
|
err: nil,
|
|
|
|
},
|
2018-11-28 15:58:48 +01:00
|
|
|
{
|
|
|
|
desc: "update channel with invalid data",
|
|
|
|
channel: things.Channel{Owner: email, Metadata: "invalid"},
|
|
|
|
err: things.ErrMalformedEntity,
|
|
|
|
},
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "update non-existing channel with existing user",
|
|
|
|
channel: things.Channel{ID: wrongID, Owner: email},
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "update existing channel ID with non-existing user",
|
|
|
|
channel: things.Channel{ID: c.ID, Owner: wrongValue},
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "update non-existing channel with non-existing user",
|
|
|
|
channel: things.Channel{ID: wrongID, Owner: wrongValue},
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
for _, tc := range cases {
|
2018-03-11 18:06:01 +01:00
|
|
|
err := chanRepo.Update(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-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSingleChannelRetrieval(t *testing.T) {
|
|
|
|
email := "channel-single-retrieval@example.com"
|
2018-05-11 01:00:10 +02:00
|
|
|
chanRepo := postgres.NewChannelRepository(db, testLog)
|
2018-10-24 11:21:03 +02:00
|
|
|
thingRepo := postgres.NewThingRepository(db, testLog)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
th := things.Thing{
|
|
|
|
Owner: email,
|
|
|
|
Key: uuid.New().ID(),
|
|
|
|
}
|
|
|
|
th.ID, _ = thingRepo.Save(th)
|
|
|
|
|
|
|
|
c := things.Channel{
|
|
|
|
Owner: email,
|
|
|
|
Things: []things.Thing{th},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ID, _ = chanRepo.Save(c)
|
|
|
|
chanRepo.Connect(email, c.ID, th.ID)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
owner string
|
2018-05-21 12:51:46 +02:00
|
|
|
ID uint64
|
2018-03-11 18:06:01 +01:00
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"retrieve channel with existing user": {
|
|
|
|
owner: c.Owner,
|
|
|
|
ID: c.ID,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"retrieve channel with existing user, non-existing channel": {
|
|
|
|
owner: c.Owner,
|
|
|
|
ID: wrongID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
"retrieve channel with non-existing owner": {
|
|
|
|
owner: wrongValue,
|
|
|
|
ID: c.ID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
2018-05-17 20:17:02 +02:00
|
|
|
_, err := chanRepo.RetrieveByID(tc.owner, tc.ID)
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiChannelRetrieval(t *testing.T) {
|
|
|
|
email := "channel-multi-retrieval@example.com"
|
2018-05-11 01:00:10 +02:00
|
|
|
chanRepo := postgres.NewChannelRepository(db, testLog)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
n := uint64(10)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
for i := uint64(0); i < n; i++ {
|
2018-05-21 12:51:46 +02:00
|
|
|
c := things.Channel{Owner: email}
|
2018-03-11 18:06:01 +01:00
|
|
|
chanRepo.Save(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2018-04-18 22:36:24 +02:00
|
|
|
owner string
|
2018-10-24 11:21:03 +02:00
|
|
|
offset uint64
|
|
|
|
limit uint64
|
|
|
|
size uint64
|
2018-03-11 18:06:01 +01:00
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"retrieve all channels with existing owner": {
|
|
|
|
owner: email,
|
|
|
|
offset: 0,
|
|
|
|
limit: n,
|
|
|
|
size: n,
|
|
|
|
},
|
|
|
|
"retrieve subset of channels with existing owner": {
|
|
|
|
owner: email,
|
|
|
|
offset: n / 2,
|
|
|
|
limit: n,
|
|
|
|
size: n / 2,
|
|
|
|
},
|
|
|
|
"retrieve channels with non-existing owner": {
|
|
|
|
owner: wrongValue,
|
|
|
|
offset: n / 2,
|
|
|
|
limit: n,
|
|
|
|
size: 0,
|
|
|
|
},
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
2018-10-24 11:21:03 +02:00
|
|
|
result := chanRepo.RetrieveAll(tc.owner, tc.offset, tc.limit)
|
|
|
|
size := uint64(len(result))
|
2018-04-18 22:36:24 +02:00
|
|
|
assert.Equal(t, tc.size, size, fmt.Sprintf("%s: expected %d got %d\n", desc, tc.size, size))
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChannelRemoval(t *testing.T) {
|
|
|
|
email := "channel-removal@example.com"
|
2018-05-11 01:00:10 +02:00
|
|
|
chanRepo := postgres.NewChannelRepository(db, testLog)
|
2018-05-21 12:51:46 +02:00
|
|
|
chanID, _ := chanRepo.Save(things.Channel{Owner: email})
|
2018-03-11 18:06:01 +01:00
|
|
|
|
|
|
|
// show that the removal works the same for both existing and non-existing
|
|
|
|
// (removed) channel
|
|
|
|
for i := 0; i < 2; i++ {
|
2018-06-16 02:30:46 +02:00
|
|
|
err := chanRepo.Remove(email, chanID)
|
|
|
|
require.Nil(t, err, fmt.Sprintf("#%d: failed to remove channel due to: %s", i, err))
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-06-16 02:30:46 +02:00
|
|
|
_, err = chanRepo.RetrieveByID(email, chanID)
|
|
|
|
require.Equal(t, things.ErrNotFound, err, fmt.Sprintf("#%d: expected %s got %s", i, things.ErrNotFound, err))
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
func TestConnect(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
email := "channel-connect@example.com"
|
2018-05-15 17:13:09 +02:00
|
|
|
thingRepo := postgres.NewThingRepository(db, testLog)
|
2018-05-16 14:28:41 +02:00
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
thing := things.Thing{
|
2018-11-28 15:58:48 +01:00
|
|
|
Owner: email,
|
|
|
|
Key: uuid.New().ID(),
|
|
|
|
Metadata: "{}",
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
2018-05-21 12:51:46 +02:00
|
|
|
thingID, _ := thingRepo.Save(thing)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-05-11 01:00:10 +02:00
|
|
|
chanRepo := postgres.NewChannelRepository(db, testLog)
|
2018-05-21 12:51:46 +02:00
|
|
|
chanID, _ := chanRepo.Save(things.Channel{Owner: email})
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-05-11 01:00:10 +02:00
|
|
|
cases := []struct {
|
2018-05-15 17:13:09 +02:00
|
|
|
desc string
|
|
|
|
owner string
|
2018-05-21 12:51:46 +02:00
|
|
|
chanID uint64
|
|
|
|
thingID uint64
|
2018-05-15 17:13:09 +02:00
|
|
|
err error
|
2018-03-11 18:06:01 +01:00
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "connect existing user, channel and thing",
|
|
|
|
owner: email,
|
|
|
|
chanID: chanID,
|
|
|
|
thingID: thingID,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "connect connected channel and thing",
|
|
|
|
owner: email,
|
|
|
|
chanID: chanID,
|
|
|
|
thingID: thingID,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "connect with non-existing user",
|
|
|
|
owner: wrongValue,
|
|
|
|
chanID: chanID,
|
|
|
|
thingID: thingID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "connect non-existing channel",
|
|
|
|
owner: email,
|
|
|
|
chanID: wrongID,
|
|
|
|
thingID: thingID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "connect non-existing thing",
|
|
|
|
owner: email,
|
|
|
|
chanID: chanID,
|
|
|
|
thingID: wrongID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2018-05-11 01:00:10 +02:00
|
|
|
for _, tc := range cases {
|
2018-05-15 17:13:09 +02:00
|
|
|
err := chanRepo.Connect(tc.owner, tc.chanID, tc.thingID)
|
2018-05-11 01:00:10 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
func TestDisconnect(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
email := "channel-disconnect@example.com"
|
2018-05-15 17:13:09 +02:00
|
|
|
thingRepo := postgres.NewThingRepository(db, testLog)
|
|
|
|
thing := things.Thing{
|
2018-11-28 15:58:48 +01:00
|
|
|
Owner: email,
|
|
|
|
Key: uuid.New().ID(),
|
|
|
|
Metadata: "{}",
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
2018-05-21 12:51:46 +02:00
|
|
|
thingID, _ := thingRepo.Save(thing)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-05-11 01:00:10 +02:00
|
|
|
chanRepo := postgres.NewChannelRepository(db, testLog)
|
2018-05-21 12:51:46 +02:00
|
|
|
chanID, _ := chanRepo.Save(things.Channel{Owner: email})
|
|
|
|
chanRepo.Connect(email, chanID, thingID)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-03-20 09:14:54 +01:00
|
|
|
cases := []struct {
|
2018-05-15 17:13:09 +02:00
|
|
|
desc string
|
|
|
|
owner string
|
2018-05-21 12:51:46 +02:00
|
|
|
chanID uint64
|
|
|
|
thingID uint64
|
2018-05-15 17:13:09 +02:00
|
|
|
err error
|
2018-03-11 18:06:01 +01:00
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "disconnect connected thing",
|
|
|
|
owner: email,
|
|
|
|
chanID: chanID,
|
|
|
|
thingID: thingID,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "disconnect non-connected thing",
|
|
|
|
owner: email,
|
|
|
|
chanID: chanID,
|
|
|
|
thingID: thingID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "disconnect non-existing user",
|
|
|
|
owner: wrongValue,
|
|
|
|
chanID: chanID,
|
|
|
|
thingID: thingID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "disconnect non-existing channel",
|
|
|
|
owner: email,
|
|
|
|
chanID: wrongID,
|
|
|
|
thingID: thingID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "disconnect non-existing thing",
|
|
|
|
owner: email,
|
|
|
|
chanID: chanID,
|
|
|
|
thingID: wrongID,
|
|
|
|
err: things.ErrNotFound,
|
|
|
|
},
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 09:14:54 +01:00
|
|
|
for _, tc := range cases {
|
2018-05-15 17:13:09 +02:00
|
|
|
err := chanRepo.Disconnect(tc.owner, tc.chanID, tc.thingID)
|
2018-03-20 09:14:54 +01:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
func TestHasThing(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
email := "channel-access-check@example.com"
|
2018-05-15 17:13:09 +02:00
|
|
|
thingRepo := postgres.NewThingRepository(db, testLog)
|
|
|
|
thing := things.Thing{
|
2018-03-11 18:06:01 +01:00
|
|
|
Owner: email,
|
2018-05-21 12:51:46 +02:00
|
|
|
Key: uuid.New().ID(),
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
2018-05-21 12:51:46 +02:00
|
|
|
thingID, _ := thingRepo.Save(thing)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-05-11 01:00:10 +02:00
|
|
|
chanRepo := postgres.NewChannelRepository(db, testLog)
|
2018-05-21 12:51:46 +02:00
|
|
|
chanID, _ := chanRepo.Save(things.Channel{Owner: email})
|
|
|
|
chanRepo.Connect(email, chanID, thingID)
|
2018-03-11 18:06:01 +01:00
|
|
|
|
|
|
|
cases := map[string]struct {
|
2018-05-21 12:51:46 +02:00
|
|
|
chanID uint64
|
2018-05-16 14:28:41 +02:00
|
|
|
key string
|
2018-03-11 18:06:01 +01:00
|
|
|
hasAccess bool
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"access check for thing that has access": {
|
|
|
|
chanID: chanID,
|
|
|
|
key: thing.Key,
|
|
|
|
hasAccess: true,
|
|
|
|
},
|
|
|
|
"access check for thing without access": {
|
|
|
|
chanID: chanID,
|
|
|
|
key: wrongValue,
|
|
|
|
hasAccess: false,
|
|
|
|
},
|
|
|
|
"access check for non-existing channel": {
|
|
|
|
chanID: wrongID,
|
|
|
|
key: thing.Key,
|
|
|
|
hasAccess: false,
|
|
|
|
},
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
2018-05-16 14:28:41 +02:00
|
|
|
_, err := chanRepo.HasThing(tc.chanID, tc.key)
|
|
|
|
hasAccess := err == nil
|
2018-03-11 18:06:01 +01:00
|
|
|
assert.Equal(t, tc.hasAccess, hasAccess, fmt.Sprintf("%s: expected %t got %t\n", desc, tc.hasAccess, hasAccess))
|
|
|
|
}
|
|
|
|
}
|