icon_integration_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package api_test
  4. import (
  5. "errors"
  6. "testing"
  7. miniflux "miniflux.app/v2/client"
  8. "miniflux.app/v2/internal/http/server/servertest"
  9. )
  10. func TestGetFeedIcon(t *testing.T) {
  11. t.Parallel()
  12. fixture := servertest.NewFeedFixture(t)
  13. regularUserClient, _ := newRegularUser(t)
  14. feedID := fixture.Subscribe(t, regularUserClient)
  15. icon, err := regularUserClient.FeedIcon(feedID)
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. if icon == nil {
  20. t.Fatal(`Expected an icon, got none`)
  21. }
  22. if icon.MimeType != "image/png" {
  23. t.Errorf(`Unexpected MIME type: got %q, want %q`, icon.MimeType, "image/png")
  24. }
  25. if len(icon.Data) == 0 {
  26. t.Error(`Expected icon data, got none`)
  27. }
  28. icon, err = regularUserClient.Icon(icon.ID)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. if icon == nil {
  33. t.Fatal(`Expected an icon, got none`)
  34. }
  35. if icon.MimeType != "image/png" {
  36. t.Errorf(`Unexpected MIME type: got %q, want %q`, icon.MimeType, "image/png")
  37. }
  38. if len(icon.Data) == 0 {
  39. t.Error(`Expected icon data, got none`)
  40. }
  41. }
  42. func TestGetFeedIconWithNonexistentFeedID(t *testing.T) {
  43. t.Parallel()
  44. client := newAdminClient(t)
  45. _, err := client.FeedIcon(nonexistentID)
  46. if !errors.Is(err, miniflux.ErrNotFound) {
  47. t.Errorf(`Fetching the icon of a nonexistent feed: got error %v, want %v`, err, miniflux.ErrNotFound)
  48. }
  49. }
  50. func TestGetIconWithNonexistentIconID(t *testing.T) {
  51. t.Parallel()
  52. client := newAdminClient(t)
  53. _, err := client.Icon(nonexistentID)
  54. if !errors.Is(err, miniflux.ErrNotFound) {
  55. t.Errorf(`Fetching a nonexistent icon: got error %v, want %v`, err, miniflux.ErrNotFound)
  56. }
  57. }
  58. func TestGetIconByIconIDFromAnotherUser(t *testing.T) {
  59. t.Parallel()
  60. fixture := servertest.NewFeedFixture(t)
  61. // The owner subscribes to a feed, which fetches and stores its icon.
  62. ownerClient, _ := newRegularUser(t)
  63. feedID := fixture.Subscribe(t, ownerClient)
  64. ownerIcon, err := ownerClient.FeedIcon(feedID)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. if ownerIcon == nil {
  69. t.Fatal(`Expected an icon, got none`)
  70. }
  71. // The owner can fetch its own icon by icon ID.
  72. if _, err := ownerClient.Icon(ownerIcon.ID); err != nil {
  73. t.Errorf(`Fetching the icon as its owner: got error %v, want nil`, err)
  74. }
  75. // Another user without access to that feed must not be able to fetch the icon.
  76. otherClient, _ := newRegularUser(t)
  77. if _, err := otherClient.Icon(ownerIcon.ID); !errors.Is(err, miniflux.ErrNotFound) {
  78. t.Errorf(`Fetching the icon as another user: got error %v, want %v`, err, miniflux.ErrNotFound)
  79. }
  80. }