added new membership tests, fix tests, cors for dev

This commit is contained in:
$(pass /github/name)
2024-08-24 21:15:38 +02:00
parent 4274f386f7
commit c03ee0b6d0
8 changed files with 96 additions and 31 deletions

View File

@@ -25,8 +25,8 @@ import (
)
type TestCase interface {
SetupContext() (*gin.Context, *httptest.ResponseRecorder)
RunHandler(*gin.Context)
SetupContext() (*gin.Context, *httptest.ResponseRecorder, *gin.Engine)
RunHandler(*gin.Context, *gin.Engine)
ValidateResponse(*httptest.ResponseRecorder) error
ValidateResult() error
}
@@ -125,23 +125,42 @@ func TestSuite(t *testing.T) {
}
func initSubscriptionPlans() error {
subscription := models.SubscriptionModel{
Name: "Basic",
Details: "Test Plan",
MonthlyFee: 2,
HourlyRate: 3,
subscriptions := []models.SubscriptionModel{
{
Name: "Basic",
Details: "Test Plan",
MonthlyFee: 2,
HourlyRate: 3,
},
{
Name: "additional",
Details: "This plan needs another membership id to validate",
RequiredMembershipField: "ParentMembershipID",
MonthlyFee: 2,
HourlyRate: 3,
},
}
result := database.DB.Create(&subscription)
if result.Error != nil {
return result.Error
for _, subscription := range subscriptions {
result := database.DB.Create(&subscription)
if result.Error != nil {
return result.Error
}
}
return nil
}
func GetMockedJSONContext(jsonStr []byte, url string) (*gin.Context, *httptest.ResponseRecorder) {
func GetMockedJSONContext(jsonStr []byte, url string) (*gin.Context, *httptest.ResponseRecorder, *gin.Engine) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
router := gin.New()
// Load HTML templates
router.LoadHTMLGlob(config.Templates.HTMLPath + "/*")
var err error
c.Request, err = http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
@@ -149,22 +168,31 @@ func GetMockedJSONContext(jsonStr []byte, url string) (*gin.Context, *httptest.R
}
c.Request.Header.Set("Content-Type", "application/json")
return c, w
return c, w, router
}
func GetMockedFormContext(formData url.Values, url string) (*gin.Context, *httptest.ResponseRecorder) {
func GetMockedFormContext(formData url.Values, url string) (*gin.Context, *httptest.ResponseRecorder, *gin.Engine) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
req, err := http.NewRequest("POST", url, bytes.NewBufferString(formData.Encode()))
router := gin.New()
// Load HTML templates
router.LoadHTMLGlob(config.Templates.HTMLPath + "/*")
req, err := http.NewRequest("POST",
url,
bytes.NewBufferString(formData.Encode()))
if err != nil {
log.Fatalf("Failed to create new Request: %#v", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
c.Request = req
return c, w
return c, w, router
}
func deleteTestDB(dbPath string) error {
@@ -176,8 +204,8 @@ func deleteTestDB(dbPath string) error {
}
func runSingleTest(tc TestCase) error {
c, w := tc.SetupContext()
tc.RunHandler(c)
c, w, router := tc.SetupContext()
tc.RunHandler(c, router)
if err := tc.ValidateResponse(w); err != nil {
return err