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

added test to auth_grpc_client.go file in grpc package #4110

Closed
wants to merge 3 commits into from
Closed
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
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.