mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
161 lines
3.5 KiB
Go
161 lines
3.5 KiB
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package redis_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/mainflux/mainflux/things/redis"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestConnect(t *testing.T) {
|
|
channelCache := redis.NewChannelCache(redisClient)
|
|
|
|
cid := "123"
|
|
tid := "321"
|
|
|
|
cases := []struct {
|
|
desc string
|
|
cid string
|
|
tid string
|
|
}{
|
|
{
|
|
desc: "connect thing to channel",
|
|
cid: cid,
|
|
tid: tid,
|
|
},
|
|
{
|
|
desc: "connect already connected thing to channel",
|
|
cid: cid,
|
|
tid: tid,
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
err := channelCache.Connect(cid, tid)
|
|
assert.Nil(t, err, fmt.Sprintf("%s: fail to connect due to: %s\n", tc.desc, err))
|
|
}
|
|
}
|
|
|
|
func TestHasThing(t *testing.T) {
|
|
channelCache := redis.NewChannelCache(redisClient)
|
|
|
|
cid := "123"
|
|
tid := "321"
|
|
|
|
err := channelCache.Connect(cid, tid)
|
|
require.Nil(t, err, fmt.Sprintf("connect thing to channel: fail to connect due to: %s\n", err))
|
|
|
|
cases := map[string]struct {
|
|
cid string
|
|
tid string
|
|
hasAccess bool
|
|
}{
|
|
"access check for thing that has access": {
|
|
cid: cid,
|
|
tid: tid,
|
|
hasAccess: true,
|
|
},
|
|
"access check for thing without access": {
|
|
cid: cid,
|
|
tid: cid,
|
|
hasAccess: false,
|
|
},
|
|
"access check for non-existing channel": {
|
|
cid: tid,
|
|
tid: tid,
|
|
hasAccess: false,
|
|
},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
hasAccess := channelCache.HasThing(tc.cid, tc.tid)
|
|
assert.Equal(t, tc.hasAccess, hasAccess, fmt.Sprintf("%s: expected %t got %t\n", desc, tc.hasAccess, hasAccess))
|
|
}
|
|
}
|
|
func TestDisconnect(t *testing.T) {
|
|
channelCache := redis.NewChannelCache(redisClient)
|
|
|
|
cid := "123"
|
|
tid := "321"
|
|
tid2 := "322"
|
|
|
|
err := channelCache.Connect(cid, tid)
|
|
require.Nil(t, err, fmt.Sprintf("connect thing to channel: fail to connect due to: %s\n", err))
|
|
|
|
cases := []struct {
|
|
desc string
|
|
cid string
|
|
tid string
|
|
hasAccess bool
|
|
}{
|
|
{
|
|
desc: "disconnecting connected thing",
|
|
cid: cid,
|
|
tid: tid,
|
|
hasAccess: false,
|
|
},
|
|
{
|
|
desc: "disconnecting non-connected thing",
|
|
cid: cid,
|
|
tid: tid2,
|
|
hasAccess: false,
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
err := channelCache.Disconnect(tc.cid, tc.tid)
|
|
assert.Nil(t, err, fmt.Sprintf("%s: fail due to: %s\n", tc.desc, err))
|
|
|
|
hasAccess := channelCache.HasThing(tc.cid, tc.tid)
|
|
assert.Equal(t, tc.hasAccess, hasAccess, fmt.Sprintf("access check after %s: expected %t got %t\n", tc.desc, tc.hasAccess, hasAccess))
|
|
}
|
|
}
|
|
|
|
func TestRemove(t *testing.T) {
|
|
channelCache := redis.NewChannelCache(redisClient)
|
|
|
|
cid := "123"
|
|
cid2 := "124"
|
|
tid := "321"
|
|
|
|
err := channelCache.Connect(cid, tid)
|
|
require.Nil(t, err, fmt.Sprintf("connect thing to channel: fail to connect due to: %s\n", err))
|
|
|
|
cases := []struct {
|
|
desc string
|
|
cid string
|
|
tid string
|
|
err error
|
|
hasAccess bool
|
|
}{
|
|
{
|
|
desc: "Remove channel from cache",
|
|
cid: cid,
|
|
tid: tid,
|
|
err: nil,
|
|
hasAccess: false,
|
|
},
|
|
{
|
|
desc: "Remove non-cached channel from cache",
|
|
cid: cid2,
|
|
tid: tid,
|
|
err: nil,
|
|
hasAccess: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
err := channelCache.Remove(tc.cid)
|
|
assert.Nil(t, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
hasAcces := channelCache.HasThing(tc.cid, tc.tid)
|
|
assert.Equal(t, tc.hasAccess, hasAcces, "%s - check access after removing channel: expected %t got %t\n", tc.desc, tc.hasAccess, hasAcces)
|
|
}
|
|
}
|