tests
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"GoMembership/internal/constants"
|
||||
"GoMembership/internal/database"
|
||||
"GoMembership/internal/models"
|
||||
"GoMembership/pkg/logger"
|
||||
@@ -15,6 +14,7 @@ import (
|
||||
)
|
||||
|
||||
type RegisterSubscriptionTest struct {
|
||||
SetupCookie func(r *http.Request)
|
||||
WantDBData map[string]interface{}
|
||||
Input string
|
||||
Name string
|
||||
@@ -23,6 +23,7 @@ type RegisterSubscriptionTest struct {
|
||||
}
|
||||
|
||||
type UpdateSubscriptionTest struct {
|
||||
SetupCookie func(r *http.Request)
|
||||
WantDBData map[string]interface{}
|
||||
Input string
|
||||
Name string
|
||||
@@ -31,6 +32,7 @@ type UpdateSubscriptionTest struct {
|
||||
}
|
||||
|
||||
type DeleteSubscriptionTest struct {
|
||||
SetupCookie func(r *http.Request)
|
||||
WantDBData map[string]interface{}
|
||||
Input string
|
||||
Name string
|
||||
@@ -38,29 +40,8 @@ type DeleteSubscriptionTest struct {
|
||||
Assert bool
|
||||
}
|
||||
|
||||
type MockUserController struct {
|
||||
UserController // Embed the UserController
|
||||
}
|
||||
|
||||
func (m *MockUserController) ExtractUserFromContext(c *gin.Context) (*models.User, error) {
|
||||
return &models.User{
|
||||
ID: 1,
|
||||
FirstName: "Admin",
|
||||
LastName: "User",
|
||||
Email: "admin@test.com",
|
||||
RoleID: constants.Roles.Admin,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func setupMockAuth() {
|
||||
// Create and assign the mock controller
|
||||
mockController := &MockUserController{}
|
||||
Mc.UserController = mockController
|
||||
}
|
||||
|
||||
func testMembershipController(t *testing.T) {
|
||||
|
||||
setupMockAuth()
|
||||
tests := getSubscriptionRegistrationData()
|
||||
for _, tt := range tests {
|
||||
logger.Error.Print("==============================================================")
|
||||
@@ -101,6 +82,7 @@ func (rt *RegisterSubscriptionTest) SetupContext() (*gin.Context, *httptest.Resp
|
||||
}
|
||||
|
||||
func (rt *RegisterSubscriptionTest) RunHandler(c *gin.Context, router *gin.Engine) {
|
||||
rt.SetupCookie(c.Request)
|
||||
Mc.RegisterSubscription(c)
|
||||
}
|
||||
|
||||
@@ -131,6 +113,7 @@ func (ut *UpdateSubscriptionTest) SetupContext() (*gin.Context, *httptest.Respon
|
||||
}
|
||||
|
||||
func (ut *UpdateSubscriptionTest) RunHandler(c *gin.Context, router *gin.Engine) {
|
||||
ut.SetupCookie(c.Request)
|
||||
Mc.UpdateHandler(c)
|
||||
}
|
||||
|
||||
@@ -150,6 +133,7 @@ func (dt *DeleteSubscriptionTest) SetupContext() (*gin.Context, *httptest.Respon
|
||||
}
|
||||
|
||||
func (dt *DeleteSubscriptionTest) RunHandler(c *gin.Context, router *gin.Engine) {
|
||||
dt.SetupCookie(c.Request)
|
||||
Mc.DeleteSubscription(c)
|
||||
}
|
||||
|
||||
@@ -183,6 +167,9 @@ func customizeSubscription(customize func(MembershipData) MembershipData) Member
|
||||
func getSubscriptionRegistrationData() []RegisterSubscriptionTest {
|
||||
return []RegisterSubscriptionTest{
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Missing details should fail",
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Just a Subscription"},
|
||||
@@ -194,6 +181,9 @@ func getSubscriptionRegistrationData() []RegisterSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Missing model name should fail",
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": ""},
|
||||
@@ -205,6 +195,9 @@ func getSubscriptionRegistrationData() []RegisterSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Negative monthly fee should fail",
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium"},
|
||||
@@ -215,6 +208,9 @@ func getSubscriptionRegistrationData() []RegisterSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Negative hourly rate should fail",
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium"},
|
||||
@@ -225,6 +221,25 @@ func getSubscriptionRegistrationData() []RegisterSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
Name: "correct entry but not authorized",
|
||||
WantResponse: http.StatusUnauthorized,
|
||||
WantDBData: map[string]interface{}{"name": "Premium"},
|
||||
Assert: false,
|
||||
Input: GenerateInputJSON(
|
||||
customizeSubscription(func(subscription MembershipData) MembershipData {
|
||||
subscription.Subscription.Conditions = "Some Condition"
|
||||
subscription.Subscription.IncludedPerYear = 0
|
||||
subscription.Subscription.IncludedPerMonth = 1
|
||||
return subscription
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "correct entry should pass",
|
||||
WantResponse: http.StatusCreated,
|
||||
WantDBData: map[string]interface{}{"name": "Premium"},
|
||||
@@ -238,6 +253,9 @@ func getSubscriptionRegistrationData() []RegisterSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Duplicate subscription name should fail",
|
||||
WantResponse: http.StatusConflict,
|
||||
WantDBData: map[string]interface{}{"name": "Premium"},
|
||||
@@ -250,6 +268,9 @@ func getSubscriptionRegistrationData() []RegisterSubscriptionTest {
|
||||
func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
return []UpdateSubscriptionTest{
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Modified Monthly Fee, should fail",
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium", "monthly_fee": "12"},
|
||||
@@ -261,6 +282,9 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Missing ID, should fail",
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium"},
|
||||
@@ -272,6 +296,9 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Modified Hourly Rate, should fail",
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium", "hourly_rate": "14"},
|
||||
@@ -283,6 +310,9 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "IncludedPerYear changed, should fail",
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium", "included_per_year": "0"},
|
||||
@@ -294,6 +324,9 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "IncludedPerMonth changed, should fail",
|
||||
WantResponse: http.StatusBadRequest,
|
||||
WantDBData: map[string]interface{}{"name": "Premium", "included_per_month": "1"},
|
||||
@@ -305,6 +338,9 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Update non-existent subscription should fail",
|
||||
WantResponse: http.StatusNotFound,
|
||||
WantDBData: map[string]interface{}{"name": "NonExistentSubscription"},
|
||||
@@ -316,6 +352,26 @@ func getSubscriptionUpdateData() []UpdateSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
Name: "Correct Update but unauthorized",
|
||||
WantResponse: http.StatusUnauthorized,
|
||||
WantDBData: map[string]interface{}{"name": "Premium", "details": "Altered Details"},
|
||||
Assert: false,
|
||||
Input: GenerateInputJSON(
|
||||
customizeSubscription(func(subscription MembershipData) MembershipData {
|
||||
subscription.Subscription.Details = "Altered Details"
|
||||
subscription.Subscription.Conditions = "Some Condition"
|
||||
subscription.Subscription.IncludedPerYear = 0
|
||||
subscription.Subscription.IncludedPerMonth = 1
|
||||
return subscription
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Correct Update should pass",
|
||||
WantResponse: http.StatusAccepted,
|
||||
WantDBData: map[string]interface{}{"name": "Premium", "details": "Altered Details"},
|
||||
@@ -338,10 +394,11 @@ func getSubscriptionDeleteData() []DeleteSubscriptionTest {
|
||||
database.DB.Where("name = ?", "Premium").First(&premiumSub)
|
||||
database.DB.Where("name = ?", "Basic").First(&basicSub)
|
||||
|
||||
logger.Error.Printf("premiumSub.ID: %v", premiumSub.ID)
|
||||
logger.Error.Printf("basicSub.ID: %v", basicSub.ID)
|
||||
return []DeleteSubscriptionTest{
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Delete non-existent subscription should fail",
|
||||
WantResponse: http.StatusNotFound,
|
||||
WantDBData: map[string]interface{}{"name": "NonExistentSubscription"},
|
||||
@@ -354,6 +411,9 @@ func getSubscriptionDeleteData() []DeleteSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Delete subscription without name should fail",
|
||||
WantResponse: http.StatusExpectationFailed,
|
||||
WantDBData: map[string]interface{}{"name": ""},
|
||||
@@ -366,6 +426,9 @@ func getSubscriptionDeleteData() []DeleteSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Delete subscription with users should fail",
|
||||
WantResponse: http.StatusExpectationFailed,
|
||||
WantDBData: map[string]interface{}{"name": "Basic"},
|
||||
@@ -378,6 +441,24 @@ func getSubscriptionDeleteData() []DeleteSubscriptionTest {
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
Name: "Delete valid subscription should succeed",
|
||||
WantResponse: http.StatusUnauthorized,
|
||||
WantDBData: map[string]interface{}{"name": "Premium"},
|
||||
Assert: true,
|
||||
Input: GenerateInputJSON(
|
||||
customizeSubscription(func(subscription MembershipData) MembershipData {
|
||||
subscription.Subscription.Name = "Premium"
|
||||
subscription.Subscription.ID = premiumSub.ID
|
||||
return subscription
|
||||
})),
|
||||
},
|
||||
{
|
||||
SetupCookie: func(req *http.Request) {
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
Name: "Delete valid subscription should succeed",
|
||||
WantResponse: http.StatusOK,
|
||||
WantDBData: map[string]interface{}{"name": "Premium"},
|
||||
|
||||
Reference in New Issue
Block a user