Skip to content

Commit

Permalink
test(grpc): change tests to correspond with new changes
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
  • Loading branch information
rodneyosodo committed May 24, 2024
1 parent 1310cc3 commit 2ef62ef
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
30 changes: 15 additions & 15 deletions auth/api/grpc/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func startGRPCServer(svc auth.Service, port int) {
}

func TestIssue(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -132,7 +132,7 @@ func TestIssue(t *testing.T) {
}

func TestRefresh(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -178,7 +178,7 @@ func TestRefresh(t *testing.T) {
}

func TestIdentify(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -222,7 +222,7 @@ func TestIdentify(t *testing.T) {
}

func TestAuthorize(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -344,7 +344,7 @@ func TestAuthorize(t *testing.T) {
}

func TestAddPolicy(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -398,7 +398,7 @@ func TestAddPolicy(t *testing.T) {
}

func TestAddPolicies(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -460,7 +460,7 @@ func TestAddPolicies(t *testing.T) {
}

func TestDeletePolicy(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -513,7 +513,7 @@ func TestDeletePolicy(t *testing.T) {
}

func TestDeletePolicies(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -576,7 +576,7 @@ func TestDeletePolicies(t *testing.T) {
}

func TestListObjects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -626,7 +626,7 @@ func TestListObjects(t *testing.T) {
}

func TestListAllObjects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -676,7 +676,7 @@ func TestListAllObjects(t *testing.T) {
}

func TestCountObects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -726,7 +726,7 @@ func TestCountObects(t *testing.T) {
}

func TestListSubjects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -776,7 +776,7 @@ func TestListSubjects(t *testing.T) {
}

func TestListAllSubjects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf(`"Unexpected error creating client connection %s"`, err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -826,7 +826,7 @@ func TestListAllSubjects(t *testing.T) {
}

func TestCountSubjects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -881,7 +881,7 @@ func TestCountSubjects(t *testing.T) {
}

func TestListPermissions(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down
47 changes: 43 additions & 4 deletions pkg/auth/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,36 @@ import (
"testing"
"time"

"github.com/absmach/magistrala"
authgrpcapi "github.com/absmach/magistrala/auth/api/grpc"
"github.com/absmach/magistrala/auth/mocks"
"github.com/absmach/magistrala/internal/server"
grpcserver "github.com/absmach/magistrala/internal/server/grpc"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/auth"
"github.com/absmach/magistrala/pkg/errors"
thingsgrpcapi "github.com/absmach/magistrala/things/api/grpc"
thmocks "github.com/absmach/magistrala/things/mocks"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
)

func TestSetupAuth(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
registerAuthServiceServer := func(srv *grpc.Server) {
magistrala.RegisterAuthServiceServer(srv, authgrpcapi.NewServer(new(mocks.Service)))
}
gs := grpcserver.New(ctx, cancel, "auth", server.Config{Port: "12345"}, registerAuthServiceServer, mglog.NewMock())
go func() {
err := gs.Start()
assert.Nil(t, err, fmt.Sprintf(`"Unexpected error creating server %s"`, err))
}()
defer func() {
err := gs.Stop()
assert.Nil(t, err, fmt.Sprintf(`"Unexpected error stopping server %s"`, err))
}()

cases := []struct {
desc string
config auth.Config
Expand All @@ -23,7 +47,7 @@ func TestSetupAuth(t *testing.T) {
{
desc: "successful",
config: auth.Config{
URL: "localhost:8080",
URL: "localhost:12345",
Timeout: time.Second,
},
err: nil,
Expand All @@ -34,7 +58,7 @@ func TestSetupAuth(t *testing.T) {
URL: "",
Timeout: time.Second,
},
err: errors.New("failed to connect to grpc server"),
err: errors.New("health check failed"),
},
}

Expand All @@ -51,6 +75,21 @@ func TestSetupAuth(t *testing.T) {
}

func TestSetupAuthz(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
registerAuthaServiceServer := func(srv *grpc.Server) {
magistrala.RegisterAuthzServiceServer(srv, thingsgrpcapi.NewServer(new(thmocks.Service)))
}
gs := grpcserver.New(ctx, cancel, "things", server.Config{Port: "12345"}, registerAuthaServiceServer, mglog.NewMock())
go func() {
err := gs.Start()
assert.Nil(t, err, fmt.Sprintf(`"Unexpected error creating server %s"`, err))
}()
defer func() {
err := gs.Stop()
assert.Nil(t, err, fmt.Sprintf(`"Unexpected error stopping server %s"`, err))
}()

cases := []struct {
desc string
config auth.Config
Expand All @@ -59,7 +98,7 @@ func TestSetupAuthz(t *testing.T) {
{
desc: "successful",
config: auth.Config{
URL: "localhost:8080",
URL: "localhost:12345",
Timeout: time.Second,
},
err: nil,
Expand All @@ -70,7 +109,7 @@ func TestSetupAuthz(t *testing.T) {
URL: "",
Timeout: time.Second,
},
err: errors.New("failed to connect to grpc server"),
err: errors.New("health check failed"),
},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestHandler(t *testing.T) {
URL: "",
Timeout: time.Second,
},
err: errors.New("failed to connect to grpc server"),
secure: "without TLS",
},
{
desc: "failed with invalid server CA file",
Expand Down
2 changes: 1 addition & 1 deletion things/api/grpc/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ 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()))
conn, _ := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
client := grpcapi.NewClient(conn, time.Second)

cases := []struct {
Expand Down

0 comments on commit 2ef62ef

Please sign in to comment.