+
+ test "find_or_make_user sets all the nessary input fields" do
+ uri = "https://social.heldscal.la/user/23211"
+ {:ok, user} = OStatus.find_or_make_user(uri)
+
+ assert user.info ==
+ %User.Info{
+ id: user.info.id,
+ ap_enabled: false,
+ background: %{},
+ banner: %{},
+ blocks: [],
+ deactivated: false,
+ default_scope: "public",
+ domain_blocks: [],
+ follower_count: 0,
+ is_admin: false,
+ is_moderator: false,
+ keys: nil,
+ locked: false,
+ no_rich_text: false,
+ note_count: 0,
+ settings: nil,
+ source_data: %{},
+ hub: "https://social.heldscal.la/main/push/hub",
+ magic_key:
+ "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB",
+ salmon: "https://social.heldscal.la/main/salmon/user/23211",
+ topic: "https://social.heldscal.la/api/statuses/user_timeline/23211.atom",
+ uri: "https://social.heldscal.la/user/23211"
+ }
+ end
+
+ test "find_make_or_update_actor takes an author element and returns an updated user" do
+ uri = "https://social.heldscal.la/user/23211"
+
+ {:ok, user} = OStatus.find_or_make_user(uri)
+ old_name = user.name
+ old_bio = user.bio
+ change = Ecto.Changeset.change(user, %{avatar: nil, bio: nil, name: nil})
+
+ {:ok, user} = Repo.update(change)
+ refute user.avatar
+
+ doc = XML.parse_document(File.read!("test/fixtures/23211.atom"))
+ [author] = :xmerl_xpath.string('//author[1]', doc)
+ {:ok, user} = OStatus.find_make_or_update_actor(author)
+ assert user.avatar["type"] == "Image"
+ assert user.name == old_name
+ assert user.bio == old_bio
+
+ {:ok, user_again} = OStatus.find_make_or_update_actor(author)
+ assert user_again == user
+ end
+
+ test "find_or_make_user disallows protocol downgrade" do
+ user = insert(:user, %{local: true})
+ {:ok, user} = OStatus.find_or_make_user(user.ap_id)
+
+ assert User.ap_enabled?(user)
+
+ user =
+ insert(:user, %{
+ ap_id: "https://social.heldscal.la/user/23211",
+ info: %{ap_enabled: true},
+ local: false
+ })
+
+ assert User.ap_enabled?(user)
+
+ {:ok, user} = OStatus.find_or_make_user(user.ap_id)
+ assert User.ap_enabled?(user)
+ end
+
+ test "find_make_or_update_actor disallows protocol downgrade" do
+ user = insert(:user, %{local: true})
+ {:ok, user} = OStatus.find_or_make_user(user.ap_id)
+
+ assert User.ap_enabled?(user)
+
+ user =
+ insert(:user, %{
+ ap_id: "https://social.heldscal.la/user/23211",
+ info: %{ap_enabled: true},
+ local: false
+ })
+
+ assert User.ap_enabled?(user)
+
+ {:ok, user} = OStatus.find_or_make_user(user.ap_id)
+ assert User.ap_enabled?(user)
+
+ doc = XML.parse_document(File.read!("test/fixtures/23211.atom"))
+ [author] = :xmerl_xpath.string('//author[1]', doc)
+ {:error, :invalid_protocol} = OStatus.find_make_or_update_actor(author)
+ end
+ end
+
+ describe "gathering user info from a user id" do
+ test "it returns user info in a hash" do
+ user = "shp@social.heldscal.la"
+
+ # TODO: make test local
+ {:ok, data} = OStatus.gather_user_info(user)
+
+ expected = %{
+ "hub" => "https://social.heldscal.la/main/push/hub",
+ "magic_key" =>
+ "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB",
+ "name" => "shp",
+ "nickname" => "shp",
+ "salmon" => "https://social.heldscal.la/main/salmon/user/29191",
+ "subject" => "acct:shp@social.heldscal.la",
+ "topic" => "https://social.heldscal.la/api/statuses/user_timeline/29191.atom",
+ "uri" => "https://social.heldscal.la/user/29191",
+ "host" => "social.heldscal.la",
+ "fqn" => user,
+ "bio" => "cofe",
+ "avatar" => %{
+ "type" => "Image",
+ "url" => [
+ %{
+ "href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg",
+ "mediaType" => "image/jpeg",
+ "type" => "Link"
+ }
+ ]
+ },
+ "subscribe_address" => "https://social.heldscal.la/main/ostatussub?profile={uri}",
+ "ap_id" => nil
+ }
+
+ assert data == expected
+ end
+
+ test "it works with the uri" do
+ user = "https://social.heldscal.la/user/29191"
+
+ # TODO: make test local
+ {:ok, data} = OStatus.gather_user_info(user)
+
+ expected = %{
+ "hub" => "https://social.heldscal.la/main/push/hub",
+ "magic_key" =>
+ "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB",
+ "name" => "shp",
+ "nickname" => "shp",
+ "salmon" => "https://social.heldscal.la/main/salmon/user/29191",
+ "subject" => "https://social.heldscal.la/user/29191",
+ "topic" => "https://social.heldscal.la/api/statuses/user_timeline/29191.atom",
+ "uri" => "https://social.heldscal.la/user/29191",
+ "host" => "social.heldscal.la",
+ "fqn" => user,
+ "bio" => "cofe",
+ "avatar" => %{
+ "type" => "Image",
+ "url" => [
+ %{
+ "href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg",
+ "mediaType" => "image/jpeg",
+ "type" => "Link"
+ }
+ ]
+ },
+ "subscribe_address" => "https://social.heldscal.la/main/ostatussub?profile={uri}",
+ "ap_id" => nil
+ }
+
+ assert data == expected
+ end
+ end
+
+ describe "fetching a status by it's HTML url" do
+ test "it builds a missing status from an html url" do
+ capture_log(fn ->
+ url = "https://shitposter.club/notice/2827873"
+ {:ok, [activity]} = OStatus.fetch_activity_from_url(url)
+
+ assert activity.data["actor"] == "https://shitposter.club/user/1"
+
+ assert activity.data["object"] ==
+ "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
+ end)
+ end
+
+ test "it works for atom notes, too" do
+ url = "https://social.sakamoto.gq/objects/0ccc1a2c-66b0-4305-b23a-7f7f2b040056"
+ {:ok, [activity]} = OStatus.fetch_activity_from_url(url)
+ assert activity.data["actor"] == "https://social.sakamoto.gq/users/eal"
+ assert activity.data["object"] == url
+ end
+ end
+
+ test "it doesn't add nil in the to field" do
+ incoming = File.read!("test/fixtures/nil_mention_entry.xml")
+ {:ok, [activity]} = OStatus.handle_incoming(incoming)
+
+ assert activity.data["to"] == [
+ "http://localhost:4001/users/atarifrosch@social.stopwatchingus-heidelberg.de/followers",
+ "https://www.w3.org/ns/activitystreams#Public"
+ ]
+ end
+
+ describe "is_representable?" do
+ test "Note objects are representable" do
+ note_activity = insert(:note_activity)
+
+ assert OStatus.is_representable?(note_activity)
+ end
+
+ test "Article objects are not representable" do
+ note_activity = insert(:note_activity)
+ note_object = Object.normalize(note_activity)
+
+ note_data =
+ note_object.data
+ |> Map.put("type", "Article")
+
+ Cachex.clear(:object_cache)
+
+ cs = Object.change(note_object, %{data: note_data})
+ {:ok, _article_object} = Repo.update(cs)
+
+ # the underlying object is now an Article instead of a note, so this should fail
+ refute OStatus.is_representable?(note_activity)
+ end
+ end
+
+ describe "make_user/2" do
+ test "creates new user" do
+ {:ok, user} = OStatus.make_user("https://social.heldscal.la/user/23211")
+
+ created_user =
+ User
+ |> Repo.get_by(ap_id: "https://social.heldscal.la/user/23211")
+ |> Map.put(:last_digest_emailed_at, nil)
+
+ assert user.info
+ assert user == created_user
+ end