Add unit tests for UpdateUserForm JSON unmarshalling
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Introduced `form_test.go` to validate the behavior of the `UpdateUserForm` struct's JSON unmarshalling, specifically for the `profile_image` field. - Added tests to ensure omitted `profile_image` is not treated as an update, null values clear the image, and valid URLs are preserved. - Enhanced the `UpdateUserForm` and `UpdateProfileForm` structs to include logic for distinguishing between omitted and explicitly null profile images during JSON unmarshalling. This update improves the robustness of user profile updates by ensuring correct handling of profile image data in JSON requests, enhancing overall data integrity in the user management system.
This commit is contained in:
+66
-6
@@ -1,6 +1,10 @@
|
||||
package user
|
||||
|
||||
import "tm/pkg/response"
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"tm/pkg/response"
|
||||
)
|
||||
|
||||
// User Registration DTOs
|
||||
|
||||
@@ -27,6 +31,46 @@ type UpdateUserForm struct {
|
||||
Position *string `json:"position" valid:"optional,length(2|100)" example:"Lead"` // Updated position
|
||||
Phone *string `json:"phone" valid:"optional,length(10|20)" example:"+18289784438"` // Updated phone number
|
||||
ProfileImage *string `json:"profile_image" valid:"optional,url" example:""` // Updated profile image URL
|
||||
profileImageSet bool
|
||||
}
|
||||
|
||||
func unmarshalProfileImageField(data []byte, profileImage **string, profileImageSet *bool) ([]byte, error) {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
profileImageRaw, ok := raw["profile_image"]
|
||||
if ok {
|
||||
*profileImageSet = true
|
||||
if string(profileImageRaw) == "null" {
|
||||
*profileImage = nil
|
||||
} else {
|
||||
var image string
|
||||
if err := json.Unmarshal(profileImageRaw, &image); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
*profileImage = &image
|
||||
}
|
||||
delete(raw, "profile_image")
|
||||
}
|
||||
|
||||
return json.Marshal(raw)
|
||||
}
|
||||
|
||||
// UnmarshalJSON distinguishes omitted profile_image from explicit null to allow clearing the image.
|
||||
func (f *UpdateUserForm) UnmarshalJSON(data []byte) error {
|
||||
rest, err := unmarshalProfileImageField(data, &f.ProfileImage, &f.profileImageSet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
type alias UpdateUserForm
|
||||
return json.Unmarshal(rest, (*alias)(f))
|
||||
}
|
||||
|
||||
func (f *UpdateUserForm) isProfileImageSet() bool {
|
||||
return f.profileImageSet
|
||||
}
|
||||
|
||||
// User Authentication DTOs
|
||||
@@ -51,11 +95,27 @@ type ChangePasswordForm struct {
|
||||
|
||||
// UpdateProfileForm represents the data for updating user profile information
|
||||
type UpdateProfileForm struct {
|
||||
FullName *string `json:"full_name,omitempty" valid:"optional,length(2|100)" example:"Admin User"` // Updated full name
|
||||
Department *string `json:"department,omitempty" valid:"optional,length(2|100)" example:"Information Technology"` // Updated department
|
||||
Position *string `json:"position,omitempty" valid:"optional,length(2|100)" example:"Lead"` // Updated position
|
||||
Phone *string `json:"phone,omitempty" valid:"optional,length(10|20)" example:"+18289784438"` // Updated phone number
|
||||
ProfileImage *string `json:"profile_image,omitempty" valid:"optional,url" example:""` // Updated profile image URL
|
||||
FullName *string `json:"full_name,omitempty" valid:"optional,length(2|100)" example:"Admin User"` // Updated full name
|
||||
Department *string `json:"department,omitempty" valid:"optional,length(2|100)" example:"Information Technology"` // Updated department
|
||||
Position *string `json:"position,omitempty" valid:"optional,length(2|100)" example:"Lead"` // Updated position
|
||||
Phone *string `json:"phone,omitempty" valid:"optional,length(10|20)" example:"+18289784438"` // Updated phone number
|
||||
ProfileImage *string `json:"profile_image,omitempty" valid:"optional,url" example:""` // Updated profile image URL
|
||||
profileImageSet bool
|
||||
}
|
||||
|
||||
// UnmarshalJSON distinguishes omitted profile_image from explicit null to allow clearing the image.
|
||||
func (f *UpdateProfileForm) UnmarshalJSON(data []byte) error {
|
||||
rest, err := unmarshalProfileImageField(data, &f.ProfileImage, &f.profileImageSet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
type alias UpdateProfileForm
|
||||
return json.Unmarshal(rest, (*alias)(f))
|
||||
}
|
||||
|
||||
func (f *UpdateProfileForm) isProfileImageSet() bool {
|
||||
return f.profileImageSet
|
||||
}
|
||||
|
||||
// ResetPasswordForm represents the email address for password reset
|
||||
|
||||
Reference in New Issue
Block a user