mastodon api: use OGP uri instead of page_url for deducing domain name, fix test
[akkoma] / test / web / mastodon_api / mastodon_api_controller_test.exs
index fdd3f12130e6f16d336827bcd3c062a5e2965080..55e778e4f906527c12b1305a680bd6c9e1688007 100644 (file)
@@ -10,6 +10,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
   alias Pleroma.Web.{OStatus, CommonAPI}
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.MastodonAPI.FilterView
+  alias Ecto.Changeset
   import Pleroma.Factory
   import ExUnit.CaptureLog
   import Tesla.Mock
@@ -396,7 +397,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
         |> assign(:user, user)
         |> get("/api/v1/filters/#{filter.filter_id}")
 
-      assert response = json_response(conn, 200)
+      assert _response = json_response(conn, 200)
     end
 
     test "update a filter", %{conn: conn} do
@@ -610,7 +611,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
         |> get("/api/v1/notifications")
 
       expected_response =
-        "hi <span><a data-user=\"#{user.id}\" href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
+        "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
+          user.ap_id
+        }\">@<span>#{user.nickname}</span></a></span>"
 
       assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
       assert response == expected_response
@@ -631,7 +634,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
         |> get("/api/v1/notifications/#{notification.id}")
 
       expected_response =
-        "hi <span><a data-user=\"#{user.id}\" href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>"
+        "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
+          user.ap_id
+        }\">@<span>#{user.nickname}</span></a></span>"
 
       assert %{"status" => %{"content" => response}} = json_response(conn, 200)
       assert response == expected_response
@@ -1367,7 +1372,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     assert [status] = json_response(first_conn, 200)
     assert status["id"] == to_string(activity.id)
 
-    assert [{"link", link_header}] =
+    assert [{"link", _link_header}] =
              Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
 
     # Honours query params
@@ -1412,9 +1417,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
       assert user = json_response(conn, 200)
 
       assert user["note"] ==
-               "I drink <a data-tag=\"cofe\" href=\"http://localhost:4001/tag/cofe\">#cofe</a> with <span><a data-user=\"#{
+               "I drink <a class=\"hashtag\" data-tag=\"cofe\" href=\"http://localhost:4001/tag/cofe\">#cofe</a> with <span class=\"h-card\"><a data-user=\"#{
                  user2.id
-               }\" href=\"#{user2.ap_id}\">@<span>#{user2.nickname}</span></a></span>"
+               }\" class=\"u-url mention\" href=\"#{user2.ap_id}\">@<span>#{user2.nickname}</span></a></span>"
     end
 
     test "updates the user's locking status", %{conn: conn} do
@@ -1479,22 +1484,51 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
   end
 
   test "get instance information", %{conn: conn} do
-    insert(:user, %{local: true})
     user = insert(:user, %{local: true})
-    insert(:user, %{local: false})
+
+    user2 = insert(:user, %{local: true})
+    {:ok, _user2} = User.deactivate(user2, !user2.info.deactivated)
+
+    insert(:user, %{local: false, nickname: "u@peer1.com"})
+    insert(:user, %{local: false, nickname: "u@peer2.com"})
 
     {:ok, _} = TwitterAPI.create_status(user, %{"status" => "cofe"})
 
+    # Stats should count users with missing or nil `info.deactivated` value
+    user = Repo.get(User, user.id)
+    info_change = Changeset.change(user.info, %{deactivated: nil})
+
+    {:ok, _user} =
+      user
+      |> Changeset.change()
+      |> Changeset.put_embed(:info, info_change)
+      |> User.update_and_set_cache()
+
     Pleroma.Stats.update_stats()
 
-    conn =
-      conn
-      |> get("/api/v1/instance")
+    conn = get(conn, "/api/v1/instance")
 
     assert result = json_response(conn, 200)
 
-    assert result["stats"]["user_count"] == 2
-    assert result["stats"]["status_count"] == 1
+    stats = result["stats"]
+
+    assert stats
+    assert stats["user_count"] == 1
+    assert stats["status_count"] == 1
+    assert stats["domain_count"] == 2
+  end
+
+  test "get peers", %{conn: conn} do
+    insert(:user, %{local: false, nickname: "u@peer1.com"})
+    insert(:user, %{local: false, nickname: "u@peer2.com"})
+
+    Pleroma.Stats.update_stats()
+
+    conn = get(conn, "/api/v1/instance/peers")
+
+    assert result = json_response(conn, 200)
+
+    assert ["peer1.com", "peer2.com"] == Enum.sort(result)
   end
 
   test "put settings", %{conn: conn} do
@@ -1505,7 +1539,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
       |> assign(:user, user)
       |> put("/api/web/settings", %{"data" => %{"programming" => "socks"}})
 
-    assert result = json_response(conn, 200)
+    assert _result = json_response(conn, 200)
 
     user = User.get_cached_by_ap_id(user.ap_id)
     assert user.info.settings == %{"programming" => "socks"}
@@ -1589,5 +1623,22 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
                |> post("/api/v1/statuses/#{activity_two.id}/pin")
                |> json_response(400)
     end
+
+    test "Status rich-media Card", %{conn: conn, user: user} do
+      {:ok, activity} = CommonAPI.post(user, %{"status" => "http://example.com/ogp"})
+
+      response =
+        conn
+        |> get("/api/v1/statuses/#{activity.id}/card")
+        |> json_response(200)
+
+      assert response == %{
+               "image" => "http://ia.media-imdb.com/images/rock.jpg",
+               "provider_name" => "www.imdb.com",
+               "title" => "The Rock",
+               "type" => "link",
+               "url" => "http://www.imdb.com/title/tt0117500/"
+             }
+    end
   end
 end