diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/fixtures/mastodon/application_actor.json | 67 | ||||
| -rw-r--r-- | test/mix/tasks/pleroma/instance_test.exs | 2 | ||||
| -rw-r--r-- | test/pleroma/activity_test.exs | 16 | ||||
| -rw-r--r-- | test/pleroma/formatter_test.exs | 6 | ||||
| -rw-r--r-- | test/pleroma/user_search_test.exs | 6 | ||||
| -rw-r--r-- | test/pleroma/user_test.exs | 7 | ||||
| -rw-r--r-- | test/pleroma/web/activity_pub/activity_pub_controller_test.exs | 136 | ||||
| -rw-r--r-- | test/pleroma/web/activity_pub/activity_pub_test.exs | 25 | ||||
| -rw-r--r-- | test/pleroma/web/activity_pub/side_effects_test.exs | 2 | ||||
| -rw-r--r-- | test/support/factory.ex | 32 | 
10 files changed, 283 insertions, 16 deletions
| diff --git a/test/fixtures/mastodon/application_actor.json b/test/fixtures/mastodon/application_actor.json new file mode 100644 index 000000000..2089ea049 --- /dev/null +++ b/test/fixtures/mastodon/application_actor.json @@ -0,0 +1,67 @@ +{ +  "@context": [ +    "https://www.w3.org/ns/activitystreams", +    "https://w3id.org/security/v1", +    { +      "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", +      "toot": "http://joinmastodon.org/ns#", +      "featured": { +        "@id": "toot:featured", +        "@type": "@id" +      }, +      "alsoKnownAs": { +        "@id": "as:alsoKnownAs", +        "@type": "@id" +      }, +      "movedTo": { +        "@id": "as:movedTo", +        "@type": "@id" +      }, +      "schema": "http://schema.org#", +      "PropertyValue": "schema:PropertyValue", +      "value": "schema:value", +      "IdentityProof": "toot:IdentityProof", +      "discoverable": "toot:discoverable", +      "Device": "toot:Device", +      "Ed25519Signature": "toot:Ed25519Signature", +      "Ed25519Key": "toot:Ed25519Key", +      "Curve25519Key": "toot:Curve25519Key", +      "EncryptedMessage": "toot:EncryptedMessage", +      "publicKeyBase64": "toot:publicKeyBase64", +      "deviceId": "toot:deviceId", +      "claim": { +        "@type": "@id", +        "@id": "toot:claim" +      }, +      "fingerprintKey": { +        "@type": "@id", +        "@id": "toot:fingerprintKey" +      }, +      "identityKey": { +        "@type": "@id", +        "@id": "toot:identityKey" +      }, +      "devices": { +        "@type": "@id", +        "@id": "toot:devices" +      }, +      "messageFranking": "toot:messageFranking", +      "messageType": "toot:messageType", +      "cipherText": "toot:cipherText" +    } +  ], +  "id": "https://{{DOMAIN}}/actor", +  "type": "Application", +  "inbox": "https://{{DOMAIN}}/actor/inbox", +  "preferredUsername": "{{DOMAIN}}", +  "url": "https://{{DOMAIN}}/about/more?instance_actor=true", +  "manuallyApprovesFollowers": true, +  "publicKey": { +    "id": "https://{{DOMAIN}}/actor#main-key", +    "owner": "https://{{DOMAIN}}/actor", +    "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAA0CA08AMIIBCgKCAQEAyi2T2FFZJgRPY+96YQrn\n6J6eF2P60J+nz+/pRc/acv/Nx+NLxxPyXby0F2s60MV7uALRQbBBnf7oNKCd/T4S\nvbr7UXMCWTdaJBpYubMKWT9uBlaUUkUfqL+WTV+IQnlcKtssQ4+AwrAKAZXza8ws\nZypevOsLHzayyEzztmm1KQC9GCUOITCLf7Q6qEhy8z/HuqLBEC0Own0pD7QsbfcS\no1peuZY7g1E/jJ9HR9GqJccMaR0H28KmJ7tT1Yzlyf5uZMRIdPxsoMR9sGLjR2B8\noegSwaf9SogR3ScP395Tt/9Ud1VVzuhpoS8Uy7jKSs+3CuLJsEGoMrib8VyOwadS\n9wIDAQAB\n-----END PUBLIC KEY-----\n" +  }, +  "endpoints": { +    "sharedInbox": "https://{{DOMAIN}}/inbox" +  } +} diff --git a/test/mix/tasks/pleroma/instance_test.exs b/test/mix/tasks/pleroma/instance_test.exs index 8a02710ee..6580fc932 100644 --- a/test/mix/tasks/pleroma/instance_test.exs +++ b/test/mix/tasks/pleroma/instance_test.exs @@ -88,7 +88,7 @@ defmodule Mix.Tasks.Pleroma.InstanceTest do      assert generated_config =~ "password: \"dbpass\""      assert generated_config =~ "configurable_from_database: true"      assert generated_config =~ "http: [ip: {127, 0, 0, 1}, port: 4000]" -    assert generated_config =~ "filters: [Pleroma.Upload.Filter.ExifTool]" +    assert generated_config =~ "filters: [Pleroma.Upload.Filter.Exiftool]"      assert File.read!(tmp_path() <> "setup.psql") == generated_setup_psql()      assert File.exists?(Path.expand("./test/instance/static/robots.txt"))    end diff --git a/test/pleroma/activity_test.exs b/test/pleroma/activity_test.exs index ee6a99cc3..3e9fe209e 100644 --- a/test/pleroma/activity_test.exs +++ b/test/pleroma/activity_test.exs @@ -231,4 +231,20 @@ defmodule Pleroma.ActivityTest do      assert [%Activity{id: ^id1}, %Activity{id: ^id2}] = activities    end + +  test "get_by_object_ap_id_with_object/1" do +    user = insert(:user) +    another = insert(:user) + +    {:ok, %{id: id, object: %{data: %{"id" => obj_id}}}} = +      Pleroma.Web.CommonAPI.post(user, %{status: "cofe"}) + +    Pleroma.Web.CommonAPI.favorite(another, id) + +    assert obj_id +           |> Pleroma.Activity.Queries.by_object_id() +           |> Repo.aggregate(:count, :id) == 2 + +    assert %{id: ^id} = Activity.get_by_object_ap_id_with_object(obj_id) +  end  end diff --git a/test/pleroma/formatter_test.exs b/test/pleroma/formatter_test.exs index f066bd50a..5781a3f01 100644 --- a/test/pleroma/formatter_test.exs +++ b/test/pleroma/formatter_test.exs @@ -241,16 +241,14 @@ defmodule Pleroma.FormatterTest do          "@@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me and @o and @@@jimm"        o = insert(:user, %{nickname: "o"}) -      jimm = insert(:user, %{nickname: "jimm"}) -      gsimg = insert(:user, %{nickname: "gsimg"}) +      _jimm = insert(:user, %{nickname: "jimm"}) +      _gsimg = insert(:user, %{nickname: "gsimg"})        archaeme = insert(:user, %{nickname: "archaeme"})        archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})        expected_mentions = [          {"@archaeme", archaeme},          {"@archaeme@archae.me", archaeme_remote}, -        {"@gsimg", gsimg}, -        {"@jimm", jimm},          {"@o", o}        ] diff --git a/test/pleroma/user_search_test.exs b/test/pleroma/user_search_test.exs index c4b805005..349797adb 100644 --- a/test/pleroma/user_search_test.exs +++ b/test/pleroma/user_search_test.exs @@ -65,12 +65,14 @@ defmodule Pleroma.UserSearchTest do        assert found_user.id == user.id      end -    test "excludes users when discoverable is false" do +    test "does NOT exclude non-discoverable users from results (as long as it's the default)" do +      # NOTE: as long as users are non-discoverable by default, +      # we can't filter out most users: #2301        insert(:user, %{nickname: "john 3000", discoverable: false})        insert(:user, %{nickname: "john 3001"})        users = User.search("john") -      assert Enum.count(users) == 1 +      assert Enum.count(users) == 2      end      test "excludes service actors from results" do diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index d8ac652af..52dcea0b3 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -877,6 +877,13 @@ defmodule Pleroma.UserTest do          refute cs.valid?        end)      end + +    test "it is invalid given a local user" do +      user = insert(:user) +      cs = User.remote_user_changeset(user, %{name: "tom from myspace"}) + +      refute cs.valid? +    end    end    describe "followers and friends" do diff --git a/test/pleroma/web/activity_pub/activity_pub_controller_test.exs b/test/pleroma/web/activity_pub/activity_pub_controller_test.exs index b696a24f4..fb5911825 100644 --- a/test/pleroma/web/activity_pub/activity_pub_controller_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_controller_test.exs @@ -766,6 +766,142 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do        assert json_response(ret_conn, 200)      end + +    @tag capture_log: true +    test "forwarded report", %{conn: conn} do +      admin = insert(:user, is_admin: true) +      actor = insert(:user, local: false) +      remote_domain = URI.parse(actor.ap_id).host +      reported_user = insert(:user) + +      note = insert(:note_activity, user: reported_user) + +      data = %{ +        "@context" => [ +          "https://www.w3.org/ns/activitystreams", +          "https://#{remote_domain}/schemas/litepub-0.1.jsonld", +          %{ +            "@language" => "und" +          } +        ], +        "actor" => actor.ap_id, +        "cc" => [ +          reported_user.ap_id +        ], +        "content" => "test", +        "context" => "context", +        "id" => "http://#{remote_domain}/activities/02be56cf-35e3-46b4-b2c6-47ae08dfee9e", +        "nickname" => reported_user.nickname, +        "object" => [ +          reported_user.ap_id, +          %{ +            "actor" => %{ +              "actor_type" => "Person", +              "approval_pending" => false, +              "avatar" => "", +              "confirmation_pending" => false, +              "deactivated" => false, +              "display_name" => "test user", +              "id" => reported_user.id, +              "local" => false, +              "nickname" => reported_user.nickname, +              "registration_reason" => nil, +              "roles" => %{ +                "admin" => false, +                "moderator" => false +              }, +              "tags" => [], +              "url" => reported_user.ap_id +            }, +            "content" => "", +            "id" => note.data["id"], +            "published" => note.data["published"], +            "type" => "Note" +          } +        ], +        "published" => note.data["published"], +        "state" => "open", +        "to" => [], +        "type" => "Flag" +      } + +      conn +      |> assign(:valid_signature, true) +      |> put_req_header("content-type", "application/activity+json") +      |> post("/users/#{reported_user.nickname}/inbox", data) +      |> json_response(200) + +      ObanHelpers.perform(all_enqueued(worker: ReceiverWorker)) + +      assert Pleroma.Repo.aggregate(Activity, :count, :id) == 2 + +      ObanHelpers.perform_all() + +      Swoosh.TestAssertions.assert_email_sent( +        to: {admin.name, admin.email}, +        html_body: ~r/Reported Account:/i +      ) +    end + +    @tag capture_log: true +    test "forwarded report from mastodon", %{conn: conn} do +      admin = insert(:user, is_admin: true) +      actor = insert(:user, local: false) +      remote_domain = URI.parse(actor.ap_id).host +      remote_actor = "https://#{remote_domain}/actor" +      [reported_user, another] = insert_list(2, :user) + +      note = insert(:note_activity, user: reported_user) + +      Pleroma.Web.CommonAPI.favorite(another, note.id) + +      mock_json_body = +        "test/fixtures/mastodon/application_actor.json" +        |> File.read!() +        |> String.replace("{{DOMAIN}}", remote_domain) + +      Tesla.Mock.mock(fn %{url: ^remote_actor} -> +        %Tesla.Env{ +          status: 200, +          body: mock_json_body, +          headers: [{"content-type", "application/activity+json"}] +        } +      end) + +      data = %{ +        "@context" => "https://www.w3.org/ns/activitystreams", +        "actor" => remote_actor, +        "content" => "test report", +        "id" => "https://#{remote_domain}/e3b12fd1-948c-446e-b93b-a5e67edbe1d8", +        "nickname" => reported_user.nickname, +        "object" => [ +          reported_user.ap_id, +          note.data["object"] +        ], +        "type" => "Flag" +      } + +      conn +      |> assign(:valid_signature, true) +      |> put_req_header("content-type", "application/activity+json") +      |> post("/users/#{reported_user.nickname}/inbox", data) +      |> json_response(200) + +      ObanHelpers.perform(all_enqueued(worker: ReceiverWorker)) + +      flag_activity = "Flag" |> Pleroma.Activity.Queries.by_type() |> Pleroma.Repo.one() +      reported_user_ap_id = reported_user.ap_id + +      [^reported_user_ap_id, flag_data] = flag_activity.data["object"] + +      Enum.each(~w(actor content id published type), &Map.has_key?(flag_data, &1)) +      ObanHelpers.perform_all() + +      Swoosh.TestAssertions.assert_email_sent( +        to: {admin.name, admin.email}, +        html_body: ~r/#{note.data["object"]}/i +      ) +    end    end    describe "GET /users/:nickname/outbox" do diff --git a/test/pleroma/web/activity_pub/activity_pub_test.exs b/test/pleroma/web/activity_pub/activity_pub_test.exs index c6ca37847..ef93b7859 100644 --- a/test/pleroma/web/activity_pub/activity_pub_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_test.exs @@ -1282,6 +1282,31 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        assert_called(Utils.maybe_federate(%{activity | data: new_data}))      end + +    test_with_mock "reverts on error", +                   %{ +                     reporter: reporter, +                     context: context, +                     target_account: target_account, +                     reported_activity: reported_activity, +                     content: content +                   }, +                   Utils, +                   [:passthrough], +                   maybe_federate: fn _ -> {:error, :reverted} end do +      assert {:error, :reverted} = +               ActivityPub.flag(%{ +                 actor: reporter, +                 context: context, +                 account: target_account, +                 statuses: [reported_activity], +                 content: content +               }) + +      assert Repo.aggregate(Activity, :count, :id) == 1 +      assert Repo.aggregate(Object, :count, :id) == 2 +      assert Repo.aggregate(Notification, :count, :id) == 0 +    end    end    test "fetch_activities/2 returns activities addressed to a list " do diff --git a/test/pleroma/web/activity_pub/side_effects_test.exs b/test/pleroma/web/activity_pub/side_effects_test.exs index 9efbaad04..297fc0b84 100644 --- a/test/pleroma/web/activity_pub/side_effects_test.exs +++ b/test/pleroma/web/activity_pub/side_effects_test.exs @@ -108,7 +108,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do    describe "update users" do      setup do -      user = insert(:user) +      user = insert(:user, local: false)        {:ok, update_data, []} = Builder.update(user, %{"id" => user.ap_id, "name" => "new name!"})        {:ok, update, _meta} = ActivityPub.persist(update_data, local: true) diff --git a/test/support/factory.ex b/test/support/factory.ex index fb82be0c4..581c4a2d8 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -24,7 +24,7 @@ defmodule Pleroma.Factory do      }    end -  def user_factory do +  def user_factory(attrs \\ %{}) do      user = %User{        name: sequence(:name, &"Test ใในใ User #{&1}"),        email: sequence(:email, &"user#{&1}@example.com"), @@ -39,13 +39,29 @@ defmodule Pleroma.Factory do        ap_enabled: true      } -    %{ -      user -      | ap_id: User.ap_id(user), -        follower_address: User.ap_followers(user), -        following_address: User.ap_following(user), -        raw_bio: user.bio -    } +    urls = +      if attrs[:local] == false do +        base_domain = Enum.random(["domain1.com", "domain2.com", "domain3.com"]) + +        ap_id = "https://#{base_domain}/users/#{user.nickname}" + +        %{ +          ap_id: ap_id, +          follower_address: ap_id <> "/followers", +          following_address: ap_id <> "/following" +        } +      else +        %{ +          ap_id: User.ap_id(user), +          follower_address: User.ap_followers(user), +          following_address: User.ap_following(user) +        } +      end + +    user +    |> Map.put(:raw_bio, user.bio) +    |> Map.merge(urls) +    |> merge_attributes(attrs)    end    def user_relationship_factory(attrs \\ %{}) do | 
