Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
remove: UpdateClientOwner from things service
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 committed Nov 27, 2023
1 parent ea0559d commit b13307c
Show file tree
Hide file tree
Showing 10 changed files with 1 addition and 225 deletions.
34 changes: 0 additions & 34 deletions api/openapi/things.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,32 +196,6 @@ paths:
"500":
$ref: "#/components/responses/ServiceError"

/things/{thingID}/owner:
patch:
summary: Updates the thing owner.
description: |
Updates owner for the thing with provided ID. Owner is updated using
authorization token and a new owner identifier received in request.
tags:
- Things
parameters:
- $ref: "#/components/parameters/ThingID"
requestBody:
$ref: "#/components/requestBodies/ThingUpdateOwnerReq"
security:
- bearerAuth: []
responses:
"200":
$ref: "#/components/responses/ThingRes"
"400":
description: Failed due to malformed JSON.
"404":
description: Failed due to non existing thing.
"401":
description: Missing or invalid access token provided.
"500":
$ref: "#/components/responses/ServiceError"

/things/{thingID}/secret:
patch:
summary: Updates Secret of the identified thing.
Expand Down Expand Up @@ -1648,14 +1622,6 @@ components:
schema:
$ref: "#/components/schemas/ThingSecret"

ThingUpdateOwnerReq:
description: JSON-formated document describing the owner of thing to be update
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/ThingOwner"

ShareThingReq:
description: JSON-formated document describing the policy related to sharing things
required: true
Expand Down
77 changes: 0 additions & 77 deletions pkg/sdk/go/things_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,83 +869,6 @@ func TestUpdateThingSecret(t *testing.T) {
}
}

func TestUpdateThingOwner(t *testing.T) {
ts, cRepo, _, _ := newClientServer()
defer ts.Close()

conf := sdk.Config{
ThingsURL: ts.URL,
}
mgsdk := sdk.NewSDK(conf)

thing = sdk.Thing{
ID: generateUUID(t),
Name: "clientname",
Tags: []string{"tag1", "tag2"},
Credentials: sdk.Credentials{Identity: "clientidentity", Secret: generateUUID(t)},
Metadata: validMetadata,
Status: mgclients.EnabledStatus.String(),
Owner: "owner",
}

thing2 := thing
thing2.ID = invalidIdentity

cases := []struct {
desc string
thing sdk.Thing
response sdk.Thing
token string
err errors.SDKError
}{
{
desc: "update thing name with valid token",
thing: thing,
response: thing,
token: adminToken,
err: nil,
},
{
desc: "update thing name with invalid token",
thing: thing2,
response: sdk.Thing{},
token: invalidToken,
err: errors.NewSDKErrorWithStatus(errors.ErrAuthentication, http.StatusUnauthorized),
},
{
desc: "update thing name with invalid id",
thing: thing2,
response: sdk.Thing{},
token: adminToken,
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, sdk.ErrFailedUpdate), http.StatusInternalServerError),
},
{
desc: "update thing that can't be marshalled",
thing: sdk.Thing{
Name: "test",
Metadata: map[string]interface{}{
"test": make(chan int),
},
},
response: sdk.Thing{},
token: token,
err: errors.NewSDKError(fmt.Errorf("json: unsupported type: chan int")),
},
}

for _, tc := range cases {
repoCall1 := cRepo.On("UpdateOwner", mock.Anything, mock.Anything).Return(convertThing(tc.response), tc.err)
uClient, err := mgsdk.UpdateThingOwner(tc.thing, tc.token)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", tc.desc, tc.err, err))
assert.Equal(t, tc.response, uClient, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.response, uClient))
if tc.err == nil {
ok := repoCall1.Parent.AssertCalled(t, "UpdateOwner", mock.Anything, mock.Anything)
assert.True(t, ok, fmt.Sprintf("UpdateOwner was not called on %s", tc.desc))
}
repoCall1.Unset()
}
}

func TestEnableThing(t *testing.T) {
ts, cRepo, _, auth := newClientServer()
defer ts.Close()
Expand Down
12 changes: 0 additions & 12 deletions things/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,6 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS
return lm.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret)
}

func (lm *loggingMiddleware) UpdateClientOwner(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method update_thing_owner for thing with id %s using token %s took %s to complete", c.ID, token, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())
return lm.svc.UpdateClientOwner(ctx, token, client)
}

func (lm *loggingMiddleware) EnableClient(ctx context.Context, token string, id string) (c mgclients.Client, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method enable_thing for thing with id %s using token %s took %s to complete", id, token, time.Since(begin))
Expand Down
8 changes: 0 additions & 8 deletions things/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ func (ms *metricsMiddleware) UpdateClientSecret(ctx context.Context, token, oldS
return ms.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret)
}

