logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

notification_controller_test.exs (2725B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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
  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. response =
  19. conn
  20. |> put_req_header("content-type", "application/json")
  21. |> post("/api/v1/pleroma/notifications/read", %{id: notification1.id})
  22. |> json_response_and_validate_schema(:ok)
  23. assert %{"pleroma" => %{"is_seen" => true}} = response
  24. assert Repo.get(Notification, notification1.id).seen
  25. refute Repo.get(Notification, notification2.id).seen
  26. end
  27. test "it marks multiple notifications as read", %{user: user1, conn: conn} do
  28. user2 = insert(:user)
  29. {:ok, _activity1} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
  30. {:ok, _activity2} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
  31. {:ok, _activity3} = CommonAPI.post(user2, %{status: "HIE @#{user1.nickname}"})
  32. [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
  33. [response1, response2] =
  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. assert %{"pleroma" => %{"is_seen" => true}} = response1
  39. assert %{"pleroma" => %{"is_seen" => true}} = response2
  40. assert Repo.get(Notification, notification1.id).seen
  41. assert Repo.get(Notification, notification2.id).seen
  42. refute Repo.get(Notification, notification3.id).seen
  43. end
  44. test "it returns error when notification not found", %{conn: conn} do
  45. response =
  46. conn
  47. |> put_req_header("content-type", "application/json")
  48. |> post("/api/v1/pleroma/notifications/read", %{
  49. id: 22_222_222_222_222
  50. })
  51. |> json_response_and_validate_schema(:bad_request)
  52. assert response == %{"error" => "Cannot get notification"}
  53. end
  54. end
  55. end