From d7e3b72c6a720a2cbb966399719c1274504b7456 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 16 Apr 2017 15:28:28 +0200 Subject: [PATCH] Add avatar objects to user, fix specs. --- lib/pleroma/user.ex | 1 + ...70416122418_add_avatar_object_to_users.exs | 9 ++++++ test/support/builders/activity_builder.ex | 4 +-- test/user_test.exs | 20 +++++++------ test/web/activity_pub/activity_pub_test.exs | 3 +- .../activity_representer_test.exs | 9 ++++-- .../representers/user_representer_test.exs | 11 ++++++- .../twitter_api_controller_test.exs | 10 +++---- test/web/twitter_api/twitter_api_test.exs | 30 +++++++++---------- 9 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 priv/repo/migrations/20170416122418_add_avatar_object_to_users.exs diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index ed85447fe..fdcc1b7d5 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -13,6 +13,7 @@ defmodule Pleroma.User do field :password_confirmation, :string, virtual: true field :following, { :array, :string }, default: [] field :ap_id, :string + field :avatar, :map timestamps() end diff --git a/priv/repo/migrations/20170416122418_add_avatar_object_to_users.exs b/priv/repo/migrations/20170416122418_add_avatar_object_to_users.exs new file mode 100644 index 000000000..b6d8742dc --- /dev/null +++ b/priv/repo/migrations/20170416122418_add_avatar_object_to_users.exs @@ -0,0 +1,9 @@ +defmodule Pleroma.Repo.Migrations.AddAvatarObjectToUsers do + use Ecto.Migration + + def change do + alter table(:users) do + add :avatar, :map + end + end +end diff --git a/test/support/builders/activity_builder.ex b/test/support/builders/activity_builder.ex index a82dc29d4..0f9cd0d15 100644 --- a/test/support/builders/activity_builder.ex +++ b/test/support/builders/activity_builder.ex @@ -3,7 +3,7 @@ defmodule Pleroma.Builders.ActivityBuilder do alias Pleroma.Web.ActivityPub.ActivityPub def build(data \\ %{}, opts \\ %{}) do - user = opts[:user] || UserBuilder.build + user = opts[:user] || Pleroma.Factory.insert(:user) activity = %{ "id" => 1, "actor" => user.ap_id, @@ -29,7 +29,7 @@ defmodule Pleroma.Builders.ActivityBuilder do end def public_and_non_public do - {:ok, user} = UserBuilder.insert + user = Pleroma.Factory.insert(:user) public = build(%{"id" => 1}, %{user: user}) non_public = build(%{"id" => 2, "to" => []}, %{user: user}) diff --git a/test/user_test.exs b/test/user_test.exs index e7843e1a8..d711adb9d 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -3,6 +3,8 @@ defmodule Pleroma.UserTest do alias Pleroma.User use Pleroma.DataCase + import Pleroma.Factory + test "ap_id returns the activity pub id for the user" do host = Application.get_env(:pleroma, Pleroma.Web.Endpoint) @@ -25,21 +27,21 @@ defmodule Pleroma.UserTest do end test "follow takes a user and another user" do - { :ok, user } = UserBuilder.insert - { :ok, following } = UserBuilder.insert(%{nickname: "guy"}) + user = insert(:user) + followed = insert(:user) - {:ok, user } = User.follow(user, following) + {:ok, user } = User.follow(user, followed) user = Repo.get(User, user.id) - assert user.following == [User.ap_followers(following)] + assert user.following == [User.ap_followers(followed)] end test "unfollow takes a user and another user" do - { :ok, following } = UserBuilder.insert(%{nickname: "guy"}) - { :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(following)]}) + followed = insert(:user) + user = insert(:user, %{following: [User.ap_followers(followed)]}) - {:ok, user } = User.unfollow(user, following) + {:ok, user } = User.unfollow(user, followed) user = Repo.get(User, user.id) @@ -47,8 +49,8 @@ defmodule Pleroma.UserTest do end test "test if a user is following another user" do - { :ok, followed } = UserBuilder.insert(%{nickname: "guy"}) - { :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(followed)]}) + followed = insert(:user) + user = insert(:user, %{following: [User.ap_followers(followed)]}) assert User.following?(user, followed) refute User.following?(followed, user) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index eeea13db3..f0bb08f3c 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -191,8 +191,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do " } - {:ok, %Object{} = object} = ActivityPub.upload(file) - IO.inspect(object) + {:ok, %Object{}} = ActivityPub.upload(file) end end end diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs index a9129bccc..d0cccb149 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -45,8 +45,11 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do test "an activity" do {:ok, user} = UserBuilder.insert - {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"}) - {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]}) + # {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"}) + mentioned_user = insert(:user, %{nickname: "shp"}) + + # {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]}) + follower = insert(:user, %{following: [User.ap_followers(user)]}) object = %Object{ data: %{ @@ -62,7 +65,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do } } - content_html = "Some content mentioning @shp" + content_html = "Some content mentioning @shp" content = HtmlSanitizeEx.strip_tags(content_html) date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601 diff --git a/test/web/twitter_api/representers/user_representer_test.exs b/test/web/twitter_api/representers/user_representer_test.exs index 76e3bd6e6..324d60982 100644 --- a/test/web/twitter_api/representers/user_representer_test.exs +++ b/test/web/twitter_api/representers/user_representer_test.exs @@ -5,13 +5,22 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter alias Pleroma.Builders.UserBuilder + import Pleroma.Factory + setup do - {:ok, user} = UserBuilder.insert + # {:ok, user} = UserBuilder.insert + user = insert(:user) [user: user] end test "A user", %{user: user} do image = "https://placehold.it/48x48" + image = if user.avatar do + + else + image + end + represented = %{ "id" => user.id, "name" => user.name, diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 3bc4eb700..3751ee703 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -94,10 +94,10 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end test "with credentials", %{conn: conn, user: current_user} do - {:ok, user} = UserBuilder.insert + user = insert(:user) activities = ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user}) returned_activities = ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user}) - {:ok, other_user} = UserBuilder.insert(%{ap_id: "glimmung", nickname: "nockame"}) + other_user = insert(:user) ActivityBuilder.insert_list(10, %{}, %{user: other_user}) since_id = List.last(activities).id @@ -110,7 +110,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do response = json_response(conn, 200) assert length(response) == 10 - assert response == Enum.map(returned_activities, fn (activity) -> ActivityRepresenter.to_map(activity, %{user: user, for: current_user}) end) + assert response == Enum.map(returned_activities, fn (activity) -> ActivityRepresenter.to_map(activity, %{user: User.get_cached_by_ap_id(activity.data["actor"]), for: current_user}) end) end end @@ -122,7 +122,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end test "with credentials", %{conn: conn, user: current_user} do - {:ok, followed } = UserBuilder.insert(%{name: "some guy"}) + followed = insert(:user) conn = conn |> with_credentials(current_user.nickname, "test") @@ -142,7 +142,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end test "with credentials", %{conn: conn, user: current_user} do - {:ok, followed } = UserBuilder.insert(%{name: "some guy"}) + followed = insert(:user) {:ok, current_user} = User.follow(current_user, followed) assert current_user.following == [User.ap_followers(followed)] diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index b8bfb4cf9..1e5204db8 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -78,7 +78,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do test "fetch public statuses" do %{ public: activity, user: user } = ActivityBuilder.public_and_non_public - {:ok, follower } = UserBuilder.insert(%{name: "dude", ap_id: "idididid", following: [User.ap_followers(user)]}) + + follower = insert(:user, following: [User.ap_followers(user)]) statuses = TwitterAPI.fetch_public_statuses(follower) @@ -87,19 +88,18 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "fetch friends' statuses" do - ActivityBuilder.public_and_non_public - + user = insert(:user, %{following: ["someguy/followers"]}) {:ok, activity} = ActivityBuilder.insert(%{"to" => ["someguy/followers"]}) - {:ok, direct_activity} = ActivityBuilder.insert(%{"to" => ["some other id"]}) - {:ok, user} = UserBuilder.insert(%{ap_id: "some other id", following: ["someguy/followers"]}) + {:ok, direct_activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]}) statuses = TwitterAPI.fetch_friend_statuses(user) activity_user = Repo.get_by(User, ap_id: activity.data["actor"]) + direct_activity_user = Repo.get_by(User, ap_id: direct_activity.data["actor"]) assert length(statuses) == 2 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user}) - assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: activity_user, mentioned: [user]}) + assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]}) end test "fetch a single status" do @@ -113,8 +113,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "Follow another user" do - { :ok, user } = UserBuilder.insert - { :ok, following } = UserBuilder.insert(%{nickname: "guy"}) + user = insert(:user) + following = insert(:user) {:ok, user, following, activity } = TwitterAPI.follow(user, following.id) @@ -126,8 +126,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "Unfollow another user" do - { :ok, following } = UserBuilder.insert(%{nickname: "guy"}) - { :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(following)]}) + following = insert(:user) + user = insert(:user, %{following: [User.ap_followers(following)]}) {:ok, user, _following } = TwitterAPI.unfollow(user, following.id) @@ -160,8 +160,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do test "it can parse mentions and return the relevant users" do text = "@gsimg According to @archaeme , that is @daggsy." - {:ok, gsimg} = UserBuilder.insert(%{nickname: "gsimg"}) - {:ok, archaeme} = UserBuilder.insert(%{nickname: "archaeme"}) + gsimg = insert(:user, %{nickname: "gsimg"}) + archaeme = insert(:user, %{nickname: "archaeme"}) expected_result = [ {"@gsimg", gsimg}, @@ -174,11 +174,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do test "it adds user links to an existing text" do text = "@gsimg According to @archaeme , that is @daggsy." - {:ok, _gsimg} = UserBuilder.insert(%{nickname: "gsimg", ap_id: "first_link" }) - {:ok, _archaeme} = UserBuilder.insert(%{nickname: "archaeme", ap_id: "second_link"}) + gsimg = insert(:user, %{nickname: "gsimg"}) + archaeme = insert(:user, %{nickname: "archaeme"}) mentions = TwitterAPI.parse_mentions(text) - expected_text = "@gsimg According to @archaeme , that is @daggsy." + expected_text = "@gsimg According to @archaeme , that is @daggsy." assert TwitterAPI.add_user_links(text, mentions) == expected_text end