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
29 changes: 28 additions & 1 deletion api/internal/pkg/pac-go-server/services/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,34 @@ func getResource(apiType string, customValues map[string]interface{}) interface{
},
ServiceExpiry: &models.ServiceExpiry{
Name: "test-service",
Expiry: time.Now().Add(3 * time.Hour),
Expiry: time.Now().Add(4 * time.Hour),
},
}
// 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-request-by-id-before-expiry":
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().Add(2 * time.Hour),
},
}
// Update request with custom values if provided
Expand Down
8 changes: 8 additions & 0 deletions api/internal/pkg/pac-go-server/services/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ func UpdateServiceExpiryRequest(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Service %s is expired, can't extend the expiry", serviceName)})
return
}
// requested expiry should be after current expiry
currentExpiry := service.Spec.Expiry.Time
requestedExpiry := expiryRequest.ServiceExpiry.Expiry
if requestedExpiry.Before(currentExpiry) {
logger.Error("Requested expiry date should be after the current expiry date")
c.JSON(http.StatusBadRequest, gin.H{"error": "Requested expiry date should be after the current expiry date"})
return
}

// service shouldn't be extended if catalog already retired
catalog, err := kubeClient.GetCatalog(service.Spec.Catalog.Name)
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 @@ -137,6 +137,17 @@ func TestUpdateServiceExpiryRequest(t *testing.T) {
}),
httpStatus: http.StatusBadRequest,
},
{
name: "service expiry request set before current expiry",
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-by-id-before-expiry", nil).(*models.Request),
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Loading