handler_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlereader // import "miniflux.app/v2/internal/googlereader"
  4. import (
  5. "errors"
  6. "maps"
  7. "net/http"
  8. "net/http/httptest"
  9. "net/url"
  10. "strings"
  11. "testing"
  12. )
  13. func TestCheckAndSimplifyTags(t *testing.T) {
  14. read := Stream{Type: ReadStream}
  15. keptUnread := Stream{Type: KeptUnreadStream}
  16. starred := Stream{Type: StarredStream}
  17. like := Stream{Type: LikeStream}
  18. broadcast := Stream{Type: BroadcastStream}
  19. scenarios := []struct {
  20. name string
  21. add []Stream
  22. remove []Stream
  23. expected map[StreamType]bool
  24. }{
  25. {"add read", []Stream{read}, nil, map[StreamType]bool{ReadStream: true}},
  26. {"remove read", nil, []Stream{read}, map[StreamType]bool{ReadStream: false}},
  27. {"add kept-unread", []Stream{keptUnread}, nil, map[StreamType]bool{ReadStream: false}},
  28. {"remove kept-unread", nil, []Stream{keptUnread}, map[StreamType]bool{ReadStream: true}},
  29. {"add starred", []Stream{starred}, nil, map[StreamType]bool{StarredStream: true}},
  30. {"remove starred", nil, []Stream{starred}, map[StreamType]bool{StarredStream: false}},
  31. {"add read twice", []Stream{read, read}, nil, map[StreamType]bool{ReadStream: true}},
  32. {"add kept-unread twice", []Stream{keptUnread, keptUnread}, nil, map[StreamType]bool{ReadStream: false}},
  33. {"remove read twice", nil, []Stream{read, read}, map[StreamType]bool{ReadStream: false}},
  34. {"remove starred twice", nil, []Stream{starred, starred}, map[StreamType]bool{StarredStream: false}},
  35. {"add read and remove kept-unread", []Stream{read}, []Stream{keptUnread}, map[StreamType]bool{ReadStream: true}},
  36. {"add kept-unread and remove read", []Stream{keptUnread}, []Stream{read}, map[StreamType]bool{ReadStream: false}},
  37. {"add read and starred", []Stream{read, starred}, nil, map[StreamType]bool{ReadStream: true, StarredStream: true}},
  38. {"add read and remove starred", []Stream{read}, []Stream{starred}, map[StreamType]bool{ReadStream: true, StarredStream: false}},
  39. {"like and broadcast are ignored", []Stream{like, broadcast}, []Stream{like, broadcast}, map[StreamType]bool{}},
  40. }
  41. for _, scenario := range scenarios {
  42. t.Run(scenario.name, func(t *testing.T) {
  43. result, err := checkAndSimplifyTags(scenario.add, scenario.remove)
  44. if err != nil {
  45. t.Fatalf(`Unexpected error: %v`, err)
  46. }
  47. if !maps.Equal(result, scenario.expected) {
  48. t.Errorf(`Unexpected tags, got %v instead of %v`, result, scenario.expected)
  49. }
  50. })
  51. }
  52. }
  53. func TestCheckAndSimplifyTagsRejectsConflicts(t *testing.T) {
  54. read := Stream{Type: ReadStream}
  55. keptUnread := Stream{Type: KeptUnreadStream}
  56. starred := Stream{Type: StarredStream}
  57. scenarios := []struct {
  58. name string
  59. add []Stream
  60. remove []Stream
  61. }{
  62. {"add and remove read", []Stream{read}, []Stream{read}},
  63. {"add read then kept-unread", []Stream{read, keptUnread}, nil},
  64. {"add kept-unread then read", []Stream{keptUnread, read}, nil},
  65. {"add and remove kept-unread", []Stream{keptUnread}, []Stream{keptUnread}},
  66. {"remove read and kept-unread", nil, []Stream{read, keptUnread}},
  67. {"remove kept-unread and read", nil, []Stream{keptUnread, read}},
  68. {"add and remove starred", []Stream{starred}, []Stream{starred}},
  69. }
  70. for _, scenario := range scenarios {
  71. t.Run(scenario.name, func(t *testing.T) {
  72. if result, err := checkAndSimplifyTags(scenario.add, scenario.remove); err == nil {
  73. t.Errorf(`Expected an error, got %v`, result)
  74. }
  75. })
  76. }
  77. }
  78. func TestCheckAndSimplifyTagsRejectsUnsupportedTags(t *testing.T) {
  79. for _, stream := range []Stream{
  80. {Type: ReadingListStream},
  81. {Type: BroadcastFriendsStream},
  82. {Type: LabelStream, ID: "Tech"},
  83. {Type: FeedStream, ID: "1"},
  84. {Type: NoStream},
  85. } {
  86. if _, err := checkAndSimplifyTags([]Stream{stream}, nil); err == nil {
  87. t.Errorf(`Expected an error when adding %v`, stream)
  88. }
  89. if _, err := checkAndSimplifyTags(nil, []Stream{stream}); err == nil {
  90. t.Errorf(`Expected an error when removing %v`, stream)
  91. }
  92. }
  93. }
  94. func TestCheckAndSimplifyTagsReturnsSimultaneousError(t *testing.T) {
  95. _, err := checkAndSimplifyTags([]Stream{{Type: ReadStream}, {Type: KeptUnreadStream}}, nil)
  96. if !errors.Is(err, errSimultaneously) {
  97. t.Errorf(`Expected errSimultaneously, got %v`, err)
  98. }
  99. }
  100. func TestCheckOutputFormat(t *testing.T) {
  101. newPostRequest := func(target string, form url.Values) *http.Request {
  102. r := httptest.NewRequest(http.MethodPost, target, strings.NewReader(form.Encode()))
  103. r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  104. return r
  105. }
  106. scenarios := []struct {
  107. name string
  108. request *http.Request
  109. valid bool
  110. }{
  111. {"GET with output=json", httptest.NewRequest(http.MethodGet, "/?output=json", nil), true},
  112. {"GET without output", httptest.NewRequest(http.MethodGet, "/", nil), false},
  113. {"GET with output=xml", httptest.NewRequest(http.MethodGet, "/?output=xml", nil), false},
  114. {"POST with output=json in the body", newPostRequest("/", url.Values{"output": {"json"}}), true},
  115. {"POST with output=json in the query string", newPostRequest("/?output=json", url.Values{}), true},
  116. {"POST without output", newPostRequest("/", url.Values{"T": {"token"}}), false},
  117. {"POST with output=atom", newPostRequest("/", url.Values{"output": {"atom"}}), false},
  118. }
  119. for _, scenario := range scenarios {
  120. t.Run(scenario.name, func(t *testing.T) {
  121. err := checkOutputFormat(scenario.request)
  122. if scenario.valid && err != nil {
  123. t.Errorf(`Unexpected error: %v`, err)
  124. }
  125. if !scenario.valid && err == nil {
  126. t.Error(`Expected an error`)
  127. }
  128. })
  129. }
  130. }