logo

pleroma

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

marker_controller_test.exs (5577B)


  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.MastodonAPI.MarkerControllerTest 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 "GET /api/v1/markers" do
  11. test "gets markers with correct scopes", %{conn: conn} do
  12. user = insert(:user)
  13. token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
  14. insert_list(7, :notification, user: user, activity: insert(:note_activity))
  15. {:ok, %{"notifications" => marker}} =
  16. Pleroma.Marker.upsert(
  17. user,
  18. %{"notifications" => %{"last_read_id" => "69420"}}
  19. )
  20. response =
  21. conn
  22. |> assign(:user, user)
  23. |> assign(:token, token)
  24. |> get("/api/v1/markers?timeline[]=notifications")
  25. |> json_response_and_validate_schema(200)
  26. assert response == %{
  27. "notifications" => %{
  28. "last_read_id" => "69420",
  29. "updated_at" => NaiveDateTime.to_iso8601(marker.updated_at),
  30. "version" => 0,
  31. "pleroma" => %{"unread_count" => 7}
  32. }
  33. }
  34. end
  35. test "gets markers with missed scopes", %{conn: conn} do
  36. user = insert(:user)
  37. token = insert(:oauth_token, user: user, scopes: [])
  38. Pleroma.Marker.upsert(user, %{"notifications" => %{"last_read_id" => "69420"}})
  39. response =
  40. conn
  41. |> assign(:user, user)
  42. |> assign(:token, token)
  43. |> get("/api/v1/markers", %{timeline: ["notifications"]})
  44. |> json_response_and_validate_schema(403)
  45. assert response == %{"error" => "Insufficient permissions: read:statuses."}
  46. end
  47. end
  48. describe "POST /api/v1/markers" do
  49. test "creates a marker with correct scopes", %{conn: conn} do
  50. user = insert(:user)
  51. token = insert(:oauth_token, user: user, scopes: ["write:statuses"])
  52. response =
  53. conn
  54. |> assign(:user, user)
  55. |> assign(:token, token)
  56. |> put_req_header("content-type", "application/json")
  57. |> post("/api/v1/markers", %{
  58. home: %{last_read_id: "777"},
  59. notifications: %{"last_read_id" => "69420"}
  60. })
  61. |> json_response_and_validate_schema(200)
  62. assert %{
  63. "notifications" => %{
  64. "last_read_id" => "69420",
  65. "updated_at" => _,
  66. "version" => 0,
  67. "pleroma" => %{"unread_count" => 0}
  68. }
  69. } = response
  70. end
  71. test "updates exist marker", %{conn: conn} do
  72. user = insert(:user)
  73. token = insert(:oauth_token, user: user, scopes: ["write:statuses"])
  74. {:ok, %{"notifications" => marker}} =
  75. Pleroma.Marker.upsert(
  76. user,
  77. %{"notifications" => %{"last_read_id" => "69477"}}
  78. )
  79. response =
  80. conn
  81. |> assign(:user, user)
  82. |> assign(:token, token)
  83. |> put_req_header("content-type", "application/json")
  84. |> post("/api/v1/markers", %{
  85. home: %{last_read_id: "777"},
  86. notifications: %{"last_read_id" => "69888"}
  87. })
  88. |> json_response_and_validate_schema(200)
  89. assert response == %{
  90. "notifications" => %{
  91. "last_read_id" => "69888",
  92. "updated_at" => NaiveDateTime.to_iso8601(marker.updated_at),
  93. "version" => 0,
  94. "pleroma" => %{"unread_count" => 0}
  95. }
  96. }
  97. end
  98. test "creates a marker with missed scopes", %{conn: conn} do
  99. user = insert(:user)
  100. token = insert(:oauth_token, user: user, scopes: [])
  101. response =
  102. conn
  103. |> assign(:user, user)
  104. |> assign(:token, token)
  105. |> put_req_header("content-type", "application/json")
  106. |> post("/api/v1/markers", %{
  107. home: %{last_read_id: "777"},
  108. notifications: %{"last_read_id" => "69420"}
  109. })
  110. |> json_response_and_validate_schema(403)
  111. assert response == %{"error" => "Insufficient permissions: write:statuses."}
  112. end
  113. test "marks notifications as read", %{conn: conn} do
  114. user1 = insert(:user)
  115. token = insert(:oauth_token, user: user1, scopes: ["write:statuses"])
  116. user2 = insert(:user)
  117. {:ok, _activity1} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
  118. {:ok, _activity2} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
  119. {:ok, _activity3} = CommonAPI.post(user2, %{status: "HIE @#{user1.nickname}"})
  120. [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
  121. refute Repo.get(Notification, notification1.id).seen
  122. refute Repo.get(Notification, notification2.id).seen
  123. refute Repo.get(Notification, notification3.id).seen
  124. conn
  125. |> assign(:user, user1)
  126. |> assign(:token, token)
  127. |> put_req_header("content-type", "application/json")
  128. |> post("/api/v1/markers", %{
  129. notifications: %{last_read_id: to_string(notification2.id)}
  130. })
  131. |> json_response_and_validate_schema(200)
  132. [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
  133. assert Repo.get(Notification, notification1.id).seen
  134. assert Repo.get(Notification, notification2.id).seen
  135. refute Repo.get(Notification, notification3.id).seen
  136. end
  137. end
  138. end