entry_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 TestValidateEntryStatus(t *testing.T) {
  31. for _, status := range []string{model.EntryStatusRead, model.EntryStatusUnread, model.EntryStatusRemoved} {
  32. if err := ValidateEntryStatus(status); err != nil {
  33. t.Error(`A valid status should not generate any error`)
  34. }
  35. }
  36. if err := ValidateEntryStatus("invalid"); err == nil {
  37. t.Error(`An invalid status should generate a error`)
  38. }
  39. }
  40. func TestValidateEntryOrder(t *testing.T) {
  41. for _, status := range []string{"id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id", "title", "author"} {
  42. if err := ValidateEntryOrder(status); err != nil {
  43. t.Error(`A valid order should not generate any error`)
  44. }
  45. }
  46. if err := ValidateEntryOrder("invalid"); err == nil {
  47. t.Error(`An invalid order should generate a error`)
  48. }
  49. }
  50. func TestValidateEntryModification(t *testing.T) {
  51. // Accepts no-op update.
  52. if err := ValidateEntryModification(&model.EntryUpdateRequest{}); err != nil {
  53. t.Errorf(`A request without changes should not generate any error: %v`, err)
  54. }
  55. empty := ""
  56. if err := ValidateEntryModification(&model.EntryUpdateRequest{Title: &empty}); err == nil {
  57. t.Error(`An empty title should generate an error`)
  58. }
  59. if err := ValidateEntryModification(&model.EntryUpdateRequest{Content: &empty}); err == nil {
  60. t.Error(`An empty content should generate an error`)
  61. }
  62. title := "Title"
  63. content := "Content"
  64. if err := ValidateEntryModification(&model.EntryUpdateRequest{Title: &title, Content: &content}); err != nil {
  65. t.Errorf(`A valid title and content should not generate any error: %v`, err)
  66. }
  67. }