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

* Fix connection request validation and EOF error handling Fix validation of connection and client id in connection request. Fix EOF error handling by returning HTTP status Bad Request. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add manager API tests and update swagger Implement unit tests for every manager API endpoint. Update client and connection mock implementations by switching to UUIDs. Update swagger file with correct status codes. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add content type check and update documentation Add content type check in implementation and update documentation accordingly. Refactor tests and add empty content type test cases. Add code coverage badge to readme. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>
146 lines
2.2 KiB
Go
146 lines
2.2 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/asaskevich/govalidator"
|
|
"github.com/mainflux/mainflux/manager"
|
|
)
|
|
|
|
type apiReq interface {
|
|
validate() error
|
|
}
|
|
|
|
type userReq struct {
|
|
user manager.User
|
|
}
|
|
|
|
func (req userReq) validate() error {
|
|
return req.user.Validate()
|
|
}
|
|
|
|
type identityReq struct {
|
|
key string
|
|
}
|
|
|
|
func (req identityReq) validate() error {
|
|
if req.key == "" {
|
|
return manager.ErrUnauthorizedAccess
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type addClientReq struct {
|
|
key string
|
|
client manager.Client
|
|
}
|
|
|
|
func (req addClientReq) validate() error {
|
|
if req.key == "" {
|
|
return manager.ErrUnauthorizedAccess
|
|
}
|
|
|
|
return req.client.Validate()
|
|
}
|
|
|
|
type updateClientReq struct {
|
|
key string
|
|
id string
|
|
client manager.Client
|
|
}
|
|
|
|
func (req updateClientReq) validate() error {
|
|
if req.key == "" {
|
|
return manager.ErrUnauthorizedAccess
|
|
}
|
|
|
|
if !govalidator.IsUUID(req.id) {
|
|
return manager.ErrNotFound
|
|
}
|
|
|
|
return req.client.Validate()
|
|
}
|
|
|
|
type createChannelReq struct {
|
|
key string
|
|
channel manager.Channel
|
|
}
|
|
|
|
func (req createChannelReq) validate() error {
|
|
if req.key == "" {
|
|
return manager.ErrUnauthorizedAccess
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type updateChannelReq struct {
|
|
key string
|
|
id string
|
|
channel manager.Channel
|
|
}
|
|
|
|
func (req updateChannelReq) validate() error {
|
|
if req.key == "" {
|
|
return manager.ErrUnauthorizedAccess
|
|
}
|
|
|
|
if !govalidator.IsUUID(req.id) {
|
|
return manager.ErrNotFound
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type viewResourceReq struct {
|
|
key string
|
|
id string
|
|
}
|
|
|
|
func (req viewResourceReq) validate() error {
|
|
if req.key == "" {
|
|
return manager.ErrUnauthorizedAccess
|
|
}
|
|
|
|
if !govalidator.IsUUID(req.id) {
|
|
return manager.ErrNotFound
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type listResourcesReq struct {
|
|
key string
|
|
size int
|
|
offset int
|
|
}
|
|
|
|
func (req listResourcesReq) validate() error {
|
|
if req.key == "" {
|
|
return manager.ErrUnauthorizedAccess
|
|
}
|
|
|
|
if req.size > 0 && req.offset >= 0 {
|
|
return nil
|
|
}
|
|
|
|
return manager.ErrMalformedEntity
|
|
}
|
|
|
|
type connectionReq struct {
|
|
key string
|
|
chanId string
|
|
clientId string
|
|
}
|
|
|
|
func (req connectionReq) validate() error {
|
|
if req.key == "" {
|
|
return manager.ErrUnauthorizedAccess
|
|
}
|
|
|
|
if !govalidator.IsUUID(req.chanId) || !govalidator.IsUUID(req.clientId) {
|
|
return manager.ErrNotFound
|
|
}
|
|
|
|
return nil
|
|
}
|