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

* Use normalizer as stream source Renamed 'writer' service to 'normalizer' and dropped Cassandra facilities from it. Extracted the common dependencies to 'mainflux' package for easier sharing. Fixed the API docs and unified environment variables. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Use docker build arguments to specify build Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Remove cassandra libraries Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Update go-kit version to 0.6.0 Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix manager configuration Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Refactor docker-compose Merged individual compose files and dropped external links. Remove CoAP container since it is not referenced from NginX config at the moment. Update port mapping in compose and nginx.conf. Dropped bin scripts. Updated service documentation. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Drop content-type check Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Implement users data access layer in PostgreSQL Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Bump version to 0.1.0 Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Use go-kit logger everywhere (except CoAP) Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Improve factory methods naming Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Implement clients data access layer on PostgreSQL Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Make tests stateless All tests are refactored to use map-based table-driven tests. No cross-tests dependencies is present anymore. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Remove gitignore Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix nginx proxying Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Mark client-user FK explicit Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Update API documentation Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Update channel model Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Add channel PostgreSQL repository tests Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Implement PostgreSQL channels DAO Replaced update queries with raw SQL. Explicitly defined M2M table due to difficulties of ensuring the referential integrity through GORM. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Expose connection endpoints Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix swagger docs and remove DB logging Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix nested query remarks Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Add unique indices Signed-off-by: Dejan Mijic <dejan@mainflux.com>
204 lines
5.2 KiB
Go
204 lines
5.2 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/mainflux/mainflux/manager"
|
|
uuid "github.com/satori/go.uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
const wrong string = "?"
|
|
|
|
var (
|
|
client manager.Client = manager.Client{Type: "app"}
|
|
channel manager.Channel = manager.Channel{}
|
|
)
|
|
|
|
func TestUserReqValidation(t *testing.T) {
|
|
cases := map[string]struct {
|
|
user manager.User
|
|
err error
|
|
}{
|
|
"valid user request": {manager.User{"foo@example.com", "pass"}, nil},
|
|
"malformed e-mail": {manager.User{wrong, "pass"}, manager.ErrMalformedEntity},
|
|
"empty e-mail": {manager.User{"", "pass"}, manager.ErrMalformedEntity},
|
|
"empty password": {manager.User{"foo@example.com", ""}, manager.ErrMalformedEntity},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
req := userReq{tc.user}
|
|
err := req.validate()
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestIdentityReqValidation(t *testing.T) {
|
|
cases := map[string]struct {
|
|
key string
|
|
err error
|
|
}{
|
|
"non-empty token": {uuid.NewV4().String(), nil},
|
|
"empty token": {"", manager.ErrUnauthorizedAccess},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
req := identityReq{tc.key}
|
|
err := req.validate()
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestAddClientReqValidation(t *testing.T) {
|
|
key := uuid.NewV4().String()
|
|
|
|
cases := map[string]struct {
|
|
client manager.Client
|
|
key string
|
|
err error
|
|
}{
|
|
"valid client addition request": {client, key, nil},
|
|
"missing token": {client, "", manager.ErrUnauthorizedAccess},
|
|
"wrong client type": {manager.Client{Type: wrong}, key, manager.ErrMalformedEntity},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
req := addClientReq{
|
|
key: tc.key,
|
|
client: tc.client,
|
|
}
|
|
|
|
err := req.validate()
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestUpdateClientReqValidation(t *testing.T) {
|
|
key := uuid.NewV4().String()
|
|
id := uuid.NewV4().String()
|
|
|
|
cases := map[string]struct {
|
|
client manager.Client
|
|
id string
|
|
key string
|
|
err error
|
|
}{
|
|
"valid client update request": {client, id, key, nil},
|
|
"non-uuid client ID": {client, wrong, key, manager.ErrNotFound},
|
|
"missing token": {client, id, "", manager.ErrUnauthorizedAccess},
|
|
"wrong client type": {manager.Client{Type: "invalid"}, id, key, manager.ErrMalformedEntity},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
req := updateClientReq{
|
|
key: tc.key,
|
|
id: tc.id,
|
|
client: tc.client,
|
|
}
|
|
|
|
err := req.validate()
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestCreateChannelReqValidation(t *testing.T) {
|
|
key := uuid.NewV4().String()
|
|
|
|
cases := map[string]struct {
|
|
channel manager.Channel
|
|
key string
|
|
err error
|
|
}{
|
|
"valid channel creation request": {channel, key, nil},
|
|
"missing token": {channel, "", manager.ErrUnauthorizedAccess},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
req := createChannelReq{
|
|
key: tc.key,
|
|
channel: tc.channel,
|
|
}
|
|
|
|
err := req.validate()
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestUpdateChannelReqValidation(t *testing.T) {
|
|
key := uuid.NewV4().String()
|
|
id := uuid.NewV4().String()
|
|
|
|
cases := map[string]struct {
|
|
channel manager.Channel
|
|
id string
|
|
key string
|
|
err error
|
|
}{
|
|
"valid channel update request": {channel, id, key, nil},
|
|
"non-uuid channel ID": {channel, wrong, key, manager.ErrNotFound},
|
|
"missing token": {channel, id, "", manager.ErrUnauthorizedAccess},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
req := updateChannelReq{
|
|
key: tc.key,
|
|
id: tc.id,
|
|
channel: tc.channel,
|
|
}
|
|
|
|
err := req.validate()
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestViewResourceReqValidation(t *testing.T) {
|
|
key := uuid.NewV4().String()
|
|
id := uuid.NewV4().String()
|
|
|
|
cases := map[string]struct {
|
|
id string
|
|
key string
|
|
err error
|
|
}{
|
|
"valid resource viewing request": {id, key, nil},
|
|
"missing token": {id, "", manager.ErrUnauthorizedAccess},
|
|
"non-uuid resource ID": {wrong, key, manager.ErrNotFound},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
req := viewResourceReq{tc.key, tc.id}
|
|
err := req.validate()
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestListResourcesReqValidation(t *testing.T) {
|
|
key := uuid.NewV4().String()
|
|
value := 10
|
|
|
|
cases := map[string]struct {
|
|
key string
|
|
size int
|
|
offset int
|
|
err error
|
|
}{
|
|
"valid listing request": {key, value, value, nil},
|
|
"missing token": {"", value, value, manager.ErrUnauthorizedAccess},
|
|
"negative offset": {key, value, -value, manager.ErrMalformedEntity},
|
|
"zero size": {key, 0, value, manager.ErrMalformedEntity},
|
|
"negative size": {key, -value, value, manager.ErrMalformedEntity},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
req := listResourcesReq{
|
|
key: tc.key,
|
|
size: tc.size,
|
|
offset: tc.offset,
|
|
}
|
|
|
|
err := req.validate()
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
}
|
|
}
|