user_representer_test.exs 2.66 KB
Newer Older
lain's avatar
lain committed
1
2
3
4
5
defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
  use Pleroma.DataCase

  alias Pleroma.User
  alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
6
  alias Pleroma.Builders.UserBuilder
lain's avatar
lain committed
7

lain's avatar
lain committed
8
9
  import Pleroma.Factory

lain's avatar
lain committed
10
  setup do
lain's avatar
lain committed
11
    user = insert(:user, bio: "<span>Here's some html</span>")
lain's avatar
lain committed
12
13
14
    [user: user]
  end

15
16
17
18
19
20
21
  test "A user with an avatar object", %{user: user} do
    image = "image"
    user = %{ user | avatar: %{ "url" => [%{"href" => image}] }}
    represented = UserRepresenter.to_map(user)
    assert represented["profile_image_url"] == image
  end

lain's avatar
lain committed
22
23
24
25
26
27
28
29
30
31
32
33
  test "A user" do
    note_activity = insert(:note_activity)
    user = User.get_cached_by_ap_id(note_activity.data["actor"])
    follower = insert(:user)
    second_follower = insert(:user)

    User.follow(follower, user)
    User.follow(second_follower, user)
    User.follow(user, follower)

    user = Repo.get!(User, user.id)

lain's avatar
lain committed
34
    image = "https://placehold.it/48x48"
lain's avatar
lain committed
35

lain's avatar
lain committed
36
37
    created_at = user.inserted_at |> DateTime.from_naive!("Etc/UTC") |> UserRepresenter.format_asctime

lain's avatar
lain committed
38
39
40
41
    represented = %{
      "id" => user.id,
      "name" => user.name,
      "screen_name" => user.nickname,
lain's avatar
lain committed
42
      "description" => HtmlSanitizeEx.strip_tags(user.bio),
lain's avatar
lain committed
43
      "created_at" => created_at,
lain's avatar
lain committed
44
45
      # Fake fields
      "favourites_count" => 0,
lain's avatar
lain committed
46
47
48
      "statuses_count" => 1,
      "friends_count" => 1,
      "followers_count" => 2,
lain's avatar
lain committed
49
50
51
52
53
      "profile_image_url" => image,
      "profile_image_url_https" => image,
      "profile_image_url_profile_size" => image,
      "profile_image_url_original" => image,
      "following" => false,
lain's avatar
lain committed
54
55
      "rights" => %{},
      "statusnet_profile_url" => user.ap_id
lain's avatar
lain committed
56
57
58
59
    }

    assert represented == UserRepresenter.to_map(user)
  end
60
61
62
63

  test "A user for a given other follower", %{user: user} do
    {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
    image = "https://placehold.it/48x48"
lain's avatar
lain committed
64
    created_at = user.inserted_at |> DateTime.from_naive!("Etc/UTC") |> UserRepresenter.format_asctime
65
66
67
68
    represented = %{
      "id" => user.id,
      "name" => user.name,
      "screen_name" => user.nickname,
lain's avatar
lain committed
69
      "description" => HtmlSanitizeEx.strip_tags(user.bio),
lain's avatar
lain committed
70
      "created_at" => created_at,
71
72
73
74
      # Fake fields
      "favourites_count" => 0,
      "statuses_count" => 0,
      "friends_count" => 0,
lain's avatar
lain committed
75
      "followers_count" => 1,
76
77
78
79
80
      "profile_image_url" => image,
      "profile_image_url_https" => image,
      "profile_image_url_profile_size" => image,
      "profile_image_url_original" => image,
      "following" => true,
lain's avatar
lain committed
81
82
      "rights" => %{},
      "statusnet_profile_url" => user.ap_id
83
84
85
86
    }

    assert represented == UserRepresenter.to_map(user, %{for: follower})
  end
lain's avatar
lain committed
87
end