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

Commit

Permalink
NOISSUE - Improve tests in things service (#200)
Browse files Browse the repository at this point in the history
Signed-off-by: felix.gateru <[email protected]>
  • Loading branch information
felixgateru authored Jan 18, 2024
1 parent 8c084b1 commit e7129fe
Show file tree
Hide file tree
Showing 13 changed files with 4,865 additions and 557 deletions.
8 changes: 7 additions & 1 deletion internal/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
errors.Contains(err, apiutil.ErrMissingMemberKind),
errors.Contains(err, apiutil.ErrLimitSize),
errors.Contains(err, apiutil.ErrBearerKey),
errors.Contains(err, apiutil.ErrNameSize):
errors.Contains(err, apiutil.ErrNameSize),
errors.Contains(err, svcerr.ErrInvalidStatus),
errors.Contains(err, apiutil.ErrInvalidIDFormat),
errors.Contains(err, apiutil.ErrInvalidQueryParams),
errors.Contains(err, apiutil.ErrInvalidStatus),
errors.Contains(err, apiutil.ErrMissingRelation),
errors.Contains(err, apiutil.ErrValidation):
w.WriteHeader(http.StatusBadRequest)
case errors.Contains(err, svcerr.ErrAuthentication),
errors.Contains(err, errors.ErrAuthentication),
Expand Down
14 changes: 7 additions & 7 deletions invitations/api/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestListInvitation(t *testing.T) {
desc: "with invalid offset",
token: validToken,
query: "offset=invalid",
status: http.StatusInternalServerError,
status: http.StatusBadRequest,
contentType: validContenType,
svcErr: nil,
},
Expand All @@ -186,7 +186,7 @@ func TestListInvitation(t *testing.T) {
desc: "with invalid limit",
token: validToken,
query: "limit=invalid",
status: http.StatusInternalServerError,
status: http.StatusBadRequest,
contentType: validContenType,
svcErr: nil,
},
Expand All @@ -202,7 +202,7 @@ func TestListInvitation(t *testing.T) {
desc: "with duplicate user_id",
token: validToken,
query: "user_id=1&user_id=2",
status: http.StatusInternalServerError,
status: http.StatusBadRequest,
contentType: validContenType,
svcErr: nil,
},
Expand All @@ -218,7 +218,7 @@ func TestListInvitation(t *testing.T) {
desc: "with duplicate invited_by",
token: validToken,
query: "invited_by=1&invited_by=2",
status: http.StatusInternalServerError,
status: http.StatusBadRequest,
contentType: validContenType,
svcErr: nil,
},
Expand All @@ -234,7 +234,7 @@ func TestListInvitation(t *testing.T) {
desc: "with duplicate relation",
token: validToken,
query: "relation=1&relation=2",
status: http.StatusInternalServerError,
status: http.StatusBadRequest,
contentType: validContenType,
svcErr: nil,
},
Expand All @@ -250,7 +250,7 @@ func TestListInvitation(t *testing.T) {
desc: "with duplicate domain_id",
token: validToken,
query: "domain_id=1&domain_id=2",
status: http.StatusInternalServerError,
status: http.StatusBadRequest,
contentType: validContenType,
svcErr: nil,
},
Expand All @@ -274,7 +274,7 @@ func TestListInvitation(t *testing.T) {
desc: "with duplicate state",
token: validToken,
query: "state=all&state=all",
status: http.StatusInternalServerError,
status: http.StatusBadRequest,
contentType: validContenType,
svcErr: nil,
},
Expand Down
179 changes: 179 additions & 0 deletions things/api/grpc/endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package grpc_test

import (
"context"
"fmt"
"net"
"testing"
"time"

"github.com/absmach/magistrala"
"github.com/absmach/magistrala/auth"
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/pkg/errors"
grpcapi "github.com/absmach/magistrala/things/api/grpc"
"github.com/absmach/magistrala/things/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
)

const port = 7000

var (
thingID = "testID"
channelID = "testID"
invalid = "invalid"
valid = "valid"
)

func startGRPCServer(svc *mocks.Service, port int) {
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
panic(fmt.Sprintf("failed to obtain port: %s", err))
}
server := grpc.NewServer()
magistrala.RegisterAuthzServiceServer(server, grpcapi.NewServer(svc))
go func() {
if err := server.Serve(listener); err != nil {
panic(fmt.Sprintf("failed to serve: %s", err))
}
}()
}

