X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=test%2Fsupport%2Ffactory.ex;h=54d385bc44f46278ce34d04232c9c2ee4f9cea88;hb=60b3c8d17be1ae27bc1f5b4b61f6f1be69c688bf;hp=6e22b66a417e7837cf4986f0d082b6f9884ef826;hpb=4a306720e8be8633e192f7b2c98f129a315939b9;p=akkoma
diff --git a/test/support/factory.ex b/test/support/factory.ex
index 6e22b66a4..54d385bc4 100644
--- a/test/support/factory.ex
+++ b/test/support/factory.ex
@@ -1,12 +1,24 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Factory do
use ExMachina.Ecto, repo: Pleroma.Repo
+
+ require Pleroma.Constants
+
alias Pleroma.Object
alias Pleroma.User
+ @rsa_keys [
+ "test/fixtures/rsa_keys/key_1.pem",
+ "test/fixtures/rsa_keys/key_2.pem",
+ "test/fixtures/rsa_keys/key_3.pem",
+ "test/fixtures/rsa_keys/key_4.pem",
+ "test/fixtures/rsa_keys/key_5.pem"
+ ]
+ |> Enum.map(&File.read!/1)
+
def participation_factory do
conversation = insert(:conversation)
user = insert(:user)
@@ -24,27 +36,51 @@ defmodule Pleroma.Factory do
}
end
- def user_factory do
+ def user_factory(attrs \\ %{}) do
+ pem = Enum.random(@rsa_keys)
+
user = %User{
name: sequence(:name, &"Test ãã¹ã User #{&1}"),
email: sequence(:email, &"user#{&1}@example.com"),
nickname: sequence(:nickname, &"nick#{&1}"),
- password_hash: Pbkdf2.hash_pwd_salt("test"),
+ password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
bio: sequence(:bio, &"Tester Number #{&1}"),
+ is_discoverable: true,
last_digest_emailed_at: NaiveDateTime.utc_now(),
last_refreshed_at: NaiveDateTime.utc_now(),
notification_settings: %Pleroma.User.NotificationSetting{},
multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
- 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
- }
+ ap_enabled: true,
+ keys: pem
+ }
+
+ urls =
+ if attrs[:local] == false do
+ base_domain = attrs[: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",
+ featured_address: ap_id <> "/collections/featured"
+ }
+ else
+ %{
+ ap_id: User.ap_id(user),
+ follower_address: User.ap_followers(user),
+ following_address: User.ap_following(user),
+ featured_address: User.ap_featured_collection(user)
+ }
+ end
+
+ attrs = Map.delete(attrs, :domain)
+
+ user
+ |> Map.put(:raw_bio, user.bio)
+ |> Map.merge(urls)
+ |> merge_attributes(attrs)
end
def user_relationship_factory(attrs \\ %{}) do
@@ -67,6 +103,7 @@ defmodule Pleroma.Factory do
data = %{
"type" => "Note",
"content" => text,
+ "source" => text,
"id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
"actor" => user.ap_id,
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
@@ -86,6 +123,54 @@ defmodule Pleroma.Factory do
}
end
+ def attachment_factory(attrs \\ %{}) do
+ user = attrs[:user] || insert(:user)
+
+ data =
+ attachment_data(user.ap_id, nil)
+ |> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
+
+ %Pleroma.Object{
+ data: merge_attributes(data, Map.get(attrs, :data, %{}))
+ }
+ end
+
+ def attachment_note_factory(attrs \\ %{}) do
+ user = attrs[:user] || insert(:user)
+ {length, attrs} = Map.pop(attrs, :length, 1)
+
+ data = %{
+ "attachment" =>
+ Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
+ |> Enum.take(length)
+ }
+
+ build(:note, Map.put(attrs, :data, data))
+ end
+
+ defp attachment_data(ap_id, href) do
+ href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
+
+ %{
+ "url" => [
+ %{
+ "href" => href,
+ "type" => "Link",
+ "mediaType" => "image/jpeg"
+ }
+ ],
+ "name" => "some name",
+ "type" => "Document",
+ "actor" => ap_id,
+ "mediaType" => "image/jpeg"
+ }
+ end
+
+ def followers_only_note_factory(attrs \\ %{}) do
+ %Pleroma.Object{data: data} = note_factory(attrs)
+ %Pleroma.Object{data: Map.merge(data, %{"to" => [data["actor"] <> "/followers"]})}
+ end
+
def audio_factory(attrs \\ %{}) do
text = sequence(:text, &"lain radio episode #{&1}")
@@ -135,8 +220,8 @@ defmodule Pleroma.Factory do
end
def article_factory do
- note_factory()
- |> Map.put("type", "Article")
+ %Pleroma.Object{data: data} = note_factory()
+ %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
end
def tombstone_factory do
@@ -152,6 +237,38 @@ defmodule Pleroma.Factory do
}
end
+ def question_factory(attrs \\ %{}) do
+ user = attrs[:user] || insert(:user)
+
+ data = %{
+ "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
+ "type" => "Question",
+ "actor" => user.ap_id,
+ "attributedTo" => user.ap_id,
+ "attachment" => [],
+ "to" => ["https://www.w3.org/ns/activitystreams#Public"],
+ "cc" => [user.follower_address],
+ "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
+ "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
+ "oneOf" => [
+ %{
+ "type" => "Note",
+ "name" => "chocolate",
+ "replies" => %{"totalItems" => 0, "type" => "Collection"}
+ },
+ %{
+ "type" => "Note",
+ "name" => "vanilla",
+ "replies" => %{"totalItems" => 0, "type" => "Collection"}
+ }
+ ]
+ }
+
+ %Pleroma.Object{
+ data: merge_attributes(data, Map.get(attrs, :data, %{}))
+ }
+ end
+
def direct_note_activity_factory do
dm = insert(:direct_note)
@@ -172,10 +289,49 @@ defmodule Pleroma.Factory do
}
end
- def note_activity_factory(attrs \\ %{}) do
+ def add_activity_factory(attrs \\ %{}) do
+ featured_collection_activity(attrs, "Add")
+ end
+
+ def remove_activity_factor(attrs \\ %{}) do
+ featured_collection_activity(attrs, "Remove")
+ end
+
+ defp featured_collection_activity(attrs, type) do
user = attrs[:user] || insert(:user)
note = attrs[:note] || insert(:note, user: user)
+ data_attrs =
+ attrs
+ |> Map.get(:data_attrs, %{})
+ |> Map.put(:type, type)
+
+ attrs = Map.drop(attrs, [:user, :note, :data_attrs])
+
+ data =
+ %{
+ "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
+ "target" => user.featured_address,
+ "object" => note.data["object"],
+ "actor" => note.data["actor"],
+ "type" => "Add",
+ "to" => [Pleroma.Constants.as_public()],
+ "cc" => [user.follower_address]
+ }
+ |> Map.merge(data_attrs)
+
+ %Pleroma.Activity{
+ data: data,
+ actor: data["actor"],
+ recipients: data["to"]
+ }
+ |> Map.merge(attrs)
+ end
+
+ def followers_only_note_activity_factory(attrs \\ %{}) do
+ user = attrs[:user] || insert(:user)
+ note = insert(:followers_only_note, user: user)
+
data_attrs = attrs[:data_attrs] || %{}
attrs = Map.drop(attrs, [:user, :note, :data_attrs])
@@ -185,7 +341,7 @@ defmodule Pleroma.Factory do
"type" => "Create",
"actor" => note.data["actor"],
"to" => note.data["to"],
- "object" => note.data["id"],
+ "object" => note.data,
"published" => DateTime.utc_now() |> DateTime.to_iso8601(),
"context" => note.data["context"]
}
@@ -199,23 +355,31 @@ defmodule Pleroma.Factory do
|> Map.merge(attrs)
end
- defp expiration_offset_by_minutes(attrs, minutes) do
- scheduled_at =
- NaiveDateTime.utc_now()
- |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
- |> NaiveDateTime.truncate(:second)
+ def note_activity_factory(attrs \\ %{}) do
+ user = attrs[:user] || insert(:user)
+ note = attrs[:note] || insert(:note, user: user)
- %Pleroma.ActivityExpiration{}
- |> Map.merge(attrs)
- |> Map.put(:scheduled_at, scheduled_at)
- end
+ data_attrs = attrs[:data_attrs] || %{}
+ attrs = Map.drop(attrs, [:user, :note, :data_attrs])
- def expiration_in_the_past_factory(attrs \\ %{}) do
- expiration_offset_by_minutes(attrs, -60)
- end
+ data =
+ %{
+ "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
+ "type" => "Create",
+ "actor" => note.data["actor"],
+ "to" => note.data["to"],
+ "object" => note.data["id"],
+ "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
+ "context" => note.data["context"]
+ }
+ |> Map.merge(data_attrs)
- def expiration_in_the_future_factory(attrs \\ %{}) do
- expiration_offset_by_minutes(attrs, 61)
+ %Pleroma.Activity{
+ data: data,
+ actor: data["actor"],
+ recipients: data["to"]
+ }
+ |> Map.merge(attrs)
end
def article_activity_factory do
@@ -260,7 +424,7 @@ defmodule Pleroma.Factory do
def like_activity_factory(attrs \\ %{}) do
note_activity = attrs[:note_activity] || insert(:note_activity)
- object = Object.normalize(note_activity)
+ object = Object.normalize(note_activity, fetch: false)
user = insert(:user)
data =
@@ -296,6 +460,57 @@ defmodule Pleroma.Factory do
}
end
+ def report_activity_factory(attrs \\ %{}) do
+ user = attrs[:user] || insert(:user)
+ activity = attrs[:activity] || insert(:note_activity)
+ state = attrs[:state] || "open"
+
+ data = %{
+ "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
+ "actor" => user.ap_id,
+ "type" => "Flag",
+ "object" => [activity.actor, activity.data["id"]],
+ "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
+ "to" => [],
+ "cc" => [activity.actor],
+ "context" => activity.data["context"],
+ "state" => state
+ }
+
+ %Pleroma.Activity{
+ data: data,
+ actor: data["actor"],
+ recipients: data["to"] ++ data["cc"]
+ }
+ end
+
+ def question_activity_factory(attrs \\ %{}) do
+ user = attrs[:user] || insert(:user)
+ question = attrs[:question] || insert(:question, user: user)
+
+ data_attrs = attrs[:data_attrs] || %{}
+ attrs = Map.drop(attrs, [:user, :question, :data_attrs])
+
+ data =
+ %{
+ "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
+ "type" => "Create",
+ "actor" => question.data["actor"],
+ "to" => question.data["to"],
+ "object" => question.data["id"],
+ "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
+ "context" => question.data["context"]
+ }
+ |> Map.merge(data_attrs)
+
+ %Pleroma.Activity{
+ data: data,
+ actor: data["actor"],
+ recipients: data["to"]
+ }
+ |> Map.merge(attrs)
+ end
+
def oauth_app_factory do
%Pleroma.Web.OAuth.App{
client_name: sequence(:client_name, &"Some client #{&1}"),
@@ -427,4 +642,36 @@ defmodule Pleroma.Factory do
user: build(:user)
}
end
+
+ def filter_factory do
+ %Pleroma.Filter{
+ user: build(:user),
+ filter_id: sequence(:filter_id, & &1),
+ phrase: "cofe",
+ context: ["home"]
+ }
+ end
+
+ def announcement_factory(params \\ %{}) do
+ data = Map.get(params, :data, %{})
+
+ {_, params} = Map.pop(params, :data)
+
+ %Pleroma.Announcement{
+ data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
+ }
+ |> Map.merge(params)
+ |> Pleroma.Announcement.add_rendered_properties()
+ end
+
+ def frontend_setting_profile_factory(params \\ %{}) do
+ %Pleroma.Akkoma.FrontendSettingsProfile{
+ user: build(:user),
+ frontend_name: "akkoma-fe",
+ profile_name: "default",
+ settings: %{"test" => "test"},
+ version: 1
+ }
+ |> Map.merge(params)
+ end
end