Add ability to set a default post expiry (#321)
[akkoma] / test / pleroma / web / mastodon_api / views / account_view_test.exs
index 8db88713765dd5c8892fe30f73e41ce8a8704ea7..c9036d67d225165ddde7bc08572412c3be162b0d 100644 (file)
@@ -3,7 +3,7 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
-  use Pleroma.DataCase
+  use Pleroma.DataCase, async: false
 
   alias Pleroma.User
   alias Pleroma.UserRelationship
@@ -12,6 +12,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
 
   import Pleroma.Factory
   import Tesla.Mock
+  import Mock
 
   setup do
     mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
@@ -25,6 +26,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
 
     user =
       insert(:user, %{
+        ap_id: "https://example.com/users/chikichikibanban",
         follower_count: 3,
         note_count: 5,
         background: background_image,
@@ -35,9 +37,12 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
         inserted_at: ~N[2017-08-15 15:47:06.597036],
         emoji: %{"karjalanpiirakka" => "/file.png"},
         raw_bio: "valid html. a\nb\nc\nd\nf '&<>\"",
-        also_known_as: ["https://shitposter.zone/users/shp"]
+        also_known_as: ["https://shitposter.zone/users/shp"],
+        status_ttl_days: 5
       })
 
+    insert(:instance, %{host: "example.com", nodeinfo: %{version: "2.1"}})
+
     expected = %{
       id: to_string(user.id),
       username: "shp",
@@ -50,6 +55,16 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
       statuses_count: 5,
       note: "<span>valid html</span>. a<br/>b<br/>c<br/>d<br/>f &#39;&amp;&lt;&gt;&quot;",
       url: user.ap_id,
+      akkoma: %{
+        instance: %{
+          name: "example.com",
+          nodeinfo: %{
+            "version" => "2.1"
+          },
+          favicon: nil
+        },
+        status_ttl_days: 5
+      },
       avatar: "http://localhost:4001/images/avi.png",
       avatar_static: "http://localhost:4001/images/avi.png",
       header: "http://localhost:4001/images/banner.png",
@@ -98,9 +113,57 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
     assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
   end
 
+  describe "nodeinfo" do
+    setup do
+      [
+        user: insert(:user, ap_id: "https://somewhere.example.com/users/chikichikibanban"),
+        instance:
+          insert(:instance, %{
+            host: "somewhere.example.com",
+            favicon: "https://example.com/favicon.ico"
+          })
+      ]
+    end
+
+    test "is embedded in the account view", %{user: user} do
+      assert %{
+               akkoma: %{
+                 instance: %{
+                   name: "somewhere.example.com",
+                   nodeinfo: %{
+                     "version" => "2.0"
+                   },
+                   favicon: "https://example.com/favicon.ico"
+                 }
+               }
+             } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
+    end
+
+    test "uses local nodeinfo for local users" do
+      user = insert(:user)
+
+      assert %{
+               akkoma: %{
+                 instance: %{
+                   name: "localhost",
+                   nodeinfo: %{
+                     software: %{
+                       name: "akkoma"
+                     }
+                   }
+                 }
+               }
+             } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
+    end
+  end
+
   describe "favicon" do
     setup do
-      [user: insert(:user)]
+      [
+        user: insert(:user, ap_id: "https://example.com/users/chikichikibanban"),
+        instance:
+          insert(:instance, %{host: "example.com", favicon: "https://example.com/favicon.ico"})
+      ]
     end
 
     test "is parsed when :instance_favicons is enabled", %{user: user} do
@@ -108,13 +171,14 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
 
       assert %{
                pleroma: %{
-                 favicon:
-                   "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
+                 favicon: "https://example.com/favicon.ico"
                }
              } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
     end
 
-    test "is nil when :instances_favicons is disabled", %{user: user} do
+    test "is nil when we have no instance", %{user: user} do
+      user = %{user | ap_id: "https://wowee.example.com/users/2"}
+
       assert %{pleroma: %{favicon: nil}} =
                AccountView.render("show.json", %{user: user, skip_visibility_check: true})
     end
@@ -176,11 +240,19 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
       },
       fqn: "shp@shitposter.club",
       last_status_at: nil,
+      akkoma: %{
+        instance: %{
+          name: "localhost",
+          favicon: "http://localhost:4001/favicon.png",
+          nodeinfo: %{version: "2.0"}
+        },
+        status_ttl_days: nil
+      },
       pleroma: %{
         ap_id: user.ap_id,
         also_known_as: [],
         background_image: nil,
-        favicon: nil,
+        favicon: "http://localhost:4001/favicon.png",
         is_confirmed: true,
         tags: [],
         is_admin: false,
@@ -196,7 +268,13 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
       }
     }
 
-    assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})
+    with_mock(
+      Pleroma.Web.Nodeinfo.NodeinfoController,
+      raw_nodeinfo: fn -> %{version: "2.0"} end
+    ) do
+      assert expected ==
+               AccountView.render("show.json", %{user: user, skip_visibility_check: true})
+    end
   end
 
   test "Represent a Funkwhale channel" do
@@ -272,6 +350,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
       subscribing: false,
       notifying: false,
       requested: false,
+      requested_by: false,
       domain_blocking: false,
       showing_reblogs: true,
       endorsed: false,
@@ -357,6 +436,24 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
     end
   end
 
+  test "represent a relationship for a user with an inbound pending follow request" do
+    follower = insert(:user)
+    followed = insert(:user, is_locked: true)
+
+    {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
+
+    follower = User.get_cached_by_id(follower.id)
+    followed = User.get_cached_by_id(followed.id)
+
+    expected =
+      Map.merge(
+        @blank_response,
+        %{requested_by: true, followed_by: false, id: to_string(follower.id)}
+      )
+
+    test_relationship_rendering(followed, follower, expected)
+  end
+
   test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
     user = insert(:user, pleroma_settings_store: %{fe: "test"})
 
@@ -578,6 +675,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
         emoji: %{"joker_smile" => "https://evil.website/society.png"}
       )
 
+    insert(:instance, %{host: "localhost", favicon: "https://evil.website/favicon.png"})
+
     with media_preview_enabled <- [false, true] do
       clear_config([:media_preview_proxy, :enabled], media_preview_enabled)
 
@@ -586,6 +685,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
         {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->
           String.starts_with?(url, Pleroma.Web.Endpoint.url())
 
+        {:akkoma, %{instance: %{favicon: favicon_url}}} ->
+          String.starts_with?(favicon_url, Pleroma.Web.Endpoint.url())
+
         {:emojis, emojis} ->
           Enum.all?(emojis, fn %{url: url, static_url: static_url} ->
             String.starts_with?(url, Pleroma.Web.Endpoint.url()) &&
@@ -598,4 +700,10 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
       |> assert()
     end
   end
+
+  test "returns nil in the instance field when no instance is held locally" do
+    user = insert(:user, ap_id: "https://example.com/users/1")
+    view = AccountView.render("show.json", %{user: user, skip_visibility_check: true})
+    assert view[:akkoma][:instance] == nil
+  end
 end