Compare commits

..

2 Commits

Author SHA1 Message Date
Alex
e553c2dc2e del: logging 2025-02-28 10:05:35 +01:00
Alex
20754b4422 backend: membership errorhandling tests 2025-02-28 10:05:25 +01:00
5 changed files with 17 additions and 20 deletions

View File

@@ -132,7 +132,7 @@ func (mc *MembershipController) DeleteSubscription(c *gin.Context) {
if err := mc.Service.DeleteSubscription(&data.Subscription.ID, &data.Subscription.Name); err != nil { if err := mc.Service.DeleteSubscription(&data.Subscription.ID, &data.Subscription.Name); err != nil {
if err == errors.ErrNoData { if err == errors.ErrNoData {
utils.RespondWithError(c, err, "Missing subscription name during deletion", http.StatusExpectationFailed, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid) utils.RespondWithError(c, err, "Missing subscription name during deletion", http.StatusExpectationFailed, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid)
} else if err == errors.ErrNotFound { } else if err == errors.ErrSubscriptionNotFound {
utils.RespondWithError(c, err, "Subscription not found", http.StatusNotFound, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.NotFound) utils.RespondWithError(c, err, "Subscription not found", http.StatusNotFound, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.NotFound)
} else if err == errors.ErrInvalidSubscriptionData { } else if err == errors.ErrInvalidSubscriptionData {
utils.RespondWithError(c, err, "Invalid subscription data", http.StatusBadRequest, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid) utils.RespondWithError(c, err, "Invalid subscription data", http.StatusBadRequest, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid)

View File

@@ -251,7 +251,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
return []UpdateSubscriptionTest{ return []UpdateSubscriptionTest{
{ {
Name: "Modified Monthly Fee, should fail", Name: "Modified Monthly Fee, should fail",
WantResponse: http.StatusNotAcceptable, WantResponse: http.StatusBadRequest,
WantDBData: map[string]interface{}{"name": "Premium", "monthly_fee": "12"}, WantDBData: map[string]interface{}{"name": "Premium", "monthly_fee": "12"},
Assert: true, Assert: true,
Input: GenerateInputJSON( Input: GenerateInputJSON(
@@ -262,7 +262,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
}, },
{ {
Name: "Missing ID, should fail", Name: "Missing ID, should fail",
WantResponse: http.StatusNotAcceptable, WantResponse: http.StatusBadRequest,
WantDBData: map[string]interface{}{"name": "Premium"}, WantDBData: map[string]interface{}{"name": "Premium"},
Assert: true, Assert: true,
Input: GenerateInputJSON( Input: GenerateInputJSON(
@@ -273,7 +273,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
}, },
{ {
Name: "Modified Hourly Rate, should fail", Name: "Modified Hourly Rate, should fail",
WantResponse: http.StatusNotAcceptable, WantResponse: http.StatusBadRequest,
WantDBData: map[string]interface{}{"name": "Premium", "hourly_rate": "14"}, WantDBData: map[string]interface{}{"name": "Premium", "hourly_rate": "14"},
Assert: true, Assert: true,
Input: GenerateInputJSON( Input: GenerateInputJSON(
@@ -284,7 +284,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
}, },
{ {
Name: "IncludedPerYear changed, should fail", Name: "IncludedPerYear changed, should fail",
WantResponse: http.StatusNotAcceptable, WantResponse: http.StatusBadRequest,
WantDBData: map[string]interface{}{"name": "Premium", "included_per_year": "0"}, WantDBData: map[string]interface{}{"name": "Premium", "included_per_year": "0"},
Assert: true, Assert: true,
Input: GenerateInputJSON( Input: GenerateInputJSON(
@@ -295,7 +295,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
}, },
{ {
Name: "IncludedPerMonth changed, should fail", Name: "IncludedPerMonth changed, should fail",
WantResponse: http.StatusNotAcceptable, WantResponse: http.StatusBadRequest,
WantDBData: map[string]interface{}{"name": "Premium", "included_per_month": "1"}, WantDBData: map[string]interface{}{"name": "Premium", "included_per_month": "1"},
Assert: true, Assert: true,
Input: GenerateInputJSON( Input: GenerateInputJSON(
@@ -306,7 +306,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
}, },
{ {
Name: "Update non-existent subscription should fail", Name: "Update non-existent subscription should fail",
WantResponse: http.StatusNotAcceptable, WantResponse: http.StatusNotFound,
WantDBData: map[string]interface{}{"name": "NonExistentSubscription"}, WantDBData: map[string]interface{}{"name": "NonExistentSubscription"},
Assert: false, Assert: false,
Input: GenerateInputJSON( Input: GenerateInputJSON(
@@ -343,7 +343,7 @@ func getSubscriptionDeleteData() []DeleteSubscriptionTest {
return []DeleteSubscriptionTest{ return []DeleteSubscriptionTest{
{ {
Name: "Delete non-existent subscription should fail", Name: "Delete non-existent subscription should fail",
WantResponse: http.StatusExpectationFailed, WantResponse: http.StatusNotFound,
WantDBData: map[string]interface{}{"name": "NonExistentSubscription"}, WantDBData: map[string]interface{}{"name": "NonExistentSubscription"},
Assert: false, Assert: false,
Input: GenerateInputJSON( Input: GenerateInputJSON(

View File

@@ -33,11 +33,12 @@ func (service *MembershipService) UpdateSubscription(subscription *models.Subscr
existingSubscription, err := repositories.GetSubscriptionByName(&subscription.Name) existingSubscription, err := repositories.GetSubscriptionByName(&subscription.Name)
if err != nil { if err != nil {
return nil, err if err.Error() == "record not found" {
}
if existingSubscription == nil {
return nil, errors.ErrSubscriptionNotFound return nil, errors.ErrSubscriptionNotFound
} }
return nil, err
}
if existingSubscription.MonthlyFee != subscription.MonthlyFee || if existingSubscription.MonthlyFee != subscription.MonthlyFee ||
existingSubscription.HourlyRate != subscription.HourlyRate || existingSubscription.HourlyRate != subscription.HourlyRate ||
existingSubscription.Conditions != subscription.Conditions || existingSubscription.Conditions != subscription.Conditions ||
@@ -56,11 +57,12 @@ func (service *MembershipService) DeleteSubscription(id *uint, name *string) err
} }
exists, err := repositories.GetSubscriptionByName(name) exists, err := repositories.GetSubscriptionByName(name)
if err != nil { if err != nil {
if err.Error() == "record not found" {
return errors.ErrSubscriptionNotFound
}
return err return err
} }
if exists == nil {
return errors.ErrNotFound
}
if *id != exists.ID { if *id != exists.ID {
return errors.ErrInvalidSubscriptionData return errors.ErrInvalidSubscriptionData
} }

View File

@@ -3,7 +3,6 @@ package utils
import ( import (
"GoMembership/internal/constants" "GoMembership/internal/constants"
"GoMembership/internal/models" "GoMembership/internal/models"
"GoMembership/pkg/logger"
"errors" "errors"
"reflect" "reflect"
) )
@@ -120,12 +119,8 @@ func FilterAllowedStructFields(input interface{}, existing interface{}, allowedF
// Only allow whitelisted fields // Only allow whitelisted fields
if !allowedFields[fullKey] { if !allowedFields[fullKey] {
logger.Error.Printf("denying update of field: %#v", fullKey)
fieldValue.Set(originField) fieldValue.Set(originField)
} else {
logger.Error.Printf("updating whitelisted field: %#v", fullKey)
} }
} }
return nil return nil
} }

View File

@@ -68,7 +68,7 @@ var Responses = struct {
PasswordAlreadyChanged: "server.validation.password_already_changed", PasswordAlreadyChanged: "server.validation.password_already_changed",
NoAuthToken: "server.error.no_auth_token", NoAuthToken: "server.error.no_auth_token",
NotFound: "server.error.not_found", NotFound: "server.error.not_found",
inUse: "server.error.in_use", InUse: "server.error.in_use",
}, },
Fields: ValidationFields{ Fields: ValidationFields{
General: "general", General: "general",