func (ms *metricsMiddleware) UpdateClientOwner(ctx context.Context, token string, client mgclients.Client) (mgclients.Client, error) {
defer func(begin time.Time) {
ms.counter.With("method", "update_thing_owner").Add(1)
ms.latency.With("method", "update_thing_owner").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.UpdateClientOwner(ctx, token, client)
}

func (ms *metricsMiddleware) EnableClient(ctx context.Context, token string, id string) (mgclients.Client, error) {
defer func(begin time.Time) {
ms.counter.With("method", "enable_thing").Add(1)
Expand Down
9 changes: 0 additions & 9 deletions things/events/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ func (es *eventStore) UpdateClient(ctx context.Context, token string, thing mgcl
return es.update(ctx, "", cli)
}

func (es *eventStore) UpdateClientOwner(ctx context.Context, token string, thing mgclients.Client) (mgclients.Client, error) {
cli, err := es.svc.UpdateClientOwner(ctx, token, thing)
if err != nil {
return cli, err
}

return es.update(ctx, "owner", cli)
}

func (es *eventStore) UpdateClientTags(ctx context.Context, token string, thing mgclients.Client) (mgclients.Client, error) {
cli, err := es.svc.UpdateClientTags(ctx, token, thing)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion things/mocks/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (m *Repository) UpdateTags(ctx context.Context, client mgclients.Client) (m
return ret.Get(0).(mgclients.Client), ret.Error(1)
}

func (m *Repository) UpdateOwner(ctx context.Context, client mgclients.Client) (mgclients.Client, error) {
func (m *Repository) UpdateRole(ctx context.Context, client mgclients.Client) (mgclients.Client, error) {
ret := m.Called(ctx, client)

if client.ID == WrongID {
Expand Down
16 changes: 0 additions & 16 deletions things/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,6 @@ func (svc service) UpdateClientSecret(ctx context.Context, token, id, key string
return svc.clients.UpdateSecret(ctx, client)
}

func (svc service) UpdateClientOwner(ctx context.Context, token string, cli mgclients.Client) (mgclients.Client, error) {
userID, err := svc.authorize(ctx, auth.UserType, auth.TokenKind, token, auth.EditPermission, auth.ThingType, cli.ID)
if err != nil {
return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthorization, err)
}

client := mgclients.Client{
ID: cli.ID,
Owner: cli.Owner,
UpdatedAt: time.Now(),
UpdatedBy: userID,
Status: mgclients.EnabledStatus,
}
return svc.clients.UpdateOwner(ctx, client)
}

func (svc service) EnableClient(ctx context.Context, token, id string) (mgclients.Client, error) {
client := mgclients.Client{
ID: id,
Expand Down
54 changes: 0 additions & 54 deletions things/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,60 +713,6 @@ func TestUpdateClientTags(t *testing.T) {
repoCall3.Unset()
}
}

func TestUpdateClientOwner(t *testing.T) {
svc, cRepo, auth := newService()

client.Owner = "[email protected]"

cases := []struct {
desc string
client mgclients.Client
response mgclients.Client
token string
err error
}{
{
desc: "update client owner with valid token",
client: client,
token: validToken,
response: client,
err: nil,
},
{
desc: "update client owner with invalid token",
client: client,
token: "non-existent",
response: mgclients.Client{},
err: svcerror.ErrAuthentication,
},
{
desc: "update client owner with invalid ID",
client: mgclients.Client{
ID: mocks.WrongID,
Owner: "[email protected]",
},
response: mgclients.Client{},
token: validToken,
err: errors.ErrNotFound,
},
}

for _, tc := range cases {
repoCall := auth.On("Identify", mock.Anything, &magistrala.IdentityReq{Token: tc.token}).Return(&magistrala.IdentityRes{Id: validID}, nil)
repoCall1 := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true}, nil)
repoCall2 := cRepo.On("RetrieveByID", context.Background(), mock.Anything).Return(mgclients.Client{}, tc.err)
repoCall3 := cRepo.On("UpdateOwner", context.Background(), mock.Anything).Return(tc.response, tc.err)
updatedClient, err := svc.UpdateClientOwner(context.Background(), tc.token, tc.client)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
assert.Equal(t, tc.response, updatedClient, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.response, updatedClient))
repoCall.Unset()
repoCall1.Unset()
repoCall2.Unset()
repoCall3.Unset()
}
}

func TestUpdateClientSecret(t *testing.T) {
svc, cRepo, auth := newService()

Expand Down
3 changes: 0 additions & 3 deletions things/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ type Service interface {
// UpdateClientSecret updates the client's secret
UpdateClientSecret(ctx context.Context, token, id, key string) (clients.Client, error)

// UpdateClientOwner updates the client's owner.
UpdateClientOwner(ctx context.Context, token string, client clients.Client) (clients.Client, error)

// EnableClient logically enableds the client identified with the provided ID
EnableClient(ctx context.Context, token, id string) (clients.Client, error)

Expand Down
11 changes: 0 additions & 11 deletions things/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@ func (tm *tracingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS
return tm.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret)
}

// UpdateClientOwner traces the "UpdateClientOwner" operation of the wrapped policies.Service.
func (tm *tracingMiddleware) UpdateClientOwner(ctx context.Context, token string, cli mgclients.Client) (mgclients.Client, error) {
ctx, span := tm.tracer.Start(ctx, "svc_update_client_owner", trace.WithAttributes(
attribute.String("id", cli.ID),
attribute.String("owner", cli.Owner),
))
defer span.End()

return tm.svc.UpdateClientOwner(ctx, token, cli)
}

// EnableClient traces the "EnableClient" operation of the wrapped policies.Service.
func (tm *tracingMiddleware) EnableClient(ctx context.Context, token, id string) (mgclients.Client, error) {
ctx, span := tm.tracer.Start(ctx, "svc_enable_client", trace.WithAttributes(attribute.String("id", id)))
Expand Down

0 comments on commit b13307c

Please sign in to comment.