Fix profile refresh identity compatibility
This commit is contained in:
78
backend/internal/handler/auth_current_user_test.go
Normal file
78
backend/internal/handler/auth_current_user_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
//go:build unit
|
||||
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAuthHandlerGetCurrentUserReturnsProfileCompatibilityFields(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
verifiedAt := time.Date(2026, 4, 20, 8, 30, 0, 0, time.UTC)
|
||||
repo := &userHandlerRepoStub{
|
||||
user: &service.User{
|
||||
ID: 31,
|
||||
Email: "me@example.com",
|
||||
Username: "linuxdo-handle",
|
||||
Role: service.RoleUser,
|
||||
Status: service.StatusActive,
|
||||
AvatarURL: "https://cdn.example.com/linuxdo.png",
|
||||
AvatarSource: "remote_url",
|
||||
},
|
||||
identities: []service.UserAuthIdentityRecord{
|
||||
{
|
||||
ProviderType: "linuxdo",
|
||||
ProviderKey: "linuxdo",
|
||||
ProviderSubject: "linuxdo-subject-31",
|
||||
VerifiedAt: &verifiedAt,
|
||||
Metadata: map[string]any{
|
||||
"username": "linuxdo-handle",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
handler := &AuthHandler{
|
||||
userService: service.NewUserService(repo, nil, nil, nil),
|
||||
}
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(recorder)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/auth/me", nil)
|
||||
c.Set(string(middleware2.ContextKeyUser), middleware2.AuthSubject{UserID: 31})
|
||||
|
||||
handler.GetCurrentUser(c)
|
||||
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
var resp struct {
|
||||
Code int `json:"code"`
|
||||
Data map[string]any `json:"data"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &resp))
|
||||
require.Equal(t, 0, resp.Code)
|
||||
require.Equal(t, true, resp.Data["email_bound"])
|
||||
require.Equal(t, true, resp.Data["linuxdo_bound"])
|
||||
require.Equal(t, "https://cdn.example.com/linuxdo.png", resp.Data["avatar_url"])
|
||||
|
||||
authBindings, ok := resp.Data["auth_bindings"].(map[string]any)
|
||||
require.True(t, ok)
|
||||
linuxdoBinding, ok := authBindings["linuxdo"].(map[string]any)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, true, linuxdoBinding["bound"])
|
||||
|
||||
_, hasAvatarSource := resp.Data["avatar_source"]
|
||||
require.False(t, hasAvatarSource)
|
||||
_, hasProfileSources := resp.Data["profile_sources"]
|
||||
require.False(t, hasProfileSources)
|
||||
}
|
||||
Reference in New Issue
Block a user