tests
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
@@ -75,8 +76,8 @@ func testUserController(t *testing.T) {
|
||||
}
|
||||
// activate user for login
|
||||
database.DB.Model(&models.User{}).Where("email = ?", "john.doe@example.com").Update("status", constants.ActiveStatus)
|
||||
loginEmail, loginCookie := testLoginHandler(t)
|
||||
logoutCookie := testCurrentUserHandler(t, loginEmail, loginCookie)
|
||||
loginEmail := testLoginHandler(t)
|
||||
testCurrentUserHandler(t, loginEmail)
|
||||
|
||||
// creating a admin cookie
|
||||
c, w, _ := GetMockedJSONContext([]byte(`{
|
||||
@@ -91,28 +92,27 @@ func testUserController(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "Login successful", response["message"])
|
||||
var adminCookie http.Cookie
|
||||
for _, cookie := range w.Result().Cookies() {
|
||||
if cookie.Name == "jwt" {
|
||||
adminCookie = *cookie
|
||||
AdminCookie = cookie
|
||||
|
||||
tokenString := adminCookie.Value
|
||||
tokenString := AdminCookie.Value
|
||||
_, claims, err := middlewares.ExtractContentFrom(tokenString)
|
||||
assert.NoError(t, err, "FAiled getting cookie string")
|
||||
assert.NoError(t, err, "Failed getting cookie string")
|
||||
jwtUserID := uint((*claims)["user_id"].(float64))
|
||||
user, err := Uc.Service.GetUserByID(jwtUserID)
|
||||
assert.NoError(t, err, "FAiled getting cookie string")
|
||||
user, err := Uc.Service.FromID(&jwtUserID)
|
||||
assert.NoError(t, err, "Failed getting cookie string")
|
||||
logger.Error.Printf("ADMIN USER: %#v", user)
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.NotEmpty(t, adminCookie)
|
||||
testUpdateUser(t, loginCookie, adminCookie)
|
||||
testLogoutHandler(t, logoutCookie)
|
||||
testCreatePasswordHandler(t, loginCookie, adminCookie)
|
||||
assert.NotEmpty(t, AdminCookie)
|
||||
testUpdateUser(t)
|
||||
testLogoutHandler(t)
|
||||
testCreatePasswordHandler(t)
|
||||
}
|
||||
|
||||
func testLogoutHandler(t *testing.T, loginCookie http.Cookie) {
|
||||
func testLogoutHandler(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -122,7 +122,7 @@ func testLogoutHandler(t *testing.T, loginCookie http.Cookie) {
|
||||
{
|
||||
name: "Logout with valid cookie",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
},
|
||||
@@ -180,9 +180,8 @@ func testLogoutHandler(t *testing.T, loginCookie http.Cookie) {
|
||||
}
|
||||
}
|
||||
|
||||
func testLoginHandler(t *testing.T) (string, http.Cookie) {
|
||||
func testLoginHandler(t *testing.T) string {
|
||||
// This test should run after the user registration test
|
||||
var loginCookie http.Cookie
|
||||
var loginInput loginInput
|
||||
t.Run("LoginHandler", func(t *testing.T) {
|
||||
// Test cases
|
||||
@@ -244,7 +243,7 @@ func testLoginHandler(t *testing.T) (string, http.Cookie) {
|
||||
assert.Equal(t, "Login successful", response["message"])
|
||||
for _, cookie := range w.Result().Cookies() {
|
||||
if cookie.Name == "jwt" {
|
||||
loginCookie = *cookie
|
||||
MemberCookie = cookie
|
||||
|
||||
// tokenString := loginCookie.Value
|
||||
// _, claims, err := middlewares.ExtractContentFrom(tokenString)
|
||||
@@ -260,7 +259,7 @@ func testLoginHandler(t *testing.T) (string, http.Cookie) {
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.NotEmpty(t, loginCookie)
|
||||
assert.NotEmpty(t, MemberCookie)
|
||||
} else {
|
||||
assert.Contains(t, response, "errors")
|
||||
assert.NotEmpty(t, response["errors"])
|
||||
@@ -269,10 +268,10 @@ func testLoginHandler(t *testing.T) (string, http.Cookie) {
|
||||
|
||||
}
|
||||
})
|
||||
return loginInput.Email, loginCookie
|
||||
return loginInput.Email
|
||||
}
|
||||
|
||||
func testCurrentUserHandler(t *testing.T, loginEmail string, loginCookie http.Cookie) http.Cookie {
|
||||
func testCurrentUserHandler(t *testing.T, loginEmail string) http.Cookie {
|
||||
// This test should run after the user login test
|
||||
invalidCookie := http.Cookie{
|
||||
Name: "jwt",
|
||||
@@ -289,7 +288,7 @@ func testCurrentUserHandler(t *testing.T, loginEmail string, loginCookie http.Co
|
||||
{
|
||||
name: "With valid cookie",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
expectedUserMail: loginEmail,
|
||||
expectedStatus: http.StatusOK,
|
||||
@@ -369,7 +368,7 @@ func testCurrentUserHandler(t *testing.T, loginEmail string, loginCookie http.Co
|
||||
}
|
||||
if tt.expectNewCookie {
|
||||
assert.NotNil(t, newCookie, "New cookie should be set for expired token")
|
||||
assert.NotEqual(t, loginCookie.Value, newCookie.Value, "Cookie value should be different")
|
||||
assert.NotEqual(t, MemberCookie.Value, newCookie.Value, "Cookie value should be different")
|
||||
assert.True(t, newCookie.MaxAge > 0, "New cookie should not be expired")
|
||||
} else {
|
||||
assert.Nil(t, newCookie, "No new cookie should be set for non-expired token")
|
||||
@@ -395,7 +394,7 @@ func testCurrentUserHandler(t *testing.T, loginEmail string, loginCookie http.Co
|
||||
|
||||
})
|
||||
}
|
||||
return loginCookie
|
||||
return *MemberCookie
|
||||
}
|
||||
|
||||
func validateUser(assert bool, wantDBData map[string]interface{}) error {
|
||||
@@ -420,6 +419,11 @@ func validateUser(assert bool, wantDBData map[string]interface{}) error {
|
||||
return fmt.Errorf("Mandate reference is invalid. Expected: %s, Got: %s", expected, user.BankAccount.MandateReference)
|
||||
}
|
||||
|
||||
// Supoorter don't get mails
|
||||
if user.IsSupporter() {
|
||||
return nil
|
||||
}
|
||||
|
||||
//check for email delivery
|
||||
messages := utils.SMTPGetMessages()
|
||||
for _, message := range messages {
|
||||
@@ -454,18 +458,18 @@ func validateUser(assert bool, wantDBData map[string]interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cookie) {
|
||||
func testUpdateUser(t *testing.T) {
|
||||
|
||||
invalidCookie := http.Cookie{
|
||||
Name: "jwt",
|
||||
Value: "invalid.token.here",
|
||||
}
|
||||
// Get the user we just created
|
||||
users, err := Uc.Service.GetUsers(map[string]interface{}{"email": "john.doe@example.com"})
|
||||
if err != nil || len(*users) == 0 {
|
||||
johnsMail := "john.doe@example.com"
|
||||
user, err := Uc.Service.FromEmail(&johnsMail)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get test user: %v", err)
|
||||
}
|
||||
user := (*users)[0]
|
||||
if user.Licence == nil {
|
||||
user.Licence = &models.Licence{
|
||||
Number: "Z021AB37X13",
|
||||
@@ -485,7 +489,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "Valid Admin Update",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&adminCookie)
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.Password = ""
|
||||
@@ -514,7 +518,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "Invalid Email Update",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.Password = ""
|
||||
@@ -532,7 +536,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "admin may change licence number",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&adminCookie)
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.Password = ""
|
||||
@@ -546,7 +550,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "Change phone number",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.Password = ""
|
||||
@@ -560,7 +564,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "Add category",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.Password = ""
|
||||
@@ -578,7 +582,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "Delete 1 and add 1 category",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.Password = ""
|
||||
@@ -597,7 +601,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "Delete 1 category",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.Password = ""
|
||||
@@ -615,7 +619,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "Delete all categories",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.Password = ""
|
||||
@@ -630,7 +634,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "User ID mismatch while not admin",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.Password = ""
|
||||
@@ -649,7 +653,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "Password Update low entropy should fail",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.FirstName = "John Updated"
|
||||
@@ -666,7 +670,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "Password Update",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.FirstName = "John Updated"
|
||||
@@ -687,7 +691,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "Admin Password Update",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&adminCookie)
|
||||
req.AddCookie(AdminCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.LastName = "Doe Updated"
|
||||
@@ -700,7 +704,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
{
|
||||
name: "Non-existent User",
|
||||
setupCookie: func(req *http.Request) {
|
||||
req.AddCookie(&loginCookie)
|
||||
req.AddCookie(MemberCookie)
|
||||
},
|
||||
updateFunc: func(u *models.User) {
|
||||
u.Password = ""
|
||||
@@ -719,7 +723,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
logger.Error.Print("==============================================================")
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Create a copy of the user and apply the updates
|
||||
updatedUser := user
|
||||
updatedUser := *user
|
||||
// logger.Error.Printf("users licence to be updated: %+v", user.Licence)
|
||||
tt.updateFunc(&updatedUser)
|
||||
|
||||
@@ -784,7 +788,7 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
assert.Equal(t, "User updated successfully", message)
|
||||
|
||||
// Verify the update in the database
|
||||
updatedUserFromDB, err := Uc.Service.GetUserByID(user.ID)
|
||||
updatedUserFromDB, err := Uc.Service.FromID(&user.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
if updatedUser.Password == "" {
|
||||
@@ -823,11 +827,17 @@ func testUpdateUser(t *testing.T, loginCookie http.Cookie, adminCookie http.Cook
|
||||
assert.Equal(t, updatedUser.Membership.SubscriptionModelID, updatedUserFromDB.Membership.SubscriptionModelID, "Membership.SubscriptionModelID mismatch")
|
||||
assert.Equal(t, updatedUser.Membership.ParentMembershipID, updatedUserFromDB.Membership.ParentMembershipID, "Membership.ParentMembershipID mismatch")
|
||||
|
||||
assert.Equal(t, updatedUser.Licence.Status, updatedUserFromDB.Licence.Status, "Licence.Status mismatch")
|
||||
assert.Equal(t, updatedUser.Licence.Number, updatedUserFromDB.Licence.Number, "Licence.Number mismatch")
|
||||
assert.Equal(t, updatedUser.Licence.IssuedDate, updatedUserFromDB.Licence.IssuedDate, "Licence.IssuedDate mismatch")
|
||||
assert.Equal(t, updatedUser.Licence.ExpirationDate, updatedUserFromDB.Licence.ExpirationDate, "Licence.ExpirationDate mismatch")
|
||||
assert.Equal(t, updatedUser.Licence.IssuingCountry, updatedUserFromDB.Licence.IssuingCountry, "Licence.IssuingCountry mismatch")
|
||||
if updatedUser.Licence == nil {
|
||||
assert.Nil(t, updatedUserFromDB.Licence, "database licence of user is not nil, but user.licence is nil")
|
||||
} else {
|
||||
logger.Error.Printf("updatedUser licence: %#v", updatedUser.Licence)
|
||||
logger.Error.Printf("dbUser licence: %#v", updatedUserFromDB.Licence)
|
||||
assert.Equal(t, updatedUser.Licence.Status, updatedUserFromDB.Licence.Status, "Licence.Status mismatch")
|
||||
assert.Equal(t, updatedUser.Licence.Number, updatedUserFromDB.Licence.Number, "Licence.Number mismatch")
|
||||
assert.Equal(t, updatedUser.Licence.IssuedDate, updatedUserFromDB.Licence.IssuedDate, "Licence.IssuedDate mismatch")
|
||||
assert.Equal(t, updatedUser.Licence.ExpirationDate, updatedUserFromDB.Licence.ExpirationDate, "Licence.ExpirationDate mismatch")
|
||||
assert.Equal(t, updatedUser.Licence.IssuingCountry, updatedUserFromDB.Licence.IssuingCountry, "Licence.IssuingCountry mismatch")
|
||||
}
|
||||
|
||||
// For slices or more complex nested structures, you might want to use deep equality checks
|
||||
assert.ElementsMatch(t, updatedUser.Consents, updatedUserFromDB.Consents, "Consents mismatch")
|
||||
@@ -936,15 +946,19 @@ func checkVerificationMail(message *utils.Email, user *models.User) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error parsing verification URL: %#v", err.Error())
|
||||
}
|
||||
if !strings.Contains(verificationURL, user.Verification.VerificationToken) {
|
||||
return fmt.Errorf("Users Verification link token(%v) has not been rendered in email verification mail. %v", user.Verification.VerificationToken, verificationURL)
|
||||
v, err := user.GetVerification(constants.VerificationTypes.Email)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error getting verification token: %v", err.Error())
|
||||
}
|
||||
if !strings.Contains(verificationURL, v.VerificationToken) {
|
||||
return fmt.Errorf("Users Verification link token(%v) has not been rendered in email verification mail. %v", v.VerificationToken, verificationURL)
|
||||
}
|
||||
|
||||
if !strings.Contains(message.Body, config.Site.BaseURL) {
|
||||
return fmt.Errorf("Base Url (%v) has not been rendered in email verification mail.", config.Site.BaseURL)
|
||||
}
|
||||
// open the provided link:
|
||||
if err := verifyMail(verificationURL); err != nil {
|
||||
if err := verifyMail(verificationURL, user.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
messages := utils.SMTPGetMessages()
|
||||
@@ -961,12 +975,14 @@ func checkVerificationMail(message *utils.Email, user *models.User) error {
|
||||
|
||||
}
|
||||
|
||||
func verifyMail(verificationURL string) error {
|
||||
func verifyMail(verificationURL string, user_id uint) error {
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
router.LoadHTMLGlob(filepath.Join(config.Templates.HTMLPath, "*"))
|
||||
|
||||
router.GET("api/users/verify", Uc.VerifyMailHandler)
|
||||
expectedUrl := fmt.Sprintf("/api/users/verify/%v", user_id)
|
||||
log.Printf("Expected URL: %v", expectedUrl)
|
||||
router.GET("/api/users/verify/:id", Uc.VerifyMailHandler)
|
||||
wv := httptest.NewRecorder()
|
||||
cv, _ := gin.CreateTestContext(wv)
|
||||
var err error
|
||||
@@ -1109,8 +1125,9 @@ func getTestUsers() []RegisterUserTest {
|
||||
Assert: false,
|
||||
Input: GenerateInputJSON(customizeInput(func(user models.User) models.User {
|
||||
user.BankAccount.IBAN = "DE1234234123134"
|
||||
user.RoleID = 0
|
||||
user.RoleID = constants.Roles.Supporter
|
||||
user.Email = "john.supporter@example.com"
|
||||
user.Membership.SubscriptionModel.Name = constants.SupporterSubscriptionModelName
|
||||
return user
|
||||
})),
|
||||
},
|
||||
@@ -1121,8 +1138,9 @@ func getTestUsers() []RegisterUserTest {
|
||||
Assert: true,
|
||||
Input: GenerateInputJSON(customizeInput(func(user models.User) models.User {
|
||||
user.BankAccount.IBAN = ""
|
||||
user.RoleID = 0
|
||||
user.RoleID = constants.Roles.Supporter
|
||||
user.Email = "john.supporter@example.com"
|
||||
user.Membership.SubscriptionModel.Name = constants.SupporterSubscriptionModelName
|
||||
return user
|
||||
})),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user