Add avatar updating from incoming messages.
[akkoma] / test / web / ostatus / ostatus_test.exs
1 defmodule Pleroma.Web.OStatusTest do
2 use Pleroma.DataCase
3 alias Pleroma.Web.OStatus
4 alias Pleroma.Web.XML
5
6 test "don't insert create notes twice" do
7 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
8 {:ok, [_activity]} = OStatus.handle_incoming(incoming)
9 assert {:ok, [{:error, "duplicate activity"}]} == OStatus.handle_incoming(incoming)
10 end
11
12 test "handle incoming note - GS, Salmon" do
13 incoming = File.read!("test/fixtures/incoming_note_activity.xml")
14 {:ok, [activity]} = OStatus.handle_incoming(incoming)
15
16 assert activity.data["type"] == "Create"
17 assert activity.data["object"]["type"] == "Note"
18 assert activity.data["object"]["id"] == "tag:gs.example.org:4040,2017-04-23:noticeId=29:objectType=note"
19 assert activity.data["published"] == "2017-04-23T14:51:03+00:00"
20 assert activity.data["context"] == "tag:gs.example.org:4040,2017-04-23:objectType=thread:nonce=f09e22f58abd5c7b"
21 assert "http://pleroma.example.org:4000/users/lain3" in activity.data["to"]
22 assert activity.local == false
23 end
24
25 test "handle incoming notes - GS, subscription" do
26 incoming = File.read!("test/fixtures/ostatus_incoming_post.xml")
27 {:ok, [activity]} = OStatus.handle_incoming(incoming)
28
29 assert activity.data["type"] == "Create"
30 assert activity.data["object"]["type"] == "Note"
31 assert activity.data["object"]["actor"] == "https://social.heldscal.la/user/23211"
32 assert activity.data["object"]["content"] == "Will it blend?"
33 end
34
35 test "handle incoming notes - GS, subscription, reply" do
36 incoming = File.read!("test/fixtures/ostatus_incoming_reply.xml")
37 {:ok, [activity]} = OStatus.handle_incoming(incoming)
38
39 assert activity.data["type"] == "Create"
40 assert activity.data["object"]["type"] == "Note"
41 assert activity.data["object"]["actor"] == "https://social.heldscal.la/user/23211"
42 assert activity.data["object"]["content"] == "@<a href=\"https://gs.archae.me/user/4687\" class=\"h-card u-url p-nickname mention\" title=\"shpbot\">shpbot</a> why not indeed."
43 assert activity.data["object"]["inReplyTo"] == "tag:gs.archae.me,2017-04-30:noticeId=778260:objectType=note"
44 end
45
46 test "handle incoming replies" do
47 incoming = File.read!("test/fixtures/incoming_note_activity_answer.xml")
48 {:ok, [activity]} = OStatus.handle_incoming(incoming)
49
50 assert activity.data["type"] == "Create"
51 assert activity.data["object"]["type"] == "Note"
52 assert activity.data["object"]["inReplyTo"] == "http://pleroma.example.org:4000/objects/55bce8fc-b423-46b1-af71-3759ab4670bc"
53 assert "http://pleroma.example.org:4000/users/lain5" in activity.data["to"]
54 end
55
56 describe "new remote user creation" do
57 test "tries to use the information in poco fields" do
58 # TODO make test local
59 uri = "https://social.heldscal.la/user/23211"
60
61 {:ok, user} = OStatus.find_or_make_user(uri)
62
63 user = Repo.get(Pleroma.User, user.id)
64 assert user.name == "Constance Variable"
65 assert user.nickname == "lambadalambda@social.heldscal.la"
66 assert user.local == false
67 assert user.info["uri"] == uri
68 assert user.ap_id == uri
69 assert user.avatar["type"] == "Image"
70
71 {:ok, user_again} = OStatus.find_or_make_user(uri)
72
73 assert user == user_again
74 end
75
76 test "find_make_or_update_user takes an author element and returns an updated user" do
77 # TODO make test local
78 uri = "https://social.heldscal.la/user/23211"
79
80 {:ok, user} = OStatus.find_or_make_user(uri)
81 change = Ecto.Changeset.change(user, %{avatar: nil})
82
83 {:ok, user} = Repo.update(change)
84 refute user.avatar
85
86 doc = XML.parse_document(File.read!("test/fixtures/23211.atom"))
87 [author] = :xmerl_xpath.string('//author[1]', doc)
88 {:ok, user} = OStatus.find_make_or_update_user(author)
89 assert user.avatar["type"] == "Image"
90
91 {:ok, user_again} = OStatus.find_make_or_update_user(author)
92 assert user_again == user
93 end
94 end
95
96 describe "gathering user info from a user id" do
97 test "it returns user info in a hash" do
98 user = "shp@social.heldscal.la"
99
100 # TODO: make test local
101 {:ok, data} = OStatus.gather_user_info(user)
102
103 expected = %{
104 hub: "https://social.heldscal.la/main/push/hub",
105 magic_key: "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB",
106 name: "shp",
107 nickname: "shp",
108 salmon: "https://social.heldscal.la/main/salmon/user/29191",
109 subject: "acct:shp@social.heldscal.la",
110 topic: "https://social.heldscal.la/api/statuses/user_timeline/29191.atom",
111 uri: "https://social.heldscal.la/user/29191",
112 host: "social.heldscal.la",
113 fqn: user,
114 avatar: %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]}
115 }
116 assert data == expected
117 end
118
119 test "it works with the uri" do
120 user = "https://social.heldscal.la/user/29191"
121
122 # TODO: make test local
123 {:ok, data} = OStatus.gather_user_info(user)
124
125 expected = %{
126 hub: "https://social.heldscal.la/main/push/hub",
127 magic_key: "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB",
128 name: "shp",
129 nickname: "shp",
130 salmon: "https://social.heldscal.la/main/salmon/user/29191",
131 subject: "https://social.heldscal.la/user/29191",
132 topic: "https://social.heldscal.la/api/statuses/user_timeline/29191.atom",
133 uri: "https://social.heldscal.la/user/29191",
134 host: "social.heldscal.la",
135 fqn: user,
136 avatar: %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]}
137 }
138 assert data == expected
139 end
140 end
141 end