96 lines
2.8 KiB
Go

package resource
import (
"net/http/httptest"
"testing"
"hyapp-admin-server/internal/appctx"
"hyapp-admin-server/internal/middleware"
"github.com/gin-gonic/gin"
)
func TestGiftRequestProtoKeepsExplicitCPRelationType(t *testing.T) {
req := giftRequest{
GiftTypeCode: "cp",
CPRelationType: "brother",
PresentationJSON: `{"cp_relation_type":"sister"}`,
}
proto := req.updateProto(newGiftRequestTestContext(), "love")
if proto.GetCpRelationType() != "brother" {
t.Fatalf("explicit cp relation type should win, got %q", proto.GetCpRelationType())
}
}
func TestGiftRequestProtoFallsBackToPresentationCPRelationType(t *testing.T) {
req := giftRequest{
GiftID: "love",
GiftTypeCode: "cp",
PresentationJSON: `{"cp_relation_type":"sister"}`,
}
proto := req.createProto(newGiftRequestTestContext())
if proto.GetCpRelationType() != "sister" {
t.Fatalf("presentation cp relation type should be forwarded, got %q", proto.GetCpRelationType())
}
}
func TestGiftRequestProtoClearsCPRelationTypeForNonCPGift(t *testing.T) {
req := giftRequest{
GiftTypeCode: "normal",
CPRelationType: "brother",
PresentationJSON: `{"cp_relation_type":"brother"}`,
}
proto := req.updateProto(newGiftRequestTestContext(), "rose")
if proto.GetCpRelationType() != "" {
t.Fatalf("non-cp gift should not forward cp relation type, got %q", proto.GetCpRelationType())
}
}
func TestResourceRequestDefaultsManagerGrantDisabled(t *testing.T) {
req := resourceRequest{
ResourceCode: "badge_default_manager_disabled",
ResourceType: resourceTypeBadge,
Name: "Default Manager Disabled Badge",
Status: "active",
}
proto := req.createProto(newGiftRequestTestContext())
if proto.ManagerGrantEnabled == nil || proto.GetManagerGrantEnabled() {
t.Fatalf("manager grant should default to disabled, got %v", proto.GetManagerGrantEnabled())
}
}
func TestResourceRequestKeepsExplicitManagerGrantEnabled(t *testing.T) {
enabled := true
req := resourceRequest{
ResourceCode: "badge_manager_enabled",
ResourceType: resourceTypeBadge,
Name: "Manager Enabled Badge",
Status: "active",
ManagerGrantEnabled: &enabled,
}
proto := req.createProto(newGiftRequestTestContext())
if proto.ManagerGrantEnabled == nil || !proto.GetManagerGrantEnabled() {
t.Fatalf("explicit manager grant enabled should be preserved, got %v", proto.GetManagerGrantEnabled())
}
}
func newGiftRequestTestContext() *gin.Context {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
request := httptest.NewRequest("POST", "/admin/gifts", nil)
c.Request = request.WithContext(appctx.WithContext(request.Context(), "lalu"))
c.Set(middleware.ContextRequestID, "resource-test-request")
return c
}