serializer_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package opml // import "miniflux.app/v2/internal/reader/opml"
  4. import (
  5. "bytes"
  6. "strings"
  7. "testing"
  8. )
  9. func TestSerialize(t *testing.T) {
  10. var subscriptions []subcription
  11. subscriptions = append(subscriptions, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: "Category 1"})
  12. subscriptions = append(subscriptions, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: "Category 1"})
  13. subscriptions = append(subscriptions, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: "Category 2"})
  14. output := serialize(subscriptions)
  15. feeds, err := parse(bytes.NewBufferString(output))
  16. if err != nil {
  17. t.Error(err)
  18. }
  19. if len(feeds) != 3 {
  20. t.Errorf("Wrong number of subscriptions: %d instead of %d", len(feeds), 3)
  21. }
  22. found := false
  23. for _, feed := range feeds {
  24. if feed.Title == "Feed 1" && feed.CategoryName == "Category 1" &&
  25. feed.FeedURL == "http://example.org/feed/1" && feed.SiteURL == "http://example.org/1" {
  26. found = true
  27. break
  28. }
  29. }
  30. if !found {
  31. t.Error("Serialized feed is incorrect")
  32. }
  33. }
  34. func TestSerializeWithMinifluxSettings(t *testing.T) {
  35. input := subcription{
  36. Title: "Feed 1",
  37. FeedURL: "http://example.org/feed/1",
  38. SiteURL: "http://example.org/1",
  39. CategoryName: "Category 1",
  40. ScraperRules: `article [class^="content"]`,
  41. RewriteRules: `replace("foo"|"bar")`,
  42. UrlRewriteRules: `rewrite("^https://old"|"https://new")`,
  43. BlocklistRules: "sponsored",
  44. KeeplistRules: "important",
  45. BlockFilterEntryRules: `EntryTitle=~"ad"`,
  46. KeepFilterEntryRules: `EntryTitle=~"news"`,
  47. UserAgent: "CustomAgent/1.0",
  48. Crawler: true,
  49. IgnoreHTTPCache: true,
  50. FetchViaProxy: true,
  51. Disabled: true,
  52. NoMediaPlayer: true,
  53. HideGlobally: true,
  54. AllowSelfSignedCertificates: true,
  55. DisableHTTP2: true,
  56. IgnoreEntryUpdates: true,
  57. }
  58. output := serialize([]subcription{input})
  59. if !strings.Contains(output, `xmlns:miniflux="https://miniflux.app/opml"`) {
  60. t.Fatal("Miniflux OPML namespace is missing")
  61. }
  62. if !strings.Contains(output, `miniflux:crawler="true"`) {
  63. t.Fatal("Miniflux settings are not serialized with the Miniflux namespace")
  64. }
  65. if strings.Contains(output, "cookie") {
  66. t.Fatal("Sensitive feed settings should not be serialized")
  67. }
  68. feeds, err := parse(bytes.NewBufferString(output))
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. if len(feeds) != 1 {
  73. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(feeds), 1)
  74. }
  75. if feeds[0] != input {
  76. t.Errorf("Round-trip failed:\ngot: %+v\nwant: %+v", feeds[0], input)
  77. }
  78. }
  79. func TestSerializePreservesNewlinesInRules(t *testing.T) {
  80. input := subcription{
  81. Title: "Feed 1",
  82. FeedURL: "http://example.org/feed/1",
  83. SiteURL: "http://example.org/1",
  84. CategoryName: "Category 1",
  85. RewriteRules: "replace(\"foo\"|\"bar\")\nadd_youtube_video",
  86. ScraperRules: "article.content\np.body",
  87. BlockFilterEntryRules: "EntryTitle=~\"ad\"\nEntryURL=~\"click\"",
  88. }
  89. output := serialize([]subcription{input})
  90. feeds, err := parse(bytes.NewBufferString(output))
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. if len(feeds) != 1 {
  95. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(feeds), 1)
  96. }
  97. if feeds[0].RewriteRules != input.RewriteRules {
  98. t.Errorf("RewriteRules newlines not preserved:\ngot: %q\nwant: %q", feeds[0].RewriteRules, input.RewriteRules)
  99. }
  100. if feeds[0].ScraperRules != input.ScraperRules {
  101. t.Errorf("ScraperRules newlines not preserved:\ngot: %q\nwant: %q", feeds[0].ScraperRules, input.ScraperRules)
  102. }
  103. if feeds[0].BlockFilterEntryRules != input.BlockFilterEntryRules {
  104. t.Errorf("BlockFilterEntryRules newlines not preserved:\ngot: %q\nwant: %q", feeds[0].BlockFilterEntryRules, input.BlockFilterEntryRules)
  105. }
  106. }
  107. func TestNormalizedCategoriesOrder(t *testing.T) {
  108. var orderTests = []struct {
  109. naturalOrderName string
  110. correctOrderName string
  111. }{
  112. {"Category 2", "Category 1"},
  113. {"Category 3", "Category 2"},
  114. {"Category 1", "Category 3"},
  115. }
  116. var subscriptions []subcription
  117. subscriptions = append(subscriptions, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: orderTests[0].naturalOrderName})
  118. subscriptions = append(subscriptions, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: orderTests[1].naturalOrderName})
  119. subscriptions = append(subscriptions, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: orderTests[2].naturalOrderName})
  120. feeds := convertSubscriptionsToOPML(subscriptions)
  121. for i, o := range orderTests {
  122. if feeds.Outlines[i].Text != o.correctOrderName {
  123. t.Fatalf("need %v, got %v", o.correctOrderName, feeds.Outlines[i].Text)
  124. }
  125. }
  126. }