entry_test.go 2.6 KB

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