func TestAuthorize(t *testing.T) {
svc := new(mocks.Service)
startGRPCServer(svc, port)
authAddr := fmt.Sprintf("localhost:%d", port)
conn, _ := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
client := grpcapi.NewClient(conn, time.Second)

cases := []struct {
desc string
authorizeReq *magistrala.AuthorizeReq
res *magistrala.AuthorizeRes
thingID string
authorizeErr error
err error
code codes.Code
}{
{
desc: "authorize successfully",
authorizeReq: &magistrala.AuthorizeReq{
Subject: thingID,
SubjectKind: auth.ThingsKind,
Permission: valid,
SubjectType: auth.ThingType,
Object: channelID,
ObjectType: auth.GroupType,
},
thingID: thingID,
res: &magistrala.AuthorizeRes{Authorized: true, Id: thingID},
err: nil,
},
{
desc: "authorize with invalid id",
authorizeReq: &magistrala.AuthorizeReq{
Subject: invalid,
SubjectKind: auth.ThingsKind,
Permission: "publish",
SubjectType: auth.ThingType,
Object: channelID,
ObjectType: auth.GroupType,
},
res: &magistrala.AuthorizeRes{},
err: errors.ErrAuthentication,
},
{
desc: "authorize with missing ID",
authorizeReq: &magistrala.AuthorizeReq{
Subject: "",
SubjectKind: auth.ThingsKind,
Permission: valid,
SubjectType: auth.ThingType,
Object: channelID,
ObjectType: auth.GroupType,
},
res: &magistrala.AuthorizeRes{},
err: apiutil.ErrMissingID,
},
{
desc: "authorize with unauthorized id",
authorizeReq: &magistrala.AuthorizeReq{
Subject: invalid,
SubjectKind: auth.ThingsKind,
Permission: valid,
SubjectType: auth.ThingType,
Object: channelID,
ObjectType: auth.GroupType,
},
res: &magistrala.AuthorizeRes{},
err: errors.ErrAuthorization,
},
{
desc: "authorize with invalid permission",
authorizeReq: &magistrala.AuthorizeReq{
Subject: thingID,
SubjectKind: auth.ThingsKind,
Permission: invalid,
SubjectType: auth.ThingType,
Object: channelID,
ObjectType: auth.GroupType,
},
res: &magistrala.AuthorizeRes{},
err: errors.ErrAuthorization,
},
{
desc: "authorize with invalid channel ID",
authorizeReq: &magistrala.AuthorizeReq{
Subject: thingID,
SubjectKind: auth.ThingsKind,
Permission: valid,
SubjectType: auth.ThingType,
Object: invalid,
ObjectType: auth.GroupType,
},
res: &magistrala.AuthorizeRes{},
err: errors.ErrAuthorization,
},
{
desc: "authorize with empty channel ID",
authorizeReq: &magistrala.AuthorizeReq{
Subject: thingID,
SubjectKind: auth.ThingsKind,
Permission: valid,
SubjectType: auth.ThingType,
Object: "",
ObjectType: auth.GroupType,
},
res: &magistrala.AuthorizeRes{},
err: errors.ErrAuthorization,
},
{
desc: "authorize with empty permission",
authorizeReq: &magistrala.AuthorizeReq{
Subject: thingID,
SubjectKind: auth.ThingsKind,
Permission: "",
SubjectType: auth.ThingType,
Object: channelID,
ObjectType: auth.GroupType,
},
res: &magistrala.AuthorizeRes{},
err: errors.ErrAuthorization,
},
}

for _, tc := range cases {
svcCall := svc.On("Authorize", mock.Anything, tc.authorizeReq).Return(tc.thingID, tc.err)
res, err := client.Authorize(context.Background(), tc.authorizeReq)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s", tc.desc, tc.err, err))
assert.Equal(t, tc.res, res, fmt.Sprintf("%s: expected %s got %s", tc.desc, tc.res, res))
svcCall.Unset()
}
}
Loading

0 comments on commit e7129fe

Please sign in to comment.