Merge remote-tracking branch 'official/develop' into develop
[akkoma] / test / support / factory.ex
1 defmodule Pleroma.Factory do
2 use ExMachina.Ecto, repo: Pleroma.Repo
3
4 def user_factory do
5 user = %Pleroma.User{
6 name: sequence(:name, &"Test ใƒ†ใ‚นใƒˆ User #{&1}"),
7 email: sequence(:email, &"user#{&1}@example.com"),
8 nickname: sequence(:nickname, &"nick#{&1}"),
9 password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
10 bio: sequence(:bio, &"Tester Number #{&1}")
11 }
12
13 %{
14 user
15 | ap_id: Pleroma.User.ap_id(user),
16 follower_address: Pleroma.User.ap_followers(user),
17 following: [Pleroma.User.ap_id(user)]
18 }
19 end
20
21 def note_factory do
22 text = sequence(:text, &"This is :moominmamma: note #{&1}")
23
24 user = insert(:user)
25
26 data = %{
27 "type" => "Note",
28 "content" => text,
29 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
30 "actor" => user.ap_id,
31 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
32 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
33 "likes" => [],
34 "like_count" => 0,
35 "context" => "2hu",
36 "summary" => "2hu",
37 "tag" => ["2hu"],
38 "emoji" => %{
39 "2hu" => "corndog.png"
40 }
41 }
42
43 %Pleroma.Object{
44 data: data
45 }
46 end
47
48 def direct_note_factory do
49 user2 = insert(:user)
50
51 %Pleroma.Object{data: data} = note_factory()
52 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
53 end
54
55 def direct_note_activity_factory do
56 dm = insert(:direct_note)
57
58 data = %{
59 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
60 "type" => "Create",
61 "actor" => dm.data["actor"],
62 "to" => dm.data["to"],
63 "object" => dm.data,
64 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
65 "context" => dm.data["context"]
66 }
67
68 %Pleroma.Activity{
69 data: data,
70 actor: data["actor"],
71 recipients: data["to"]
72 }
73 end
74
75 def note_activity_factory do
76 note = insert(:note)
77
78 data = %{
79 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
80 "type" => "Create",
81 "actor" => note.data["actor"],
82 "to" => note.data["to"],
83 "object" => note.data,
84 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
85 "context" => note.data["context"]
86 }
87
88 %Pleroma.Activity{
89 data: data,
90 actor: data["actor"],
91 recipients: data["to"]
92 }
93 end
94
95 def announce_activity_factory do
96 note_activity = insert(:note_activity)
97 user = insert(:user)
98
99 data = %{
100 "type" => "Announce",
101 "actor" => note_activity.actor,
102 "object" => note_activity.data["id"],
103 "to" => [user.follower_address, note_activity.data["actor"]],
104 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
105 "context" => note_activity.data["context"]
106 }
107
108 %Pleroma.Activity{
109 data: data,
110 actor: user.ap_id,
111 recipients: data["to"]
112 }
113 end
114
115 def like_activity_factory do
116 note_activity = insert(:note_activity)
117 user = insert(:user)
118
119 data = %{
120 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
121 "actor" => user.ap_id,
122 "type" => "Like",
123 "object" => note_activity.data["object"]["id"],
124 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
125 }
126
127 %Pleroma.Activity{
128 data: data
129 }
130 end
131
132 def follow_activity_factory do
133 follower = insert(:user)
134 followed = insert(:user)
135
136 data = %{
137 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
138 "actor" => follower.ap_id,
139 "type" => "Follow",
140 "object" => followed.ap_id,
141 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
142 }
143
144 %Pleroma.Activity{
145 data: data,
146 actor: follower.ap_id
147 }
148 end
149
150 def websub_subscription_factory do
151 %Pleroma.Web.Websub.WebsubServerSubscription{
152 topic: "http://example.org",
153 callback: "http://example/org/callback",
154 secret: "here's a secret",
155 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
156 state: "requested"
157 }
158 end
159
160 def websub_client_subscription_factory do
161 %Pleroma.Web.Websub.WebsubClientSubscription{
162 topic: "http://example.org",
163 secret: "here's a secret",
164 valid_until: nil,
165 state: "requested",
166 subscribers: []
167 }
168 end
169
170 def oauth_app_factory do
171 %Pleroma.Web.OAuth.App{
172 client_name: "Some client",
173 redirect_uris: "https://example.com/callback",
174 scopes: "read",
175 website: "https://example.com",
176 client_id: "aaabbb==",
177 client_secret: "aaa;/&bbb"
178 }
179 end
180 end