Skip to content

Commit

Permalink
added test to auth_grpc_client.go file in grpc package
Browse files Browse the repository at this point in the history
Signed-off-by: punithnayak <[email protected]>
  • Loading branch information
punithnayak committed Aug 18, 2023
1 parent 28f9079 commit d65f743
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions chaoscenter/graphql/server/pkg/grpc/auth_grpc_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package grpc

import (
"context"
"testing"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/protos"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
)

type MockAuthRpcServiceClient struct{}

func (m *MockAuthRpcServiceClient) ValidateRequest(ctx context.Context, req *protos.ValidationRequest, opts ...grpc.CallOption) (*protos.ValidationResponse, error) {
// You can implement custom logic here for the mock, based on the input req
if req.Invitation == "invalid-invitation" {
return &protos.ValidationResponse{
IsValid: false,
Error: "Invalid invitation",
}, nil
}
return &protos.ValidationResponse{
IsValid: true,
}, nil
}

func (m *MockAuthRpcServiceClient) GetProjectById(ctx context.Context, req *protos.GetProjectByIdRequest, opts ...grpc.CallOption) (*protos.GetProjectByIdResponse, error) {
// Mock implementation for GetProjectById if needed
return nil, nil
}

func (m *MockAuthRpcServiceClient) GetUserById(ctx context.Context, req *protos.GetUserByIdRequest, opts ...grpc.CallOption) (*protos.GetUserByIdResponse, error) {
// Mock implementation for GetUserById if needed
return nil, nil
}

func TestValidatorGRPCRequest_Valid(t *testing.T) {
// Create a mock client
mockClient := &MockAuthRpcServiceClient{}

// Call the ValidatorGRPCRequest function with the mock client
err := ValidatorGRPCRequest(mockClient, "mock-jwt", "mock-project-id", []string{"role1", "role2"}, "invitation")
assert.NoError(t, err, "Expected no error when validation is successful")
}

func TestValidatorGRPCRequest_Invalid(t *testing.T) {
// Create a mock client
mockClient := &MockAuthRpcServiceClient{}

// Call the ValidatorGRPCRequest function with the mock client, making it fail
err := ValidatorGRPCRequest(mockClient, "mock-jwt", "mock-project-id", []string{"role1", "role2"}, "invalid-invitation")
assert.Error(t, err, "Expected an error when validation fails")
}

// Additional tests can be written for other functions in the grpc package if required.

0 comments on commit d65f743

Please sign in to comment.