logo

pleroma

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

subscription_controller_test.exs (7421B)


  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.SubscriptionControllerTest do
  5. use Pleroma.Web.ConnCase, async: false
  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. assert %{"error" => "Web push subscription is disabled on this Pleroma instance"} ==
  31. unquote(yield)
  32. end
  33. end
  34. describe "when disabled" do
  35. setup do
  36. vapid_config = Application.get_env(:web_push_encryption, :vapid_details)
  37. Application.put_env(:web_push_encryption, :vapid_details, [])
  38. on_exit(fn -> Application.put_env(:web_push_encryption, :vapid_details, vapid_config) end)
  39. end
  40. test "POST returns error", %{conn: conn} do
  41. assert_error_when_disable_push do
  42. conn
  43. |> post("/api/v1/push/subscription", %{
  44. "data" => %{"alerts" => %{"mention" => true}},
  45. "subscription" => @sub
  46. })
  47. |> json_response_and_validate_schema(403)
  48. end
  49. end
  50. test "GET returns error", %{conn: conn} do
  51. assert_error_when_disable_push do
  52. conn
  53. |> get("/api/v1/push/subscription", %{})
  54. |> json_response_and_validate_schema(403)
  55. end
  56. end
  57. test "PUT returns error", %{conn: conn} do
  58. assert_error_when_disable_push do
  59. conn
  60. |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
  61. |> json_response_and_validate_schema(403)
  62. end
  63. end
  64. test "DELETE returns error", %{conn: conn} do
  65. assert_error_when_disable_push do
  66. conn
  67. |> delete("/api/v1/push/subscription", %{})
  68. |> json_response_and_validate_schema(403)
  69. end
  70. end
  71. end
  72. describe "creates push subscription" do
  73. test "ignores unsupported types", %{conn: conn} do
  74. result =
  75. conn
  76. |> post("/api/v1/push/subscription", %{
  77. "data" => %{
  78. "alerts" => %{
  79. "fake_unsupported_type" => true
  80. }
  81. },
  82. "subscription" => @sub
  83. })
  84. |> json_response_and_validate_schema(200)
  85. refute %{
  86. "alerts" => %{
  87. "fake_unsupported_type" => true
  88. }
  89. } == result
  90. end
  91. test "successful creation", %{conn: conn} do
  92. result =
  93. conn
  94. |> post("/api/v1/push/subscription", %{
  95. "data" => %{
  96. "alerts" => %{
  97. "mention" => true,
  98. "favourite" => true,
  99. "follow" => true,
  100. "reblog" => true,
  101. "pleroma:chat_mention" => true,
  102. "pleroma:emoji_reaction" => true
  103. }
  104. },
  105. "subscription" => @sub
  106. })
  107. |> json_response_and_validate_schema(200)
  108. [subscription] = Pleroma.Repo.all(Subscription)
  109. assert %{
  110. "alerts" => %{
  111. "mention" => true,
  112. "favourite" => true,
  113. "follow" => true,
  114. "reblog" => true,
  115. "pleroma:chat_mention" => true,
  116. "pleroma:emoji_reaction" => true
  117. },
  118. "endpoint" => subscription.endpoint,
  119. "id" => to_string(subscription.id),
  120. "server_key" => @server_key
  121. } == result
  122. end
  123. end
  124. describe "gets a user subscription" do
  125. test "returns error when user hasn't subscription", %{conn: conn} do
  126. res =
  127. conn
  128. |> get("/api/v1/push/subscription", %{})
  129. |> json_response_and_validate_schema(404)
  130. assert %{"error" => "Record not found"} == res
  131. end
  132. test "returns a user subsciption", %{conn: conn, user: user, token: token} do
  133. subscription =
  134. insert(:push_subscription,
  135. user: user,
  136. token: token,
  137. data: %{"alerts" => %{"mention" => true}}
  138. )
  139. res =
  140. conn
  141. |> get("/api/v1/push/subscription", %{})
  142. |> json_response_and_validate_schema(200)
  143. expect = %{
  144. "alerts" => %{"mention" => true},
  145. "endpoint" => "https://example.com/example/1234",
  146. "id" => to_string(subscription.id),
  147. "server_key" => @server_key
  148. }
  149. assert expect == res
  150. end
  151. end
  152. describe "updates a user subsciption" do
  153. setup %{conn: conn, user: user, token: token} do
  154. subscription =
  155. insert(:push_subscription,
  156. user: user,
  157. token: token,
  158. data: %{
  159. "alerts" => %{
  160. "mention" => true,
  161. "favourite" => true,
  162. "follow" => true,
  163. "reblog" => true,
  164. "pleroma:chat_mention" => true,
  165. "pleroma:emoji_reaction" => true
  166. }
  167. }
  168. )
  169. %{conn: conn, user: user, token: token, subscription: subscription}
  170. end
  171. test "returns updated subsciption", %{conn: conn, subscription: subscription} do
  172. res =
  173. conn
  174. |> put("/api/v1/push/subscription", %{
  175. data: %{
  176. "alerts" => %{
  177. "mention" => false,
  178. "favourite" => false,
  179. "follow" => false,
  180. "reblog" => false,
  181. "pleroma:chat_mention" => false,
  182. "pleroma:emoji_reaction" => false
  183. }
  184. }
  185. })
  186. |> json_response_and_validate_schema(200)
  187. expect = %{
  188. "alerts" => %{
  189. "mention" => false,
  190. "favourite" => false,
  191. "follow" => false,
  192. "reblog" => false,
  193. "pleroma:chat_mention" => false,
  194. "pleroma:emoji_reaction" => false
  195. },
  196. "endpoint" => "https://example.com/example/1234",
  197. "id" => to_string(subscription.id),
  198. "server_key" => @server_key
  199. }
  200. assert expect == res
  201. end
  202. end
  203. describe "deletes the user subscription" do
  204. test "returns error when user hasn't subscription", %{conn: conn} do
  205. res =
  206. conn
  207. |> delete("/api/v1/push/subscription", %{})
  208. |> json_response_and_validate_schema(404)
  209. assert %{"error" => "Record not found"} == res
  210. end
  211. test "returns empty result and delete user subsciption", %{
  212. conn: conn,
  213. user: user,
  214. token: token
  215. } do
  216. subscription =
  217. insert(:push_subscription,
  218. user: user,
  219. token: token,
  220. data: %{"alerts" => %{"mention" => true}}
  221. )
  222. res =
  223. conn
  224. |> delete("/api/v1/push/subscription", %{})
  225. |> json_response_and_validate_schema(200)
  226. assert %{} == res
  227. refute Pleroma.Repo.get(Subscription, subscription.id)
  228. end
  229. end
  230. end