removing try block in tesla request
authorAlexander Strizhakov <alex.strizhakov@gmail.com>
Thu, 5 Mar 2020 14:31:06 +0000 (17:31 +0300)
committerAlexander Strizhakov <alex.strizhakov@gmail.com>
Thu, 5 Mar 2020 14:31:06 +0000 (17:31 +0300)
added mocks for tests which fail with Tesla.Mock.Error

12 files changed:
lib/pleroma/http/http.ex
lib/pleroma/pool/request.ex
lib/pleroma/web/push/impl.ex
lib/pleroma/web/web_finger/web_finger.ex
test/fixtures/users_mock/localhost.json [new file with mode: 0644]
test/notification_test.exs
test/web/activity_pub/mrf/anti_link_spam_policy_test.exs
test/web/activity_pub/relay_test.exs
test/web/mastodon_api/controllers/notification_controller_test.exs
test/web/mastodon_api/views/notification_view_test.exs
test/web/mastodon_api/views/status_view_test.exs
test/web/streamer/streamer_test.exs

index 7b7c79b649532ef78413d5d51b40a34e172e80dd..466a94adc3af4195ff0c88b638f59f1ef04fea42 100644 (file)
@@ -88,15 +88,11 @@ defmodule Pleroma.HTTP do
   end
 
   @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
-    request_try(client, request)
-  end
+  def request(%Client{} = client, request, %{body_as: :chunks}), do: request(client, request)
 
-  def request(%Client{} = client, request, %{pool_alive?: false}) do
-    request_try(client, request)
-  end
+  def request(%Client{} = client, request, %{pool_alive?: false}), do: request(client, request)
 
   def request(%Client{} = client, request, %{pool: pool, timeout: timeout}) do
     :poolboy.transaction(
@@ -106,18 +102,8 @@ defmodule Pleroma.HTTP do
     )
   end
 
-  @spec request_try(Client.t(), keyword()) :: {:ok, Env.t()} | {:error, any()}
-  def request_try(client, request) do
-    try do
-      Tesla.request(client, request)
-    rescue
-      e ->
-        {:error, e}
-    catch
-      :exit, e ->
-        {:error, e}
-    end
-  end
+  @spec request(Client.t(), keyword()) :: {:ok, Env.t()} | {:error, any()}
+  def request(client, request), do: Tesla.request(client, request)
 
   defp build_request(method, headers, options, url, body, params) do
     Builder.new()
index 0f271b3d052b57bc23e3e2c47122ac617d834473..db7c10c01f7c51d91e9cc9a3cee38f5b63588fe1 100644 (file)
@@ -22,7 +22,7 @@ defmodule Pleroma.Pool.Request do
 
   @impl true
   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}
   end
index afa510f086184f29ce55489e747e47f5ba0642d9..233e55f21f73580a81a589a9f4d3ce97c1858097 100644 (file)
@@ -32,7 +32,7 @@ defmodule Pleroma.Web.Push.Impl do
     type = Activity.mastodon_notification_type(notif.activity)
     gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)
     avatar_url = User.avatar_url(actor)
-    object = Object.normalize(activity)
+    object = Object.normalize(activity) || activity
     user = User.get_cached_by_id(user_id)
     direct_conversation_id = Activity.direct_conversation_id(activity, user)
 
index db567a02e0fbebf5832be41f03e8b371fd1522b9..7ffd0e51bbfbef242fddcb22bc97790cb00fd3a6 100644 (file)
@@ -173,7 +173,8 @@ defmodule Pleroma.Web.WebFinger do
       get_template_from_xml(body)
     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)
         else
           e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
diff --git a/test/fixtures/users_mock/localhost.json b/test/fixtures/users_mock/localhost.json
new file mode 100644 (file)
index 0000000..a49935d
--- /dev/null
@@ -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}}"
+}
\ No newline at end of file
index 56a5818103ecf7a0ecc615f1e95d9db50f580386..c71df4e07b0afcf32ce3fde5c53fa3ff1096ffb0 100644 (file)
@@ -649,12 +649,20 @@ defmodule Pleroma.NotificationTest do
         "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)
       ObanHelpers.perform_all()
 
       assert Enum.empty?(Notification.for_user(local_user))
     end
 
+    @tag capture_log: true
     test "move activity generates a notification" do
       %{ap_id: old_ap_id} = old_user = insert(:user)
       %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
@@ -664,6 +672,18 @@ defmodule Pleroma.NotificationTest do
       User.follow(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)
       ObanHelpers.perform_all()
 
index fc0be6f9156daf30323f31b36303f075b43a054e..1a13699bef1bffd63dde77ae4ea85972b5b09e3a 100644 (file)
@@ -110,6 +110,15 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicyTest do
   end
 
   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
       message =
         @linkless_message
index e3115dcd8c4eb717dd58fccc8436c3804703c4e2..12bf90d90039b492fd6205ea2502d2abea8fd21c 100644 (file)
@@ -89,6 +89,11 @@ defmodule Pleroma.Web.ActivityPub.RelayTest 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 Relay.publish(activity) == {:error, nil}
              end) =~ "[error] error: nil"
index d452ddbdd5d45d4479ed261dedb20bef6d4c54a5..0f0a060d241fb3c5573b8c81a4a4808425134558 100644 (file)
@@ -407,11 +407,24 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do
     assert length(json_response(conn, 200)) == 1
   end
 
+  @tag capture_log: true
   test "see move notifications with `with_move` parameter" do
     old_user = insert(:user)
     new_user = insert(:user, also_known_as: [old_user.ap_id])
     %{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)
     Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
     Pleroma.Tests.ObanHelpers.perform_all()
index 4df9c3c036c61bc752edf11d8b4422f083d533f0..57e4c8f1e90b08adf0ff84b2dfb2407c6257caa5 100644 (file)
@@ -108,11 +108,24 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
              NotificationView.render("index.json", %{notifications: [notification], for: followed})
   end
 
+  @tag capture_log: true
   test "Move notification" do
     old_user = insert(:user)
     new_user = insert(:user, also_known_as: [old_user.ap_id])
     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)
     Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
     Pleroma.Tests.ObanHelpers.perform_all()
index 191895c6fe9b3f459d833d394d10cfae19fc1a2f..7df72decbe6b0f5450f9503efade448512d73d8d 100644 (file)
@@ -92,6 +92,23 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
     Repo.delete(user)
     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)
 
     assert ms_user.acct == "erroruser@example.com"
index 339f99bbf8b39f2dc2b13fc8971bac19995e6a77..a04d70f21612d18fe48d71f8e1725e3bf7a3031b 100644 (file)
@@ -122,6 +122,18 @@ defmodule Pleroma.Web.StreamerTest do
     test "it sends follow activities to the 'user:notification' stream", %{
       user: user
     } 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)
       task = Task.async(fn -> assert_receive {:text, _}, @streamer_timeout end)