Extract auth actions from `MastodonAPIController` to `AuthController`
[akkoma] / test / web / mastodon_api / mastodon_api_controller_test.exs
index da56061652f9e7b21cfeddf879eecd32e1f2c8e4..2ec5ad2bed6e99b24cac5c651552edf41b9437aa 100644 (file)
@@ -8,17 +8,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
   alias Ecto.Changeset
   alias Pleroma.Config
   alias Pleroma.Notification
-  alias Pleroma.Object
   alias Pleroma.Repo
-  alias Pleroma.Tests.ObanHelpers
   alias Pleroma.User
   alias Pleroma.Web.CommonAPI
-  alias Pleroma.Web.OAuth.App
-  alias Pleroma.Web.Push
 
-  import ExUnit.CaptureLog
   import Pleroma.Factory
-  import Swoosh.TestAssertions
   import Tesla.Mock
 
   setup do
@@ -29,53 +23,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
   clear_config([:instance, :public])
   clear_config([:rich_media, :enabled])
 
-  test "apps/verify_credentials", %{conn: conn} do
-    token = insert(:oauth_token)
-
-    conn =
-      conn
-      |> assign(:user, token.user)
-      |> assign(:token, token)
-      |> get("/api/v1/apps/verify_credentials")
-
-    app = Repo.preload(token, :app).app
-
-    expected = %{
-      "name" => app.client_name,
-      "website" => app.website,
-      "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
-    }
-
-    assert expected == json_response(conn, 200)
-  end
-
-  test "creates an oauth app", %{conn: conn} do
-    user = insert(:user)
-    app_attrs = build(:oauth_app)
-
-    conn =
-      conn
-      |> assign(:user, user)
-      |> post("/api/v1/apps", %{
-        client_name: app_attrs.client_name,
-        redirect_uris: app_attrs.redirect_uris
-      })
-
-    [app] = Repo.all(App)
-
-    expected = %{
-      "name" => app.client_name,
-      "website" => app.website,
-      "client_id" => app.client_id,
-      "client_secret" => app.client_secret,
-      "id" => app.id |> to_string(),
-      "redirect_uri" => app.redirect_uris,
-      "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
-    }
-
-    assert expected == json_response(conn, 200)
-  end
-
   test "getting a list of mutes", %{conn: conn} do
     user = insert(:user)
     other_user = insert(:user)
@@ -354,176 +301,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
 
       assert return_to == path
     end
-
-    test "redirects to the saved path after log in", %{conn: conn, path: path} do
-      app = insert(:oauth_app, client_name: "Mastodon-Local", redirect_uris: ".")
-      auth = insert(:oauth_authorization, app: app)
-
-      conn =
-        conn
-        |> put_session(:return_to, path)
-        |> get("/web/login", %{code: auth.token})
-
-      assert conn.status == 302
-      assert redirected_to(conn) == path
-    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: ".")
-      auth = insert(:oauth_authorization, app: app)
-
-      conn = get(conn, "/web/login", %{code: auth.token})
-
-      assert conn.status == 302
-      assert redirected_to(conn) == "/web/getting-started"
-    end
-  end
-
-  describe "POST /auth/password, with valid parameters" do
-    setup %{conn: conn} do
-      user = insert(:user)
-      conn = post(conn, "/auth/password?email=#{user.email}")
-      %{conn: conn, user: user}
-    end
-
-    test "it returns 204", %{conn: conn} do
-      assert json_response(conn, :no_content)
-    end
-
-    test "it creates a PasswordResetToken record for user", %{user: user} do
-      token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)
-      assert token_record
-    end
-
-    test "it sends an email to user", %{user: user} do
-      ObanHelpers.perform_all()
-      token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)
-
-      email = Pleroma.Emails.UserEmail.password_reset_email(user, token_record.token)
-      notify_email = Config.get([:instance, :notify_email])
-      instance_name = Config.get([:instance, :name])
-
-      assert_email_sent(
-        from: {instance_name, notify_email},
-        to: {user.name, user.email},
-        html_body: email.html_body
-      )
-    end
-  end
-
-  describe "POST /auth/password, with invalid parameters" do
-    setup do
-      user = insert(:user)
-      {:ok, user: user}
-    end
-
-    test "it returns 404 when user is not found", %{conn: conn, user: user} do
-      conn = post(conn, "/auth/password?email=nonexisting_#{user.email}")
-      assert conn.status == 404
-      assert conn.resp_body == ""
-    end
-
-    test "it returns 400 when user is not local", %{conn: conn, user: user} do
-      {:ok, user} = Repo.update(Changeset.change(user, local: false))
-      conn = post(conn, "/auth/password?email=#{user.email}")
-      assert conn.status == 400
-      assert conn.resp_body == ""
-    end
-  end
-
-  describe "GET /api/v1/suggestions" do
-    setup do
-      user = insert(:user)
-      other_user = insert(:user)
-      host = Config.get([Pleroma.Web.Endpoint, :url, :host])
-      url500 = "http://test500?#{host}&#{user.nickname}"
-      url200 = "http://test200?#{host}&#{user.nickname}"
-
-      mock(fn
-        %{method: :get, url: ^url500} ->
-          %Tesla.Env{status: 500, body: "bad request"}
-
-        %{method: :get, url: ^url200} ->
-          %Tesla.Env{
-            status: 200,
-            body:
-              ~s([{"acct":"yj455","avatar":"https://social.heldscal.la/avatar/201.jpeg","avatar_static":"https://social.heldscal.la/avatar/s/201.jpeg"}, {"acct":"#{
-                other_user.ap_id
-              }","avatar":"https://social.heldscal.la/avatar/202.jpeg","avatar_static":"https://social.heldscal.la/avatar/s/202.jpeg"}])
-          }
-      end)
-
-      [user: user, other_user: other_user]
-    end
-
-    clear_config(:suggestions)
-
-    test "returns empty result when suggestions disabled", %{conn: conn, user: user} do
-      Config.put([:suggestions, :enabled], false)
-
-      res =
-        conn
-        |> assign(:user, user)
-        |> get("/api/v1/suggestions")
-        |> json_response(200)
-
-      assert res == []
-    end
-
-    test "returns error", %{conn: conn, user: user} do
-      Config.put([:suggestions, :enabled], true)
-      Config.put([:suggestions, :third_party_engine], "http://test500?{{host}}&{{user}}")
-
-      assert capture_log(fn ->
-               res =
-                 conn
-                 |> assign(:user, user)
-                 |> get("/api/v1/suggestions")
-                 |> json_response(500)
-
-               assert res == "Something went wrong"
-             end) =~ "Could not retrieve suggestions"
-    end
-
-    test "returns suggestions", %{conn: conn, user: user, other_user: other_user} do
-      Config.put([:suggestions, :enabled], true)
-      Config.put([:suggestions, :third_party_engine], "http://test200?{{host}}&{{user}}")
-
-      res =
-        conn
-        |> assign(:user, user)
-        |> get("/api/v1/suggestions")
-        |> json_response(200)
-
-      assert res == [
-               %{
-                 "acct" => "yj455",
-                 "avatar" => "https://social.heldscal.la/avatar/201.jpeg",
-                 "avatar_static" => "https://social.heldscal.la/avatar/s/201.jpeg",
-                 "id" => 0
-               },
-               %{
-                 "acct" => other_user.ap_id,
-                 "avatar" => "https://social.heldscal.la/avatar/202.jpeg",
-                 "avatar_static" => "https://social.heldscal.la/avatar/s/202.jpeg",
-                 "id" => other_user.id
-               }
-             ]
-    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 "empty_array, stubs for mastodon api" do