Skip to content
Merged
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions api/internal/pkg/pac-go-server/services/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
12 changes: 6 additions & 6 deletions api/internal/pkg/pac-go-server/services/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions api/internal/pkg/pac-go-server/services/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading