logo

pleroma

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

subscription_controller_test.exs (5780B)


  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.MastodonAPI.SubscriptionControllerTest do
  5. use Pleroma.Web.ConnCase
  6. import Pleroma.Factory
  7. alias Pleroma.Web.Push
  8. alias Pleroma.Web.Push.Subscription
  9. @sub %{
  10. "endpoint" => "https://example.com/example/1234",
  11. "keys" => %{
  12. "auth" => "8eDyX_uCN0XRhSbY5hs7Hg==",
  13. "p256dh" =>
  14. "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
  15. }
  16. }
  17. @server_key Keyword.get(Push.vapid_config(), :public_key)
  18. setup do
  19. user = insert(:user)
  20. token = insert(:oauth_token, user: user, scopes: ["push"])
  21. conn =
  22. build_conn()
  23. |> assign(:user, user)
  24. |> assign(:token, token)
  25. |> put_req_header("content-type", "application/json")
  26. %{conn: conn, user: user, token: token}
  27. end
  28. defmacro assert_error_when_disable_push(do: yield) do
  29. quote do
  30. vapid_details = Application.get_env(:web_push_encryption, :vapid_details, [])
  31. Application.put_env(:web_push_encryption, :vapid_details, [])
  32. assert %{"error" => "Web push subscription is disabled on this Pleroma instance"} ==
  33. unquote(yield)
  34. Application.put_env(:web_push_encryption, :vapid_details, vapid_details)
  35. end
  36. end
  37. describe "creates push subscription" do
  38. test "returns error when push disabled ", %{conn: conn} do
  39. assert_error_when_disable_push do
  40. conn
  41. |> post("/api/v1/push/subscription", %{subscription: @sub})
  42. |> json_response_and_validate_schema(403)
  43. end
  44. end
  45. test "successful creation", %{conn: conn} do
  46. result =
  47. conn
  48. |> post("/api/v1/push/subscription", %{
  49. "data" => %{
  50. "alerts" => %{"mention" => true, "test" => true, "pleroma:chat_mention" => true}
  51. },
  52. "subscription" => @sub
  53. })
  54. |> json_response_and_validate_schema(200)
  55. [subscription] = Pleroma.Repo.all(Subscription)
  56. assert %{
  57. "alerts" => %{"mention" => true, "pleroma:chat_mention" => true},
  58. "endpoint" => subscription.endpoint,
  59. "id" => to_string(subscription.id),
  60. "server_key" => @server_key
  61. } == result
  62. end
  63. end
  64. describe "gets a user subscription" do
  65. test "returns error when push disabled ", %{conn: conn} do
  66. assert_error_when_disable_push do
  67. conn
  68. |> get("/api/v1/push/subscription", %{})
  69. |> json_response_and_validate_schema(403)
  70. end
  71. end
  72. test "returns error when user hasn't subscription", %{conn: conn} do
  73. res =
  74. conn
  75. |> get("/api/v1/push/subscription", %{})
  76. |> json_response_and_validate_schema(404)
  77. assert %{"error" => "Record not found"} == res
  78. end
  79. test "returns a user subsciption", %{conn: conn, user: user, token: token} do
  80. subscription =
  81. insert(:push_subscription,
  82. user: user,
  83. token: token,
  84. data: %{"alerts" => %{"mention" => true}}
  85. )
  86. res =
  87. conn
  88. |> get("/api/v1/push/subscription", %{})
  89. |> json_response_and_validate_schema(200)
  90. expect = %{
  91. "alerts" => %{"mention" => true},
  92. "endpoint" => "https://example.com/example/1234",
  93. "id" => to_string(subscription.id),
  94. "server_key" => @server_key
  95. }
  96. assert expect == res
  97. end
  98. end
  99. describe "updates a user subsciption" do
  100. setup %{conn: conn, user: user, token: token} do
  101. subscription =
  102. insert(:push_subscription,
  103. user: user,
  104. token: token,
  105. data: %{"alerts" => %{"mention" => true}}
  106. )
  107. %{conn: conn, user: user, token: token, subscription: subscription}
  108. end
  109. test "returns error when push disabled ", %{conn: conn} do
  110. assert_error_when_disable_push do
  111. conn
  112. |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
  113. |> json_response_and_validate_schema(403)
  114. end
  115. end
  116. test "returns updated subsciption", %{conn: conn, subscription: subscription} do
  117. res =
  118. conn
  119. |> put("/api/v1/push/subscription", %{
  120. data: %{"alerts" => %{"mention" => false, "follow" => true}}
  121. })
  122. |> json_response_and_validate_schema(200)
  123. expect = %{
  124. "alerts" => %{"follow" => true, "mention" => false},
  125. "endpoint" => "https://example.com/example/1234",
  126. "id" => to_string(subscription.id),
  127. "server_key" => @server_key
  128. }
  129. assert expect == res
  130. end
  131. end
  132. describe "deletes the user subscription" do
  133. test "returns error when push disabled ", %{conn: conn} do
  134. assert_error_when_disable_push do
  135. conn
  136. |> delete("/api/v1/push/subscription", %{})
  137. |> json_response_and_validate_schema(403)
  138. end
  139. end
  140. test "returns error when user hasn't subscription", %{conn: conn} do
  141. res =
  142. conn
  143. |> delete("/api/v1/push/subscription", %{})
  144. |> json_response_and_validate_schema(404)
  145. assert %{"error" => "Record not found"} == res
  146. end
  147. test "returns empty result and delete user subsciption", %{
  148. conn: conn,
  149. user: user,
  150. token: token
  151. } do
  152. subscription =
  153. insert(:push_subscription,
  154. user: user,
  155. token: token,
  156. data: %{"alerts" => %{"mention" => true}}
  157. )
  158. res =
  159. conn
  160. |> delete("/api/v1/push/subscription", %{})
  161. |> json_response_and_validate_schema(200)
  162. assert %{} == res
  163. refute Pleroma.Repo.get(Subscription, subscription.id)
  164. end
  165. end
  166. end