diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/plugs/authentication_plug_test.exs | 7 | ||||
| -rw-r--r-- | test/plugs/legacy_authentication_plug_test.exs | 6 | ||||
| -rw-r--r-- | test/plugs/oauth_scopes_plug_test.exs | 3 | ||||
| -rw-r--r-- | test/support/factory.ex | 2 | ||||
| -rw-r--r-- | test/tasks/app_test.exs | 65 | ||||
| -rw-r--r-- | test/web/activity_pub/side_effects_test.exs | 12 | ||||
| -rw-r--r-- | test/web/admin_api/admin_api_controller_test.exs | 185 | ||||
| -rw-r--r-- | test/web/auth/basic_auth_test.exs | 46 | ||||
| -rw-r--r-- | test/web/mastodon_api/controllers/account_controller_test.exs | 67 | 
9 files changed, 386 insertions, 7 deletions
| diff --git a/test/plugs/authentication_plug_test.exs b/test/plugs/authentication_plug_test.exs index ae2f3f8ec..646bda9d3 100644 --- a/test/plugs/authentication_plug_test.exs +++ b/test/plugs/authentication_plug_test.exs @@ -6,6 +6,8 @@ defmodule Pleroma.Plugs.AuthenticationPlugTest do    use Pleroma.Web.ConnCase, async: true    alias Pleroma.Plugs.AuthenticationPlug +  alias Pleroma.Plugs.OAuthScopesPlug +  alias Pleroma.Plugs.PlugHelper    alias Pleroma.User    import ExUnit.CaptureLog @@ -36,13 +38,16 @@ defmodule Pleroma.Plugs.AuthenticationPlugTest do      assert ret_conn == conn    end -  test "with a correct password in the credentials, it assigns the auth_user", %{conn: conn} do +  test "with a correct password in the credentials, " <> +         "it assigns the auth_user and marks OAuthScopesPlug as skipped", +       %{conn: conn} do      conn =        conn        |> assign(:auth_credentials, %{password: "guy"})        |> AuthenticationPlug.call(%{})      assert conn.assigns.user == conn.assigns.auth_user +    assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)    end    test "with a wrong password in the credentials, it does nothing", %{conn: conn} do diff --git a/test/plugs/legacy_authentication_plug_test.exs b/test/plugs/legacy_authentication_plug_test.exs index 7559de7d3..3b8c07627 100644 --- a/test/plugs/legacy_authentication_plug_test.exs +++ b/test/plugs/legacy_authentication_plug_test.exs @@ -8,6 +8,8 @@ defmodule Pleroma.Plugs.LegacyAuthenticationPlugTest do    import Pleroma.Factory    alias Pleroma.Plugs.LegacyAuthenticationPlug +  alias Pleroma.Plugs.OAuthScopesPlug +  alias Pleroma.Plugs.PlugHelper    alias Pleroma.User    setup do @@ -36,7 +38,8 @@ defmodule Pleroma.Plugs.LegacyAuthenticationPlugTest do    end    @tag :skip_on_mac -  test "it authenticates the auth_user if present and password is correct and resets the password", +  test "if `auth_user` is present and password is correct, " <> +         "it authenticates the user, resets the password, marks OAuthScopesPlug as skipped",         %{           conn: conn,           user: user @@ -49,6 +52,7 @@ defmodule Pleroma.Plugs.LegacyAuthenticationPlugTest do      conn = LegacyAuthenticationPlug.call(conn, %{})      assert conn.assigns.user.id == user.id +    assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)    end    @tag :skip_on_mac diff --git a/test/plugs/oauth_scopes_plug_test.exs b/test/plugs/oauth_scopes_plug_test.exs index abab7abb0..edbc94227 100644 --- a/test/plugs/oauth_scopes_plug_test.exs +++ b/test/plugs/oauth_scopes_plug_test.exs @@ -7,7 +7,6 @@ defmodule Pleroma.Plugs.OAuthScopesPlugTest do    alias Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug    alias Pleroma.Plugs.OAuthScopesPlug -  alias Pleroma.Plugs.PlugHelper    alias Pleroma.Repo    import Mock @@ -21,7 +20,7 @@ defmodule Pleroma.Plugs.OAuthScopesPlugTest do      with_mock OAuthScopesPlug, [:passthrough], perform: &passthrough([&1, &2]) do        conn =          conn -        |> PlugHelper.append_to_skipped_plugs(OAuthScopesPlug) +        |> OAuthScopesPlug.skip_plug()          |> OAuthScopesPlug.call(%{scopes: ["random_scope"]})        refute called(OAuthScopesPlug.perform(:_, :_)) diff --git a/test/support/factory.ex b/test/support/factory.ex index af639b6cd..f0b797fd4 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -294,7 +294,7 @@ defmodule Pleroma.Factory do    def oauth_app_factory do      %Pleroma.Web.OAuth.App{ -      client_name: "Some client", +      client_name: sequence(:client_name, &"Some client #{&1}"),        redirect_uris: "https://example.com/callback",        scopes: ["read", "write", "follow", "push", "admin"],        website: "https://example.com", diff --git a/test/tasks/app_test.exs b/test/tasks/app_test.exs new file mode 100644 index 000000000..b8f03566d --- /dev/null +++ b/test/tasks/app_test.exs @@ -0,0 +1,65 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Mix.Tasks.Pleroma.AppTest do +  use Pleroma.DataCase, async: true + +  setup_all do +    Mix.shell(Mix.Shell.Process) + +    on_exit(fn -> +      Mix.shell(Mix.Shell.IO) +    end) +  end + +  describe "creates new app" do +    test "with default scopes" do +      name = "Some name" +      redirect = "https://example.com" +      Mix.Tasks.Pleroma.App.run(["create", "-n", name, "-r", redirect]) + +      assert_app(name, redirect, ["read", "write", "follow", "push"]) +    end + +    test "with custom scopes" do +      name = "Another name" +      redirect = "https://example.com" + +      Mix.Tasks.Pleroma.App.run([ +        "create", +        "-n", +        name, +        "-r", +        redirect, +        "-s", +        "read,write,follow,push,admin" +      ]) + +      assert_app(name, redirect, ["read", "write", "follow", "push", "admin"]) +    end +  end + +  test "with errors" do +    Mix.Tasks.Pleroma.App.run(["create"]) +    {:mix_shell, :error, ["Creating failed:"]} +    {:mix_shell, :error, ["name: can't be blank"]} +    {:mix_shell, :error, ["redirect_uris: can't be blank"]} +  end + +  defp assert_app(name, redirect, scopes) do +    app = Repo.get_by(Pleroma.Web.OAuth.App, client_name: name) + +    assert_received {:mix_shell, :info, [message]} +    assert message == "#{name} successfully created:" + +    assert_received {:mix_shell, :info, [message]} +    assert message == "App client_id: #{app.client_id}" + +    assert_received {:mix_shell, :info, [message]} +    assert message == "App client_secret: #{app.client_secret}" + +    assert app.scopes == scopes +    assert app.redirect_uris == redirect +  end +end diff --git a/test/web/activity_pub/side_effects_test.exs b/test/web/activity_pub/side_effects_test.exs index b67bd14b3..0b6b55156 100644 --- a/test/web/activity_pub/side_effects_test.exs +++ b/test/web/activity_pub/side_effects_test.exs @@ -5,7 +5,9 @@  defmodule Pleroma.Web.ActivityPub.SideEffectsTest do    use Pleroma.DataCase +  alias Pleroma.Notification    alias Pleroma.Object +  alias Pleroma.Repo    alias Pleroma.Web.ActivityPub.ActivityPub    alias Pleroma.Web.ActivityPub.Builder    alias Pleroma.Web.ActivityPub.SideEffects @@ -15,13 +17,14 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do    describe "like objects" do      setup do +      poster = insert(:user)        user = insert(:user) -      {:ok, post} = CommonAPI.post(user, %{"status" => "hey"}) +      {:ok, post} = CommonAPI.post(poster, %{"status" => "hey"})        {:ok, like_data, _meta} = Builder.like(user, post.object)        {:ok, like, _meta} = ActivityPub.persist(like_data, local: true) -      %{like: like, user: user} +      %{like: like, user: user, poster: poster}      end      test "add the like to the original object", %{like: like, user: user} do @@ -30,5 +33,10 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do        assert object.data["like_count"] == 1        assert user.ap_id in object.data["likes"]      end + +    test "creates a notification", %{like: like, poster: poster} do +      {:ok, like, _} = SideEffects.handle(like) +      assert Repo.get_by(Notification, user_id: poster.id, activity_id: like.id) +    end    end  end diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 158966365..f80dbf8dd 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -3517,6 +3517,191 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                 response["status_visibility"]      end    end + +  describe "POST /api/pleroma/admin/oauth_app" do +    test "errors", %{conn: conn} do +      response = conn |> post("/api/pleroma/admin/oauth_app", %{}) |> json_response(200) + +      assert response == %{"name" => "can't be blank", "redirect_uris" => "can't be blank"} +    end + +    test "success", %{conn: conn} do +      base_url = Pleroma.Web.base_url() +      app_name = "Trusted app" + +      response = +        conn +        |> post("/api/pleroma/admin/oauth_app", %{ +          name: app_name, +          redirect_uris: base_url +        }) +        |> json_response(200) + +      assert %{ +               "client_id" => _, +               "client_secret" => _, +               "name" => ^app_name, +               "redirect_uri" => ^base_url, +               "trusted" => false +             } = response +    end + +    test "with trusted", %{conn: conn} do +      base_url = Pleroma.Web.base_url() +      app_name = "Trusted app" + +      response = +        conn +        |> post("/api/pleroma/admin/oauth_app", %{ +          name: app_name, +          redirect_uris: base_url, +          trusted: true +        }) +        |> json_response(200) + +      assert %{ +               "client_id" => _, +               "client_secret" => _, +               "name" => ^app_name, +               "redirect_uri" => ^base_url, +               "trusted" => true +             } = response +    end +  end + +  describe "GET /api/pleroma/admin/oauth_app" do +    setup do +      app = insert(:oauth_app) +      {:ok, app: app} +    end + +    test "list", %{conn: conn} do +      response = +        conn +        |> get("/api/pleroma/admin/oauth_app") +        |> json_response(200) + +      assert %{"apps" => apps, "count" => count, "page_size" => _} = response + +      assert length(apps) == count +    end + +    test "with page size", %{conn: conn} do +      insert(:oauth_app) +      page_size = 1 + +      response = +        conn +        |> get("/api/pleroma/admin/oauth_app", %{page_size: to_string(page_size)}) +        |> json_response(200) + +      assert %{"apps" => apps, "count" => _, "page_size" => ^page_size} = response + +      assert length(apps) == page_size +    end + +    test "search by client name", %{conn: conn, app: app} do +      response = +        conn +        |> get("/api/pleroma/admin/oauth_app", %{name: app.client_name}) +        |> json_response(200) + +      assert %{"apps" => [returned], "count" => _, "page_size" => _} = response + +      assert returned["client_id"] == app.client_id +      assert returned["name"] == app.client_name +    end + +    test "search by client id", %{conn: conn, app: app} do +      response = +        conn +        |> get("/api/pleroma/admin/oauth_app", %{client_id: app.client_id}) +        |> json_response(200) + +      assert %{"apps" => [returned], "count" => _, "page_size" => _} = response + +      assert returned["client_id"] == app.client_id +      assert returned["name"] == app.client_name +    end + +    test "only trusted", %{conn: conn} do +      app = insert(:oauth_app, trusted: true) + +      response = +        conn +        |> get("/api/pleroma/admin/oauth_app", %{trusted: true}) +        |> json_response(200) + +      assert %{"apps" => [returned], "count" => _, "page_size" => _} = response + +      assert returned["client_id"] == app.client_id +      assert returned["name"] == app.client_name +    end +  end + +  describe "DELETE /api/pleroma/admin/oauth_app/:id" do +    test "with id", %{conn: conn} do +      app = insert(:oauth_app) + +      response = +        conn +        |> delete("/api/pleroma/admin/oauth_app/" <> to_string(app.id)) +        |> json_response(:no_content) + +      assert response == "" +    end + +    test "with non existance id", %{conn: conn} do +      response = +        conn +        |> delete("/api/pleroma/admin/oauth_app/0") +        |> json_response(:bad_request) + +      assert response == "" +    end +  end + +  describe "PATCH /api/pleroma/admin/oauth_app/:id" do +    test "with id", %{conn: conn} do +      app = insert(:oauth_app) + +      name = "another name" +      url = "https://example.com" +      scopes = ["admin"] +      id = app.id +      website = "http://website.com" + +      response = +        conn +        |> patch("/api/pleroma/admin/oauth_app/" <> to_string(app.id), %{ +          name: name, +          trusted: true, +          redirect_uris: url, +          scopes: scopes, +          website: website +        }) +        |> json_response(200) + +      assert %{ +               "client_id" => _, +               "client_secret" => _, +               "id" => ^id, +               "name" => ^name, +               "redirect_uri" => ^url, +               "trusted" => true, +               "website" => ^website +             } = response +    end + +    test "without id", %{conn: conn} do +      response = +        conn +        |> patch("/api/pleroma/admin/oauth_app/0") +        |> json_response(:bad_request) + +      assert response == "" +    end +  end  end  # Needed for testing diff --git a/test/web/auth/basic_auth_test.exs b/test/web/auth/basic_auth_test.exs new file mode 100644 index 000000000..64f8a6863 --- /dev/null +++ b/test/web/auth/basic_auth_test.exs @@ -0,0 +1,46 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.Auth.BasicAuthTest do +  use Pleroma.Web.ConnCase + +  import Pleroma.Factory + +  test "with HTTP Basic Auth used, grants access to OAuth scope-restricted endpoints", %{ +    conn: conn +  } do +    user = insert(:user) +    assert Comeonin.Pbkdf2.checkpw("test", user.password_hash) + +    basic_auth_contents = +      (URI.encode_www_form(user.nickname) <> ":" <> URI.encode_www_form("test")) +      |> Base.encode64() + +    # Succeeds with HTTP Basic Auth +    response = +      conn +      |> put_req_header("authorization", "Basic " <> basic_auth_contents) +      |> get("/api/v1/accounts/verify_credentials") +      |> json_response(200) + +    user_nickname = user.nickname +    assert %{"username" => ^user_nickname} = response + +    # Succeeds with a properly scoped OAuth token +    valid_token = insert(:oauth_token, scopes: ["read:accounts"]) + +    conn +    |> put_req_header("authorization", "Bearer #{valid_token.token}") +    |> get("/api/v1/accounts/verify_credentials") +    |> json_response(200) + +    # Fails with a wrong-scoped OAuth token (proof of restriction) +    invalid_token = insert(:oauth_token, scopes: ["read:something"]) + +    conn +    |> put_req_header("authorization", "Bearer #{invalid_token.token}") +    |> get("/api/v1/accounts/verify_credentials") +    |> json_response(403) +  end +end diff --git a/test/web/mastodon_api/controllers/account_controller_test.exs b/test/web/mastodon_api/controllers/account_controller_test.exs index a450a732c..61c2697b2 100644 --- a/test/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/web/mastodon_api/controllers/account_controller_test.exs @@ -944,6 +944,73 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do        res = post(conn, "/api/v1/accounts", valid_params)        assert json_response(res, 403) == %{"error" => "Invalid credentials"}      end + +    test "registration from trusted app" do +      clear_config([Pleroma.Captcha, :enabled], true) +      app = insert(:oauth_app, trusted: true, scopes: ["read", "write", "follow", "push"]) + +      conn = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "client_credentials", +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) + +      assert %{"access_token" => token, "token_type" => "Bearer"} = json_response(conn, 200) + +      response = +        build_conn() +        |> Plug.Conn.put_req_header("authorization", "Bearer " <> token) +        |> post("/api/v1/accounts", %{ +          nickname: "nickanme", +          agreement: true, +          email: "email@example.com", +          fullname: "Lain", +          username: "Lain", +          password: "some_password", +          confirm: "some_password" +        }) +        |> json_response(200) + +      assert %{ +               "access_token" => access_token, +               "created_at" => _, +               "scope" => ["read", "write", "follow", "push"], +               "token_type" => "Bearer" +             } = response + +      response = +        build_conn() +        |> Plug.Conn.put_req_header("authorization", "Bearer " <> access_token) +        |> get("/api/v1/accounts/verify_credentials") +        |> json_response(200) + +      assert %{ +               "acct" => "Lain", +               "bot" => false, +               "display_name" => "Lain", +               "follow_requests_count" => 0, +               "followers_count" => 0, +               "following_count" => 0, +               "locked" => false, +               "note" => "", +               "source" => %{ +                 "fields" => [], +                 "note" => "", +                 "pleroma" => %{ +                   "actor_type" => "Person", +                   "discoverable" => false, +                   "no_rich_text" => false, +                   "show_role" => true +                 }, +                 "privacy" => "public", +                 "sensitive" => false +               }, +               "statuses_count" => 0, +               "username" => "Lain" +             } = response +    end    end    describe "create account by app / rate limit" do | 
