logo

pleroma

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

marker_controller_test.exs (4200B)


  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. import Pleroma.Factory
  7. describe "GET /api/v1/markers" do
  8. test "gets markers with correct scopes", %{conn: conn} do
  9. user = insert(:user)
  10. token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
  11. insert_list(7, :notification, user: user, activity: insert(:note_activity))
  12. {:ok, %{"notifications" => marker}} =
  13. Pleroma.Marker.upsert(
  14. user,
  15. %{"notifications" => %{"last_read_id" => "69420"}}
  16. )
  17. response =
  18. conn
  19. |> assign(:user, user)
  20. |> assign(:token, token)
  21. |> get("/api/v1/markers?timeline[]=notifications")
  22. |> json_response_and_validate_schema(200)
  23. assert response == %{
  24. "notifications" => %{
  25. "last_read_id" => "69420",
  26. "updated_at" => NaiveDateTime.to_iso8601(marker.updated_at),
  27. "version" => 0,
  28. "pleroma" => %{"unread_count" => 7}
  29. }
  30. }
  31. end
  32. test "gets markers with missed scopes", %{conn: conn} do
  33. user = insert(:user)
  34. token = insert(:oauth_token, user: user, scopes: [])
  35. Pleroma.Marker.upsert(user, %{"notifications" => %{"last_read_id" => "69420"}})
  36. response =
  37. conn
  38. |> assign(:user, user)
  39. |> assign(:token, token)
  40. |> get("/api/v1/markers", %{timeline: ["notifications"]})
  41. |> json_response_and_validate_schema(403)
  42. assert response == %{"error" => "Insufficient permissions: read:statuses."}
  43. end
  44. end
  45. describe "POST /api/v1/markers" do
  46. test "creates a marker with correct scopes", %{conn: conn} do
  47. user = insert(:user)
  48. token = insert(:oauth_token, user: user, scopes: ["write:statuses"])
  49. response =
  50. conn
  51. |> assign(:user, user)
  52. |> assign(:token, token)
  53. |> put_req_header("content-type", "application/json")
  54. |> post("/api/v1/markers", %{
  55. home: %{last_read_id: "777"},
  56. notifications: %{"last_read_id" => "69420"}
  57. })
  58. |> json_response_and_validate_schema(200)
  59. assert %{
  60. "notifications" => %{
  61. "last_read_id" => "69420",
  62. "updated_at" => _,
  63. "version" => 0,
  64. "pleroma" => %{"unread_count" => 0}
  65. }
  66. } = response
  67. end
  68. test "updates exist marker", %{conn: conn} do
  69. user = insert(:user)
  70. token = insert(:oauth_token, user: user, scopes: ["write:statuses"])
  71. {:ok, %{"notifications" => marker}} =
  72. Pleroma.Marker.upsert(
  73. user,
  74. %{"notifications" => %{"last_read_id" => "69477"}}
  75. )
  76. response =
  77. conn
  78. |> assign(:user, user)
  79. |> assign(:token, token)
  80. |> put_req_header("content-type", "application/json")
  81. |> post("/api/v1/markers", %{
  82. home: %{last_read_id: "777"},
  83. notifications: %{"last_read_id" => "69888"}
  84. })
  85. |> json_response_and_validate_schema(200)
  86. assert response == %{
  87. "notifications" => %{
  88. "last_read_id" => "69888",
  89. "updated_at" => NaiveDateTime.to_iso8601(marker.updated_at),
  90. "version" => 0,
  91. "pleroma" => %{"unread_count" => 0}
  92. }
  93. }
  94. end
  95. test "creates a marker with missed scopes", %{conn: conn} do
  96. user = insert(:user)
  97. token = insert(:oauth_token, user: user, scopes: [])
  98. response =
  99. conn
  100. |> assign(:user, user)
  101. |> assign(:token, token)
  102. |> put_req_header("content-type", "application/json")
  103. |> post("/api/v1/markers", %{
  104. home: %{last_read_id: "777"},
  105. notifications: %{"last_read_id" => "69420"}
  106. })
  107. |> json_response_and_validate_schema(403)
  108. assert response == %{"error" => "Insufficient permissions: write:statuses."}
  109. end
  110. end
  111. end