Files
Mazyar ce8a18aa8b
continuous-integration/drone/push Build is passing
Add unit tests for UpdateUserForm JSON unmarshalling
- 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.
2026-06-22 22:09:30 +03:30

48 lines
1.4 KiB
Go

package user
import (
"encoding/json"
"testing"
)
func TestUpdateUserForm_UnmarshalJSON_ProfileImage(t *testing.T) {
t.Run("omitted profile_image is not treated as an update", func(t *testing.T) {
var form UpdateUserForm
err := json.Unmarshal([]byte(`{"full_name":"Jane Doe"}`), &form)
if err != nil {
t.Fatalf("unmarshal failed: %v", err)
}
if form.isProfileImageSet() {
t.Fatal("expected profile image to be unset when omitted")
}
})
t.Run("null profile_image clears the image", func(t *testing.T) {
var form UpdateUserForm
err := json.Unmarshal([]byte(`{"profile_image":null}`), &form)
if err != nil {
t.Fatalf("unmarshal failed: %v", err)
}
if !form.isProfileImageSet() {
t.Fatal("expected profile image to be marked as set")
}
if form.ProfileImage != nil {
t.Fatalf("expected nil profile image, got %#v", form.ProfileImage)
}
})
t.Run("profile image url is preserved", func(t *testing.T) {
var form UpdateUserForm
err := json.Unmarshal([]byte(`{"profile_image":"https://example.com/avatar.png"}`), &form)
if err != nil {
t.Fatalf("unmarshal failed: %v", err)
}
if !form.isProfileImageSet() {
t.Fatal("expected profile image to be marked as set")
}
if form.ProfileImage == nil || *form.ProfileImage != "https://example.com/avatar.png" {
t.Fatalf("unexpected profile image: %#v", form.ProfileImage)
}
})
}