diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/http/tzdata_test.exs | 35 | ||||
| -rw-r--r-- | test/http_test.exs | 9 | ||||
| -rw-r--r-- | test/web/activity_pub/activity_pub_controller_test.exs | 10 | ||||
| -rw-r--r-- | test/web/activity_pub/activity_pub_test.exs | 46 | ||||
| -rw-r--r-- | test/web/activity_pub/object_validator_test.exs | 32 | ||||
| -rw-r--r-- | test/web/activity_pub/side_effects_test.exs | 25 | ||||
| -rw-r--r-- | test/web/activity_pub/transmogrifier/user_update_handling_test.exs | 159 | ||||
| -rw-r--r-- | test/web/activity_pub/transmogrifier_test.exs | 156 | 
8 files changed, 267 insertions, 205 deletions
| diff --git a/test/http/tzdata_test.exs b/test/http/tzdata_test.exs new file mode 100644 index 000000000..3e605d33b --- /dev/null +++ b/test/http/tzdata_test.exs @@ -0,0 +1,35 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.HTTP.TzdataTest do +  use ExUnit.Case + +  import Tesla.Mock +  alias Pleroma.HTTP +  @url "https://data.iana.org/time-zones/tzdata-latest.tar.gz" + +  setup do +    mock(fn +      %{method: :head, url: @url} -> +        %Tesla.Env{status: 200, body: ""} + +      %{method: :get, url: @url} -> +        %Tesla.Env{status: 200, body: "hello"} +    end) + +    :ok +  end + +  describe "head/1" do +    test "returns successfully result" do +      assert HTTP.Tzdata.head(@url, [], []) == {:ok, {200, []}} +    end +  end + +  describe "get/1" do +    test "returns successfully result" do +      assert HTTP.Tzdata.get(@url, [], []) == {:ok, {200, [], "hello"}} +    end +  end +end diff --git a/test/http_test.exs b/test/http_test.exs index 618485b55..d394bb942 100644 --- a/test/http_test.exs +++ b/test/http_test.exs @@ -17,6 +17,9 @@ defmodule Pleroma.HTTPTest do        } ->          json(%{"my" => "data"}) +      %{method: :head, url: "http://example.com/hello"} -> +        %Tesla.Env{status: 200, body: ""} +        %{method: :get, url: "http://example.com/hello"} ->          %Tesla.Env{status: 200, body: "hello"} @@ -27,6 +30,12 @@ defmodule Pleroma.HTTPTest do      :ok    end +  describe "head/1" do +    test "returns successfully result" do +      assert HTTP.head("http://example.com/hello") == {:ok, %Tesla.Env{status: 200, body: ""}} +    end +  end +    describe "get/1" do      test "returns successfully result" do        assert HTTP.get("http://example.com/hello") == { diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs index e490a5744..e722f7c04 100644 --- a/test/web/activity_pub/activity_pub_controller_test.exs +++ b/test/web/activity_pub/activity_pub_controller_test.exs @@ -536,6 +536,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do        assert_receive {:mix_shell, :info, ["relay.mastodon.host"]}      end +    @tag capture_log: true      test "without valid signature, " <>             "it only accepts Create activities and requires enabled federation",           %{conn: conn} do @@ -648,11 +649,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do      test "it accepts announces with to as string instead of array", %{conn: conn} do        user = insert(:user) +      {:ok, post} = CommonAPI.post(user, %{status: "hey"}) +      announcer = insert(:user, local: false) +        data = %{          "@context" => "https://www.w3.org/ns/activitystreams", -        "actor" => "http://mastodon.example.org/users/admin", -        "id" => "http://mastodon.example.org/users/admin/statuses/19512778738411822/activity", -        "object" => "https://mastodon.social/users/emelie/statuses/101849165031453009", +        "actor" => announcer.ap_id, +        "id" => "#{announcer.ap_id}/statuses/19512778738411822/activity", +        "object" => post.data["object"],          "to" => "https://www.w3.org/ns/activitystreams#Public",          "cc" => [user.ap_id],          "type" => "Announce" diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 1c684df1a..be7ab2ae4 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -1092,52 +1092,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do      end    end -  describe "update" do -    setup do: clear_config([:instance, :max_pinned_statuses]) - -    test "it creates an update activity with the new user data" do -      user = insert(:user) -      {:ok, user} = User.ensure_keys_present(user) -      user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user}) - -      {:ok, update} = -        ActivityPub.update(%{ -          actor: user_data["id"], -          to: [user.follower_address], -          cc: [], -          object: user_data -        }) - -      assert update.data["actor"] == user.ap_id -      assert update.data["to"] == [user.follower_address] -      assert embedded_object = update.data["object"] -      assert embedded_object["id"] == user_data["id"] -      assert embedded_object["type"] == user_data["type"] -    end -  end - -  test "returned pinned statuses" do -    Config.put([:instance, :max_pinned_statuses], 3) -    user = insert(:user) - -    {:ok, activity_one} = CommonAPI.post(user, %{status: "HI!!!"}) -    {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"}) -    {:ok, activity_three} = CommonAPI.post(user, %{status: "HI!!!"}) - -    CommonAPI.pin(activity_one.id, user) -    user = refresh_record(user) - -    CommonAPI.pin(activity_two.id, user) -    user = refresh_record(user) - -    CommonAPI.pin(activity_three.id, user) -    user = refresh_record(user) - -    activities = ActivityPub.fetch_user_activities(user, nil, %{pinned: true}) - -    assert 3 = length(activities) -  end -    describe "flag/1" do      setup do        reporter = insert(:user) diff --git a/test/web/activity_pub/object_validator_test.exs b/test/web/activity_pub/object_validator_test.exs index 31224abe0..770a8dcf8 100644 --- a/test/web/activity_pub/object_validator_test.exs +++ b/test/web/activity_pub/object_validator_test.exs @@ -622,4 +622,36 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do        assert {:actor, {"can not announce this object publicly", []}} in cng.errors      end    end + +  describe "updates" do +    setup do +      user = insert(:user) + +      object = %{ +        "id" => user.ap_id, +        "name" => "A new name", +        "summary" => "A new bio" +      } + +      {:ok, valid_update, []} = Builder.update(user, object) + +      %{user: user, valid_update: valid_update} +    end + +    test "validates a basic object", %{valid_update: valid_update} do +      assert {:ok, _update, []} = ObjectValidator.validate(valid_update, []) +    end + +    test "returns an error if the object can't be updated by the actor", %{ +      valid_update: valid_update +    } do +      other_user = insert(:user) + +      update = +        valid_update +        |> Map.put("actor", other_user.ap_id) + +      assert {:error, _cng} = ObjectValidator.validate(update, []) +    end +  end  end diff --git a/test/web/activity_pub/side_effects_test.exs b/test/web/activity_pub/side_effects_test.exs index 6bbbaae87..12c9ef1da 100644 --- a/test/web/activity_pub/side_effects_test.exs +++ b/test/web/activity_pub/side_effects_test.exs @@ -64,6 +64,31 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do      end    end +  describe "update users" do +    setup do +      user = insert(:user) +      {:ok, update_data, []} = Builder.update(user, %{"id" => user.ap_id, "name" => "new name!"}) +      {:ok, update, _meta} = ActivityPub.persist(update_data, local: true) + +      %{user: user, update_data: update_data, update: update} +    end + +    test "it updates the user", %{user: user, update: update} do +      {:ok, _, _} = SideEffects.handle(update) +      user = User.get_by_id(user.id) +      assert user.name == "new name!" +    end + +    test "it uses a given changeset to update", %{user: user, update: update} do +      changeset = Ecto.Changeset.change(user, %{default_scope: "direct"}) + +      assert user.default_scope == "public" +      {:ok, _, _} = SideEffects.handle(update, user_update_changeset: changeset) +      user = User.get_by_id(user.id) +      assert user.default_scope == "direct" +    end +  end +    describe "delete objects" do      setup do        user = insert(:user) diff --git a/test/web/activity_pub/transmogrifier/user_update_handling_test.exs b/test/web/activity_pub/transmogrifier/user_update_handling_test.exs new file mode 100644 index 000000000..64636656c --- /dev/null +++ b/test/web/activity_pub/transmogrifier/user_update_handling_test.exs @@ -0,0 +1,159 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.Transmogrifier.UserUpdateHandlingTest do +  use Pleroma.DataCase + +  alias Pleroma.Activity +  alias Pleroma.User +  alias Pleroma.Web.ActivityPub.Transmogrifier + +  import Pleroma.Factory + +  test "it works for incoming update activities" do +    user = insert(:user, local: false) + +    update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!() + +    object = +      update_data["object"] +      |> Map.put("actor", user.ap_id) +      |> Map.put("id", user.ap_id) + +    update_data = +      update_data +      |> Map.put("actor", user.ap_id) +      |> Map.put("object", object) + +    {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data) + +    assert data["id"] == update_data["id"] + +    user = User.get_cached_by_ap_id(data["actor"]) +    assert user.name == "gargle" + +    assert user.avatar["url"] == [ +             %{ +               "href" => +                 "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg" +             } +           ] + +    assert user.banner["url"] == [ +             %{ +               "href" => +                 "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png" +             } +           ] + +    assert user.bio == "<p>Some bio</p>" +  end + +  test "it works with alsoKnownAs" do +    %{ap_id: actor} = insert(:user, local: false) + +    assert User.get_cached_by_ap_id(actor).also_known_as == [] + +    {:ok, _activity} = +      "test/fixtures/mastodon-update.json" +      |> File.read!() +      |> Poison.decode!() +      |> Map.put("actor", actor) +      |> Map.update!("object", fn object -> +        object +        |> Map.put("actor", actor) +        |> Map.put("id", actor) +        |> Map.put("alsoKnownAs", [ +          "http://mastodon.example.org/users/foo", +          "http://example.org/users/bar" +        ]) +      end) +      |> Transmogrifier.handle_incoming() + +    assert User.get_cached_by_ap_id(actor).also_known_as == [ +             "http://mastodon.example.org/users/foo", +             "http://example.org/users/bar" +           ] +  end + +  test "it works with custom profile fields" do +    user = insert(:user, local: false) + +    assert user.fields == [] + +    update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!() + +    object = +      update_data["object"] +      |> Map.put("actor", user.ap_id) +      |> Map.put("id", user.ap_id) + +    update_data = +      update_data +      |> Map.put("actor", user.ap_id) +      |> Map.put("object", object) + +    {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data) + +    user = User.get_cached_by_ap_id(user.ap_id) + +    assert user.fields == [ +             %{"name" => "foo", "value" => "updated"}, +             %{"name" => "foo1", "value" => "updated"} +           ] + +    Pleroma.Config.put([:instance, :max_remote_account_fields], 2) + +    update_data = +      update_data +      |> put_in(["object", "attachment"], [ +        %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"}, +        %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"}, +        %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"} +      ]) +      |> Map.put("id", update_data["id"] <> ".") + +    {:ok, _} = Transmogrifier.handle_incoming(update_data) + +    user = User.get_cached_by_ap_id(user.ap_id) + +    assert user.fields == [ +             %{"name" => "foo", "value" => "updated"}, +             %{"name" => "foo1", "value" => "updated"} +           ] + +    update_data = +      update_data +      |> put_in(["object", "attachment"], []) +      |> Map.put("id", update_data["id"] <> ".") + +    {:ok, _} = Transmogrifier.handle_incoming(update_data) + +    user = User.get_cached_by_ap_id(user.ap_id) + +    assert user.fields == [] +  end + +  test "it works for incoming update activities which lock the account" do +    user = insert(:user, local: false) + +    update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!() + +    object = +      update_data["object"] +      |> Map.put("actor", user.ap_id) +      |> Map.put("id", user.ap_id) +      |> Map.put("manuallyApprovesFollowers", true) + +    update_data = +      update_data +      |> Map.put("actor", user.ap_id) +      |> Map.put("object", object) + +    {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(update_data) + +    user = User.get_cached_by_ap_id(user.ap_id) +    assert user.locked == true +  end +end diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 47d6e843a..100821056 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -401,162 +401,6 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        refute Map.has_key?(object_data, "reaction_count")      end -    test "it works for incoming update activities" do -      data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!() - -      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) -      update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!() - -      object = -        update_data["object"] -        |> Map.put("actor", data["actor"]) -        |> Map.put("id", data["actor"]) - -      update_data = -        update_data -        |> Map.put("actor", data["actor"]) -        |> Map.put("object", object) - -      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data) - -      assert data["id"] == update_data["id"] - -      user = User.get_cached_by_ap_id(data["actor"]) -      assert user.name == "gargle" - -      assert user.avatar["url"] == [ -               %{ -                 "href" => -                   "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg" -               } -             ] - -      assert user.banner["url"] == [ -               %{ -                 "href" => -                   "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png" -               } -             ] - -      assert user.bio == "<p>Some bio</p>" -    end - -    test "it works with alsoKnownAs" do -      {:ok, %Activity{data: %{"actor" => actor}}} = -        "test/fixtures/mastodon-post-activity.json" -        |> File.read!() -        |> Poison.decode!() -        |> Transmogrifier.handle_incoming() - -      assert User.get_cached_by_ap_id(actor).also_known_as == ["http://example.org/users/foo"] - -      {:ok, _activity} = -        "test/fixtures/mastodon-update.json" -        |> File.read!() -        |> Poison.decode!() -        |> Map.put("actor", actor) -        |> Map.update!("object", fn object -> -          object -          |> Map.put("actor", actor) -          |> Map.put("id", actor) -          |> Map.put("alsoKnownAs", [ -            "http://mastodon.example.org/users/foo", -            "http://example.org/users/bar" -          ]) -        end) -        |> Transmogrifier.handle_incoming() - -      assert User.get_cached_by_ap_id(actor).also_known_as == [ -               "http://mastodon.example.org/users/foo", -               "http://example.org/users/bar" -             ] -    end - -    test "it works with custom profile fields" do -      {:ok, activity} = -        "test/fixtures/mastodon-post-activity.json" -        |> File.read!() -        |> Poison.decode!() -        |> Transmogrifier.handle_incoming() - -      user = User.get_cached_by_ap_id(activity.actor) - -      assert user.fields == [ -               %{"name" => "foo", "value" => "bar"}, -               %{"name" => "foo1", "value" => "bar1"} -             ] - -      update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!() - -      object = -        update_data["object"] -        |> Map.put("actor", user.ap_id) -        |> Map.put("id", user.ap_id) - -      update_data = -        update_data -        |> Map.put("actor", user.ap_id) -        |> Map.put("object", object) - -      {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data) - -      user = User.get_cached_by_ap_id(user.ap_id) - -      assert user.fields == [ -               %{"name" => "foo", "value" => "updated"}, -               %{"name" => "foo1", "value" => "updated"} -             ] - -      Pleroma.Config.put([:instance, :max_remote_account_fields], 2) - -      update_data = -        put_in(update_data, ["object", "attachment"], [ -          %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"}, -          %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"}, -          %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"} -        ]) - -      {:ok, _} = Transmogrifier.handle_incoming(update_data) - -      user = User.get_cached_by_ap_id(user.ap_id) - -      assert user.fields == [ -               %{"name" => "foo", "value" => "updated"}, -               %{"name" => "foo1", "value" => "updated"} -             ] - -      update_data = put_in(update_data, ["object", "attachment"], []) - -      {:ok, _} = Transmogrifier.handle_incoming(update_data) - -      user = User.get_cached_by_ap_id(user.ap_id) - -      assert user.fields == [] -    end - -    test "it works for incoming update activities which lock the account" do -      data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!() - -      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) -      update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!() - -      object = -        update_data["object"] -        |> Map.put("actor", data["actor"]) -        |> Map.put("id", data["actor"]) -        |> Map.put("manuallyApprovesFollowers", true) - -      update_data = -        update_data -        |> Map.put("actor", data["actor"]) -        |> Map.put("object", object) - -      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data) - -      user = User.get_cached_by_ap_id(data["actor"]) -      assert user.locked == true -    end -      test "it works for incomming unfollows with an existing follow" do        user = insert(:user) | 
