mirror of
https://github.com/absmach/supermq.git
synced 2026-06-23 06:30:22 +00:00
MG-888 - Update things SDK tests (#2328)
Signed-off-by: 1998-felix <felix.gateru@gmail.com>
This commit is contained in:
@@ -99,27 +99,6 @@ var cmdThings = []cobra.Command{
|
||||
logOK()
|
||||
},
|
||||
},
|
||||
{
|
||||
Use: "identify <thing_key>",
|
||||
Short: "Identify thing",
|
||||
Long: "Validates thing's key and returns its ID\n" +
|
||||
"Usage:\n" +
|
||||
"\tmagistrala-cli things identify <thing_key>\n",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) != 1 {
|
||||
logUsage(cmd.Use)
|
||||
return
|
||||
}
|
||||
|
||||
i, err := sdk.IdentifyThing(args[0])
|
||||
if err != nil {
|
||||
logError(err)
|
||||
return
|
||||
}
|
||||
|
||||
logJSON(i)
|
||||
},
|
||||
},
|
||||
{
|
||||
Use: "update [<thing_id> <JSON_string> | tags <thing_id> <tags> | secret <thing_id> <secret> ] <user_auth_token>",
|
||||
Short: "Update thing",
|
||||
|
||||
@@ -14,8 +14,7 @@ import (
|
||||
)
|
||||
|
||||
func TestHealth(t *testing.T) {
|
||||
ths, auth := setupThingsMinimal()
|
||||
auth.Test(t)
|
||||
ths, _ := setupThings()
|
||||
defer ths.Close()
|
||||
|
||||
usclsv, _ := setupUsers()
|
||||
|
||||
@@ -79,10 +79,6 @@ type SubscriptionPage struct {
|
||||
PageRes
|
||||
}
|
||||
|
||||
type identifyThingResp struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
}
|
||||
|
||||
type DomainsPage struct {
|
||||
Domains []Domain `json:"domains"`
|
||||
PageRes
|
||||
|
||||
@@ -454,13 +454,6 @@ type SDK interface {
|
||||
// fmt.Println(thing)
|
||||
DisableThing(id, token string) (Thing, errors.SDKError)
|
||||
|
||||
// IdentifyThing validates thing's key and returns its ID
|
||||
//
|
||||
// example:
|
||||
// id, _ := sdk.IdentifyThing("thingKey")
|
||||
// fmt.Println(id)
|
||||
IdentifyThing(key string) (string, errors.SDKError)
|
||||
|
||||
// ShareThing shares thing with other users.
|
||||
//
|
||||
// example:
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
mggroups "github.com/absmach/magistrala/pkg/groups"
|
||||
sdk "github.com/absmach/magistrala/pkg/sdk/go"
|
||||
"github.com/absmach/magistrala/pkg/uuid"
|
||||
"github.com/absmach/magistrala/users/hasher"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -30,26 +29,15 @@ const (
|
||||
|
||||
var (
|
||||
idProvider = uuid.New()
|
||||
phasher = hasher.New()
|
||||
validMetadata = sdk.Metadata{"role": "client"}
|
||||
user = generateTestUser(&testing.T{})
|
||||
thing = sdk.Thing{
|
||||
Name: "thingname",
|
||||
Tags: []string{"tag1", "tag2"},
|
||||
Credentials: sdk.Credentials{Identity: "clientidentity", Secret: generateUUID(&testing.T{})},
|
||||
Metadata: validMetadata,
|
||||
Status: mgclients.EnabledStatus.String(),
|
||||
}
|
||||
description = "shortdescription"
|
||||
gName = "groupname"
|
||||
description = "shortdescription"
|
||||
gName = "groupname"
|
||||
|
||||
limit uint64 = 5
|
||||
offset uint64 = 0
|
||||
total uint64 = 200
|
||||
|
||||
subject = generateUUID(&testing.T{})
|
||||
object = generateUUID(&testing.T{})
|
||||
passRegex = regexp.MustCompile("^.{8,}$")
|
||||
limit uint64 = 5
|
||||
offset uint64 = 0
|
||||
total uint64 = 200
|
||||
passRegex = regexp.MustCompile("^.{8,}$")
|
||||
)
|
||||
|
||||
func generateUUID(t *testing.T) string {
|
||||
@@ -59,12 +47,6 @@ func generateUUID(t *testing.T) string {
|
||||
return ulid
|
||||
}
|
||||
|
||||
func convertThingsPage(cp sdk.ThingsPage) mgclients.ClientsPage {
|
||||
return mgclients.ClientsPage{
|
||||
Clients: convertThings(cp.Things...),
|
||||
}
|
||||
}
|
||||
|
||||
func convertClients(cs []sdk.User) []mgclients.Client {
|
||||
ccs := []mgclients.Client{}
|
||||
|
||||
@@ -105,26 +87,6 @@ func convertChannels(cs []sdk.Channel) []mggroups.Group {
|
||||
return cgs
|
||||
}
|
||||
|
||||
func convertClientPage(p sdk.PageMetadata) mgclients.Page {
|
||||
if p.Status == "" {
|
||||
p.Status = mgclients.EnabledStatus.String()
|
||||
}
|
||||
status, err := mgclients.ToStatus(p.Status)
|
||||
if err != nil {
|
||||
return mgclients.Page{}
|
||||
}
|
||||
|
||||
return mgclients.Page{
|
||||
Status: status,
|
||||
Total: p.Total,
|
||||
Offset: p.Offset,
|
||||
Limit: p.Limit,
|
||||
Name: p.Name,
|
||||
Tag: p.Tag,
|
||||
Metadata: mgclients.Metadata(p.Metadata),
|
||||
}
|
||||
}
|
||||
|
||||
func convertGroup(g sdk.Group) mggroups.Group {
|
||||
if g.Status == "" {
|
||||
g.Status = mgclients.EnabledStatus.String()
|
||||
|
||||
+12
-18
@@ -9,6 +9,7 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/absmach/magistrala/pkg/apiutil"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -117,6 +118,9 @@ func (sdk mgSDK) ThingsByChannel(chanID string, pm PageMetadata, token string) (
|
||||
}
|
||||
|
||||
func (sdk mgSDK) Thing(id, token string) (Thing, errors.SDKError) {
|
||||
if id == "" {
|
||||
return Thing{}, errors.NewSDKError(apiutil.ErrMissingID)
|
||||
}
|
||||
url := fmt.Sprintf("%s/%s/%s", sdk.thingsURL, thingsEndpoint, id)
|
||||
|
||||
_, body, sdkerr := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK)
|
||||
@@ -149,13 +153,16 @@ func (sdk mgSDK) ThingPermissions(id, token string) (Thing, errors.SDKError) {
|
||||
}
|
||||
|
||||
func (sdk mgSDK) UpdateThing(t Thing, token string) (Thing, errors.SDKError) {
|
||||
if t.ID == "" {
|
||||
return Thing{}, errors.NewSDKError(apiutil.ErrMissingID)
|
||||
}
|
||||
url := fmt.Sprintf("%s/%s/%s", sdk.thingsURL, thingsEndpoint, t.ID)
|
||||
|
||||
data, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
return Thing{}, errors.NewSDKError(err)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/%s/%s", sdk.thingsURL, thingsEndpoint, t.ID)
|
||||
|
||||
_, body, sdkerr := sdk.processRequest(http.MethodPatch, url, token, data, nil, http.StatusOK)
|
||||
if sdkerr != nil {
|
||||
return Thing{}, sdkerr
|
||||
@@ -237,22 +244,6 @@ func (sdk mgSDK) changeThingStatus(id, status, token string) (Thing, errors.SDKE
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func (sdk mgSDK) IdentifyThing(key string) (string, errors.SDKError) {
|
||||
url := fmt.Sprintf("%s/%s", sdk.thingsURL, identifyEndpoint)
|
||||
|
||||
_, body, sdkerr := sdk.processRequest(http.MethodPost, url, ThingPrefix+key, nil, nil, http.StatusOK)
|
||||
if sdkerr != nil {
|
||||
return "", sdkerr
|
||||
}
|
||||
|
||||
var i identifyThingResp
|
||||
if err := json.Unmarshal(body, &i); err != nil {
|
||||
return "", errors.NewSDKError(err)
|
||||
}
|
||||
|
||||
return i.ID, nil
|
||||
}
|
||||
|
||||
func (sdk mgSDK) ShareThing(thingID string, req UsersRelationRequest, token string) errors.SDKError {
|
||||
data, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
@@ -296,6 +287,9 @@ func (sdk mgSDK) ListThingUsers(thingID string, pm PageMetadata, token string) (
|
||||
}
|
||||
|
||||
func (sdk mgSDK) DeleteThing(id, token string) errors.SDKError {
|
||||
if id == "" {
|
||||
return errors.NewSDKError(apiutil.ErrMissingID)
|
||||
}
|
||||
url := fmt.Sprintf("%s/%s/%s", sdk.thingsURL, thingsEndpoint, id)
|
||||
_, _, sdkerr := sdk.processRequest(http.MethodDelete, url, token, nil, nil, http.StatusNoContent)
|
||||
return sdkerr
|
||||
|
||||
+1646
-945
File diff suppressed because it is too large
Load Diff
@@ -1316,36 +1316,6 @@ func (_m *SDK) Health(service string) (sdk.HealthInfo, errors.SDKError) {
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// IdentifyThing provides a mock function with given fields: key
|
||||
func (_m *SDK) IdentifyThing(key string) (string, errors.SDKError) {
|
||||
ret := _m.Called(key)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for IdentifyThing")
|
||||
}
|
||||
|
||||
var r0 string
|
||||
var r1 errors.SDKError
|
||||
if rf, ok := ret.Get(0).(func(string) (string, errors.SDKError)); ok {
|
||||
return rf(key)
|
||||
}
|
||||
if rf, ok := ret.Get(0).(func(string) string); ok {
|
||||
r0 = rf(key)
|
||||
} else {
|
||||
r0 = ret.Get(0).(string)
|
||||
}
|
||||
|
||||
if rf, ok := ret.Get(1).(func(string) errors.SDKError); ok {
|
||||
r1 = rf(key)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(errors.SDKError)
|
||||
}
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// Invitation provides a mock function with given fields: userID, domainID, token
|
||||
func (_m *SDK) Invitation(userID string, domainID string, token string) (sdk.Invitation, error) {
|
||||
ret := _m.Called(userID, domainID, token)
|
||||
|
||||
@@ -7,8 +7,6 @@ import (
|
||||
"github.com/absmach/magistrala/internal/api"
|
||||
"github.com/absmach/magistrala/pkg/apiutil"
|
||||
mgclients "github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
)
|
||||
|
||||
type createClientReq struct {
|
||||
@@ -335,10 +333,10 @@ type thingShareRequest struct {
|
||||
|
||||
func (req *thingShareRequest) validate() error {
|
||||
if req.thingID == "" {
|
||||
return errors.ErrMalformedEntity
|
||||
return apiutil.ErrMissingID
|
||||
}
|
||||
if req.Relation == "" || len(req.UserIDs) == 0 {
|
||||
return svcerr.ErrCreateEntity
|
||||
return apiutil.ErrMalformedPolicy
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -352,10 +350,10 @@ type thingUnshareRequest struct {
|
||||
|
||||
func (req *thingUnshareRequest) validate() error {
|
||||
if req.thingID == "" {
|
||||
return errors.ErrMalformedEntity
|
||||
return apiutil.ErrMissingID
|
||||
}
|
||||
if req.Relation == "" || len(req.UserIDs) == 0 {
|
||||
return svcerr.ErrCreateEntity
|
||||
return apiutil.ErrMalformedPolicy
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -11,8 +11,6 @@ import (
|
||||
"github.com/absmach/magistrala/internal/testsutil"
|
||||
"github.com/absmach/magistrala/pkg/apiutil"
|
||||
mgclients "github.com/absmach/magistrala/pkg/clients"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -815,7 +813,7 @@ func TestThingShareRequestValidate(t *testing.T) {
|
||||
UserIDs: []string{validID},
|
||||
Relation: valid,
|
||||
},
|
||||
err: errors.ErrMalformedEntity,
|
||||
err: apiutil.ErrMissingID,
|
||||
},
|
||||
{
|
||||
desc: "empty user ids",
|
||||
@@ -825,7 +823,7 @@ func TestThingShareRequestValidate(t *testing.T) {
|
||||
UserIDs: []string{},
|
||||
Relation: valid,
|
||||
},
|
||||
err: svcerr.ErrCreateEntity,
|
||||
err: apiutil.ErrMalformedPolicy,
|
||||
},
|
||||
{
|
||||
desc: "empty relation",
|
||||
@@ -835,7 +833,7 @@ func TestThingShareRequestValidate(t *testing.T) {
|
||||
UserIDs: []string{validID},
|
||||
Relation: "",
|
||||
},
|
||||
err: svcerr.ErrCreateEntity,
|
||||
err: apiutil.ErrMalformedPolicy,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
@@ -868,7 +866,7 @@ func TestThingUnshareRequestValidate(t *testing.T) {
|
||||
UserIDs: []string{validID},
|
||||
Relation: valid,
|
||||
},
|
||||
err: errors.ErrMalformedEntity,
|
||||
err: apiutil.ErrMissingID,
|
||||
},
|
||||
{
|
||||
desc: "empty user ids",
|
||||
@@ -878,7 +876,7 @@ func TestThingUnshareRequestValidate(t *testing.T) {
|
||||
UserIDs: []string{},
|
||||
Relation: valid,
|
||||
},
|
||||
err: svcerr.ErrCreateEntity,
|
||||
err: apiutil.ErrMalformedPolicy,
|
||||
},
|
||||
{
|
||||
desc: "empty relation",
|
||||
@@ -888,7 +886,7 @@ func TestThingUnshareRequestValidate(t *testing.T) {
|
||||
UserIDs: []string{validID},
|
||||
Relation: "",
|
||||
},
|
||||
err: svcerr.ErrCreateEntity,
|
||||
err: apiutil.ErrMalformedPolicy,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
|
||||
Reference in New Issue
Block a user