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 tests for environments handler #4175

Merged
merged 19 commits into from
Nov 15, 2023
Merged
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
187 changes: 187 additions & 0 deletions chaoscenter/graphql/server/pkg/environment/handler/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package handler_test

import (
"context"
"errors"
"testing"
"time"

"github.com/golang-jwt/jwt"
"github.com/google/uuid"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
dbOperationsEnvironment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
dbMocks "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/mocks"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/environment/handler"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"
"github.com/stretchr/testify/mock"
"go.mongodb.org/mongo-driver/bson"
)

var (
mongodbMockOperator = new(dbMocks.MongoOperator)
environmentOperator = dbOperationsEnvironment.NewEnvironmentOperator(mongodbMockOperator)
)

const JwtSecret = "testsecret"

func getSignedJWT(name string) (string, error) {
token := jwt.New(jwt.SigningMethodHS512)
claims := token.Claims.(jwt.MapClaims)
claims["uid"] = uuid.NewString()
claims["role"] = uuid.NewString()
claims["username"] = name
claims["exp"] = time.Now().Add(time.Minute).Unix()

tokenString, err := token.SignedString([]byte(JwtSecret))
if err != nil {
return "", err
}
return tokenString, nil
}

func TestCreateEnvironment(t *testing.T) {
utils.Config.JwtSecret = JwtSecret
testCases := []struct {
Freedisch marked this conversation as resolved.
Show resolved Hide resolved
name string
projectID string
input *model.CreateEnvironmentRequest
mockInsertFunc func(ctx context.Context, env environments.Environment) error
expectedEnv *model.Environment
expectedErr error
given func() string
}{
{
name: "success",
projectID: "testProject",
input: &model.CreateEnvironmentRequest{
EnvironmentID: "testEnvID",
Name: "testName",
},
mockInsertFunc: func(ctx context.Context, env environments.Environment) error {
return nil
},
expectedEnv: nil,
expectedErr: errors.New("invalid Token"),
given: func() string {
token, err := getSignedJWT("testUser")
if err != nil {
return token
}
return "invalid Token"
},
},
{
name: "Failed environment creation due to invalid token",
projectID: "testProject",
input: &model.CreateEnvironmentRequest{
EnvironmentID: "testEnvID",
Name: "testName",
},
mockInsertFunc: func(ctx context.Context, env environments.Environment) error {
return nil
},
expectedEnv: nil,
expectedErr: errors.New("invalid Token"),
given: func() string {
return "invalid Token"
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mongodbMockOperator.On("Insert", mock.Anything, mock.AnythingOfType("environments.Environment")).
Return(tc.mockInsertFunc)
token := tc.given()
ctx := context.WithValue(context.Background(), authorization.AuthKey, token)
mockOperator := environmentOperator
service := handler.NewEnvironmentService(mockOperator)

env, err := service.CreateEnvironment(ctx, tc.projectID, tc.input)
if (err != nil && tc.expectedErr == nil) ||
(err == nil && tc.expectedErr != nil) ||
(err != nil && tc.expectedErr != nil && err.Error() != tc.expectedErr.Error()) {
t.Fatalf("Expected error %v, but got %v", tc.expectedErr, err)
}

if tc.expectedEnv != nil && (env.EnvironmentID != tc.expectedEnv.EnvironmentID ||
env.Name != tc.expectedEnv.Name) {
t.Fatalf("Expected environment %+v, but got %+v", tc.expectedEnv, env)
}
})
}
}

func TestDeleteEnvironment(t *testing.T) {
utils.Config.JwtSecret = JwtSecret
testCases := []struct {
Freedisch marked this conversation as resolved.
Show resolved Hide resolved
name string
projectID string
environmentID string
mockGetEnvironmentFunc func(query bson.D) (environments.Environment, error)
mockUpdateFunc func(ctx context.Context, query bson.D, update bson.D) error
expectedResult string
expectedErr error
given func() string
}{
{
name: "success",
projectID: "testProject",
environmentID: "testEnvID",
mockGetEnvironmentFunc: func(query bson.D) (environments.Environment, error) {
return environments.Environment{
EnvironmentID: "testEnvID",
}, nil
},
mockUpdateFunc: func(ctx context.Context, query bson.D, update bson.D) error {
return nil
},
expectedErr: errors.New("invalid Token"),
given: func() string {
token, err := getSignedJWT("testUser")
if err != nil {
return token
}
return "invalid Token"
},
},
{
name: "Failed environment",
projectID: "testProject",
environmentID: "testEnvID",
mockGetEnvironmentFunc: func(query bson.D) (environments.Environment, error) {
return environments.Environment{
EnvironmentID: "testEnvID",
}, nil
},
mockUpdateFunc: func(ctx context.Context, query, update bson.D) error {
return nil
},
expectedErr: errors.New("invalid Token"),
given: func() string {
return "invalid Token"
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mongodbMockOperator.On("Insert", mock.Anything, mock.AnythingOfType("environments.Environment")).
Return(tc.mockUpdateFunc)
token := tc.given()
ctx := context.WithValue(context.Background(), authorization.AuthKey, token)

mockOperator := environmentOperator
service := handler.NewEnvironmentService(mockOperator)

_, err := service.DeleteEnvironment(ctx, tc.projectID, tc.environmentID)
if (err != nil && tc.expectedErr == nil) ||
(err == nil && tc.expectedErr != nil) ||
(err != nil && tc.expectedErr != nil && err.Error() != tc.expectedErr.Error()) {
t.Fatalf("Expected error %v, but got %v", tc.expectedErr, err)
}
})
}
}
Loading