Repair multiline PEM inside private_key before Firebase init; add unit test; use normalized JSON for credentials. Co-authored-by: Cursor <cursoragent@cursor.com>
41 lines
1014 B
Go
41 lines
1014 B
Go
package notificationservice
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNormalizeFCMCredentialsJSON_multilinePrivateKey(t *testing.T) {
|
|
raw := `{` +
|
|
`"type":"service_account",` +
|
|
`"project_id":"my-proj",` +
|
|
`"private_key":"-----BEGIN PRIVATE KEY-----` +
|
|
`\nLINETWO\n` +
|
|
`-----END PRIVATE KEY-----\n",` +
|
|
`"client_email":"x@some.iam.gserviceaccount.com"` +
|
|
`}`
|
|
raw = strings.ReplaceAll(raw, "\\n", "\n")
|
|
|
|
var broken map[string]any
|
|
if err := json.Unmarshal([]byte(raw), &broken); err == nil {
|
|
t.Fatal("expected broken JSON fixture to fail raw parse")
|
|
}
|
|
|
|
fixed, err := normalizeFCMCredentialsJSON(raw)
|
|
if err != nil {
|
|
t.Fatalf("normalize: %v", err)
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal(fixed, &m); err != nil {
|
|
t.Fatalf("unmarshal normalized: %v", err)
|
|
}
|
|
if got := m["project_id"]; got != "my-proj" {
|
|
t.Fatalf("project_id: %v", got)
|
|
}
|
|
pk, _ := m["private_key"].(string)
|
|
if !strings.Contains(pk, "LINETWO") {
|
|
t.Fatalf("private_key body missing")
|
|
}
|
|
}
|