package lkgateway import ( "context" "errors" "fmt" "net/http" "net/http/httptest" "net/url" "os" "path/filepath" "strings" "testing" "time" "git.zetit.ru/zuevav/Bridge-and-Join-s/internal/license" "git.zetit.ru/zuevav/Bridge-and-Join-s/internal/release" ) func TestAccPrefix(t *testing.T) { for in, want := range map[string]string{"ВТНКЦ": "ВТНКЦ", " БР ": "БР", "AB_1": "AB_1", "a'b": "", "x%": "", "": ""} { if got := cleanAccPrefix(in); got != want { t.Errorf("cleanAccPrefix(%q)=%q, want %q", in, got, want) } } if q := accSQL("LIKE N'{ACC}%'"); !strings.Contains(q, "N'ВТНКЦ%'") { t.Errorf("accSQL по умолчанию: %s", q) } } func TestDoneNSDUnknownProfile(t *testing.T) { var s Settings s.NSD.IGWBaseURL, s.NSD.Profile = "http://localhost:8090", "prod" if doneNSD(s) { t.Fatal("профиль prod неизвестен bj — шаг не должен считаться выполненным") } s.NSD.Profile = "prod-gost" if !doneNSD(s) { t.Fatal("prod-gost — известный профиль") } } func TestStoreCertKindAndExpiry(t *testing.T) { bundle := map[string]bool{} own := NSDStoreCert{Kind: "x509", Subject: "O=Предприятие 1046,CN=Сотрудник 1046,INN=078000001046", SubjectCN: "Сотрудник 1046", IssuerCN: "Тестовый УЦ", Serial: "AA01"} nsd := NSDStoreCert{Kind: "x509", Subject: "O=НКО АО НРД,CN=Петров,INN=007702165310", SubjectCN: "Петров", IssuerCN: "Тестовый УЦ", Serial: "BB02"} ca := NSDStoreCert{Kind: "x509", SubjectCN: "Тестовый УЦ", SelfSigned: true} for c, want := range map[*NSDStoreCert]string{&own: "own", &nsd: "counterpart", &ca: "ca"} { if got := storeCertKind(*c, bundle); got != want { t.Errorf("%s: %s, want %s", c.SubjectCN, got, want) } } if subjectINN(nsd.Subject) != "7702165310" { t.Errorf("subjectINN: %q", subjectINN(nsd.Subject)) } // уведомления: за 20 дней — одно (отметка 30), повторно не шлётся; за 5 дней — следующее (7) dir := t.TempDir() certExpiry.file = filepath.Join(dir, "cert-expiry.json") rc, err := NewRuntimeConfig(filepath.Join(dir, "setup.json")) if err != nil { t.Fatal(err) } own.NotAfter = time.Now().AddDate(0, 0, 20).Format("02.01.2006") st := rc.Snapshot().NSDCerts st.LastStore = []NSDStoreCert{own, nsd, ca} _ = rc.UpdateNSDCerts(st) count := func() int { return len(rc.Snapshot().News.Items) } certExpiryTick(rc) n1 := count() certExpiryTick(rc) if n1 != 1 || count() != 1 { t.Fatalf("за 20 дней: ожидалось одно уведомление, было %d, затем %d", n1, count()) } own.NotAfter = time.Now().AddDate(0, 0, 5).Format("02.01.2006") st.LastStore = []NSDStoreCert{own} _ = rc.UpdateNSDCerts(st) certExpiryTick(rc) if count() != 2 { t.Fatalf("за 5 дней: ожидалось второе уведомление, всего %d", count()) } raw, _ := os.ReadFile(certExpiry.file) if !strings.Contains(string(raw), "7") { t.Fatalf("отметка 7 не сохранена: %s", raw) } } func TestSafeReturnPathKeepsStep(t *testing.T) { for in, want := range map[string]string{ "/admin/wizard?step=4": "/admin/wizard?step=4", "/admin/wizard?step=3&sub=done&flash=x": "/admin/wizard?step=3&sub=done", "/admin/setup#sec-org": "/admin/setup", "/admin/wizard?step=4%0d%0aX": "/admin/wizard", "//evil.example/admin": "", "/admin/x?a=