Merge branch 'develop' into tests/mastodon_api_controller.ex
[akkoma] / test / web / mastodon_api / mastodon_api_controller_test.exs
index cd672132bb6a0542488f83dab8fd50877427ddf6..1e9829886546fd13375aea70cebd14ca0c430042 100644 (file)
@@ -1287,6 +1287,17 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
 
       assert to_string(other_user.id) == relationship["id"]
     end
+
+    test "returns an empty list on a bad request", %{conn: conn} do
+      user = insert(:user)
+
+      conn =
+        conn
+        |> assign(:user, user)
+        |> get("/api/v1/accounts/relationships", %{})
+
+      assert [] = json_response(conn, 200)
+    end
   end
 
   describe "media upload" do
@@ -1488,70 +1499,72 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     end
   end
 
-  test "mascot upload", %{conn: conn} do
-    user = insert(:user)
+  describe "/api/v1/pleroma/mascot" do
+    test "mascot upload", %{conn: conn} do
+      user = insert(:user)
 
-    non_image_file = %Plug.Upload{
-      content_type: "audio/mpeg",
-      path: Path.absname("test/fixtures/sound.mp3"),
-      filename: "sound.mp3"
-    }
+      non_image_file = %Plug.Upload{
+        content_type: "audio/mpeg",
+        path: Path.absname("test/fixtures/sound.mp3"),
+        filename: "sound.mp3"
+      }
 
-    conn =
-      conn
-      |> assign(:user, user)
-      |> put("/api/v1/pleroma/mascot", %{"file" => non_image_file})
+      conn =
+        conn
+        |> assign(:user, user)
+        |> put("/api/v1/pleroma/mascot", %{"file" => non_image_file})
 
-    assert json_response(conn, 415)
+      assert json_response(conn, 415)
 
-    file = %Plug.Upload{
-      content_type: "image/jpg",
-      path: Path.absname("test/fixtures/image.jpg"),
-      filename: "an_image.jpg"
-    }
+      file = %Plug.Upload{
+        content_type: "image/jpg",
+        path: Path.absname("test/fixtures/image.jpg"),
+        filename: "an_image.jpg"
+      }
 
-    conn =
-      build_conn()
-      |> assign(:user, user)
-      |> put("/api/v1/pleroma/mascot", %{"file" => file})
+      conn =
+        build_conn()
+        |> assign(:user, user)
+        |> put("/api/v1/pleroma/mascot", %{"file" => file})
 
-    assert %{"id" => _, "type" => image} = json_response(conn, 200)
-  end
+      assert %{"id" => _, "type" => image} = json_response(conn, 200)
+    end
 
-  test "mascot retrieving", %{conn: conn} do
-    user = insert(:user)
-    # When user hasn't set a mascot, we should just get pleroma tan back
-    conn =
-      conn
-      |> assign(:user, user)
-      |> get("/api/v1/pleroma/mascot")
+    test "mascot retrieving", %{conn: conn} do
+      user = insert(:user)
+      # When user hasn't set a mascot, we should just get pleroma tan back
+      conn =
+        conn
+        |> assign(:user, user)
+        |> get("/api/v1/pleroma/mascot")
 
-    assert %{"url" => url} = json_response(conn, 200)
-    assert url =~ "pleroma-fox-tan-smol"
+      assert %{"url" => url} = json_response(conn, 200)
+      assert url =~ "pleroma-fox-tan-smol"
 
-    # When a user sets their mascot, we should get that back
-    file = %Plug.Upload{
-      content_type: "image/jpg",
-      path: Path.absname("test/fixtures/image.jpg"),
-      filename: "an_image.jpg"
-    }
+      # When a user sets their mascot, we should get that back
+      file = %Plug.Upload{
+        content_type: "image/jpg",
+        path: Path.absname("test/fixtures/image.jpg"),
+        filename: "an_image.jpg"
+      }
 
-    conn =
-      build_conn()
-      |> assign(:user, user)
-      |> put("/api/v1/pleroma/mascot", %{"file" => file})
+      conn =
+        build_conn()
+        |> assign(:user, user)
+        |> put("/api/v1/pleroma/mascot", %{"file" => file})
 
-    assert json_response(conn, 200)
+      assert json_response(conn, 200)
 
-    user = User.get_cached_by_id(user.id)
+      user = User.get_cached_by_id(user.id)
 
-    conn =
-      build_conn()
-      |> assign(:user, user)
-      |> get("/api/v1/pleroma/mascot")
+      conn =
+        build_conn()
+        |> assign(:user, user)
+        |> get("/api/v1/pleroma/mascot")
 
-    assert %{"url" => url, "type" => "image"} = json_response(conn, 200)
-    assert url =~ "an_image"
+      assert %{"url" => url, "type" => "image"} = json_response(conn, 200)
+      assert url =~ "an_image"
+    end
   end
 
   test "hashtag timeline", %{conn: conn} do
@@ -1919,23 +1932,51 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     end
   end
 
