forked from YokaiRick/akkoma
removing try block in tesla request
added mocks for tests which fail with Tesla.Mock.Error
This commit is contained in:
parent
eb324467d9
commit
f0753eed0f
12 changed files with 139 additions and 22 deletions
|
@ -88,15 +88,11 @@ def request(method, url, body, headers, options) when is_binary(url) do
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec request(Client.t(), keyword(), map()) :: {:ok, Env.t()} | {:error, any()}
|
@spec request(Client.t(), keyword(), map()) :: {:ok, Env.t()} | {:error, any()}
|
||||||
def request(%Client{} = client, request, %{env: :test}), do: request_try(client, request)
|
def request(%Client{} = client, request, %{env: :test}), do: request(client, request)
|
||||||
|
|
||||||
def request(%Client{} = client, request, %{body_as: :chunks}) do
|
def request(%Client{} = client, request, %{body_as: :chunks}), do: request(client, request)
|
||||||
request_try(client, request)
|
|
||||||
end
|
|
||||||
|
|
||||||
def request(%Client{} = client, request, %{pool_alive?: false}) do
|
def request(%Client{} = client, request, %{pool_alive?: false}), do: request(client, request)
|
||||||
request_try(client, request)
|
|
||||||
end
|
|
||||||
|
|
||||||
def request(%Client{} = client, request, %{pool: pool, timeout: timeout}) do
|
def request(%Client{} = client, request, %{pool: pool, timeout: timeout}) do
|
||||||
:poolboy.transaction(
|
:poolboy.transaction(
|
||||||
|
@ -106,18 +102,8 @@ def request(%Client{} = client, request, %{pool: pool, timeout: timeout}) do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec request_try(Client.t(), keyword()) :: {:ok, Env.t()} | {:error, any()}
|
@spec request(Client.t(), keyword()) :: {:ok, Env.t()} | {:error, any()}
|
||||||
def request_try(client, request) do
|
def request(client, request), do: Tesla.request(client, request)
|
||||||
try do
|
|
||||||
Tesla.request(client, request)
|
|
||||||
rescue
|
|
||||||
e ->
|
|
||||||
{:error, e}
|
|
||||||
catch
|
|
||||||
:exit, e ->
|
|
||||||
{:error, e}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp build_request(method, headers, options, url, body, params) do
|
defp build_request(method, headers, options, url, body, params) do
|
||||||
Builder.new()
|
Builder.new()
|
||||||
|
|
|
@ -22,7 +22,7 @@ def execute(pid, client, request, timeout) do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_call({:execute, client, request}, _from, state) do
|
def handle_call({:execute, client, request}, _from, state) do
|
||||||
response = Pleroma.HTTP.request_try(client, request)
|
response = Pleroma.HTTP.request(client, request)
|
||||||
|
|
||||||
{:reply, response, state}
|
{:reply, response, state}
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,7 +32,7 @@ def perform(
|
||||||
type = Activity.mastodon_notification_type(notif.activity)
|
type = Activity.mastodon_notification_type(notif.activity)
|
||||||
gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)
|
gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)
|
||||||
avatar_url = User.avatar_url(actor)
|
avatar_url = User.avatar_url(actor)
|
||||||
object = Object.normalize(activity)
|
object = Object.normalize(activity) || activity
|
||||||
user = User.get_cached_by_id(user_id)
|
user = User.get_cached_by_id(user_id)
|
||||||
direct_conversation_id = Activity.direct_conversation_id(activity, user)
|
direct_conversation_id = Activity.direct_conversation_id(activity, user)
|
||||||
|
|
||||||
|
|
|
@ -173,7 +173,8 @@ def find_lrdd_template(domain) do
|
||||||
get_template_from_xml(body)
|
get_template_from_xml(body)
|
||||||
else
|
else
|
||||||
_ ->
|
_ ->
|
||||||
with {:ok, %{body: body}} <- HTTP.get("https://#{domain}/.well-known/host-meta", []) do
|
with {:ok, %{body: body, status: status}} when status in 200..299 <-
|
||||||
|
HTTP.get("https://#{domain}/.well-known/host-meta", []) do
|
||||||
get_template_from_xml(body)
|
get_template_from_xml(body)
|
||||||
else
|
else
|
||||||
e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
|
e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
|
||||||
|
|
41
test/fixtures/users_mock/localhost.json
vendored
Normal file
41
test/fixtures/users_mock/localhost.json
vendored
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
{
|
||||||
|
"@context": [
|
||||||
|
"https://www.w3.org/ns/activitystreams",
|
||||||
|
"http://localhost:4001/schemas/litepub-0.1.jsonld",
|
||||||
|
{
|
||||||
|
"@language": "und"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"attachment": [],
|
||||||
|
"endpoints": {
|
||||||
|
"oauthAuthorizationEndpoint": "http://localhost:4001/oauth/authorize",
|
||||||
|
"oauthRegistrationEndpoint": "http://localhost:4001/api/v1/apps",
|
||||||
|
"oauthTokenEndpoint": "http://localhost:4001/oauth/token",
|
||||||
|
"sharedInbox": "http://localhost:4001/inbox"
|
||||||
|
},
|
||||||
|
"followers": "http://localhost:4001/users/{{nickname}}/followers",
|
||||||
|
"following": "http://localhost:4001/users/{{nickname}}/following",
|
||||||
|
"icon": {
|
||||||
|
"type": "Image",
|
||||||
|
"url": "http://localhost:4001/media/4e914f5b84e4a259a3f6c2d2edc9ab642f2ab05f3e3d9c52c81fc2d984b3d51e.jpg"
|
||||||
|
},
|
||||||
|
"id": "http://localhost:4001/users/{{nickname}}",
|
||||||
|
"image": {
|
||||||
|
"type": "Image",
|
||||||
|
"url": "http://localhost:4001/media/f739efddefeee49c6e67e947c4811fdc911785c16ae43da4c3684051fbf8da6a.jpg?name=f739efddefeee49c6e67e947c4811fdc911785c16ae43da4c3684051fbf8da6a.jpg"
|
||||||
|
},
|
||||||
|
"inbox": "http://localhost:4001/users/{{nickname}}/inbox",
|
||||||
|
"manuallyApprovesFollowers": false,
|
||||||
|
"name": "{{nickname}}",
|
||||||
|
"outbox": "http://localhost:4001/users/{{nickname}}/outbox",
|
||||||
|
"preferredUsername": "{{nickname}}",
|
||||||
|
"publicKey": {
|
||||||
|
"id": "http://localhost:4001/users/{{nickname}}#main-key",
|
||||||
|
"owner": "http://localhost:4001/users/{{nickname}}",
|
||||||
|
"publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5DLtwGXNZElJyxFGfcVc\nXANhaMadj/iYYQwZjOJTV9QsbtiNBeIK54PJrYuU0/0YIdrvS1iqheX5IwXRhcwa\nhm3ZyLz7XeN9st7FBni4BmZMBtMpxAuYuu5p/jbWy13qAiYOhPreCx0wrWgm/lBD\n9mkgaxIxPooBE0S4ZWEJIDIV1Vft3AWcRUyWW1vIBK0uZzs6GYshbQZB952S0yo4\nFzI1hABGHncH8UvuFauh4EZ8tY7/X5I0pGRnDOcRN1dAht5w5yTA+6r5kebiFQjP\nIzN/eCO/a9Flrj9YGW7HDNtjSOH0A31PLRGlJtJO3yK57dnf5ppyCZGfL4emShQo\ncQIDAQAB\n-----END PUBLIC KEY-----\n\n"
|
||||||
|
},
|
||||||
|
"summary": "your friendly neighborhood pleroma developer<br>I like cute things and distributed systems, and really hate delete and redrafts",
|
||||||
|
"tag": [],
|
||||||
|
"type": "Person",
|
||||||
|
"url": "http://localhost:4001/users/{{nickname}}"
|
||||||
|
}
|
|
@ -649,12 +649,20 @@ test "notifications are deleted if a remote user is deleted" do
|
||||||
"object" => remote_user.ap_id
|
"object" => remote_user.ap_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remote_user_url = remote_user.ap_id
|
||||||
|
|
||||||
|
Tesla.Mock.mock(fn
|
||||||
|
%{method: :get, url: ^remote_user_url} ->
|
||||||
|
%Tesla.Env{status: 404, body: ""}
|
||||||
|
end)
|
||||||
|
|
||||||
{:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
|
{:ok, _delete_activity} = Transmogrifier.handle_incoming(delete_user_message)
|
||||||
ObanHelpers.perform_all()
|
ObanHelpers.perform_all()
|
||||||
|
|
||||||
assert Enum.empty?(Notification.for_user(local_user))
|
assert Enum.empty?(Notification.for_user(local_user))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@tag capture_log: true
|
||||||
test "move activity generates a notification" do
|
test "move activity generates a notification" do
|
||||||
%{ap_id: old_ap_id} = old_user = insert(:user)
|
%{ap_id: old_ap_id} = old_user = insert(:user)
|
||||||
%{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
|
%{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
|
||||||
|
@ -664,6 +672,18 @@ test "move activity generates a notification" do
|
||||||
User.follow(follower, old_user)
|
User.follow(follower, old_user)
|
||||||
User.follow(other_follower, old_user)
|
User.follow(other_follower, old_user)
|
||||||
|
|
||||||
|
old_user_url = old_user.ap_id
|
||||||
|
|
||||||
|
body =
|
||||||
|
File.read!("test/fixtures/users_mock/localhost.json")
|
||||||
|
|> String.replace("{{nickname}}", old_user.nickname)
|
||||||
|
|> Jason.encode!()
|
||||||
|
|
||||||
|
Tesla.Mock.mock(fn
|
||||||
|
%{method: :get, url: ^old_user_url} ->
|
||||||
|
%Tesla.Env{status: 200, body: body}
|
||||||
|
end)
|
||||||
|
|
||||||
Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
|
Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
|
||||||
ObanHelpers.perform_all()
|
ObanHelpers.perform_all()
|
||||||
|
|
||||||
|
|
|
@ -110,6 +110,15 @@ test "it allows posts with links" do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "with unknown actors" do
|
describe "with unknown actors" do
|
||||||
|
setup do
|
||||||
|
Tesla.Mock.mock(fn
|
||||||
|
%{method: :get, url: "http://invalid.actor"} ->
|
||||||
|
%Tesla.Env{status: 500, body: ""}
|
||||||
|
end)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
test "it rejects posts without links" do
|
test "it rejects posts without links" do
|
||||||
message =
|
message =
|
||||||
@linkless_message
|
@linkless_message
|
||||||
|
|
|
@ -89,6 +89,11 @@ test "returns error when object is unknown" do
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Tesla.Mock.mock(fn
|
||||||
|
%{method: :get, url: "http://mastodon.example.org/eee/99541947525187367"} ->
|
||||||
|
%Tesla.Env{status: 500, body: ""}
|
||||||
|
end)
|
||||||
|
|
||||||
assert capture_log(fn ->
|
assert capture_log(fn ->
|
||||||
assert Relay.publish(activity) == {:error, nil}
|
assert Relay.publish(activity) == {:error, nil}
|
||||||
end) =~ "[error] error: nil"
|
end) =~ "[error] error: nil"
|
||||||
|
|
|
@ -407,11 +407,24 @@ test "see notifications after muting user with notifications and with_muted para
|
||||||
assert length(json_response(conn, 200)) == 1
|
assert length(json_response(conn, 200)) == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@tag capture_log: true
|
||||||
test "see move notifications with `with_move` parameter" do
|
test "see move notifications with `with_move` parameter" do
|
||||||
old_user = insert(:user)
|
old_user = insert(:user)
|
||||||
new_user = insert(:user, also_known_as: [old_user.ap_id])
|
new_user = insert(:user, also_known_as: [old_user.ap_id])
|
||||||
%{user: follower, conn: conn} = oauth_access(["read:notifications"])
|
%{user: follower, conn: conn} = oauth_access(["read:notifications"])
|
||||||
|
|
||||||
|
old_user_url = old_user.ap_id
|
||||||
|
|
||||||
|
body =
|
||||||
|
File.read!("test/fixtures/users_mock/localhost.json")
|
||||||
|
|> String.replace("{{nickname}}", old_user.nickname)
|
||||||
|
|> Jason.encode!()
|
||||||
|
|
||||||
|
Tesla.Mock.mock(fn
|
||||||
|
%{method: :get, url: ^old_user_url} ->
|
||||||
|
%Tesla.Env{status: 200, body: body}
|
||||||
|
end)
|
||||||
|
|
||||||
User.follow(follower, old_user)
|
User.follow(follower, old_user)
|
||||||
Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
|
Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
|
||||||
Pleroma.Tests.ObanHelpers.perform_all()
|
Pleroma.Tests.ObanHelpers.perform_all()
|
||||||
|
|
|
@ -108,11 +108,24 @@ test "Follow notification" do
|
||||||
NotificationView.render("index.json", %{notifications: [notification], for: followed})
|
NotificationView.render("index.json", %{notifications: [notification], for: followed})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@tag capture_log: true
|
||||||
test "Move notification" do
|
test "Move notification" do
|
||||||
old_user = insert(:user)
|
old_user = insert(:user)
|
||||||
new_user = insert(:user, also_known_as: [old_user.ap_id])
|
new_user = insert(:user, also_known_as: [old_user.ap_id])
|
||||||
follower = insert(:user)
|
follower = insert(:user)
|
||||||
|
|
||||||
|
old_user_url = old_user.ap_id
|
||||||
|
|
||||||
|
body =
|
||||||
|
File.read!("test/fixtures/users_mock/localhost.json")
|
||||||
|
|> String.replace("{{nickname}}", old_user.nickname)
|
||||||
|
|> Jason.encode!()
|
||||||
|
|
||||||
|
Tesla.Mock.mock(fn
|
||||||
|
%{method: :get, url: ^old_user_url} ->
|
||||||
|
%Tesla.Env{status: 200, body: body}
|
||||||
|
end)
|
||||||
|
|
||||||
User.follow(follower, old_user)
|
User.follow(follower, old_user)
|
||||||
Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
|
Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
|
||||||
Pleroma.Tests.ObanHelpers.perform_all()
|
Pleroma.Tests.ObanHelpers.perform_all()
|
||||||
|
|
|
@ -92,6 +92,23 @@ test "returns a temporary ap_id based user for activities missing db users" do
|
||||||
Repo.delete(user)
|
Repo.delete(user)
|
||||||
Cachex.clear(:user_cache)
|
Cachex.clear(:user_cache)
|
||||||
|
|
||||||
|
finger_url =
|
||||||
|
"https://localhost/.well-known/webfinger?resource=acct:#{user.nickname}@localhost"
|
||||||
|
|
||||||
|
Tesla.Mock.mock_global(fn
|
||||||
|
%{method: :get, url: "http://localhost/.well-known/host-meta"} ->
|
||||||
|
%Tesla.Env{status: 404, body: ""}
|
||||||
|
|
||||||
|
%{method: :get, url: "https://localhost/.well-known/host-meta"} ->
|
||||||
|
%Tesla.Env{status: 404, body: ""}
|
||||||
|
|
||||||
|
%{
|
||||||
|
method: :get,
|
||||||
|
url: ^finger_url
|
||||||
|
} ->
|
||||||
|
%Tesla.Env{status: 404, body: ""}
|
||||||
|
end)
|
||||||
|
|
||||||
%{account: ms_user} = StatusView.render("show.json", activity: activity)
|
%{account: ms_user} = StatusView.render("show.json", activity: activity)
|
||||||
|
|
||||||
assert ms_user.acct == "erroruser@example.com"
|
assert ms_user.acct == "erroruser@example.com"
|
||||||
|
|
|
@ -122,6 +122,18 @@ test "it doesn't send notify to the 'user:notification' stream' when a domain is
|
||||||
test "it sends follow activities to the 'user:notification' stream", %{
|
test "it sends follow activities to the 'user:notification' stream", %{
|
||||||
user: user
|
user: user
|
||||||
} do
|
} do
|
||||||
|
user_url = user.ap_id
|
||||||
|
|
||||||
|
body =
|
||||||
|
File.read!("test/fixtures/users_mock/localhost.json")
|
||||||
|
|> String.replace("{{nickname}}", user.nickname)
|
||||||
|
|> Jason.encode!()
|
||||||
|
|
||||||
|
Tesla.Mock.mock_global(fn
|
||||||
|
%{method: :get, url: ^user_url} ->
|
||||||
|
%Tesla.Env{status: 200, body: body}
|
||||||
|
end)
|
||||||
|
|
||||||
user2 = insert(:user)
|
user2 = insert(:user)
|
||||||
task = Task.async(fn -> assert_receive {:text, _}, @streamer_timeout end)
|
task = Task.async(fn -> assert_receive {:text, _}, @streamer_timeout end)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue