2017-09-23 01:03:27 +02:00
|
|
|
package manager_test
|
|
|
|
|
|
|
|
import (
|
2017-10-01 01:07:37 +02:00
|
|
|
"fmt"
|
2017-09-23 01:03:27 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux/manager"
|
|
|
|
"github.com/mainflux/mainflux/manager/mocks"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
const wrong string = "wrong-value"
|
|
|
|
|
2017-09-23 01:03:27 +02:00
|
|
|
var (
|
2018-03-11 18:06:01 +01:00
|
|
|
user manager.User = manager.User{"user@example.com", "password"}
|
|
|
|
client manager.Client = manager.Client{ID: "1", Type: "app", Name: "test", Key: "1"}
|
|
|
|
channel manager.Channel = manager.Channel{ID: "1", Name: "test", Clients: []manager.Client{client}}
|
2017-09-23 01:03:27 +02:00
|
|
|
)
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
func newService() manager.Service {
|
|
|
|
users := mocks.NewUserRepository()
|
|
|
|
clients := mocks.NewClientRepository()
|
|
|
|
channels := mocks.NewChannelRepository()
|
|
|
|
hasher := mocks.NewHasher()
|
|
|
|
idp := mocks.NewIdentityProvider()
|
|
|
|
|
|
|
|
return manager.New(users, clients, channels, hasher, idp)
|
|
|
|
}
|
|
|
|
|
2017-09-23 01:03:27 +02:00
|
|
|
func TestRegister(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
svc := newService()
|
|
|
|
|
2018-03-20 09:14:54 +01:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2017-09-23 01:03:27 +02:00
|
|
|
user manager.User
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-20 09:14:54 +01:00
|
|
|
{"register new user", user, nil},
|
|
|
|
{"register existing user", user, manager.ErrConflict},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 09:14:54 +01:00
|
|
|
for _, tc := range cases {
|
2018-03-11 18:06:01 +01:00
|
|
|
err := svc.Register(tc.user)
|
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))
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogin(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
user manager.User
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"login with good credentials": {user, nil},
|
|
|
|
"login with wrong e-mail": {manager.User{wrong, user.Password}, manager.ErrUnauthorizedAccess},
|
|
|
|
"login with wrong password": {manager.User{user.Email, wrong}, manager.ErrUnauthorizedAccess},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
|
|
|
_, err := svc.Login(tc.user)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 01:03:27 +02:00
|
|
|
func TestAddClient(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
client manager.Client
|
2018-03-11 18:06:01 +01:00
|
|
|
key string
|
2017-09-23 01:03:27 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"add new app": {manager.Client{Type: "app", Name: "a"}, key, nil},
|
|
|
|
"add new device": {manager.Client{Type: "device", Name: "b"}, key, nil},
|
|
|
|
"add client with wrong credentials": {manager.Client{Type: "app", Name: "d"}, wrong, manager.ErrUnauthorizedAccess},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
|
|
|
_, err := svc.AddClient(tc.key, tc.client)
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-09-24 19:44:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 01:03:27 +02:00
|
|
|
func TestUpdateClient(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
svc.AddClient(key, client)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
client manager.Client
|
2018-03-11 18:06:01 +01:00
|
|
|
key string
|
2017-09-23 01:03:27 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"update existing client": {client, key, nil},
|
|
|
|
"update client with wrong credentials": {client, wrong, manager.ErrUnauthorizedAccess},
|
|
|
|
"update non-existing client": {manager.Client{ID: "2", Type: "app", Name: "d"}, key, manager.ErrNotFound},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-09-23 01:03:27 +02:00
|
|
|
err := svc.UpdateClient(tc.key, tc.client)
|
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-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestViewClient(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
svc.AddClient(key, client)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
id string
|
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"view existing client": {client.ID, key, nil},
|
|
|
|
"view client with wrong credentials": {client.ID, wrong, manager.ErrUnauthorizedAccess},
|
|
|
|
"view non-existing client": {wrong, key, manager.ErrNotFound},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-09-23 01:03:27 +02:00
|
|
|
_, err := svc.ViewClient(tc.key, 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))
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListClients(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"list clients": {key, nil},
|
|
|
|
"list clients with wrong credentials": {wrong, manager.ErrUnauthorizedAccess},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-09-23 01:03:27 +02:00
|
|
|
_, err := svc.ListClients(tc.key)
|
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-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveClient(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
svc.AddClient(key, client)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
id string
|
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"remove client with wrong credentials": {client.ID, "?", manager.ErrUnauthorizedAccess},
|
|
|
|
"remove existing client": {client.ID, key, nil},
|
|
|
|
"remove removed client": {client.ID, key, nil},
|
|
|
|
"remove non-existing client": {"?", key, nil},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-09-23 01:03:27 +02:00
|
|
|
err := svc.RemoveClient(tc.key, 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))
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateChannel(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
channel manager.Channel
|
2018-03-11 18:06:01 +01:00
|
|
|
key string
|
2017-09-23 01:03:27 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"create channel": {manager.Channel{}, key, nil},
|
|
|
|
"create channel with wrong credentials": {manager.Channel{}, wrong, manager.ErrUnauthorizedAccess},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
|
|
|
_, err := svc.CreateChannel(tc.key, tc.channel)
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateChannel(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
svc.CreateChannel(key, channel)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
channel manager.Channel
|
2018-03-11 18:06:01 +01:00
|
|
|
key string
|
2017-09-23 01:03:27 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"update existing channel": {channel, key, nil},
|
|
|
|
"update channel with wrong credentials": {channel, wrong, manager.ErrUnauthorizedAccess},
|
|
|
|
"update non-existing channel": {manager.Channel{ID: "2", Name: "test"}, key, manager.ErrNotFound},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-09-23 01:03:27 +02:00
|
|
|
err := svc.UpdateChannel(tc.key, tc.channel)
|
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-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestViewChannel(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
svc.CreateChannel(key, channel)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
id string
|
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"view existing channel": {channel.ID, key, nil},
|
|
|
|
"view channel with wrong credentials": {channel.ID, wrong, manager.ErrUnauthorizedAccess},
|
|
|
|
"view non-existing channel": {wrong, key, manager.ErrNotFound},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-09-23 01:03:27 +02:00
|
|
|
_, err := svc.ViewChannel(tc.key, 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))
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListChannels(t *testing.T) {
|
2018-03-11 18:06:01 +01:00
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"list channels": {key, nil},
|
|
|
|
"list channels with wrong credentials": {wrong, manager.ErrUnauthorizedAccess},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
2017-09-23 01:03:27 +02:00
|
|
|
_, err := svc.ListChannels(tc.key)
|
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-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
func TestRemoveChannel(t *testing.T) {
|
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
svc.CreateChannel(key, channel)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-12-29 10:40:44 +01:00
|
|
|
id string
|
2018-03-11 18:06:01 +01:00
|
|
|
key string
|
2017-12-29 10:40:44 +01:00
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"remove channel with wrong credentials": {channel.ID, wrong, manager.ErrUnauthorizedAccess},
|
|
|
|
"remove existing channel": {channel.ID, key, nil},
|
|
|
|
"remove removed channel": {channel.ID, key, nil},
|
|
|
|
"remove non-existing channel": {channel.ID, key, nil},
|
2017-12-29 10:40:44 +01:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
|
|
|
err := svc.RemoveChannel(tc.key, tc.id)
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-12-29 10:40:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
func TestConnect(t *testing.T) {
|
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
|
|
|
|
clientId, _ := svc.AddClient(key, client)
|
|
|
|
chanId, _ := svc.CreateChannel(key, channel)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
key string
|
|
|
|
chanId string
|
|
|
|
clientId string
|
|
|
|
err error
|
2017-09-23 01:03:27 +02:00
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"connect client": {key, chanId, clientId, nil},
|
|
|
|
"connect client with wrong credentials": {wrong, chanId, clientId, manager.ErrUnauthorizedAccess},
|
|
|
|
"connect client to non-existing channel": {key, wrong, clientId, manager.ErrNotFound},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
|
|
|
err := svc.Connect(tc.key, tc.chanId, tc.clientId)
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
func TestDisconnect(t *testing.T) {
|
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
|
|
|
|
clientId, _ := svc.AddClient(key, client)
|
|
|
|
chanId, _ := svc.CreateChannel(key, channel)
|
|
|
|
|
|
|
|
svc.Connect(key, chanId, clientId)
|
|
|
|
|
2018-03-20 09:14:54 +01:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2018-03-11 18:06:01 +01:00
|
|
|
key string
|
|
|
|
chanId string
|
|
|
|
clientId string
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-20 09:14:54 +01:00
|
|
|
{"disconnect connected client", key, chanId, clientId, nil},
|
|
|
|
{"disconnect disconnected client", key, chanId, clientId, manager.ErrNotFound},
|
|
|
|
{"disconnect client with wrong credentials", wrong, chanId, clientId, manager.ErrUnauthorizedAccess},
|
|
|
|
{"disconnect client from non-existing channel", key, wrong, clientId, manager.ErrNotFound},
|
|
|
|
{"disconnect non-existing client", key, chanId, wrong, manager.ErrNotFound},
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 09:14:54 +01:00
|
|
|
for _, tc := range cases {
|
2018-03-11 18:06:01 +01:00
|
|
|
err := svc.Disconnect(tc.key, tc.chanId, tc.clientId)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentity(t *testing.T) {
|
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-03-11 18:06:01 +01:00
|
|
|
"valid token's identity": {key, nil},
|
|
|
|
"invalid token's identity": {"", manager.ErrUnauthorizedAccess},
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for desc, tc := range cases {
|
|
|
|
_, err := svc.Identity(tc.key)
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCanAccess(t *testing.T) {
|
|
|
|
svc := newService()
|
|
|
|
svc.Register(user)
|
|
|
|
key, _ := svc.Login(user)
|
|
|
|
|
|
|
|
svc.AddClient(key, client)
|
|
|
|
svc.CreateChannel(key, channel)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
key string
|
|
|
|
channel string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
"allowed access": {client.Key, channel.ID, nil},
|
|
|
|
"not-connected cannot access": {wrong, channel.ID, manager.ErrUnauthorizedAccess},
|
|
|
|
"access non-existing channel": {client.Key, wrong, manager.ErrUnauthorizedAccess},
|
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
|
|
|
_, err := svc.CanAccess(tc.key, tc.channel)
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|