-  test "subscribing / unsubscribing to a user", %{conn: conn} do
-    user = insert(:user)
-    subscription_target = insert(:user)
+  describe "subscribing / unsubscribing" do
+    test "subscribing / unsubscribing to a user", %{conn: conn} do
+      user = insert(:user)
+      subscription_target = insert(:user)
 
-    conn =
-      conn
-      |> assign(:user, user)
-      |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
+      conn =
+        conn
+        |> assign(:user, user)
+        |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
 
-    assert %{"id" => _id, "subscribing" => true} = json_response(conn, 200)
+      assert %{"id" => _id, "subscribing" => true} = json_response(conn, 200)
 
-    conn =
-      build_conn()
-      |> assign(:user, user)
-      |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
+      conn =
+        build_conn()
+        |> assign(:user, user)
+        |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
 
-    assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200)
+      assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200)
+    end
+  end
+
+  describe "subscribing" do
+    test "returns 404 when subscription_target not found", %{conn: conn} do
+      user = insert(:user)
+
+      conn =
+        conn
+        |> assign(:user, user)
+        |> post("/api/v1/pleroma/accounts/target_id/subscribe")
+
+      assert %{"error" => "Record not found"} = json_response(conn, 404)
+    end
+  end
+
+  describe "unsubscribing" do
+    test "returns 404 when subscription_target not found", %{conn: conn} do
+      user = insert(:user)
+
+      conn =
+        conn
+        |> assign(:user, user)
+        |> post("/api/v1/pleroma/accounts/target_id/unsubscribe")
+
+      assert %{"error" => "Record not found"} = json_response(conn, 404)
+    end
   end
 
   test "getting a list of mutes", %{conn: conn} do
@@ -2477,6 +2518,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
       %{user: user}
     end
 
+    test "returns empty result when rich_media disabled", %{conn: conn, user: user} do
+      Config.put([:rich_media, :enabled], false)
+      {:ok, activity} = CommonAPI.post(user, %{"status" => "https://example.com/ogp"})
+
+      response =
+        conn
+        |> get("/api/v1/statuses/#{activity.id}/card")
+        |> json_response(200)
+
+      assert response == nil
+    end
+
     test "returns rich-media card", %{conn: conn, user: user} do
       {:ok, activity} = CommonAPI.post(user, %{"status" => "https://example.com/ogp"})
 
@@ -2547,6 +2600,25 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
                }
              }
     end
+
+    test "returns 404 response when id invalid", %{conn: conn} do
+      assert %{"error" => "Record not found"} =
+               conn
+               |> get("/api/v1/statuses/9eoozpwTul5mjSEDRI/card")
+               |> json_response(404)
+    end
+
+    test "returns 404 response when id isn't FlakeID", %{conn: conn} do
+      assert %{"error" => "Record not found"} =
+               conn
+               |> get("/api/v1/statuses/3ebbadd1-eb14-4e20-8118/card")
+               |> json_response(404)
+
+      assert %{"error" => "Record not found"} =
+               conn
+               |> get("/api/v1/statuses/8118/card")
+               |> json_response(404)
+    end
   end
 
   test "bookmarks" do
@@ -2866,6 +2938,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
       assert conn.status == 302
       assert redirected_to(conn) == path
     end
+  end
+
+  describe "GET /web/login" do
+    test "redirects to /oauth/authorize", %{conn: conn} do
+      app = insert(:oauth_app, client_name: "Mastodon-Local", redirect_uris: ".")
+      conn = get(conn, "/web/login", %{})
+
+      assert conn.status == 302
+
+      assert redirected_to(conn) ==
+               "/oauth/authorize?response_type=code&client_id=#{app.client_id}&redirect_uri=.&scope=read+write+follow+push"
+    end
 
     test "redirects to the getting-started page when referer is not present", %{conn: conn} do
       app = insert(:oauth_app, client_name: "Mastodon-Local", redirect_uris: ".")
@@ -2876,6 +2960,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
       assert conn.status == 302
       assert redirected_to(conn) == "/web/getting-started"
     end
+
+    test "redirects to the getting-started page when user assigned", %{conn: conn} do
+      user = insert(:user)
+
+      conn =
+        conn
+        |> assign(:user, user)
+        |> get("/web/login", %{})
+
+      assert conn.status == 302
+      assert redirected_to(conn) == "/web/getting-started"
+    end
   end
 
   describe "scheduled activities" do
@@ -3134,6 +3230,17 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
   end
 
   describe "create account by app" do
+    setup do
+      valid_params = %{
+        username: "lain",
+        email: "lain@example.org",
+        password: "PlzDontHackLain",
+        agreement: true
+      }
+
+      [valid_params: valid_params]
+    end
+
     test "Account registration via Application", %{conn: conn} do
       conn =
         conn
@@ -3177,6 +3284,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
           username: "lain",
           email: "lain@example.org",
           password: "PlzDontHackLain",
+          bio: "Test Bio",
           agreement: true
         })
 
@@ -3195,6 +3303,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
       assert token_from_db.user.info.confirmation_pending
     end
 
