logo

pleroma

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

subscription_controller_test.exs (5302B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
      6   use Pleroma.Web.ConnCase
      7 
      8   import Pleroma.Factory
      9   alias Pleroma.Web.Push
     10   alias Pleroma.Web.Push.Subscription
     11 
     12   @sub %{
     13     "endpoint" => "https://example.com/example/1234",
     14     "keys" => %{
     15       "auth" => "8eDyX_uCN0XRhSbY5hs7Hg==",
     16       "p256dh" =>
     17         "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
     18     }
     19   }
     20   @server_key Keyword.get(Push.vapid_config(), :public_key)
     21 
     22   setup do
     23     user = insert(:user)
     24     token = insert(:oauth_token, user: user, scopes: ["push"])
     25 
     26     conn =
     27       build_conn()
     28       |> assign(:user, user)
     29       |> assign(:token, token)
     30 
     31     %{conn: conn, user: user, token: token}
     32   end
     33 
     34   defmacro assert_error_when_disable_push(do: yield) do
     35     quote do
     36       vapid_details = Application.get_env(:web_push_encryption, :vapid_details, [])
     37       Application.put_env(:web_push_encryption, :vapid_details, [])
     38       assert "Something went wrong" == unquote(yield)
     39       Application.put_env(:web_push_encryption, :vapid_details, vapid_details)
     40     end
     41   end
     42 
     43   describe "creates push subscription" do
     44     test "returns error when push disabled ", %{conn: conn} do
     45       assert_error_when_disable_push do
     46         conn
     47         |> post("/api/v1/push/subscription", %{})
     48         |> json_response(500)
     49       end
     50     end
     51 
     52     test "successful creation", %{conn: conn} do
     53       result =
     54         conn
     55         |> post("/api/v1/push/subscription", %{
     56           "data" => %{"alerts" => %{"mention" => true, "test" => true}},
     57           "subscription" => @sub
     58         })
     59         |> json_response(200)
     60 
     61       [subscription] = Pleroma.Repo.all(Subscription)
     62 
     63       assert %{
     64                "alerts" => %{"mention" => true},
     65                "endpoint" => subscription.endpoint,
     66                "id" => to_string(subscription.id),
     67                "server_key" => @server_key
     68              } == result
     69     end
     70   end
     71 
     72   describe "gets a user subscription" do
     73     test "returns error when push disabled ", %{conn: conn} do
     74       assert_error_when_disable_push do
     75         conn
     76         |> get("/api/v1/push/subscription", %{})
     77         |> json_response(500)
     78       end
     79     end
     80 
     81     test "returns error when user hasn't subscription", %{conn: conn} do
     82       res =
     83         conn
     84         |> get("/api/v1/push/subscription", %{})
     85         |> json_response(404)
     86 
     87       assert "Not found" == res
     88     end
     89 
     90     test "returns a user subsciption", %{conn: conn, user: user, token: token} do
     91       subscription =
     92         insert(:push_subscription,
     93           user: user,
     94           token: token,
     95           data: %{"alerts" => %{"mention" => true}}
     96         )
     97 
     98       res =
     99         conn
    100         |> get("/api/v1/push/subscription", %{})
    101         |> json_response(200)
    102 
    103       expect = %{
    104         "alerts" => %{"mention" => true},
    105         "endpoint" => "https://example.com/example/1234",
    106         "id" => to_string(subscription.id),
    107         "server_key" => @server_key
    108       }
    109 
    110       assert expect == res
    111     end
    112   end
    113 
    114   describe "updates a user subsciption" do
    115     setup %{conn: conn, user: user, token: token} do
    116       subscription =
    117         insert(:push_subscription,
    118           user: user,
    119           token: token,
    120           data: %{"alerts" => %{"mention" => true}}
    121         )
    122 
    123       %{conn: conn, user: user, token: token, subscription: subscription}
    124     end
    125 
    126     test "returns error when push disabled ", %{conn: conn} do
    127       assert_error_when_disable_push do
    128         conn
    129         |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
    130         |> json_response(500)
    131       end
    132     end
    133 
    134     test "returns updated subsciption", %{conn: conn, subscription: subscription} do
    135       res =
    136         conn
    137         |> put("/api/v1/push/subscription", %{
    138           data: %{"alerts" => %{"mention" => false, "follow" => true}}
    139         })
    140         |> json_response(200)
    141 
    142       expect = %{
    143         "alerts" => %{"follow" => true, "mention" => false},
    144         "endpoint" => "https://example.com/example/1234",
    145         "id" => to_string(subscription.id),
    146         "server_key" => @server_key
    147       }
    148 
    149       assert expect == res
    150     end
    151   end
    152 
    153   describe "deletes the user subscription" do
    154     test "returns error when push disabled ", %{conn: conn} do
    155       assert_error_when_disable_push do
    156         conn
    157         |> delete("/api/v1/push/subscription", %{})
    158         |> json_response(500)
    159       end
    160     end
    161 
    162     test "returns error when user hasn't subscription", %{conn: conn} do
    163       res =
    164         conn
    165         |> delete("/api/v1/push/subscription", %{})
    166         |> json_response(404)
    167 
    168       assert "Not found" == res
    169     end
    170 
    171     test "returns empty result and delete user subsciption", %{
    172       conn: conn,
    173       user: user,
    174       token: token
    175     } do
    176       subscription =
    177         insert(:push_subscription,
    178           user: user,
    179           token: token,
    180           data: %{"alerts" => %{"mention" => true}}
    181         )
    182 
    183       res =
    184         conn
    185         |> delete("/api/v1/push/subscription", %{})
    186         |> json_response(200)
    187 
    188       assert %{} == res
    189       refute Pleroma.Repo.get(Subscription, subscription.id)
    190     end
    191   end
    192 end