Skip to content

Commit

Permalink
backend unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andoriyaprashant committed Jan 18, 2025
1 parent afc4314 commit f4c0c51
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 61 deletions.
16 changes: 8 additions & 8 deletions chaoscenter/graphql/server/graph/environment.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func generateJWTTokenFromClaims(claims jwt.MapClaims) (string, error) {
}

func FuzzUserValidateJWT(f *testing.F) {
operator := &Operator{}
f.Fuzz(func(t *testing.T, data []byte) {
fuzzConsumer := fuzz.NewConsumer(data)
inputClaims := &jwt.MapClaims{}
Expand All @@ -72,7 +73,7 @@ func FuzzUserValidateJWT(f *testing.F) {
}

// Run the test with the generated JWT token
claims, err := UserValidateJWT(tokenString, "")
claims, err := operator.UserValidateJWT(tokenString, "")
if err != nil {
t.Errorf("Error encountered: %v", err)
}
Expand Down
18 changes: 14 additions & 4 deletions chaoscenter/graphql/server/pkg/chaos_infrastructure/cluster_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ import (
"github.com/golang-jwt/jwt"
)

type Operator struct {
authConfigOperator *authConfig.Operator
}

func NewChaosInfrastructureOperator(mongodbOperator mongodb.MongoOperator) *Operator {
return &Operator{
authConfigOperator: authConfig.NewAuthConfigOperator(mongodbOperator),
}
}

// InfraCreateJWT generates jwt used in chaos_infra registration
func InfraCreateJWT(id string) (string, error) {
func (o *Operator) InfraCreateJWT(id string) (string, error) {
claims := jwt.MapClaims{}
claims["chaos_infra_id"] = id
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
config, err := authConfig.NewAuthConfigOperator(mongodb.Operator).GetAuthConfig(context.Background())
config, err := o.authConfigOperator.GetAuthConfig(context.Background())
if err != nil {
return "", err
}
Expand All @@ -29,12 +39,12 @@ func InfraCreateJWT(id string) (string, error) {
}

// InfraValidateJWT validates the chaos_infra jwt
func InfraValidateJWT(token string) (string, error) {
func (o *Operator) InfraValidateJWT(token string) (string, error) {
tkn, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
config, err := authConfig.NewAuthConfigOperator(mongodb.Operator).GetAuthConfig(context.Background())
config, err := o.authConfigOperator.GetAuthConfig(context.Background())
if err != nil {
return "", err
}
Expand Down
10 changes: 6 additions & 4 deletions chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ type Service interface {
type infraService struct {
infraOperator *dbChaosInfra.Operator
envOperator *dbEnvironments.Operator
authConfigOperator *authorization.Operator
}

// NewChaosInfrastructureService returns a new instance of Service
func NewChaosInfrastructureService(infraOperator *dbChaosInfra.Operator, envOperator *dbEnvironments.Operator) Service {
func NewChaosInfrastructureService(infraOperator *dbChaosInfra.Operator, envOperator *dbEnvironments.Operator, authConfigOperator *authorization.Operator) Service {
return &infraService{
infraOperator: infraOperator,
envOperator: envOperator,
authConfigOperator: authConfigOperator,
}
}

Expand Down Expand Up @@ -100,7 +102,7 @@ func (in *infraService) RegisterInfra(c context.Context, projectID string, input
)

tkn := c.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := in.authConfigOperator.GetUsername(tkn)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -221,7 +223,7 @@ func (in *infraService) RegisterInfra(c context.Context, projectID string, input
// DeleteInfra takes infraIDs and r parameters, deletes the infras from the database and sends a request to the subscriber for clean-up
func (in *infraService) DeleteInfra(ctx context.Context, projectID string, infraId string, r store.StateData) (string, error) {
tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := in.authConfigOperator.GetUsername(tkn)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -306,7 +308,7 @@ func (in *infraService) DeleteInfra(ctx context.Context, projectID string, infra
func (in *infraService) GetInfra(ctx context.Context, projectID string, infraID string) (*model.Infra, error) {

tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := in.authConfigOperator.GetUsername(tkn)
if err != nil {
return nil, err
}
Expand Down
31 changes: 17 additions & 14 deletions chaoscenter/graphql/server/pkg/chaoshub/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,18 @@ type Service interface {
GetChaosHubStats(ctx context.Context, projectID string) (*model.GetChaosHubStatsResponse, error)
}


type chaosHubService struct {
chaosHubOperator *dbSchemaChaosHub.Operator
chaosHubOperator *dbSchemaChaosHub.Operator
authConfigOperator *authorization.Operator
}

// NewService returns a new instance of Service
func NewService(chaosHubOperator *dbSchemaChaosHub.Operator) Service {
return &chaosHubService{
chaosHubOperator: chaosHubOperator,
}
func NewService(chaosHubOperator *dbSchemaChaosHub.Operator, authConfigOperator *authorization.Operator) Service {
return &chaosHubService{
chaosHubOperator: chaosHubOperator,
authConfigOperator: authConfigOperator,
}
}

// AddChaosHub is used for Adding a new ChaosHub
Expand All @@ -75,7 +78,7 @@ func (c *chaosHubService) AddChaosHub(ctx context.Context, chaosHub model.Create
}

tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)
if err != nil {
log.Error("error getting username: ", err)
return nil, err
Expand Down Expand Up @@ -129,7 +132,7 @@ func (c *chaosHubService) AddChaosHub(ctx context.Context, chaosHub model.Create
return newHub.GetOutputChaosHub(), nil
}

func (c *chaosHubService) AddRemoteChaosHub(ctx context.Context, chaosHub model.CreateRemoteChaosHub, projectID string) (*model.ChaosHub, error) {
func (c *chaosHubService) AddRemoteChaosHub(ctx context.Context, chaosHub model.CreateRemoteChaosHub, projectID string, authConfigOperator *authorization.Operator) (*model.ChaosHub, error) {
IsExist, err := c.IsChaosHubAvailable(ctx, chaosHub.Name, projectID)
if err != nil {
return nil, err
Expand All @@ -144,7 +147,7 @@ func (c *chaosHubService) AddRemoteChaosHub(ctx context.Context, chaosHub model.
currentTime := time.Now()

tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)

if err != nil {
log.Error("error getting userID: ", err)
Expand Down Expand Up @@ -198,7 +201,7 @@ func (c *chaosHubService) AddRemoteChaosHub(ctx context.Context, chaosHub model.
}

// SaveChaosHub is used for Adding a new ChaosHub
func (c *chaosHubService) SaveChaosHub(ctx context.Context, chaosHub model.CreateChaosHubRequest, projectID string) (*model.ChaosHub, error) {
func (c *chaosHubService) SaveChaosHub(ctx context.Context, chaosHub model.CreateChaosHubRequest, projectID string, authConfigOperator *authorization.Operator) (*model.ChaosHub, error) {

IsExist, err := c.IsChaosHubAvailable(ctx, chaosHub.Name, projectID)
if err != nil {
Expand All @@ -211,7 +214,7 @@ func (c *chaosHubService) SaveChaosHub(ctx context.Context, chaosHub model.Creat
// Initialize a UID for new Hub.
uuid := uuid.New()
tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)

if err != nil {
log.Error("error getting userID: ", err)
Expand Down Expand Up @@ -310,7 +313,7 @@ func (c *chaosHubService) SyncChaosHub(ctx context.Context, hubID string, projec
return "Successfully synced ChaosHub", nil
}

func (c *chaosHubService) UpdateChaosHub(ctx context.Context, chaosHub model.UpdateChaosHubRequest, projectID string) (*model.ChaosHub, error) {
func (c *chaosHubService) UpdateChaosHub(ctx context.Context, chaosHub model.UpdateChaosHubRequest, projectID string, authConfigOperator *authorization.Operator) (*model.ChaosHub, error) {

cloneHub := model.CloningInput{
RepoBranch: chaosHub.RepoBranch,
Expand Down Expand Up @@ -367,7 +370,7 @@ func (c *chaosHubService) UpdateChaosHub(ctx context.Context, chaosHub model.Upd

time := time.Now().UnixMilli()
tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)

query := bson.D{{"hub_id", chaosHub.ID}, {"is_removed", false}}
update := bson.D{
Expand Down Expand Up @@ -408,9 +411,9 @@ func (c *chaosHubService) UpdateChaosHub(ctx context.Context, chaosHub model.Upd
return &newChaosHub, nil
}

func (c *chaosHubService) DeleteChaosHub(ctx context.Context, hubID string, projectID string) (bool, error) {
func (c *chaosHubService) DeleteChaosHub(ctx context.Context, hubID string, projectID string, authConfigOperator *authorization.Operator) (bool, error) {
tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)
if err != nil {
return false, err
}
Expand Down
20 changes: 10 additions & 10 deletions chaoscenter/graphql/server/pkg/handlers/file_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,45 @@ import (
func FileHandler(mongodbOperator mongodb.MongoOperator) gin.HandlerFunc {
return func(c *gin.Context) {
token := strings.TrimSuffix(c.Param("key"), ".yaml")

infraId, err := chaos_infrastructure.InfraValidateJWT(token)
chaosInfraOperator := chaos_infrastructure.NewChaosInfrastructureOperator(mongodbOperator)
infraId, err := chaosInfraOperator.InfraValidateJWT(token)
if err != nil {
logrus.Error(err)
logrus.Error("Error validating JWT: ", err)
utils.WriteHeaders(&c.Writer, 500)
c.Writer.Write([]byte(err.Error()))
}

infra, err := dbChaosInfra.NewInfrastructureOperator(mongodbOperator).GetInfra(infraId)
if err != nil {
logrus.Error(err)
logrus.Error("Error fetching infra details: ", err)
utils.WriteHeaders(&c.Writer, 500)
c.Writer.Write([]byte(err.Error()))
}

reqHeader, ok := c.Value("request-header").(http.Header)
if !ok {
logrus.Error("unable to parse referer header")
logrus.Error("Unable to parse Referer header")
utils.WriteHeaders(&c.Writer, 500)
c.Writer.Write([]byte("unable to parse referer header"))
c.Writer.Write([]byte("Unable to parse Referer header"))
}

referrer := reqHeader.Get("Referer")
if referrer == "" {
logrus.Error("unable to parse referer header")
logrus.Error("Referer header is empty")
utils.WriteHeaders(&c.Writer, 500)
c.Writer.Write([]byte("unable to parse referer header"))
c.Writer.Write([]byte("Referer header is empty"))
}

referrerURL, err := url.Parse(referrer)
if err != nil {
logrus.Error(err)
logrus.Error("Error parsing Referer URL: ", err)
utils.WriteHeaders(&c.Writer, 500)
c.Writer.Write([]byte(err.Error()))
}

response, err := chaos_infrastructure.GetK8sInfraYaml(fmt.Sprintf("%s://%s", referrerURL.Scheme, referrerURL.Host), infra)
if err != nil {
logrus.Error(err)
logrus.Error("Error generating Kubernetes infra YAML: ", err)
utils.WriteHeaders(&c.Writer, 500)
c.Writer.Write([]byte(err.Error()))
}
Expand Down
Loading

0 comments on commit f4c0c51

Please sign in to comment.