Skip to content

Commit

Permalink
MG-888 - Update things SDK tests (absmach#2328)
Browse files Browse the repository at this point in the history
Signed-off-by: 1998-felix <[email protected]>
  • Loading branch information
felixgateru authored Jul 4, 2024
1 parent 8596b73 commit 083e655
Show file tree
Hide file tree
Showing 10 changed files with 1,616 additions and 1,026 deletions.
21 changes: 0 additions & 21 deletions cli/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions pkg/sdk/go/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
)

func TestHealth(t *testing.T) {
ths, auth := setupThingsMinimal()
auth.Test(t)
ths, _ := setupThings()
defer ths.Close()

usclsv, _ := setupUsers()
Expand Down
4 changes: 0 additions & 4 deletions pkg/sdk/go/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ type SubscriptionPage struct {
PageRes
}

type identifyThingResp struct {
ID string `json:"id,omitempty"`
}

type DomainsPage struct {
Domains []Domain `json:"domains"`
PageRes
Expand Down
7 changes: 0 additions & 7 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
50 changes: 6 additions & 44 deletions pkg/sdk/go/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"

limit uint64 = 5
offset uint64 = 0
total uint64 = 200
description = "shortdescription"
gName = "groupname"

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 {
Expand All @@ -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{}

Expand Down Expand Up @@ -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()
Expand Down
30 changes: 12 additions & 18 deletions pkg/sdk/go/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"time"

"github.com/absmach/magistrala/pkg/apiutil"
"github.com/absmach/magistrala/pkg/errors"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 083e655

Please sign in to comment.