backend: membership errorhandling tests
This commit is contained in:
@@ -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 == errors.ErrNoData {
|
||||
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)
|
||||
} else if err == errors.ErrInvalidSubscriptionData {
|
||||
utils.RespondWithError(c, err, "Invalid subscription data", http.StatusBadRequest, errors.Responses.Fields.SubscriptionModel, errors.Responses.Keys.Invalid)
|
||||
|
||||
@@ -251,7 +251,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
return []UpdateSubscriptionTest{
|
||||
{
|
||||
Name: "Modified Monthly Fee, should fail",
|
||||
WantResponse: http.StatusNotAcceptable,
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium", "monthly_fee": "12"},
|
||||
Assert: true,
|
||||
Input: GenerateInputJSON(
|
||||
@@ -262,7 +262,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
},
|
||||
{
|
||||
Name: "Missing ID, should fail",
|
||||
WantResponse: http.StatusNotAcceptable,
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium"},
|
||||
Assert: true,
|
||||
Input: GenerateInputJSON(
|
||||
@@ -273,7 +273,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
},
|
||||
{
|
||||
Name: "Modified Hourly Rate, should fail",
|
||||
WantResponse: http.StatusNotAcceptable,
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium", "hourly_rate": "14"},
|
||||
Assert: true,
|
||||
Input: GenerateInputJSON(
|
||||
@@ -284,7 +284,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
},
|
||||
{
|
||||
Name: "IncludedPerYear changed, should fail",
|
||||
WantResponse: http.StatusNotAcceptable,
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium", "included_per_year": "0"},
|
||||
Assert: true,
|
||||
Input: GenerateInputJSON(
|
||||
@@ -295,7 +295,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
},
|
||||
{
|
||||
Name: "IncludedPerMonth changed, should fail",
|
||||
WantResponse: http.StatusNotAcceptable,
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium", "included_per_month": "1"},
|
||||
Assert: true,
|
||||
Input: GenerateInputJSON(
|
||||
@@ -306,7 +306,7 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
},
|
||||
{
|
||||
Name: "Update non-existent subscription should fail",
|
||||
WantResponse: http.StatusNotAcceptable,
|
||||
WantResponse: http.StatusNotFound,
|
||||
WantDBData: map[string]interface{}{"name": "NonExistentSubscription"},
|
||||
Assert: false,
|
||||
Input: GenerateInputJSON(
|
||||
@@ -343,7 +343,7 @@ func getSubscriptionDeleteData() []DeleteSubscriptionTest {
|
||||
return []DeleteSubscriptionTest{
|
||||
{
|
||||
Name: "Delete non-existent subscription should fail",
|
||||
WantResponse: http.StatusExpectationFailed,
|
||||
WantResponse: http.StatusNotFound,
|
||||
WantDBData: map[string]interface{}{"name": "NonExistentSubscription"},
|
||||
Assert: false,
|
||||
Input: GenerateInputJSON(
|
||||
|
||||
@@ -33,11 +33,12 @@ func (service *MembershipService) UpdateSubscription(subscription *models.Subscr
|
||||
|
||||
existingSubscription, err := repositories.GetSubscriptionByName(&subscription.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if existingSubscription == nil {
|
||||
if err.Error() == "record not found" {
|
||||
return nil, errors.ErrSubscriptionNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if existingSubscription.MonthlyFee != subscription.MonthlyFee ||
|
||||
existingSubscription.HourlyRate != subscription.HourlyRate ||
|
||||
existingSubscription.Conditions != subscription.Conditions ||
|
||||
@@ -56,11 +57,12 @@ func (service *MembershipService) DeleteSubscription(id *uint, name *string) err
|
||||
}
|
||||
exists, err := repositories.GetSubscriptionByName(name)
|
||||
if err != nil {
|
||||
if err.Error() == "record not found" {
|
||||
return errors.ErrSubscriptionNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
if exists == nil {
|
||||
return errors.ErrNotFound
|
||||
}
|
||||
|
||||
if *id != exists.ID {
|
||||
return errors.ErrInvalidSubscriptionData
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ var Responses = struct {
|
||||
PasswordAlreadyChanged: "server.validation.password_already_changed",
|
||||
NoAuthToken: "server.error.no_auth_token",
|
||||
NotFound: "server.error.not_found",
|
||||
inUse: "server.error.in_use",
|
||||
InUse: "server.error.in_use",
|
||||
},
|
||||
Fields: ValidationFields{
|
||||
General: "general",
|
||||
|
||||
Reference in New Issue
Block a user