integration_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package integration
  4. import (
  5. "bytes"
  6. "log/slog"
  7. "net/http"
  8. "net/http/httptest"
  9. "strings"
  10. "testing"
  11. "miniflux.app/v2/internal/config"
  12. "miniflux.app/v2/internal/model"
  13. )
  14. func TestSendEntryLogsLinkwardenCollectionID(t *testing.T) {
  15. var buf bytes.Buffer
  16. handler := slog.NewJSONHandler(&buf, nil)
  17. logger := slog.New(handler)
  18. prev := slog.Default()
  19. slog.SetDefault(logger)
  20. defer slog.SetDefault(prev)
  21. entry := &model.Entry{ID: 52, URL: "https://example.org/test.html", Title: "Test"}
  22. coll := int64(12345)
  23. userIntegrations := &model.Integration{
  24. UserID: 1,
  25. LinkwardenEnabled: true,
  26. LinkwardenCollectionID: &coll,
  27. LinkwardenURL: "",
  28. LinkwardenAPIKey: "",
  29. }
  30. SendEntry(entry, userIntegrations)
  31. out := buf.String()
  32. if !strings.Contains(out, `"collection_id":12345`) {
  33. t.Fatalf("expected collection_id in logs; got: %s", out)
  34. }
  35. }
  36. func TestSendEntryLogsLinkwardenWithoutCollectionID(t *testing.T) {
  37. var buf bytes.Buffer
  38. handler := slog.NewJSONHandler(&buf, nil)
  39. logger := slog.New(handler)
  40. prev := slog.Default()
  41. slog.SetDefault(logger)
  42. defer slog.SetDefault(prev)
  43. entry := &model.Entry{ID: 52, URL: "https://example.org/test.html", Title: "Test"}
  44. userIntegrations := &model.Integration{
  45. UserID: 1,
  46. LinkwardenEnabled: true,
  47. LinkwardenURL: "",
  48. LinkwardenAPIKey: "",
  49. }
  50. SendEntry(entry, userIntegrations)
  51. out := buf.String()
  52. if strings.Contains(out, "collection_id") {
  53. t.Fatalf("did not expect collection_id in logs; got: %s", out)
  54. }
  55. }
  56. func TestSendEntryDoesNotLogWebhookURL(t *testing.T) {
  57. configureIntegrationAllowPrivateNetworksOption(t)
  58. logs := captureDebugLogs(t)
  59. webhookURL := newFailingWebhookURL(t)
  60. entry := &model.Entry{
  61. ID: 52,
  62. URL: "https://example.org/test.html",
  63. Title: "Test",
  64. Feed: &model.Feed{ID: 1, Category: &model.Category{ID: 1}},
  65. }
  66. userIntegrations := &model.Integration{UserID: 1, WebhookEnabled: true, WebhookURL: webhookURL}
  67. SendEntry(entry, userIntegrations)
  68. assertLogsDoNotContainWebhookURL(t, logs, "Unable to send entry to Webhook")
  69. }
  70. func TestPushEntriesDoesNotLogWebhookURL(t *testing.T) {
  71. configureIntegrationAllowPrivateNetworksOption(t)
  72. logs := captureDebugLogs(t)
  73. webhookURL := newFailingWebhookURL(t)
  74. feed := &model.Feed{ID: 1, Category: &model.Category{ID: 1}, WebhookURL: webhookURL}
  75. entries := model.Entries{&model.Entry{ID: 52, URL: "https://example.org/test.html", Title: "Test"}}
  76. userIntegrations := &model.Integration{UserID: 1, WebhookEnabled: true}
  77. PushEntries(feed, entries, userIntegrations)
  78. assertLogsDoNotContainWebhookURL(t, logs, "Unable to send new entries to Webhook")
  79. }
  80. // newFailingWebhookURL returns a webhook URL with secrets in its userinfo, path and query, served by a failing server.
  81. func newFailingWebhookURL(t *testing.T) string {
  82. t.Helper()
  83. server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
  84. writer.WriteHeader(http.StatusInternalServerError)
  85. }))
  86. t.Cleanup(server.Close)
  87. return strings.Replace(server.URL, "://", "://user:secret-password@", 1) + "/api/webhook/secret-id?token=secret-token"
  88. }
  89. func assertLogsDoNotContainWebhookURL(t *testing.T, logs *bytes.Buffer, expectedMessage string) {
  90. t.Helper()
  91. if !strings.Contains(logs.String(), expectedMessage) {
  92. t.Fatalf("expected %q in logs; got: %s", expectedMessage, logs.String())
  93. }
  94. for _, secret := range []string{"secret-password", "secret-id", "secret-token"} {
  95. if strings.Contains(logs.String(), secret) {
  96. t.Errorf("expected logs to not contain %q; got: %s", secret, logs.String())
  97. }
  98. }
  99. }
  100. func captureDebugLogs(t *testing.T) *bytes.Buffer {
  101. t.Helper()
  102. var logs bytes.Buffer
  103. previousLogger := slog.Default()
  104. slog.SetDefault(slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug})))
  105. t.Cleanup(func() { slog.SetDefault(previousLogger) })
  106. return &logs
  107. }
  108. func configureIntegrationAllowPrivateNetworksOption(t *testing.T) {
  109. t.Helper()
  110. t.Setenv("INTEGRATION_ALLOW_PRIVATE_NETWORKS", "1")
  111. parsedOptions, err := config.NewConfigParser().ParseEnvironmentVariables()
  112. if err != nil {
  113. t.Fatalf("Unable to configure test options: %v", err)
  114. }
  115. previousOptions := config.Opts
  116. config.Opts = parsedOptions
  117. t.Cleanup(func() {
  118. config.Opts = previousOptions
  119. })
  120. }