+    test "returns error when user already registred", %{conn: conn, valid_params: valid_params} do
+      _user = insert(:user, email: "lain@example.org")
+      app_token = insert(:oauth_token, user: nil)
+
+      conn =
+        conn
+        |> put_req_header("authorization", "Bearer " <> app_token.token)
+
+      res = post(conn, "/api/v1/accounts", valid_params)
+      assert json_response(res, 400) == %{"error" => "{\"email\":[\"has already been taken\"]}"}
+    end
+
     test "rate limit", %{conn: conn} do
       app_token = insert(:oauth_token, user: nil)
 
@@ -3238,6 +3358,41 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
 
       assert json_response(conn, :too_many_requests) == %{"error" => "Throttled"}
     end
+
+    test "returns bad_request if missing required params", %{
+      conn: conn,
+      valid_params: valid_params
+    } do
+      app_token = insert(:oauth_token, user: nil)
+
+      conn =
+        conn
+        |> put_req_header("authorization", "Bearer " <> app_token.token)
+
+      res = post(conn, "/api/v1/accounts", valid_params)
+      assert json_response(res, 200)
+
+      [{127, 0, 0, 1}, {127, 0, 0, 2}, {127, 0, 0, 3}, {127, 0, 0, 4}]
+      |> Stream.zip(valid_params)
+      |> Enum.each(fn {ip, {attr, _}} ->
+        res =
+          conn
+          |> Map.put(:remote_ip, ip)
+          |> post("/api/v1/accounts", Map.delete(valid_params, attr))
+          |> json_response(400)
+
+        assert res == %{"error" => "Missing parameters"}
+      end)
+    end
+
+    test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
+      conn =
+        conn
+        |> put_req_header("authorization", "Bearer " <> "invalid-token")
+
+      res = post(conn, "/api/v1/accounts", valid_params)
+      assert json_response(res, 403) == %{"error" => "Invalid credentials"}
+    end
   end
 
   describe "GET /api/v1/polls/:id" do
@@ -3771,4 +3926,115 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
              ]
     end
   end
+
+  describe "PUT /api/v1/media/:id" do
+    setup do
+      actor = insert(:user)
+
+      file = %Plug.Upload{
+        content_type: "image/jpg",
+        path: Path.absname("test/fixtures/image.jpg"),
+        filename: "an_image.jpg"
+      }
+
+      {:ok, %Object{} = object} =
+        ActivityPub.upload(
+          file,
+          actor: User.ap_id(actor),
+          description: "test-m"
+        )
+
+      [actor: actor, object: object]
+    end
+
+    test "updates name of media", %{conn: conn, actor: actor, object: object} do
+      media =
+        conn
+        |> assign(:user, actor)
+        |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
+        |> json_response(:ok)
+
+      assert media["description"] == "test-media"
+      assert refresh_record(object).data["name"] == "test-media"
+    end
+
+    test "returns error wheb request is bad", %{conn: conn, actor: actor, object: object} do
+      media =
+        conn
+        |> assign(:user, actor)
+        |> put("/api/v1/media/#{object.id}", %{})
+        |> json_response(400)
+
+      assert media == %{"error" => "bad_request"}
+    end
+  end
+
+  describe "DELETE /auth/sign_out" do
+    test "redirect to root page", %{conn: conn} do
+      user = insert(:user)
+
+      conn =
+        conn
+        |> assign(:user, user)
+        |> delete("/auth/sign_out")
+
+      assert conn.status == 302
+      assert redirected_to(conn) == "/"
+    end
+  end
+
+  describe "GET /api/v1/accounts/:id/lists - account_lists" do
+    test "returns lists to which the account belongs", %{conn: conn} do
+      user = insert(:user)
+      other_user = insert(:user)
+      assert {:ok, %Pleroma.List{} = list} = Pleroma.List.create("Test List", user)
+      {:ok, %{following: _following}} = Pleroma.List.follow(list, other_user)
+
+      res =
+        conn
+        |> assign(:user, user)
+        |> get("/api/v1/accounts/#{other_user.id}/lists")
+        |> json_response(200)
+
+      assert res == [%{"id" => to_string(list.id), "title" => "Test List"}]
+    end
+  end
+
+  describe "empty_array, stubs for mastodon api" do
+    test "GET /api/v1/accounts/:id/identity_proofs", %{conn: conn} do
+      user = insert(:user)
+
+      res =
+        conn
+        |> assign(:user, user)
+        |> get("/api/v1/accounts/#{user.id}/identity_proofs")
+        |> json_response(200)
+
+      assert res == []
+    end
+
+    test "GET /api/v1/endorsements", %{conn: conn} do
+      user = insert(:user)
+
+      res =
+        conn
+        |> assign(:user, user)
+        |> get("/api/v1/endorsements")
+        |> json_response(200)
+
+      assert res == []
+    end
+
+    test "GET /api/v1/trends", %{conn: conn} do
+      user = insert(:user)
+
+      res =
+        conn
+        |> assign(:user, user)
+        |> get("/api/v1/trends")
+        |> json_response(200)
+
+      assert res == []
+    end
+  end
 end