logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

notification_controller_test.exs (2764B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.PleromaAPI.NotificationControllerTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. alias Pleroma.Notification
  7. alias Pleroma.Repo
  8. alias Pleroma.Web.CommonAPI
  9. import Pleroma.Factory
  10. describe "POST /api/v1/pleroma/notifications/read" do
  11. setup do: oauth_access(["write:notifications"])
  12. test "it marks a single notification as read", %{user: user1, conn: conn} do
  13. user2 = insert(:user)
  14. {:ok, activity1} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
  15. {:ok, activity2} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
  16. {:ok, [notification1]} = Notification.create_notifications(activity1)
  17. {:ok, [notification2]} = Notification.create_notifications(activity2)
  18. conn
  19. |> put_req_header("content-type", "application/json")
  20. |> post("/api/v1/pleroma/notifications/read", %{id: notification1.id})
  21. |> json_response_and_validate_schema(:ok)
  22. assert Repo.get(Notification, notification1.id).seen
  23. refute Repo.get(Notification, notification2.id).seen
  24. end
  25. test "it marks multiple notifications as read", %{user: user1, conn: conn} do
  26. user2 = insert(:user)
  27. {:ok, _activity1} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
  28. {:ok, _activity2} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
  29. {:ok, _activity3} = CommonAPI.post(user2, %{status: "HIE @#{user1.nickname}"})
  30. [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
  31. refute Repo.get(Notification, notification1.id).seen
  32. refute Repo.get(Notification, notification2.id).seen
  33. refute Repo.get(Notification, notification3.id).seen
  34. conn
  35. |> put_req_header("content-type", "application/json")
  36. |> post("/api/v1/pleroma/notifications/read", %{max_id: notification2.id})
  37. |> json_response_and_validate_schema(:ok)
  38. [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
  39. assert Repo.get(Notification, notification1.id).seen
  40. assert Repo.get(Notification, notification2.id).seen
  41. refute Repo.get(Notification, notification3.id).seen
  42. end
  43. test "it returns error when notification not found", %{conn: conn} do
  44. response =
  45. conn
  46. |> put_req_header("content-type", "application/json")
  47. |> post("/api/v1/pleroma/notifications/read", %{
  48. id: 22_222_222_222_222
  49. })
  50. |> json_response_and_validate_schema(:bad_request)
  51. assert response == %{"error" => "Cannot get notification"}
  52. end
  53. end
  54. end