package model import ( "encoding/json" "testing" "git.sendico.io/tech/sxchnge/bff/pkg/person" "go.mongodb.org/mongo-driver/v2/bson" ) func TestPaymentMethodMarshalRussianBankAccountUsesJSONRecipientNames(t *testing.T) { raw, err := bson.Marshal(RussianBankAccountPaymentData{ Recipient: person.Name{ FirstName: "Success", LastName: "Success", }, Inn: "7707083893", Kpp: "770701001", BankName: "AsterBank Test Bank", Bik: "044525225", AccountNumber: "40702810000000080001", CorrespondentAccount: "30101810400000000225", }) if err != nil { t.Fatalf("marshal bank account data: %v", err) } payload, err := json.Marshal(PaymentMethod{ Type: PaymentTypeBankAccount, Data: raw, }) if err != nil { t.Fatalf("marshal payment method: %v", err) } var decoded struct { Type PaymentType `json:"type"` Data map[string]any `json:"data"` } if err := json.Unmarshal(payload, &decoded); err != nil { t.Fatalf("unmarshal payload: %v", err) } if decoded.Type != PaymentTypeBankAccount { t.Fatalf("type mismatch: got=%s want=%s", decoded.Type, PaymentTypeBankAccount) } recipient, ok := decoded.Data["recipient"].(map[string]any) if !ok { t.Fatalf("recipient is not an object: %#v", decoded.Data["recipient"]) } if got, want := recipient["first_name"], "Success"; got != want { t.Fatalf("recipient.first_name mismatch: got=%#v want=%q", got, want) } if got, want := recipient["last_name"], "Success"; got != want { t.Fatalf("recipient.last_name mismatch: got=%#v want=%q", got, want) } if _, ok := recipient["firstName"]; ok { t.Fatalf("recipient must not expose BSON firstName: %#v", recipient) } if _, ok := recipient["lastName"]; ok { t.Fatalf("recipient must not expose BSON lastName: %#v", recipient) } } func TestPaymentMethodUnmarshalRussianBankAccountStoresBSONRecipientNames(t *testing.T) { payload := []byte(`{ "type": "bankAccount", "data": { "recipient": { "first_name": "Success", "last_name": "Success" }, "inn": "7707083893", "kpp": "770701001", "bankName": "AsterBank Test Bank", "bik": "044525225", "accountNumber": "40702810000000080001", "correspondentAccount": "30101810400000000225" } }`) var method PaymentMethod if err := json.Unmarshal(payload, &method); err != nil { t.Fatalf("unmarshal payment method: %v", err) } var stored bson.M if err := bson.Unmarshal(method.Data, &stored); err != nil { t.Fatalf("unmarshal stored data: %v", err) } recipient, ok := bsonDocumentMap(stored["recipient"]) if !ok { t.Fatalf("stored recipient is not an object: %#v", stored["recipient"]) } if got, want := recipient["firstName"], "Success"; got != want { t.Fatalf("stored recipient.firstName mismatch: got=%#v want=%q", got, want) } if got, want := recipient["lastName"], "Success"; got != want { t.Fatalf("stored recipient.lastName mismatch: got=%#v want=%q", got, want) } if _, ok := recipient["first_name"]; ok { t.Fatalf("stored recipient must not use JSON first_name: %#v", recipient) } if _, ok := recipient["last_name"]; ok { t.Fatalf("stored recipient must not use JSON last_name: %#v", recipient) } } func bsonDocumentMap(value any) (map[string]any, bool) { switch typed := value.(type) { case bson.M: return typed, true case bson.D: out := make(map[string]any, len(typed)) for _, element := range typed { out[element.Key] = element.Value } return out, true default: return nil, false } }