2019-03-06 06:20:12 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-25 23:11:42 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-03-06 06:20:12 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
|
2020-12-21 04:21:40 -07:00
|
|
|
use Pleroma.Web.ConnCase, async: true
|
2019-03-06 06:20:12 -07:00
|
|
|
|
|
|
|
import Pleroma.Factory
|
2020-05-05 06:43:00 -06:00
|
|
|
|
2019-03-06 06:20:12 -07:00
|
|
|
alias Pleroma.Web.Push
|
|
|
|
alias Pleroma.Web.Push.Subscription
|
|
|
|
|
|
|
|
@sub %{
|
|
|
|
"endpoint" => "https://example.com/example/1234",
|
|
|
|
"keys" => %{
|
|
|
|
"auth" => "8eDyX_uCN0XRhSbY5hs7Hg==",
|
|
|
|
"p256dh" =>
|
|
|
|
"BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@server_key Keyword.get(Push.vapid_config(), :public_key)
|
|
|
|
|
|
|
|
setup do
|
|
|
|
user = insert(:user)
|
|
|
|
token = insert(:oauth_token, user: user, scopes: ["push"])
|
|
|
|
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> assign(:token, token)
|
2020-05-05 06:43:00 -06:00
|
|
|
|> put_req_header("content-type", "application/json")
|
2019-03-06 06:20:12 -07:00
|
|
|
|
|
|
|
%{conn: conn, user: user, token: token}
|
|
|
|
end
|
|
|
|
|
|
|
|
defmacro assert_error_when_disable_push(do: yield) do
|
|
|
|
quote do
|
|
|
|
vapid_details = Application.get_env(:web_push_encryption, :vapid_details, [])
|
|
|
|
Application.put_env(:web_push_encryption, :vapid_details, [])
|
2020-04-15 12:59:25 -06:00
|
|
|
|
|
|
|
assert %{"error" => "Web push subscription is disabled on this Pleroma instance"} ==
|
|
|
|
unquote(yield)
|
|
|
|
|
2019-03-06 06:20:12 -07:00
|
|
|
Application.put_env(:web_push_encryption, :vapid_details, vapid_details)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-17 15:18:22 -07:00
|
|
|
describe "when disabled" do
|
|
|
|
test "POST returns error", %{conn: conn} do
|
|
|
|
assert_error_when_disable_push do
|
|
|
|
conn
|
|
|
|
|> post("/api/v1/push/subscription", %{
|
|
|
|
"data" => %{"alerts" => %{"mention" => true}},
|
|
|
|
"subscription" => @sub
|
|
|
|
})
|
|
|
|
|> json_response_and_validate_schema(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "GET returns error", %{conn: conn} do
|
|
|
|
assert_error_when_disable_push do
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/push/subscription", %{})
|
|
|
|
|> json_response_and_validate_schema(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "PUT returns error", %{conn: conn} do
|
|
|
|
assert_error_when_disable_push do
|
|
|
|
conn
|
|
|
|
|> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
|
|
|
|
|> json_response_and_validate_schema(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "DELETE returns error", %{conn: conn} do
|
2019-03-06 06:20:12 -07:00
|
|
|
assert_error_when_disable_push do
|
|
|
|
conn
|
2020-11-17 15:18:22 -07:00
|
|
|
|> delete("/api/v1/push/subscription", %{})
|
2020-05-05 06:43:00 -06:00
|
|
|
|> json_response_and_validate_schema(403)
|
2019-03-06 06:20:12 -07:00
|
|
|
end
|
|
|
|
end
|
2020-11-17 15:18:22 -07:00
|
|
|
end
|
2019-03-06 06:20:12 -07:00
|
|
|
|
2020-11-17 15:18:22 -07:00
|
|
|
describe "creates push subscription" do
|
2020-11-17 13:18:51 -07:00
|
|
|
test "ignores unsupported types", %{conn: conn} do
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> post("/api/v1/push/subscription", %{
|
|
|
|
"data" => %{
|
|
|
|
"alerts" => %{
|
|
|
|
"fake_unsupported_type" => true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"subscription" => @sub
|
|
|
|
})
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
refute %{
|
|
|
|
"alerts" => %{
|
|
|
|
"fake_unsupported_type" => true
|
|
|
|
}
|
|
|
|
} == result
|
|
|
|
end
|
|
|
|
|
2019-03-06 06:20:12 -07:00
|
|
|
test "successful creation", %{conn: conn} do
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> post("/api/v1/push/subscription", %{
|
2020-06-04 12:28:33 -06:00
|
|
|
"data" => %{
|
2020-11-17 12:15:11 -07:00
|
|
|
"alerts" => %{
|
|
|
|
"mention" => true,
|
2020-11-17 13:14:38 -07:00
|
|
|
"favourite" => true,
|
|
|
|
"follow" => true,
|
|
|
|
"reblog" => true,
|
2020-11-17 12:15:11 -07:00
|
|
|
"pleroma:chat_mention" => true,
|
2020-11-17 13:18:51 -07:00
|
|
|
"pleroma:emoji_reaction" => true
|
2020-11-17 12:15:11 -07:00
|
|
|
}
|
2020-06-04 12:28:33 -06:00
|
|
|
},
|
2019-03-06 06:20:12 -07:00
|
|
|
"subscription" => @sub
|
|
|
|
})
|
2020-05-05 06:43:00 -06:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-03-06 06:20:12 -07:00
|
|
|
|
|
|
|
[subscription] = Pleroma.Repo.all(Subscription)
|
|
|
|
|
|
|
|
assert %{
|
2020-11-17 12:15:11 -07:00
|
|
|
"alerts" => %{
|
|
|
|
"mention" => true,
|
2020-11-17 13:14:38 -07:00
|
|
|
"favourite" => true,
|
|
|
|
"follow" => true,
|
|
|
|
"reblog" => true,
|
2020-11-17 12:15:11 -07:00
|
|
|
"pleroma:chat_mention" => true,
|
|
|
|
"pleroma:emoji_reaction" => true
|
|
|
|
},
|
2019-03-06 06:20:12 -07:00
|
|
|
"endpoint" => subscription.endpoint,
|
|
|
|
"id" => to_string(subscription.id),
|
|
|
|
"server_key" => @server_key
|
|
|
|
} == result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "gets a user subscription" do
|
|
|
|
test "returns error when user hasn't subscription", %{conn: conn} do
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/push/subscription", %{})
|
2020-05-05 06:43:00 -06:00
|
|
|
|> json_response_and_validate_schema(404)
|
2019-03-06 06:20:12 -07:00
|
|
|
|
2020-05-05 06:43:00 -06:00
|
|
|
assert %{"error" => "Record not found"} == res
|
2019-03-06 06:20:12 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns a user subsciption", %{conn: conn, user: user, token: token} do
|
|
|
|
subscription =
|
|
|
|
insert(:push_subscription,
|
|
|
|
user: user,
|
|
|
|
token: token,
|
|
|
|
data: %{"alerts" => %{"mention" => true}}
|
|
|
|
)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/push/subscription", %{})
|
2020-05-05 06:43:00 -06:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-03-06 06:20:12 -07:00
|
|
|
|
|
|
|
expect = %{
|
|
|
|
"alerts" => %{"mention" => true},
|
|
|
|
"endpoint" => "https://example.com/example/1234",
|
|
|
|
"id" => to_string(subscription.id),
|
|
|
|
"server_key" => @server_key
|
|
|
|
}
|
|
|
|
|
|
|
|
assert expect == res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "updates a user subsciption" do
|
|
|
|
setup %{conn: conn, user: user, token: token} do
|
|
|
|
subscription =
|
|
|
|
insert(:push_subscription,
|
|
|
|
user: user,
|
|
|
|
token: token,
|
2020-11-17 13:14:38 -07:00
|
|
|
data: %{
|
|
|
|
"alerts" => %{
|
|
|
|
"mention" => true,
|
|
|
|
"favourite" => true,
|
|
|
|
"follow" => true,
|
|
|
|
"reblog" => true,
|
|
|
|
"pleroma:chat_mention" => true,
|
|
|
|
"pleroma:emoji_reaction" => true
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 06:20:12 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
%{conn: conn, user: user, token: token, subscription: subscription}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns updated subsciption", %{conn: conn, subscription: subscription} do
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> put("/api/v1/push/subscription", %{
|
2020-11-17 13:14:38 -07:00
|
|
|
data: %{
|
|
|
|
"alerts" => %{
|
|
|
|
"mention" => false,
|
|
|
|
"favourite" => false,
|
|
|
|
"follow" => false,
|
|
|
|
"reblog" => false,
|
|
|
|
"pleroma:chat_mention" => false,
|
|
|
|
"pleroma:emoji_reaction" => false
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 06:20:12 -07:00
|
|
|
})
|
2020-05-05 06:43:00 -06:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-03-06 06:20:12 -07:00
|
|
|
|
|
|
|
expect = %{
|
2020-11-17 13:14:38 -07:00
|
|
|
"alerts" => %{
|
|
|
|
"mention" => false,
|
|
|
|
"favourite" => false,
|
|
|
|
"follow" => false,
|
|
|
|
"reblog" => false,
|
|
|
|
"pleroma:chat_mention" => false,
|
|
|
|
"pleroma:emoji_reaction" => false
|
|
|
|
},
|
2019-03-06 06:20:12 -07:00
|
|
|
"endpoint" => "https://example.com/example/1234",
|
|
|
|
"id" => to_string(subscription.id),
|
|
|
|
"server_key" => @server_key
|
|
|
|
}
|
|
|
|
|
|
|
|
assert expect == res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "deletes the user subscription" do
|
|
|
|
test "returns error when user hasn't subscription", %{conn: conn} do
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> delete("/api/v1/push/subscription", %{})
|
2020-05-05 06:43:00 -06:00
|
|
|
|> json_response_and_validate_schema(404)
|
2019-03-06 06:20:12 -07:00
|
|
|
|
2020-05-05 06:43:00 -06:00
|
|
|
assert %{"error" => "Record not found"} == res
|
2019-03-06 06:20:12 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns empty result and delete user subsciption", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user,
|
|
|
|
token: token
|
|
|
|
} do
|
|
|
|
subscription =
|
|
|
|
insert(:push_subscription,
|
|
|
|
user: user,
|
|
|
|
token: token,
|
|
|
|
data: %{"alerts" => %{"mention" => true}}
|
|
|
|
)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> delete("/api/v1/push/subscription", %{})
|
2020-05-05 06:43:00 -06:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-03-06 06:20:12 -07:00
|
|
|
|
|
|
|
assert %{} == res
|
|
|
|
refute Pleroma.Repo.get(Subscription, subscription.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|