Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MG-2457 - Update groups tests #2509

Open
wants to merge 1 commit into
base: auth-refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion groups/api/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type grpcClient struct {
// NewClient returns new gRPC client instance.
func NewClient(conn *grpc.ClientConn, timeout time.Duration) grpcGroupsV1.GroupsServiceClient {
return &grpcClient{

retrieveEntity: kitgrpc.NewClient(
conn,
svcName,
Expand Down
3 changes: 0 additions & 3 deletions groups/api/grpc/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ import (

func retrieveEntityEndpoint(svc groups.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {

req := request.(retrieveEntityReq)
group, err := svc.RetrieveById(ctx, req.Id)

if err != nil {
return retrieveEntityRes{}, err
}

return retrieveEntityRes{id: group.ID, domain: group.Domain, status: uint8(group.Status)}, nil

}
}
159 changes: 159 additions & 0 deletions groups/api/grpc/endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package grpc_test

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

"github.com/absmach/magistrala/groups"
grpcapi "github.com/absmach/magistrala/groups/api/grpc"
prmocks "github.com/absmach/magistrala/groups/private/mocks"
grpcCommonV1 "github.com/absmach/magistrala/internal/grpc/common/v1"
grpcGroupsV1 "github.com/absmach/magistrala/internal/grpc/groups/v1"
"github.com/absmach/magistrala/internal/testsutil"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

const port = 7004

var (
validID = testsutil.GenerateUUID(&testing.T{})
valid = "valid"
validGroupResp = groups.Group{
ID: testsutil.GenerateUUID(&testing.T{}),
Name: valid,
Description: valid,
Domain: testsutil.GenerateUUID(&testing.T{}),
Parent: testsutil.GenerateUUID(&testing.T{}),
Metadata: groups.Metadata{
"name": "test",
},
Children: []*groups.Group{},
CreatedAt: time.Now().Add(-1 * time.Second),
UpdatedAt: time.Now(),
UpdatedBy: testsutil.GenerateUUID(&testing.T{}),
Status: groups.EnabledStatus,
}
)

func startGRPCServer(svc *prmocks.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()
grpcGroupsV1.RegisterGroupsServiceServer(server, grpcapi.NewServer(svc))
go func() {
if err := server.Serve(listener); err != nil {
panic(fmt.Sprintf("failed to serve: %s", err))
}
}()
}

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

cases := []struct {
desc string
req *grpcCommonV1.RetrieveEntityReq
svcRes groups.Group
svcErr error
res *grpcCommonV1.RetrieveEntityRes
err error
}{
{
desc: "retrieve group successfully",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcRes: validGroupResp,
svcErr: nil,
res: &grpcCommonV1.RetrieveEntityRes{
Entity: &grpcCommonV1.EntityBasic{
Id: validGroupResp.ID,
DomainId: validGroupResp.Domain,
Status: uint32(validGroupResp.Status),
},
},
err: nil,
},
{
desc: "retrieve group with authentication error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: svcerr.ErrAuthentication,
res: &grpcCommonV1.RetrieveEntityRes{},
err: svcerr.ErrAuthentication,
},
{
desc: "retrieve group with authorization error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: svcerr.ErrAuthorization,
res: &grpcCommonV1.RetrieveEntityRes{},
err: svcerr.ErrAuthorization,
},
{
desc: "retrieve group with not found error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: svcerr.ErrNotFound,
res: &grpcCommonV1.RetrieveEntityRes{},
err: svcerr.ErrNotFound,
},
{
desc: "retrieve group with malformed entity error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: errors.ErrMalformedEntity,
res: &grpcCommonV1.RetrieveEntityRes{},
err: errors.ErrMalformedEntity,
},
{
desc: "retrieve group with conflict error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: svcerr.ErrConflict,
res: &grpcCommonV1.RetrieveEntityRes{},
err: svcerr.ErrConflict,
},
{
desc: "retrieve group with unknown error",
req: &grpcCommonV1.RetrieveEntityReq{
Id: validID,
},
svcErr: errors.ErrUnidentified,
res: &grpcCommonV1.RetrieveEntityRes{},
err: errors.ErrUnidentified,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
svcCall := svc.On("RetrieveById", mock.Anything, tc.req.Id).Return(tc.svcRes, tc.svcErr)
res, err := client.RetrieveEntity(context.Background(), tc.req)
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()
})
}
}
1 change: 1 addition & 0 deletions groups/api/http/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func decodeHierarchyPageMeta(r *http.Request) (mggroups.HierarchyPageMeta, error
Tree: tree,
}, nil
}

func decodePageMeta(r *http.Request) (mggroups.PageMeta, error) {
s, err := apiutil.ReadStringQuery(r, api.StatusKey, api.DefGroupStatus)
if err != nil {
Expand Down
Loading