logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

user_view_test.exs (10897B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 defmodule Pleroma.Web.TwitterAPI.UserViewTest do
      6   use Pleroma.DataCase
      7 
      8   alias Pleroma.User
      9   alias Pleroma.Web.CommonAPI.Utils
     10   alias Pleroma.Web.TwitterAPI.UserView
     11 
     12   import Pleroma.Factory
     13 
     14   setup do
     15     user = insert(:user, bio: "<span>Here's some html</span>")
     16     [user: user]
     17   end
     18 
     19   test "A user with only a nickname", %{user: user} do
     20     user = %{user | name: nil, nickname: "scarlett@catgirl.science"}
     21     represented = UserView.render("show.json", %{user: user})
     22     assert represented["name"] == user.nickname
     23     assert represented["name_html"] == user.nickname
     24   end
     25 
     26   test "A user with an avatar object", %{user: user} do
     27     image = "image"
     28     user = %{user | avatar: %{"url" => [%{"href" => image}]}}
     29     represented = UserView.render("show.json", %{user: user})
     30     assert represented["profile_image_url"] == image
     31   end
     32 
     33   test "A user with emoji in username" do
     34     expected =
     35       "<img class=\"emoji\" alt=\"karjalanpiirakka\" title=\"karjalanpiirakka\" src=\"/file.png\" /> man"
     36 
     37     user =
     38       insert(:user, %{
     39         info: %{
     40           source_data: %{
     41             "tag" => [
     42               %{
     43                 "type" => "Emoji",
     44                 "icon" => %{"url" => "/file.png"},
     45                 "name" => ":karjalanpiirakka:"
     46               }
     47             ]
     48           }
     49         },
     50         name: ":karjalanpiirakka: man"
     51       })
     52 
     53     represented = UserView.render("show.json", %{user: user})
     54     assert represented["name_html"] == expected
     55   end
     56 
     57   test "A user" do
     58     note_activity = insert(:note_activity)
     59     user = User.get_cached_by_ap_id(note_activity.data["actor"])
     60     {:ok, user} = User.update_note_count(user)
     61     follower = insert(:user)
     62     second_follower = insert(:user)
     63 
     64     User.follow(follower, user)
     65     User.follow(second_follower, user)
     66     User.follow(user, follower)
     67     {:ok, user} = User.update_follower_count(user)
     68     Cachex.put(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
     69 
     70     image = "http://localhost:4001/images/avi.png"
     71     banner = "http://localhost:4001/images/banner.png"
     72 
     73     represented = %{
     74       "id" => user.id,
     75       "name" => user.name,
     76       "screen_name" => user.nickname,
     77       "name_html" => user.name,
     78       "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("<br>", "\n")),
     79       "description_html" => HtmlSanitizeEx.basic_html(user.bio),
     80       "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
     81       "favourites_count" => 0,
     82       "statuses_count" => 1,
     83       "friends_count" => 1,
     84       "followers_count" => 2,
     85       "profile_image_url" => image,
     86       "profile_image_url_https" => image,
     87       "profile_image_url_profile_size" => image,
     88       "profile_image_url_original" => image,
     89       "following" => false,
     90       "follows_you" => false,
     91       "statusnet_blocking" => false,
     92       "statusnet_profile_url" => user.ap_id,
     93       "cover_photo" => banner,
     94       "background_image" => nil,
     95       "is_local" => true,
     96       "locked" => false,
     97       "hide_follows" => false,
     98       "hide_followers" => false,
     99       "fields" => [],
    100       "pleroma" => %{
    101         "confirmation_pending" => false,
    102         "tags" => [],
    103         "skip_thread_containment" => false
    104       },
    105       "rights" => %{"admin" => false, "delete_others_notice" => false},
    106       "role" => "member"
    107     }
    108 
    109     assert represented == UserView.render("show.json", %{user: user})
    110   end
    111 
    112   test "User exposes settings for themselves and only for themselves", %{user: user} do
    113     as_user = UserView.render("show.json", %{user: user, for: user})
    114     assert as_user["default_scope"] == user.info.default_scope
    115     assert as_user["no_rich_text"] == user.info.no_rich_text
    116     assert as_user["pleroma"]["notification_settings"] == user.info.notification_settings
    117     as_stranger = UserView.render("show.json", %{user: user})
    118     refute as_stranger["default_scope"]
    119     refute as_stranger["no_rich_text"]
    120     refute as_stranger["pleroma"]["notification_settings"]
    121   end
    122 
    123   test "A user for a given other follower", %{user: user} do
    124     follower = insert(:user, %{following: [User.ap_followers(user)]})
    125     {:ok, user} = User.update_follower_count(user)
    126     image = "http://localhost:4001/images/avi.png"
    127     banner = "http://localhost:4001/images/banner.png"
    128 
    129     represented = %{
    130       "id" => user.id,
    131       "name" => user.name,
    132       "screen_name" => user.nickname,
    133       "name_html" => user.name,
    134       "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("<br>", "\n")),
    135       "description_html" => HtmlSanitizeEx.basic_html(user.bio),
    136       "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
    137       "favourites_count" => 0,
    138       "statuses_count" => 0,
    139       "friends_count" => 0,
    140       "followers_count" => 1,
    141       "profile_image_url" => image,
    142       "profile_image_url_https" => image,
    143       "profile_image_url_profile_size" => image,
    144       "profile_image_url_original" => image,
    145       "following" => true,
    146       "follows_you" => false,
    147       "statusnet_blocking" => false,
    148       "statusnet_profile_url" => user.ap_id,
    149       "cover_photo" => banner,
    150       "background_image" => nil,
    151       "is_local" => true,
    152       "locked" => false,
    153       "hide_follows" => false,
    154       "hide_followers" => false,
    155       "fields" => [],
    156       "pleroma" => %{
    157         "confirmation_pending" => false,
    158         "tags" => [],
    159         "skip_thread_containment" => false
    160       },
    161       "rights" => %{"admin" => false, "delete_others_notice" => false},
    162       "role" => "member"
    163     }
    164 
    165     assert represented == UserView.render("show.json", %{user: user, for: follower})
    166   end
    167 
    168   test "A user that follows you", %{user: user} do
    169     follower = insert(:user)
    170     {:ok, follower} = User.follow(follower, user)
    171     {:ok, user} = User.update_follower_count(user)
    172     image = "http://localhost:4001/images/avi.png"
    173     banner = "http://localhost:4001/images/banner.png"
    174 
    175     represented = %{
    176       "id" => follower.id,
    177       "name" => follower.name,
    178       "screen_name" => follower.nickname,
    179       "name_html" => follower.name,
    180       "description" => HtmlSanitizeEx.strip_tags(follower.bio |> String.replace("<br>", "\n")),
    181       "description_html" => HtmlSanitizeEx.basic_html(follower.bio),
    182       "created_at" => follower.inserted_at |> Utils.format_naive_asctime(),
    183       "favourites_count" => 0,
    184       "statuses_count" => 0,
    185       "friends_count" => 1,
    186       "followers_count" => 0,
    187       "profile_image_url" => image,
    188       "profile_image_url_https" => image,
    189       "profile_image_url_profile_size" => image,
    190       "profile_image_url_original" => image,
    191       "following" => false,
    192       "follows_you" => true,
    193       "statusnet_blocking" => false,
    194       "statusnet_profile_url" => follower.ap_id,
    195       "cover_photo" => banner,
    196       "background_image" => nil,
    197       "is_local" => true,
    198       "locked" => false,
    199       "hide_follows" => false,
    200       "hide_followers" => false,
    201       "fields" => [],
    202       "pleroma" => %{
    203         "confirmation_pending" => false,
    204         "tags" => [],
    205         "skip_thread_containment" => false
    206       },
    207       "rights" => %{"admin" => false, "delete_others_notice" => false},
    208       "role" => "member"
    209     }
    210 
    211     assert represented == UserView.render("show.json", %{user: follower, for: user})
    212   end
    213 
    214   test "a user that is a moderator" do
    215     user = insert(:user, %{info: %{is_moderator: true}})
    216     represented = UserView.render("show.json", %{user: user, for: user})
    217 
    218     assert represented["rights"]["delete_others_notice"]
    219     assert represented["role"] == "moderator"
    220   end
    221 
    222   test "a user that is a admin" do
    223     user = insert(:user, %{info: %{is_admin: true}})
    224     represented = UserView.render("show.json", %{user: user, for: user})
    225 
    226     assert represented["rights"]["admin"]
    227     assert represented["role"] == "admin"
    228   end
    229 
    230   test "A moderator with hidden role for another user", %{user: user} do
    231     admin = insert(:user, %{info: %{is_moderator: true, show_role: false}})
    232     represented = UserView.render("show.json", %{user: admin, for: user})
    233 
    234     assert represented["role"] == nil
    235   end
    236 
    237   test "An admin with hidden role for another user", %{user: user} do
    238     admin = insert(:user, %{info: %{is_admin: true, show_role: false}})
    239     represented = UserView.render("show.json", %{user: admin, for: user})
    240 
    241     assert represented["role"] == nil
    242   end
    243 
    244   test "A regular user for the admin", %{user: user} do
    245     admin = insert(:user, %{info: %{is_admin: true}})
    246     represented = UserView.render("show.json", %{user: user, for: admin})
    247 
    248     assert represented["pleroma"]["deactivated"] == false
    249   end
    250 
    251   test "A blocked user for the blocker" do
    252     user = insert(:user)
    253     blocker = insert(:user)
    254     User.block(blocker, user)
    255     image = "http://localhost:4001/images/avi.png"
    256     banner = "http://localhost:4001/images/banner.png"
    257 
    258     represented = %{
    259       "id" => user.id,
    260       "name" => user.name,
    261       "screen_name" => user.nickname,
    262       "name_html" => user.name,
    263       "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("<br>", "\n")),
    264       "description_html" => HtmlSanitizeEx.basic_html(user.bio),
    265       "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
    266       "favourites_count" => 0,
    267       "statuses_count" => 0,
    268       "friends_count" => 0,
    269       "followers_count" => 0,
    270       "profile_image_url" => image,
    271       "profile_image_url_https" => image,
    272       "profile_image_url_profile_size" => image,
    273       "profile_image_url_original" => image,
    274       "following" => false,
    275       "follows_you" => false,
    276       "statusnet_blocking" => true,
    277       "statusnet_profile_url" => user.ap_id,
    278       "cover_photo" => banner,
    279       "background_image" => nil,
    280       "is_local" => true,
    281       "locked" => false,
    282       "hide_follows" => false,
    283       "hide_followers" => false,
    284       "fields" => [],
    285       "pleroma" => %{
    286         "confirmation_pending" => false,
    287         "tags" => [],
    288         "skip_thread_containment" => false
    289       },
    290       "rights" => %{"admin" => false, "delete_others_notice" => false},
    291       "role" => "member"
    292     }
    293 
    294     blocker = User.get_cached_by_id(blocker.id)
    295     assert represented == UserView.render("show.json", %{user: user, for: blocker})
    296   end
    297 
    298   test "a user with mastodon fields" do
    299     fields = [
    300       %{
    301         "name" => "Pronouns",
    302         "value" => "she/her"
    303       },
    304       %{
    305         "name" => "Website",
    306         "value" => "https://example.org/"
    307       }
    308     ]
    309 
    310     user =
    311       insert(:user, %{
    312         info: %{
    313           source_data: %{
    314             "attachment" =>
    315               Enum.map(fields, fn field -> Map.put(field, "type", "PropertyValue") end)
    316           }
    317         }
    318       })
    319 
    320     userview = UserView.render("show.json", %{user: user})
    321     assert userview["fields"] == fields
    322   end
    323 end