1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Mainflux.mainflux/bootstrap/api/requests_test.go
Dušan Borovčanin 3300814026
MF-552 - Use event sourcing to keep Bootstrap service in sync with Things service (#603)
* Use separate table for Channels

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add inital event sourcing subscription

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add Channel update sync

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add Thing remove sync

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add Channel remove sync

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update service add method marshalling metadata

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Make separate methods for connection update

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Add diconnect event sync

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update Configs repository mock

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix service tests

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update repository tests

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update API docs

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update Location header

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update README.md

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix tests mutex lock

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix method names in logs

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>
2019-03-04 17:41:38 +01:00

266 lines
4.8 KiB
Go

package api
import (
"fmt"
"testing"
"github.com/mainflux/mainflux/bootstrap"
"github.com/stretchr/testify/assert"
)
func TestAddReqValidation(t *testing.T) {
cases := []struct {
desc string
key string
externalID string
externalKey string
err error
}{
{
desc: "empty key",
key: "",
externalID: "external-id",
externalKey: "external-key",
err: bootstrap.ErrUnauthorizedAccess,
},
{
desc: "empty external ID",
key: "key",
externalID: "",
externalKey: "external-key",
err: bootstrap.ErrMalformedEntity,
},
{
desc: "empty external key",
key: "key",
externalID: "external-id",
externalKey: "",
err: bootstrap.ErrMalformedEntity,
},
}
for _, tc := range cases {
req := addReq{
key: tc.key,
ExternalID: tc.externalID,
ExternalKey: tc.externalKey,
}
err := req.validate()
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
}
}
func TestEntityReqValidation(t *testing.T) {
cases := []struct {
desc string
key string
id string
err error
}{
{
desc: "empty key",
key: "",
id: "id",
err: bootstrap.ErrUnauthorizedAccess,
},
{
desc: "empty id",
key: "key",
id: "",
err: bootstrap.ErrMalformedEntity,
},
}
for _, tc := range cases {
req := entityReq{
key: tc.key,
}
err := req.validate()
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
}
}
func TestUpdateReqValidation(t *testing.T) {
cases := []struct {
desc string
key string
id string
err error
}{
{
desc: "empty key",
key: "",
id: "id",
err: bootstrap.ErrUnauthorizedAccess,
},
{
desc: "empty id",
key: "key",
id: "",
err: bootstrap.ErrMalformedEntity,
},
}
for _, tc := range cases {
req := updateReq{
key: tc.key,
id: tc.id,
}
err := req.validate()
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
}
}
func TestUpdateConnReqValidation(t *testing.T) {
cases := []struct {
desc string
key string
id string
err error
}{
{
desc: "empty key",
key: "",
id: "id",
err: bootstrap.ErrUnauthorizedAccess,
},
{
desc: "empty id",
key: "key",
id: "",
err: bootstrap.ErrMalformedEntity,
},
}
for _, tc := range cases {
req := updateReq{
key: tc.key,
id: tc.id,
}
err := req.validate()
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
}
}
func TestListReqValidation(t *testing.T) {
cases := []struct {
desc string
offset uint64
key string
limit uint64
err error
}{
{
desc: "empty key",
key: "",
offset: 0,
limit: 1,
err: bootstrap.ErrUnauthorizedAccess,
},
{
desc: "too large limit",
key: "key",
offset: 0,
limit: maxLimit + 1,
err: bootstrap.ErrMalformedEntity,
},
{
desc: "zero limit",
key: "key",
offset: 0,
limit: 0,
err: bootstrap.ErrMalformedEntity,
},
}
for _, tc := range cases {
req := listReq{
key: tc.key,
offset: tc.offset,
limit: tc.limit,
}
err := req.validate()
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
}
}
func TestBootstrapReqValidation(t *testing.T) {
cases := []struct {
desc string
externKey string
externID string
err error
}{
{
desc: "empty external key",
externKey: "",
externID: "id",
err: bootstrap.ErrUnauthorizedAccess,
},
{
desc: "empty external id",
externKey: "key",
externID: "",
err: bootstrap.ErrMalformedEntity,
},
}
for _, tc := range cases {
req := bootstrapReq{
id: tc.externID,
key: tc.externKey,
}
err := req.validate()
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
}
}
func TestChangeStateReqValidation(t *testing.T) {
cases := []struct {
desc string
key string
id string
state bootstrap.State
err error
}{
{
desc: "empty key",
key: "",
id: "id",
state: bootstrap.State(1),
err: bootstrap.ErrUnauthorizedAccess,
},
{
desc: "empty id",
key: "key",
id: "",
state: bootstrap.State(0),
err: bootstrap.ErrMalformedEntity,
},
{
desc: "invalid state",
key: "key",
id: "id",
state: bootstrap.State(14),
err: bootstrap.ErrMalformedEntity,
},
}
for _, tc := range cases {
req := changeStateReq{
key: tc.key,
id: tc.id,
State: tc.state,
}
err := req.validate()
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
}
}