webhook_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package webhook
  4. import (
  5. "net/http"
  6. "net/http/httptest"
  7. "strings"
  8. "testing"
  9. "miniflux.app/v2/internal/config"
  10. "miniflux.app/v2/internal/model"
  11. )
  12. var webhookURLSecrets = []string{"secret-password", "secret-id", "secret-token"}
  13. func secretWebhookURL(serverURL string) string {
  14. return strings.Replace(serverURL, "://", "://user:secret-password@", 1) + "/api/webhook/secret-id?token=secret-token"
  15. }
  16. func TestSendNewEntriesWebhookEventErrorsDoNotContainWebhookURL(t *testing.T) {
  17. configureIntegrationAllowPrivateNetworksOption(t)
  18. closeConnection := func(t *testing.T, writer http.ResponseWriter) {
  19. connection, _, err := writer.(http.Hijacker).Hijack()
  20. if err != nil {
  21. t.Errorf("unable to hijack connection: %v", err)
  22. return
  23. }
  24. connection.Close()
  25. }
  26. scenarios := []struct {
  27. name string
  28. handler func(t *testing.T, writer http.ResponseWriter)
  29. wantErrContains string
  30. }{
  31. {
  32. name: "connection fails",
  33. handler: func(t *testing.T, writer http.ResponseWriter) {
  34. closeConnection(t, writer)
  35. },
  36. wantErrContains: "webhook: unable to send request",
  37. },
  38. {
  39. name: "error status",
  40. handler: func(t *testing.T, writer http.ResponseWriter) {
  41. writer.WriteHeader(http.StatusInternalServerError)
  42. },
  43. wantErrContains: "webhook: incorrect response status code 500",
  44. },
  45. }
  46. for _, scenario := range scenarios {
  47. t.Run(scenario.name, func(t *testing.T) {
  48. server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
  49. scenario.handler(t, writer)
  50. }))
  51. defer server.Close()
  52. feed := &model.Feed{ID: 1, Title: "Feed", Category: &model.Category{ID: 1, Title: "Category"}}
  53. entries := model.Entries{&model.Entry{ID: 1, Title: "Entry", URL: "https://example.org/entry"}}
  54. err := NewClient(secretWebhookURL(server.URL), "signing-secret").SendNewEntriesWebhookEvent(feed, entries)
  55. if err == nil {
  56. t.Fatal("expected an error, got nil")
  57. }
  58. for _, secret := range webhookURLSecrets {
  59. if strings.Contains(err.Error(), secret) {
  60. t.Errorf("expected the error to not contain %q, got %q", secret, err)
  61. }
  62. }
  63. if !strings.Contains(err.Error(), scenario.wantErrContains) {
  64. t.Errorf("expected the error to contain %q, got %q", scenario.wantErrContains, err)
  65. }
  66. })
  67. }
  68. }
  69. func configureIntegrationAllowPrivateNetworksOption(t *testing.T) {
  70. t.Helper()
  71. t.Setenv("INTEGRATION_ALLOW_PRIVATE_NETWORKS", "1")
  72. configParser := config.NewConfigParser()
  73. parsedOptions, err := configParser.ParseEnvironmentVariables()
  74. if err != nil {
  75. t.Fatalf("Unable to configure test options: %v", err)
  76. }
  77. previousOptions := config.Opts
  78. config.Opts = parsedOptions
  79. t.Cleanup(func() {
  80. config.Opts = previousOptions
  81. })
  82. }