package api_test import ( "context" "io" "net/http" "net/http/httptest" "strings" "sync" "testing" "github.com/jackc/pgx/v5" "git.zb-server.de/ZB-Server/uptimesebi/internal/i18n" "git.zb-server.de/ZB-Server/uptimesebi/internal/notify" ) func TestProvidersAreTranslated(t *testing.T) { catalog, err := i18n.Load() if err != nil { t.Fatal(err) } for _, lang := range catalog.Supported() { ctx := i18n.NewContext(context.Background(), catalog.Match(lang)) for _, p := range notify.Providers { ids := []string{"channels.type_" + p.Type} for _, f := range p.Fields { ids = append(ids, "channels.field_"+f.Name) } for _, id := range ids { if i18n.T(ctx, id) == id { t.Errorf("%s: %s is not translated", lang, id) } } } } } func TestProviderChannel(t *testing.T) { e := newEnv(t) var mu sync.Mutex var paths, bodies []string telegram := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { b, _ := io.ReadAll(r.Body) mu.Lock() paths, bodies = append(paths, r.URL.Path), append(bodies, string(b)) mu.Unlock() })) defer telegram.Close() owner := e.browser(t) owner.signup("o@"+uniq("pr")+".test", uniq("pr-")) st, body := owner.do("POST", "/api/v1/org/channels", map[string]any{"name": "Ops chat", "type": "telegram", "settings": map[string]string{"chat_id": "-100"}}) expect(t, "missing bot token", st, body, http.StatusUnprocessableEntity, "invalid_channel_settings") st, body = owner.do("POST", "/api/v1/org/channels", map[string]any{"name": "x", "type": "carrierpigeon"}) expect(t, "unknown type", st, body, http.StatusUnprocessableEntity, "invalid_input") st, body = owner.do("POST", "/api/v1/org/channels", map[string]any{ "name": "Ops chat", "type": "telegram", "locale": "de", "settings": map[string]string{"bot_token": "4711:very-secret", "chat_id": "-100123", "api_url": telegram.URL}, }) if st != http.StatusCreated { t.Fatalf("create telegram channel = %d %v", st, body) } ch := body["channel"].(map[string]any) id := ch["id"].(string) if raw := mustJSON(body); strings.Contains(raw, "very-secret") || ch["settings"].(map[string]any)["chat_id"] != "-100123" || mustJSON(ch["secrets_set"]) != `["bot_token"]` { t.Errorf("create answer = %s", raw) } var stored string _ = e.asSystem(t, func(tx pgx.Tx) error { return tx.QueryRow(context.Background(), `SELECT config::text FROM notification_channels WHERE id = $1`, id).Scan(&stored) }) if strings.Contains(stored, "very-secret") { t.Error("the bot token is stored in plain text") } if _, list := owner.do("GET", "/api/v1/org/channels", nil); strings.Contains(mustJSON(list), "very-secret") { t.Error("the channel list shows the bot token") } if st, body := owner.do("POST", "/api/v1/org/channels/"+id+"/test", nil); st != http.StatusNoContent { t.Fatalf("test send = %d %v", st, body) } // Saving without the token keeps it. st, body = owner.do("PUT", "/api/v1/org/channels/"+id, map[string]any{ "name": "Ops chat", "type": "telegram", "locale": "de", "settings": map[string]string{"chat_id": "-100999", "api_url": telegram.URL}, }) if st != http.StatusOK { t.Fatalf("update = %d %v", st, body) } owner.do("POST", "/api/v1/org/channels/"+id+"/test", nil) mu.Lock() defer mu.Unlock() if len(paths) != 2 || paths[0] != "/bot4711:very-secret/sendMessage" || paths[1] != paths[0] { t.Errorf("telegram requests = %v", paths) } if !strings.Contains(bodies[0], "Test von UptimeSebi") || !strings.Contains(bodies[1], `"chat_id":"-100999"`) { t.Errorf("telegram bodies = %v", bodies) } if _, _, page := owner.page("/settings/channels"); !strings.Contains(page, `name="settings.bot_token"`) || !strings.Contains(page, "-100999") { t.Error("the channels page lacks the service form or the channel") } }