diff --git a/api/internal/pkg/pac-go-server/services/backend_test.go b/api/internal/pkg/pac-go-server/services/backend_test.go index 22f73dd..367778e 100644 --- a/api/internal/pkg/pac-go-server/services/backend_test.go +++ b/api/internal/pkg/pac-go-server/services/backend_test.go @@ -415,6 +415,33 @@ func getResource(apiType string, customValues map[string]interface{}) interface{ } } return &request + case "get-request-more-than-5days": + request := models.Request{ + ID: [12]byte{1}, + UserID: "12345", + Justification: "justification", + Comment: "comment", + CreatedAt: time.Time{}, + RequestType: "SERVICE_EXPIRY", + GroupAdmission: &models.GroupAdmission{ + GroupID: "test-group", + Group: "manager", + Requester: "test-user", + }, + ServiceExpiry: &models.ServiceExpiry{ + Name: "test-service", + Expiry: time.Now().AddDate(0, 0, 6), + }, + } + // Update request with custom values if provided + for key, value := range customValues { + if fieldValue := reflect.ValueOf(&request).Elem().FieldByName(key); fieldValue.IsValid() { + if value != nil { + fieldValue.Set(reflect.ValueOf(value)) + } + } + } + return &request case "get-key-by-id": key := models.Key{ ID: [12]byte{1}, diff --git a/api/internal/pkg/pac-go-server/services/request.go b/api/internal/pkg/pac-go-server/services/request.go index d58987f..39383d6 100644 --- a/api/internal/pkg/pac-go-server/services/request.go +++ b/api/internal/pkg/pac-go-server/services/request.go @@ -142,6 +142,12 @@ func UpdateServiceExpiryRequest(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": "Requested expiry date should be after the current expiry date"}) return } + // maximum extension allowed is 5 days from current expiry + if requestedExpiry.After(currentExpiry.AddDate(0, 0, 5)) { + logger.Error("Maximum extension allowed is 5 days from the current expiry date") + c.JSON(http.StatusBadRequest, gin.H{"error": "Maximum extension allowed is 5 days from the current expiry date"}) + return + } // service shouldn't be extended if catalog already retired catalog, err := kubeClient.GetCatalog(service.Spec.Catalog.Name) @@ -176,12 +182,6 @@ func UpdateServiceExpiryRequest(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": "You have already requested to extend service expiry"}) return } - requestedDate := expiryRequest.ServiceExpiry.Expiry - if requestedDate.After(request.ServiceExpiry.Expiry.AddDate(0, 0, 5)) { - logger.Error("Maximum extension allowed is 5 days from the current expiry date") - c.JSON(http.StatusBadRequest, gin.H{"error": "Maximum extension allowed is 5 days from the current expiry date"}) - return - } } // insert the request into the database diff --git a/api/internal/pkg/pac-go-server/services/request_test.go b/api/internal/pkg/pac-go-server/services/request_test.go index 0ace75c..19181b3 100644 --- a/api/internal/pkg/pac-go-server/services/request_test.go +++ b/api/internal/pkg/pac-go-server/services/request_test.go @@ -148,6 +148,17 @@ func TestUpdateServiceExpiryRequest(t *testing.T) { httpStatus: http.StatusBadRequest, request: getResource("get-request-by-id-before-expiry", nil).(*models.Request), }, + { + name: "service expiry extension beyond maximum allowed", + mockFunc: func() { + mockClient.EXPECT().GetService(gomock.Any()).Return(getResource("get-service", nil).(pac.Service), nil).Times(1) + }, + requestContext: formContext(customValues{ + "userid": "12345", + }), + httpStatus: http.StatusBadRequest, + request: getResource("get-request-more-than-5days", nil).(*models.Request), + }, } for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) {