59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package common
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
|
)
|
|
|
|
func TestFilterFieldsKeepsOnlyAllowedKeys(t *testing.T) {
|
|
got := FilterFields(map[string]any{"name": "n", "password_hash": "x"}, []string{"name"})
|
|
if len(got) != 1 || got["name"] != "n" {
|
|
t.Fatalf("unexpected filtered fields: %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestResourceResponseStripsInternalIDsRecursively(t *testing.T) {
|
|
got := ResourceResponse(map[string]any{
|
|
"id": uint64(1), "identity": "root",
|
|
"child": map[string]any{"gasorder_basic_id": uint64(2), "identity": "child"},
|
|
}).(map[string]any)
|
|
if _, exists := got["id"]; exists {
|
|
t.Fatal("root database ID was exposed")
|
|
}
|
|
child := got["child"].(map[string]any)
|
|
if _, exists := child["gasorder_basic_id"]; exists {
|
|
t.Fatal("relation database ID was exposed")
|
|
}
|
|
}
|
|
|
|
func TestPublicFieldProtectionMasksGasorderContacts(t *testing.T) {
|
|
value := map[string]any{"contact_name": "张三", "contact_phone": "13800138000"}
|
|
ProtectPublicFields(value, false, false, false)
|
|
if _, exists := value["contact_name"]; exists {
|
|
t.Fatal("contact name remains public")
|
|
}
|
|
if _, exists := value["contact_phone"]; exists {
|
|
t.Fatal("contact phone remains public")
|
|
}
|
|
}
|
|
|
|
func TestResourceRelationOptionalEmptyIdentityClearsRelation(t *testing.T) {
|
|
values, err := ResolveResourceRelations(
|
|
map[string]any{"warehouse_identity": ""},
|
|
nil,
|
|
[]ResourceRelation{{Input: "warehouse_identity", Column: "warehouse_id", Model: &models.ProductWarehouse{}}},
|
|
false,
|
|
)
|
|
if err != nil || values["warehouse_id"] != uint64(0) {
|
|
t.Fatalf("optional relation clear = (%#v, %v)", values, err)
|
|
}
|
|
}
|
|
|
|
func TestCommonMethodModesRemainHTTPCompatible(t *testing.T) {
|
|
if http.MethodGet == "" || http.MethodPost == "" {
|
|
t.Fatal("standard HTTP methods unavailable")
|
|
}
|
|
}
|