Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f5cf97f

Browse files
committedJan 18, 2025·
backend unit tests
Signed-off-by: andoriyaprashant <prashantandoriya@gmail.com>
1 parent 484a0ba commit f5cf97f

File tree

7 files changed

+81
-61
lines changed

7 files changed

+81
-61
lines changed
 

‎chaoscenter/graphql/server/graph/environment.resolvers.go

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chaoscenter/graphql/server/pkg/authorization/authorization_fuzz_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func generateJWTTokenFromClaims(claims jwt.MapClaims) (string, error) {
5858
}
5959

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

7475
// Run the test with the generated JWT token
75-
claims, err := UserValidateJWT(tokenString, "")
76+
claims, err := operator.UserValidateJWT(tokenString, "")
7677
if err != nil {
7778
t.Errorf("Error encountered: %v", err)
7879
}

‎chaoscenter/graphql/server/pkg/chaos_infrastructure/cluster_jwt.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@ import (
1111
"github.com/golang-jwt/jwt"
1212
)
1313

14+
type Operator struct {
15+
authConfigOperator *authConfig.Operator
16+
}
17+
18+
func NewChaosInfrastructureOperator(mongodbOperator mongodb.MongoOperator) *Operator {
19+
return &Operator{
20+
authConfigOperator: authConfig.NewAuthConfigOperator(mongodbOperator),
21+
}
22+
}
23+
1424
// InfraCreateJWT generates jwt used in chaos_infra registration
15-
func InfraCreateJWT(id string) (string, error) {
25+
func (o *Operator) InfraCreateJWT(id string) (string, error) {
1626
claims := jwt.MapClaims{}
1727
claims["chaos_infra_id"] = id
1828
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
19-
config, err := authConfig.NewAuthConfigOperator(mongodb.Operator).GetAuthConfig(context.Background())
29+
config, err := o.authConfigOperator.GetAuthConfig(context.Background())
2030
if err != nil {
2131
return "", err
2232
}
@@ -29,12 +39,12 @@ func InfraCreateJWT(id string) (string, error) {
2939
}
3040

3141
// InfraValidateJWT validates the chaos_infra jwt
32-
func InfraValidateJWT(token string) (string, error) {
42+
func (o *Operator) InfraValidateJWT(token string) (string, error) {
3343
tkn, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
3444
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
3545
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
3646
}
37-
config, err := authConfig.NewAuthConfigOperator(mongodb.Operator).GetAuthConfig(context.Background())
47+
config, err := o.authConfigOperator.GetAuthConfig(context.Background())
3848
if err != nil {
3949
return "", err
4050
}

‎chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ type Service interface {
5959
type infraService struct {
6060
infraOperator *dbChaosInfra.Operator
6161
envOperator *dbEnvironments.Operator
62+
authConfigOperator *authorization.Operator
6263
}
6364

6465
// NewChaosInfrastructureService returns a new instance of Service
65-
func NewChaosInfrastructureService(infraOperator *dbChaosInfra.Operator, envOperator *dbEnvironments.Operator) Service {
66+
func NewChaosInfrastructureService(infraOperator *dbChaosInfra.Operator, envOperator *dbEnvironments.Operator, authConfigOperator *authorization.Operator) Service {
6667
return &infraService{
6768
infraOperator: infraOperator,
6869
envOperator: envOperator,
70+
authConfigOperator: authConfigOperator,
6971
}
7072
}
7173

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

102104
tkn := c.Value(authorization.AuthKey).(string)
103-
username, err := authorization.GetUsername(tkn)
105+
username, err := in.authConfigOperator.GetUsername(tkn)
104106
if err != nil {
105107
return nil, err
106108
}
@@ -221,7 +223,7 @@ func (in *infraService) RegisterInfra(c context.Context, projectID string, input
221223
// DeleteInfra takes infraIDs and r parameters, deletes the infras from the database and sends a request to the subscriber for clean-up
222224
func (in *infraService) DeleteInfra(ctx context.Context, projectID string, infraId string, r store.StateData) (string, error) {
223225
tkn := ctx.Value(authorization.AuthKey).(string)
224-
username, err := authorization.GetUsername(tkn)
226+
username, err := in.authConfigOperator.GetUsername(tkn)
225227
if err != nil {
226228
return "", err
227229
}
@@ -306,7 +308,7 @@ func (in *infraService) DeleteInfra(ctx context.Context, projectID string, infra
306308
func (in *infraService) GetInfra(ctx context.Context, projectID string, infraID string) (*model.Infra, error) {
307309

308310
tkn := ctx.Value(authorization.AuthKey).(string)
309-
username, err := authorization.GetUsername(tkn)
311+
username, err := in.authConfigOperator.GetUsername(tkn)
310312
if err != nil {
311313
return nil, err
312314
}

‎chaoscenter/graphql/server/pkg/chaoshub/service.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,18 @@ type Service interface {
4949
GetChaosHubStats(ctx context.Context, projectID string) (*model.GetChaosHubStatsResponse, error)
5050
}
5151

52+
5253
type chaosHubService struct {
53-
chaosHubOperator *dbSchemaChaosHub.Operator
54+
chaosHubOperator *dbSchemaChaosHub.Operator
55+
authConfigOperator *authorization.Operator
5456
}
5557

5658
// NewService returns a new instance of Service
57-
func NewService(chaosHubOperator *dbSchemaChaosHub.Operator) Service {
58-
return &chaosHubService{
59-
chaosHubOperator: chaosHubOperator,
60-
}
59+
func NewService(chaosHubOperator *dbSchemaChaosHub.Operator, authConfigOperator *authorization.Operator) Service {
60+
return &chaosHubService{
61+
chaosHubOperator: chaosHubOperator,
62+
authConfigOperator: authConfigOperator,
63+
}
6164
}
6265

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

7780
tkn := ctx.Value(authorization.AuthKey).(string)
78-
username, err := authorization.GetUsername(tkn)
81+
username, err := c.authConfigOperator.GetUsername(tkn)
7982
if err != nil {
8083
log.Error("error getting username: ", err)
8184
return nil, err
@@ -129,7 +132,7 @@ func (c *chaosHubService) AddChaosHub(ctx context.Context, chaosHub model.Create
129132
return newHub.GetOutputChaosHub(), nil
130133
}
131134

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

146149
tkn := ctx.Value(authorization.AuthKey).(string)
147-
username, err := authorization.GetUsername(tkn)
150+
username, err := c.authConfigOperator.GetUsername(tkn)
148151

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

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

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

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

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

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

368371
time := time.Now().UnixMilli()
369372
tkn := ctx.Value(authorization.AuthKey).(string)
370-
username, err := authorization.GetUsername(tkn)
373+
username, err := c.authConfigOperator.GetUsername(tkn)
371374

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

411-
func (c *chaosHubService) DeleteChaosHub(ctx context.Context, hubID string, projectID string) (bool, error) {
414+
func (c *chaosHubService) DeleteChaosHub(ctx context.Context, hubID string, projectID string, authConfigOperator *authorization.Operator) (bool, error) {
412415
tkn := ctx.Value(authorization.AuthKey).(string)
413-
username, err := authorization.GetUsername(tkn)
416+
username, err := c.authConfigOperator.GetUsername(tkn)
414417
if err != nil {
415418
return false, err
416419
}

‎chaoscenter/graphql/server/pkg/handlers/file_handler.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,45 @@ import (
1919
func FileHandler(mongodbOperator mongodb.MongoOperator) gin.HandlerFunc {
2020
return func(c *gin.Context) {
2121
token := strings.TrimSuffix(c.Param("key"), ".yaml")
22-
23-
infraId, err := chaos_infrastructure.InfraValidateJWT(token)
22+
chaosInfraOperator := chaos_infrastructure.NewChaosInfrastructureOperator(mongodbOperator)
23+
infraId, err := chaosInfraOperator.InfraValidateJWT(token)
2424
if err != nil {
25-
logrus.Error(err)
25+
logrus.Error("Error validating JWT: ", err)
2626
utils.WriteHeaders(&c.Writer, 500)
2727
c.Writer.Write([]byte(err.Error()))
2828
}
2929

3030
infra, err := dbChaosInfra.NewInfrastructureOperator(mongodbOperator).GetInfra(infraId)
3131
if err != nil {
32-
logrus.Error(err)
32+
logrus.Error("Error fetching infra details: ", err)
3333
utils.WriteHeaders(&c.Writer, 500)
3434
c.Writer.Write([]byte(err.Error()))
3535
}
3636

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

4444
referrer := reqHeader.Get("Referer")
4545
if referrer == "" {
46-
logrus.Error("unable to parse referer header")
46+
logrus.Error("Referer header is empty")
4747
utils.WriteHeaders(&c.Writer, 500)
48-
c.Writer.Write([]byte("unable to parse referer header"))
48+
c.Writer.Write([]byte("Referer header is empty"))
4949
}
5050

5151
referrerURL, err := url.Parse(referrer)
5252
if err != nil {
53-
logrus.Error(err)
53+
logrus.Error("Error parsing Referer URL: ", err)
5454
utils.WriteHeaders(&c.Writer, 500)
5555
c.Writer.Write([]byte(err.Error()))
5656
}
5757

5858
response, err := chaos_infrastructure.GetK8sInfraYaml(fmt.Sprintf("%s://%s", referrerURL.Scheme, referrerURL.Host), infra)
5959
if err != nil {
60-
logrus.Error(err)
60+
logrus.Error("Error generating Kubernetes infra YAML: ", err)
6161
utils.WriteHeaders(&c.Writer, 500)
6262
c.Writer.Write([]byte(err.Error()))
6363
}

0 commit comments

Comments
 (0)
Please sign in to comment.