X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=inline;f=test%2Fsupport%2Ffactory.ex;h=c267dba4ef41140f404ae2902662197c086b37aa;hb=5f8a9b671f0eb501a386af43e85cc9f182e23067;hp=486eda8da5676fc0b86dc39ce16a7b093ec32016;hpb=5876d5123ceb29e658d0ccbcb6ec8fef98340a55;p=akkoma
diff --git a/test/support/factory.ex b/test/support/factory.ex
index 486eda8da..c267dba4e 100644
--- a/test/support/factory.ex
+++ b/test/support/factory.ex
@@ -1,9 +1,12 @@
# 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
@@ -24,13 +27,14 @@ 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"),
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{},
@@ -38,13 +42,33 @@ 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 = 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
@@ -87,6 +111,37 @@ defmodule Pleroma.Factory do
}
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 audio_factory(attrs \\ %{}) do
text = sequence(:text, &"lain radio episode #{&1}")
@@ -136,8 +191,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
@@ -173,6 +228,45 @@ defmodule Pleroma.Factory do
}
end
+ 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 note_activity_factory(attrs \\ %{}) do
user = attrs[:user] || insert(:user)
note = attrs[:note] || insert(:note, user: user)
@@ -200,25 +294,6 @@ 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)
-
- %Pleroma.ActivityExpiration{}
- |> Map.merge(attrs)
- |> Map.put(:scheduled_at, scheduled_at)
- end
-
- def expiration_in_the_past_factory(attrs \\ %{}) do
- expiration_offset_by_minutes(attrs, -60)
- end
-
- def expiration_in_the_future_factory(attrs \\ %{}) do
- expiration_offset_by_minutes(attrs, 61)
- end
-
def article_activity_factory do
article = insert(:article)
@@ -261,7 +336,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 =
@@ -457,7 +532,8 @@ defmodule Pleroma.Factory do
%Pleroma.Filter{
user: build(:user),
filter_id: sequence(:filter_id, & &1),
- phrase: "cofe"
+ phrase: "cofe",
+ context: ["home"]
}
end
end