entry_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package validator // import "miniflux.app/v2/internal/validator"
  4. import (
  5. "testing"
  6. "miniflux.app/v2/internal/model"
  7. )
  8. func TestValidateEntriesStatusUpdateRequest(t *testing.T) {
  9. err := ValidateEntriesStatusUpdateRequest(&model.EntriesStatusUpdateRequest{
  10. Status: model.EntryStatusRead,
  11. EntryIDs: []int64{int64(123), int64(456)},
  12. })
  13. if err != nil {
  14. t.Error(`A valid request should not be rejected`)
  15. }
  16. err = ValidateEntriesStatusUpdateRequest(&model.EntriesStatusUpdateRequest{
  17. Status: model.EntryStatusRead,
  18. })
  19. if err == nil {
  20. t.Error(`An empty list of entries is not valid`)
  21. }
  22. err = ValidateEntriesStatusUpdateRequest(&model.EntriesStatusUpdateRequest{
  23. Status: "invalid",
  24. EntryIDs: []int64{int64(123)},
  25. })
  26. if err == nil {
  27. t.Error(`Only a valid status should be accepted`)
  28. }
  29. }
  30. func TestValidateEntriesStatusAndStarredUpdateRequest(t *testing.T) {
  31. err := ValidateEntriesStatusAndStarredUpdateRequest(&model.EntriesStatusUpdateRequest{
  32. Status: model.EntryStatusRead,
  33. EntryIDs: []int64{int64(123), int64(456)},
  34. })
  35. if err != nil {
  36. t.Error(`A valid request should not be rejected`)
  37. }
  38. err = ValidateEntriesStatusAndStarredUpdateRequest(&model.EntriesStatusUpdateRequest{
  39. Status: model.EntryStatusRead,
  40. })
  41. if err == nil {
  42. t.Error(`An empty list of entries is not valid`)
  43. }
  44. err = ValidateEntriesStatusAndStarredUpdateRequest(&model.EntriesStatusUpdateRequest{
  45. Status: "invalid",
  46. EntryIDs: []int64{int64(123)},
  47. })
  48. if err == nil {
  49. t.Error(`Only a valid status should be accepted`)
  50. }
  51. starred := true
  52. err = ValidateEntriesStatusAndStarredUpdateRequest(&model.EntriesStatusUpdateRequest{
  53. Starred: &starred,
  54. EntryIDs: []int64{int64(123)},
  55. })
  56. if err != nil {
  57. t.Error(`A request with only the starred field should be accepted`)
  58. }
  59. notStarred := false
  60. err = ValidateEntriesStatusAndStarredUpdateRequest(&model.EntriesStatusUpdateRequest{
  61. Starred: &notStarred,
  62. EntryIDs: []int64{int64(123)},
  63. })
  64. if err != nil {
  65. t.Error(`A request with starred set to false should be accepted`)
  66. }
  67. err = ValidateEntriesStatusAndStarredUpdateRequest(&model.EntriesStatusUpdateRequest{
  68. Status: model.EntryStatusRead,
  69. Starred: &starred,
  70. EntryIDs: []int64{int64(123)},
  71. })
  72. if err != nil {
  73. t.Error(`A request with both status and starred should be accepted`)
  74. }
  75. err = ValidateEntriesStatusAndStarredUpdateRequest(&model.EntriesStatusUpdateRequest{
  76. EntryIDs: []int64{int64(123)},
  77. })
  78. if err == nil {
  79. t.Error(`A request without status or starred should be rejected`)
  80. }
  81. err = ValidateEntriesStatusAndStarredUpdateRequest(&model.EntriesStatusUpdateRequest{
  82. Status: "invalid",
  83. Starred: &starred,
  84. EntryIDs: []int64{int64(123)},
  85. })
  86. if err == nil {
  87. t.Error(`An invalid status should be rejected even when starred is specified`)
  88. }
  89. }
  90. func TestValidateEntryStatus(t *testing.T) {
  91. for _, status := range []string{model.EntryStatusRead, model.EntryStatusUnread} {
  92. if err := ValidateEntryStatus(status); err != nil {
  93. t.Error(`A valid status should not generate any error`)
  94. }
  95. }
  96. if err := ValidateEntryStatus("removed"); err == nil {
  97. t.Error(`The "removed" status is no longer accepted`)
  98. }
  99. if err := ValidateEntryStatus("invalid"); err == nil {
  100. t.Error(`An invalid status should generate a error`)
  101. }
  102. }
  103. func TestValidateEntryOrder(t *testing.T) {
  104. for _, status := range []string{"id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id", "title", "author"} {
  105. if err := ValidateEntryOrder(status); err != nil {
  106. t.Error(`A valid order should not generate any error`)
  107. }
  108. }
  109. if err := ValidateEntryOrder("invalid"); err == nil {
  110. t.Error(`An invalid order should generate a error`)
  111. }
  112. }
  113. func TestValidateEntryModification(t *testing.T) {
  114. // Accepts no-op update.
  115. if err := ValidateEntryModification(&model.EntryUpdateRequest{}); err != nil {
  116. t.Errorf(`A request without changes should not generate any error: %v`, err)
  117. }
  118. empty := ""
  119. if err := ValidateEntryModification(&model.EntryUpdateRequest{Title: &empty}); err == nil {
  120. t.Error(`An empty title should generate an error`)
  121. }
  122. if err := ValidateEntryModification(&model.EntryUpdateRequest{Content: &empty}); err == nil {
  123. t.Error(`An empty content should generate an error`)
  124. }
  125. title := "Title"
  126. content := "Content"
  127. if err := ValidateEntryModification(&model.EntryUpdateRequest{Title: &title, Content: &content}); err != nil {
  128. t.Errorf(`A valid title and content should not generate any error: %v`, err)
  129. }
  130. }