From 42d034505a1904fae83856ab96f528fe874e70be Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Sun, 17 Mar 2019 15:37:55 +0100 Subject: Add test for conversation API beforehand --- .../mastodon_api/mastodon_api_controller_test.exs | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'test') diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 059d5237d..1560ec79c 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -248,6 +248,57 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert status["url"] != direct.data["id"] end + test "Conversations", %{conn: conn} do + user_one = insert(:user) + user_two = insert(:user) + + {:ok, user_two} = User.follow(user_two, user_one) + + {:ok, direct} = + CommonAPI.post(user_one, %{ + "status" => "Hi @#{user_two.nickname}!", + "visibility" => "direct" + }) + + {:ok, _follower_only} = + CommonAPI.post(user_one, %{ + "status" => "Hi @#{user_two.nickname}!", + "visibility" => "private" + }) + + res_conn = + conn + |> assign(:user, user) + |> get("/api/v1/conversations") + + assert response = json_response(res_conn, 200) + + assert %{ + "id" => res_id, + "accounts" => res_accounts, + "last_status" => res_last_status, + "unread" => unread + } = reponse + + assert unread == false + + # Apparently undocumented API endpoint + res_conn = + conn + |> assign(:user, user) + |> get("/api/v1/conversations/#{res_id}/read") + + assert response == json_response(res_conn, 200) + + # (vanilla) Mastodon frontend behaviour + res_conn = + conn + |> assign(:user, user) + |> get("/api/v1/statuses/#{res_last_status.id}/context") + + assert %{ancestors: [], descendants: []} == json_response(res_conn, 200) + end + test "doesn't include DMs from blocked users", %{conn: conn} do blocker = insert(:user) blocked = insert(:user) -- cgit v1.2.3 From 10fdc080a0048a4776abb4bd1b5aa22d8c65e2da Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 31 Mar 2019 20:35:10 +0200 Subject: BBS: Tests and formatting. --- test/bbs/handler_test.exs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 test/bbs/handler_test.exs (limited to 'test') diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs new file mode 100644 index 000000000..ee5f194bb --- /dev/null +++ b/test/bbs/handler_test.exs @@ -0,0 +1,30 @@ +defmodule Pleroma.BBS.HandlerTest do + use Pleroma.DataCase + alias Pleroma.BBS.Handler + alias Pleroma.Web.CommonAPI + alias Pleroma.User + + import ExUnit.CaptureIO + import Pleroma.Factory + + test "getting the home timeline" do + user = insert(:user) + followed = insert(:user) + + {:ok, user} = User.follow(user, followed) + + {:ok, _first} = CommonAPI.post(user, %{"status" => "hey"}) + {:ok, _second} = CommonAPI.post(followed, %{"status" => "hello"}) + + output = + capture_io(fn -> + Handler.handle_command(%{user: user}, "home") + end) + + assert output =~ user.nickname + assert output =~ followed.nickname + + assert output =~ "hey" + assert output =~ "hello" + end +end -- cgit v1.2.3 From e3bf6655ba412268e4f5fee645609c9738e453ef Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 31 Mar 2019 21:14:21 +0200 Subject: Add replying. --- test/bbs/handler_test.exs | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'test') diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs index ee5f194bb..a22c6d64d 100644 --- a/test/bbs/handler_test.exs +++ b/test/bbs/handler_test.exs @@ -3,9 +3,12 @@ defmodule Pleroma.BBS.HandlerTest do alias Pleroma.BBS.Handler alias Pleroma.Web.CommonAPI alias Pleroma.User + alias Pleroma.Repo + alias Pleroma.Activity import ExUnit.CaptureIO import Pleroma.Factory + import Ecto.Query test "getting the home timeline" do user = insert(:user) @@ -27,4 +30,51 @@ defmodule Pleroma.BBS.HandlerTest do assert output =~ "hey" assert output =~ "hello" end + + test "posting" do + user = insert(:user) + + output = + capture_io(fn -> + Handler.handle_command(%{user: user}, "p this is a test post") + end) + + assert output =~ "Posted" + + activity = + Repo.one( + from(a in Activity, + where: fragment("?->>'type' = ?", a.data, "Create") + ) + ) + + assert activity.actor == user.ap_id + assert activity.data["object"]["content"] == "this is a test post" + end + + test "replying" do + user = insert(:user) + another_user = insert(:user) + + {:ok, activity} = CommonAPI.post(another_user, %{"status" => "this is a test post"}) + + output = + capture_io(fn -> + Handler.handle_command(%{user: user}, "r #{activity.id} this is a reply") + end) + + assert output =~ "Replied" + + reply = + Repo.one( + from(a in Activity, + where: fragment("?->>'type' = ?", a.data, "Create"), + where: a.actor == ^user.ap_id + ) + ) + + assert reply.actor == user.ap_id + assert reply.data["object"]["content"] == "this is a reply" + assert reply.data["object"]["inReplyTo"] == activity.data["object"]["id"] + end end -- cgit v1.2.3 From b5cecebbc14c80d08f1a9f541722218c48dc514f Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 10 Apr 2019 09:32:17 +0200 Subject: Conversations: Fix specs. --- test/web/mastodon_api/mastodon_api_controller_test.exs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 9e19fb48e..519ad8f4d 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -320,7 +320,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do res_conn = conn - |> assign(:user, user) + |> assign(:user, user_one) |> get("/api/v1/conversations") assert response = json_response(res_conn, 200) @@ -330,22 +330,22 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do "accounts" => res_accounts, "last_status" => res_last_status, "unread" => unread - } = reponse + } = response assert unread == false # Apparently undocumented API endpoint res_conn = conn - |> assign(:user, user) - |> get("/api/v1/conversations/#{res_id}/read") + |> assign(:user, user_one) + |> post("/api/v1/conversations/#{res_id}/read") assert response == json_response(res_conn, 200) # (vanilla) Mastodon frontend behaviour res_conn = conn - |> assign(:user, user) + |> assign(:user, user_one) |> get("/api/v1/statuses/#{res_last_status.id}/context") assert %{ancestors: [], descendants: []} == json_response(res_conn, 200) -- cgit v1.2.3 From d1da6b155ab758ae4eb8fa154997a0a2a179897c Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 10 Apr 2019 09:34:53 +0200 Subject: Conversation: Add Conversations and Participations. --- test/conversation/participation_test.exs | 22 ++++++++++++++++++++++ test/conversation_test.exs | 12 ++++++++++++ test/support/factory.ex | 6 ++++++ 3 files changed, 40 insertions(+) create mode 100644 test/conversation/participation_test.exs create mode 100644 test/conversation_test.exs (limited to 'test') diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs new file mode 100644 index 000000000..8dc15a802 --- /dev/null +++ b/test/conversation/participation_test.exs @@ -0,0 +1,22 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Conversation.ParticipationTest do + use Pleroma.DataCase + + import Pleroma.Factory + + alias Pleroma.Conversation.Participation + + test "it creates a participation for a conversation and a user" do + user = insert(:user) + conversation = insert(:conversation) + + {:ok, %Participation{} = participation} = + Participation.create_for_user_and_conversation(user, conversation) + + assert participation.user_id == user.id + assert participation.conversation_id == conversation.id + end +end diff --git a/test/conversation_test.exs b/test/conversation_test.exs new file mode 100644 index 000000000..8fb55d51c --- /dev/null +++ b/test/conversation_test.exs @@ -0,0 +1,12 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.ConversationTest do + use Pleroma.DataCase + alias Pleroma.Conversation + + test "it creates a conversation for given ap_id" do + assert {:ok, %Conversation{}} = Conversation.create_for_ap_id("https://some_ap_id") + end +end diff --git a/test/support/factory.ex b/test/support/factory.ex index ea59912cf..af38be46c 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -5,6 +5,12 @@ defmodule Pleroma.Factory do use ExMachina.Ecto, repo: Pleroma.Repo + def conversation_factory do + %Pleroma.Conversation{ + ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}") + } + end + def user_factory do user = %Pleroma.User{ name: sequence(:name, &"Test テスト User #{&1}"), -- cgit v1.2.3 From 64c1c3a4071f3f99a59f38e2dcde499bda3969cf Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 10 Apr 2019 15:12:01 +0200 Subject: Participations: Add marking as read and unread. --- test/conversation/participation_test.exs | 14 ++++++++++++++ test/support/factory.ex | 11 +++++++++++ 2 files changed, 25 insertions(+) (limited to 'test') diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs index 8dc15a802..eae1873ca 100644 --- a/test/conversation/participation_test.exs +++ b/test/conversation/participation_test.exs @@ -19,4 +19,18 @@ defmodule Pleroma.Conversation.ParticipationTest do assert participation.user_id == user.id assert participation.conversation_id == conversation.id end + + test "it marks a participation as read" do + participation = insert(:participation, %{read: false}) + {:ok, participation} = Participation.mark_as_read(participation) + + assert participation.read + end + + test "it marks a participation as unread" do + participation = insert(:participation, %{read: true}) + {:ok, participation} = Participation.mark_as_unread(participation) + + refute participation.read + end end diff --git a/test/support/factory.ex b/test/support/factory.ex index af38be46c..2a2954ad6 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -5,6 +5,17 @@ defmodule Pleroma.Factory do use ExMachina.Ecto, repo: Pleroma.Repo + def participation_factory do + conversation = insert(:conversation) + user = insert(:user) + + %Pleroma.Conversation.Participation{ + conversation: conversation, + user: user, + read: false + } + end + def conversation_factory do %Pleroma.Conversation{ ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}") -- cgit v1.2.3 From 280172f6f6d74872349e3b4e6f1feaa9c95b3900 Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 10 Apr 2019 16:33:45 +0200 Subject: Conversations: Create or bump on inserting a dm. --- test/conversation/participation_test.exs | 22 ++++++++- test/conversation_test.exs | 84 +++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs index eae1873ca..4e7d9dc92 100644 --- a/test/conversation/participation_test.exs +++ b/test/conversation/participation_test.exs @@ -4,9 +4,7 @@ defmodule Pleroma.Conversation.ParticipationTest do use Pleroma.DataCase - import Pleroma.Factory - alias Pleroma.Conversation.Participation test "it creates a participation for a conversation and a user" do @@ -18,6 +16,26 @@ defmodule Pleroma.Conversation.ParticipationTest do assert participation.user_id == user.id assert participation.conversation_id == conversation.id + + :timer.sleep(1000) + # Creating again returns the same participation + {:ok, %Participation{} = participation_two} = + Participation.create_for_user_and_conversation(user, conversation) + + assert participation.id == participation_two.id + refute participation.updated_at == participation_two.updated_at + end + + test "recreating an existing participations sets it to unread" do + participation = insert(:participation, %{read: true}) + + {:ok, participation} = + Participation.create_for_user_and_conversation( + participation.user, + participation.conversation + ) + + refute participation.read end test "it marks a participation as read" do diff --git a/test/conversation_test.exs b/test/conversation_test.exs index 8fb55d51c..1c9d485ff 100644 --- a/test/conversation_test.exs +++ b/test/conversation_test.exs @@ -5,8 +5,90 @@ defmodule Pleroma.ConversationTest do use Pleroma.DataCase alias Pleroma.Conversation + alias Pleroma.Web.CommonAPI + + import Pleroma.Factory test "it creates a conversation for given ap_id" do - assert {:ok, %Conversation{}} = Conversation.create_for_ap_id("https://some_ap_id") + assert {:ok, %Conversation{} = conversation} = + Conversation.create_for_ap_id("https://some_ap_id") + + # Inserting again returns the same + assert {:ok, conversation_two} = Conversation.create_for_ap_id("https://some_ap_id") + assert conversation_two.id == conversation.id + end + + test "public posts don't create conversations" do + user = insert(:user) + {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey"}) + + context = activity.data["object"]["context"] + + conversation = Conversation.get_for_ap_id(context) + + refute conversation + end + + test "it creates or updates a conversation and participations for a given DM" do + har = insert(:user) + jafnhar = insert(:user) + tridi = insert(:user) + + {:ok, activity} = + CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"}) + + context = activity.data["object"]["context"] + + conversation = + Conversation.get_for_ap_id(context) + |> Repo.preload(:participations) + + assert conversation + [har_participation, jafnhar_participation] = conversation.participations + + assert har_participation.user_id == har.id + assert jafnhar_participation.user_id == jafnhar.id + + {:ok, activity} = + CommonAPI.post(jafnhar, %{ + "status" => "Hey @#{har.nickname}", + "visibility" => "direct", + "in_reply_to_status_id" => activity.id + }) + + context = activity.data["object"]["context"] + + conversation_two = + Conversation.get_for_ap_id(context) + |> Repo.preload(:participations) + + assert conversation_two.id == conversation.id + + [har_participation_two, jafnhar_participation_two] = conversation_two.participations + + assert har_participation_two.user_id == har.id + assert jafnhar_participation_two.user_id == jafnhar.id + + {:ok, activity} = + CommonAPI.post(tridi, %{ + "status" => "Hey @#{har.nickname}", + "visibility" => "direct", + "in_reply_to_status_id" => activity.id + }) + + context = activity.data["object"]["context"] + + conversation_three = + Conversation.get_for_ap_id(context) + |> Repo.preload(:participations) + + assert conversation_three.id == conversation.id + + [har_participation_three, jafnhar_participation_three, tridi_participation] = + conversation_three.participations + + assert har_participation_three.user_id == har.id + assert jafnhar_participation_three.user_id == jafnhar.id + assert tridi_participation.user_id == tridi.id end end -- cgit v1.2.3 From 20d9b9076051d2dea60919ad85aaf88154629dc4 Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 10 Apr 2019 17:05:33 +0200 Subject: Participation: Get for a user. --- test/conversation/participation_test.exs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'test') diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs index 4e7d9dc92..c52b4ed88 100644 --- a/test/conversation/participation_test.exs +++ b/test/conversation/participation_test.exs @@ -6,6 +6,7 @@ defmodule Pleroma.Conversation.ParticipationTest do use Pleroma.DataCase import Pleroma.Factory alias Pleroma.Conversation.Participation + alias Pleroma.Web.CommonAPI test "it creates a participation for a conversation and a user" do user = insert(:user) @@ -51,4 +52,33 @@ defmodule Pleroma.Conversation.ParticipationTest do refute participation.read end + + test "gets all the participations for a user, ordered by updated at descending" do + user = insert(:user) + {:ok, activity_one} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"}) + :timer.sleep(1000) + {:ok, activity_two} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"}) + :timer.sleep(1000) + + {:ok, activity_three} = + CommonAPI.post(user, %{ + "status" => "x", + "visibility" => "direct", + "in_reply_to_status_id" => activity_one.id + }) + + assert [participation_one, participation_two] = + Participation.for_user(user) + |> Repo.preload(:conversation) + + assert participation_one.conversation.ap_id == activity_three.data["object"]["context"] + assert participation_two.conversation.ap_id == activity_two.data["object"]["context"] + + # Pagination + assert [participation_one] = + Participation.for_user(user, %{limit: 1}) + |> Repo.preload(:conversation) + + assert participation_one.conversation.ap_id == activity_three.data["object"]["context"] + end end -- cgit v1.2.3 From cf353514feff50c2ccb9a8079ce5e695eb7f8cb6 Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 10 Apr 2019 17:28:02 +0200 Subject: Participations: Add last activity. --- test/conversation/participation_test.exs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'test') diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs index c52b4ed88..5791fa0db 100644 --- a/test/conversation/participation_test.exs +++ b/test/conversation/participation_test.exs @@ -80,5 +80,12 @@ defmodule Pleroma.Conversation.ParticipationTest do |> Repo.preload(:conversation) assert participation_one.conversation.ap_id == activity_three.data["object"]["context"] + + # With last_activity_id + assert [participation_one] = + Participation.for_user_with_last_activity_id(user, %{limit: 1}) + |> Repo.preload(:conversation) + + assert participation_one.last_activity_id == activity_three.id end end -- cgit v1.2.3 From c352a0aba601ae444bf5b479ab3c643728a8b35e Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 10 Apr 2019 17:48:31 +0200 Subject: Conversations: Make tests run. --- .../mastodon_api/mastodon_api_controller_test.exs | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'test') diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 519ad8f4d..d1d22edde 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -325,14 +325,17 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert response = json_response(res_conn, 200) - assert %{ - "id" => res_id, - "accounts" => res_accounts, - "last_status" => res_last_status, - "unread" => unread - } = response + assert [ + %{ + "id" => res_id, + "accounts" => res_accounts, + "last_status" => res_last_status, + "unread" => unread + } + ] = response - assert unread == false + assert unread == true + assert res_last_status == direct.id # Apparently undocumented API endpoint res_conn = @@ -340,15 +343,16 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do |> assign(:user, user_one) |> post("/api/v1/conversations/#{res_id}/read") - assert response == json_response(res_conn, 200) + assert response = json_response(res_conn, 200) + assert response["unread"] == false # (vanilla) Mastodon frontend behaviour res_conn = conn |> assign(:user, user_one) - |> get("/api/v1/statuses/#{res_last_status.id}/context") + |> get("/api/v1/statuses/#{res_last_status}/context") - assert %{ancestors: [], descendants: []} == json_response(res_conn, 200) + assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200) end test "doesn't include DMs from blocked users", %{conn: conn} do -- cgit v1.2.3 From 36ec8d969405fc8e58a43100554c358fbeb6b4fc Mon Sep 17 00:00:00 2001 From: lain Date: Thu, 11 Apr 2019 13:20:46 +0200 Subject: ActivityPub: Fix specs. --- test/web/activity_pub/activity_pub_test.exs | 48 ++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 17fec05b1..c3911500e 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -126,9 +126,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end test "doesn't drop activities with content being null" do + user = insert(:user) + data = %{ - "ok" => true, + "actor" => user.ap_id, + "to" => [], "object" => %{ + "actor" => user.ap_id, + "to" => [], + "type" => "Note", "content" => nil } } @@ -144,8 +150,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end test "inserts a given map into the activity database, giving it an id if it has none." do + user = insert(:user) + data = %{ - "ok" => true + "actor" => user.ap_id, + "to" => [], + "object" => %{ + "actor" => user.ap_id, + "to" => [], + "type" => "Note", + "content" => "hey" + } } {:ok, %Activity{} = activity} = ActivityPub.insert(data) @@ -155,9 +170,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do given_id = "bla" data = %{ - "ok" => true, "id" => given_id, - "context" => "blabla" + "actor" => user.ap_id, + "to" => [], + "context" => "blabla", + "object" => %{ + "actor" => user.ap_id, + "to" => [], + "type" => "Note", + "content" => "hey" + } } {:ok, %Activity{} = activity} = ActivityPub.insert(data) @@ -168,10 +190,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end test "adds a context when none is there" do + user = insert(:user) + data = %{ - "id" => "some_id", + "actor" => user.ap_id, + "to" => [], "object" => %{ - "id" => "object_id" + "actor" => user.ap_id, + "to" => [], + "type" => "Note", + "content" => "hey" } } @@ -184,10 +212,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do + user = insert(:user) + data = %{ + "actor" => user.ap_id, + "to" => [], "object" => %{ + "actor" => user.ap_id, + "to" => [], "type" => "Note", - "ok" => true + "content" => "hey" } } -- cgit v1.2.3 From 6f880b162736861189526ef0602f54bae6ff6153 Mon Sep 17 00:00:00 2001 From: lain Date: Thu, 11 Apr 2019 13:31:20 +0200 Subject: Conversation: Fix tests. --- test/conversation_test.exs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'test') diff --git a/test/conversation_test.exs b/test/conversation_test.exs index 1c9d485ff..4e3e86c8d 100644 --- a/test/conversation_test.exs +++ b/test/conversation_test.exs @@ -44,10 +44,12 @@ defmodule Pleroma.ConversationTest do |> Repo.preload(:participations) assert conversation - [har_participation, jafnhar_participation] = conversation.participations - assert har_participation.user_id == har.id - assert jafnhar_participation.user_id == jafnhar.id + assert Enum.find(conversation.participations, fn %{user_id: user_id} -> har.id == user_id end) + + assert Enum.find(conversation.participations, fn %{user_id: user_id} -> + jafnhar.id == user_id + end) {:ok, activity} = CommonAPI.post(jafnhar, %{ @@ -64,10 +66,13 @@ defmodule Pleroma.ConversationTest do assert conversation_two.id == conversation.id - [har_participation_two, jafnhar_participation_two] = conversation_two.participations + assert Enum.find(conversation_two.participations, fn %{user_id: user_id} -> + har.id == user_id + end) - assert har_participation_two.user_id == har.id - assert jafnhar_participation_two.user_id == jafnhar.id + assert Enum.find(conversation_two.participations, fn %{user_id: user_id} -> + jafnhar.id == user_id + end) {:ok, activity} = CommonAPI.post(tridi, %{ @@ -84,11 +89,16 @@ defmodule Pleroma.ConversationTest do assert conversation_three.id == conversation.id - [har_participation_three, jafnhar_participation_three, tridi_participation] = - conversation_three.participations + assert Enum.find(conversation_three.participations, fn %{user_id: user_id} -> + har.id == user_id + end) + + assert Enum.find(conversation_three.participations, fn %{user_id: user_id} -> + jafnhar.id == user_id + end) - assert har_participation_three.user_id == har.id - assert jafnhar_participation_three.user_id == jafnhar.id - assert tridi_participation.user_id == tridi.id + assert Enum.find(conversation_three.participations, fn %{user_id: user_id} -> + tridi.id == user_id + end) end end -- cgit v1.2.3 From c1ebb38d3adc1d222be832405ec0d7497b61f94a Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 15 Apr 2019 21:45:25 +0200 Subject: Conversation: Also create participations for remote users. Needed to get the participating user list. --- test/conversation_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/conversation_test.exs b/test/conversation_test.exs index 4e3e86c8d..150d55631 100644 --- a/test/conversation_test.exs +++ b/test/conversation_test.exs @@ -31,7 +31,7 @@ defmodule Pleroma.ConversationTest do test "it creates or updates a conversation and participations for a given DM" do har = insert(:user) - jafnhar = insert(:user) + jafnhar = insert(:user, local: false) tridi = insert(:user) {:ok, activity} = -- cgit v1.2.3 From 0da985182f26927de0d2d9733816600afb89e79e Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 15 Apr 2019 21:58:58 +0200 Subject: Conversation: Return full status object, id is a string. --- test/web/mastodon_api/mastodon_api_controller_test.exs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index d1d22edde..bd13f870c 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -334,8 +334,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do } ] = response + assert is_binary(res_id) assert unread == true - assert res_last_status == direct.id + assert res_last_status["id"] == direct.id # Apparently undocumented API endpoint res_conn = @@ -350,7 +351,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do res_conn = conn |> assign(:user, user_one) - |> get("/api/v1/statuses/#{res_last_status}/context") + |> get("/api/v1/statuses/#{res_last_status["id"]}/context") assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200) end -- cgit v1.2.3 From 76999c73a790232ff0c30fff7a51589fb44651a3 Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 15 Apr 2019 22:28:42 +0200 Subject: Conversation: Add accounts to output. --- test/conversation_test.exs | 14 +++++++++++++- test/web/mastodon_api/mastodon_api_controller_test.exs | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/conversation_test.exs b/test/conversation_test.exs index 150d55631..239dda04f 100644 --- a/test/conversation_test.exs +++ b/test/conversation_test.exs @@ -85,7 +85,7 @@ defmodule Pleroma.ConversationTest do conversation_three = Conversation.get_for_ap_id(context) - |> Repo.preload(:participations) + |> Repo.preload([:participations, :users]) assert conversation_three.id == conversation.id @@ -100,5 +100,17 @@ defmodule Pleroma.ConversationTest do assert Enum.find(conversation_three.participations, fn %{user_id: user_id} -> tridi.id == user_id end) + + assert Enum.find(conversation_three.users, fn %{id: user_id} -> + har.id == user_id + end) + + assert Enum.find(conversation_three.users, fn %{id: user_id} -> + jafnhar.id == user_id + end) + + assert Enum.find(conversation_three.users, fn %{id: user_id} -> + tridi.id == user_id + end) end end diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index bd13f870c..4fa5254f3 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -334,6 +334,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do } ] = response + assert length(res_accounts) == 2 assert is_binary(res_id) assert unread == true assert res_last_status["id"] == direct.id -- cgit v1.2.3 From 2662bea4e0d945edf7a24a44edf3ed39b2e64de9 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Sun, 21 Apr 2019 20:26:13 +0700 Subject: Add accounts and last_status to conversation read response --- test/web/mastodon_api/mastodon_api_controller_test.exs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test') diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 4fa5254f3..cf77dff78 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -346,6 +346,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do |> post("/api/v1/conversations/#{res_id}/read") assert response = json_response(res_conn, 200) + assert length(response["accounts"]) == 2 + assert response["last_status"]["id"] == direct.id assert response["unread"] == false # (vanilla) Mastodon frontend behaviour -- cgit v1.2.3 From 963d5774af7efb57fa306b3ac164049f8958a72c Mon Sep 17 00:00:00 2001 From: Sachin Joshi Date: Wed, 24 Apr 2019 07:06:17 +0545 Subject: fix the status notification with special char --- test/web/twitter_api/views/activity_view_test.exs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test') diff --git a/test/web/twitter_api/views/activity_view_test.exs b/test/web/twitter_api/views/activity_view_test.exs index d84ab7420..85815ba7e 100644 --- a/test/web/twitter_api/views/activity_view_test.exs +++ b/test/web/twitter_api/views/activity_view_test.exs @@ -371,4 +371,14 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do assert length(result["attachments"]) == 1 assert result["summary"] == "Friday Night" end + + test "special characters are not escaped in text field for status created" do + text = "<3 is on the way" + + {:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text}) + + result = ActivityView.render("activity.json", activity: activity) + + assert result["text"] == text + end end -- cgit v1.2.3 From f11e7037c21d611cddd7f2eab64ebfc39630a078 Mon Sep 17 00:00:00 2001 From: Alex S Date: Wed, 1 May 2019 16:09:53 +0700 Subject: test fixes --- test/formatter_test.exs | 2 +- test/user_test.exs | 24 ++++++++++++------------ test/web/activity_pub/transmogrifier_test.exs | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) (limited to 'test') diff --git a/test/formatter_test.exs b/test/formatter_test.exs index 97eb2f583..fdaf29742 100644 --- a/test/formatter_test.exs +++ b/test/formatter_test.exs @@ -147,7 +147,7 @@ defmodule Pleroma.FormatterTest do end test "gives a replacement for user links when the user is using Osada" do - mike = User.get_or_fetch("mike@osada.macgirvin.com") + {:ok, mike} = User.get_or_fetch("mike@osada.macgirvin.com") text = "@mike@osada.macgirvin.com test" diff --git a/test/user_test.exs b/test/user_test.exs index 7be47e5fb..67266cb7a 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -362,7 +362,7 @@ defmodule Pleroma.UserTest do describe "get_or_fetch/1" do test "gets an existing user by nickname" do user = insert(:user) - fetched_user = User.get_or_fetch(user.nickname) + {:ok, fetched_user} = User.get_or_fetch(user.nickname) assert user == fetched_user end @@ -379,7 +379,7 @@ defmodule Pleroma.UserTest do info: %{} ) - fetched_user = User.get_or_fetch(ap_id) + {:ok, fetched_user} = User.get_or_fetch(ap_id) freshed_user = refresh_record(user) assert freshed_user == fetched_user end @@ -388,14 +388,14 @@ defmodule Pleroma.UserTest do describe "fetching a user from nickname or trying to build one" do test "gets an existing user" do user = insert(:user) - fetched_user = User.get_or_fetch_by_nickname(user.nickname) + {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname) assert user == fetched_user end test "gets an existing user, case insensitive" do user = insert(:user, nickname: "nick") - fetched_user = User.get_or_fetch_by_nickname("NICK") + {:ok, fetched_user} = User.get_or_fetch_by_nickname("NICK") assert user == fetched_user end @@ -403,7 +403,7 @@ defmodule Pleroma.UserTest do test "gets an existing user by fully qualified nickname" do user = insert(:user) - fetched_user = + {:ok, fetched_user} = User.get_or_fetch_by_nickname(user.nickname <> "@" <> Pleroma.Web.Endpoint.host()) assert user == fetched_user @@ -413,24 +413,24 @@ defmodule Pleroma.UserTest do user = insert(:user, nickname: "nick") casing_altered_fqn = String.upcase(user.nickname <> "@" <> Pleroma.Web.Endpoint.host()) - fetched_user = User.get_or_fetch_by_nickname(casing_altered_fqn) + {:ok, fetched_user} = User.get_or_fetch_by_nickname(casing_altered_fqn) assert user == fetched_user end test "fetches an external user via ostatus if no user exists" do - fetched_user = User.get_or_fetch_by_nickname("shp@social.heldscal.la") + {:ok, fetched_user} = User.get_or_fetch_by_nickname("shp@social.heldscal.la") assert fetched_user.nickname == "shp@social.heldscal.la" end test "returns nil if no user could be fetched" do - fetched_user = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la") - assert fetched_user == nil + {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la") + assert fetched_user == "not found nonexistant@social.heldscal.la" end test "returns nil for nonexistant local user" do - fetched_user = User.get_or_fetch_by_nickname("nonexistant") - assert fetched_user == nil + {:error, fetched_user} = User.get_or_fetch_by_nickname("nonexistant") + assert fetched_user == "not found nonexistant" end test "updates an existing user, if stale" do @@ -448,7 +448,7 @@ defmodule Pleroma.UserTest do assert orig_user.last_refreshed_at == a_week_ago - user = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin") + {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin") assert user.info.source_data["endpoints"] refute user.last_refreshed_at == orig_user.last_refreshed_at diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 78429c7c6..c24b50f8c 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -219,7 +219,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do Pleroma.Config.put([:user, :deny_follow_blocked], true) user = insert(:user) - target = User.get_or_fetch("http://mastodon.example.org/users/admin") + {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin") {:ok, user} = User.block(user, target) -- cgit v1.2.3 From c854bff8f528b4f1560707ac3aa74f4a37044f52 Mon Sep 17 00:00:00 2001 From: Maksim Date: Wed, 1 May 2019 13:28:04 +0000 Subject: Refactored Pleroma.Web.Auth.Authenticator --- test/media_proxy_test.exs | 14 ++++++------ test/web/auth/authenticator_test.exs | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 test/web/auth/authenticator_test.exs (limited to 'test') diff --git a/test/media_proxy_test.exs b/test/media_proxy_test.exs index a4331478e..0a02039a6 100644 --- a/test/media_proxy_test.exs +++ b/test/media_proxy_test.exs @@ -7,15 +7,15 @@ defmodule Pleroma.MediaProxyTest do import Pleroma.Web.MediaProxy alias Pleroma.Web.MediaProxy.MediaProxyController + setup do + enabled = Pleroma.Config.get([:media_proxy, :enabled]) + on_exit(fn -> Pleroma.Config.put([:media_proxy, :enabled], enabled) end) + :ok + end + describe "when enabled" do setup do - enabled = Pleroma.Config.get([:media_proxy, :enabled]) - - unless enabled do - Pleroma.Config.put([:media_proxy, :enabled], true) - on_exit(fn -> Pleroma.Config.put([:media_proxy, :enabled], enabled) end) - end - + Pleroma.Config.put([:media_proxy, :enabled], true) :ok end diff --git a/test/web/auth/authenticator_test.exs b/test/web/auth/authenticator_test.exs new file mode 100644 index 000000000..fea5c8209 --- /dev/null +++ b/test/web/auth/authenticator_test.exs @@ -0,0 +1,42 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.Auth.AuthenticatorTest do + use Pleroma.Web.ConnCase + + alias Pleroma.Web.Auth.Authenticator + import Pleroma.Factory + + describe "fetch_user/1" do + test "returns user by name" do + user = insert(:user) + assert Authenticator.fetch_user(user.nickname) == user + end + + test "returns user by email" do + user = insert(:user) + assert Authenticator.fetch_user(user.email) == user + end + + test "returns nil" do + assert Authenticator.fetch_user("email") == nil + end + end + + describe "fetch_credentials/1" do + test "returns name and password from authorization params" do + params = %{"authorization" => %{"name" => "test", "password" => "test-pass"}} + assert Authenticator.fetch_credentials(params) == {:ok, {"test", "test-pass"}} + end + + test "returns name and password with grant_type 'password'" do + params = %{"grant_type" => "password", "username" => "test", "password" => "test-pass"} + assert Authenticator.fetch_credentials(params) == {:ok, {"test", "test-pass"}} + end + + test "returns error" do + assert Authenticator.fetch_credentials(%{}) == {:error, :invalid_credentials} + end + end +end -- cgit v1.2.3 From 8af55728e47f9f62d237704cd5a33fba5f946fa2 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Mon, 29 Apr 2019 03:44:04 +0700 Subject: Fix tests --- test/conversation/participation_test.exs | 20 +++++++++----------- test/conversation_test.exs | 12 ++++++++---- test/web/activity_pub/activity_pub_test.exs | 5 +++-- 3 files changed, 20 insertions(+), 17 deletions(-) (limited to 'test') diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs index 5791fa0db..568953b07 100644 --- a/test/conversation/participation_test.exs +++ b/test/conversation/participation_test.exs @@ -67,24 +67,22 @@ defmodule Pleroma.Conversation.ParticipationTest do "in_reply_to_status_id" => activity_one.id }) - assert [participation_one, participation_two] = - Participation.for_user(user) - |> Repo.preload(:conversation) + assert [participation_one, participation_two] = Participation.for_user(user) - assert participation_one.conversation.ap_id == activity_three.data["object"]["context"] - assert participation_two.conversation.ap_id == activity_two.data["object"]["context"] + object2 = Pleroma.Object.normalize(activity_two) + object3 = Pleroma.Object.normalize(activity_three) + + assert participation_one.conversation.ap_id == object3.data["context"] + assert participation_two.conversation.ap_id == object2.data["context"] # Pagination - assert [participation_one] = - Participation.for_user(user, %{limit: 1}) - |> Repo.preload(:conversation) + assert [participation_one] = Participation.for_user(user, %{"limit" => 1}) - assert participation_one.conversation.ap_id == activity_three.data["object"]["context"] + assert participation_one.conversation.ap_id == object3.data["context"] # With last_activity_id assert [participation_one] = - Participation.for_user_with_last_activity_id(user, %{limit: 1}) - |> Repo.preload(:conversation) + Participation.for_user_with_last_activity_id(user, %{"limit" => 1}) assert participation_one.last_activity_id == activity_three.id end diff --git a/test/conversation_test.exs b/test/conversation_test.exs index 239dda04f..763183d6b 100644 --- a/test/conversation_test.exs +++ b/test/conversation_test.exs @@ -22,7 +22,8 @@ defmodule Pleroma.ConversationTest do user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey"}) - context = activity.data["object"]["context"] + object = Pleroma.Object.normalize(activity) + context = object.data["context"] conversation = Conversation.get_for_ap_id(context) @@ -37,7 +38,8 @@ defmodule Pleroma.ConversationTest do {:ok, activity} = CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"}) - context = activity.data["object"]["context"] + object = Pleroma.Object.normalize(activity) + context = object.data["context"] conversation = Conversation.get_for_ap_id(context) @@ -58,7 +60,8 @@ defmodule Pleroma.ConversationTest do "in_reply_to_status_id" => activity.id }) - context = activity.data["object"]["context"] + object = Pleroma.Object.normalize(activity) + context = object.data["context"] conversation_two = Conversation.get_for_ap_id(context) @@ -81,7 +84,8 @@ defmodule Pleroma.ConversationTest do "in_reply_to_status_id" => activity.id }) - context = activity.data["object"]["context"] + object = Pleroma.Object.normalize(activity) + context = object.data["context"] conversation_three = Conversation.get_for_ap_id(context) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 15276ba7b..047270a2a 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -208,11 +208,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do } {:ok, %Activity{} = activity} = ActivityPub.insert(data) + object = Pleroma.Object.normalize(activity) assert is_binary(activity.data["context"]) - assert is_binary(activity.data["object"]["context"]) + assert is_binary(object.data["context"]) assert activity.data["context_id"] - assert activity.data["object"]["context_id"] + assert object.data["context_id"] end test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do -- cgit v1.2.3 From a53a6c9d64f2c32ca3b53a4317980b3e7c0b37a5 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Thu, 2 May 2019 22:25:21 +0900 Subject: Add oauth plug tests for url and body parameters --- test/plugs/oauth_plug_test.exs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'test') diff --git a/test/plugs/oauth_plug_test.exs b/test/plugs/oauth_plug_test.exs index 17fdba916..5a2ed11cc 100644 --- a/test/plugs/oauth_plug_test.exs +++ b/test/plugs/oauth_plug_test.exs @@ -38,6 +38,26 @@ defmodule Pleroma.Plugs.OAuthPlugTest do assert conn.assigns[:user] == opts[:user] end + test "with valid token(downcase) in url parameters, it assings the user", opts do + conn = + :get + |> build_conn("/?access_token=#{opts[:token]}") + |> put_req_header("content-type", "application/json") + |> fetch_query_params() + |> OAuthPlug.call(%{}) + + assert conn.assigns[:user] == opts[:user] + end + + test "with valid token(downcase) in body parameters, it assigns the user", opts do + conn = + :post + |> build_conn("/api/v1/statuses", access_token: opts[:token], status: "test") + |> OAuthPlug.call(%{}) + + assert conn.assigns[:user] == opts[:user] + end + test "with invalid token, it not assigns the user", %{conn: conn} do conn = conn -- cgit v1.2.3 From 81d1aa424d65b364ec8f2aee45247e7d95e3f255 Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 3 May 2019 13:39:14 +0200 Subject: Streamer: Stream out Conversations/Participations. --- test/conversation_test.exs | 17 +++++++++++++++++ test/web/activity_pub/activity_pub_test.exs | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) (limited to 'test') diff --git a/test/conversation_test.exs b/test/conversation_test.exs index 763183d6b..f3300e7d1 100644 --- a/test/conversation_test.exs +++ b/test/conversation_test.exs @@ -117,4 +117,21 @@ defmodule Pleroma.ConversationTest do tridi.id == user_id end) end + + test "create_or_bump_for returns the conversation with participations" do + har = insert(:user) + jafnhar = insert(:user, local: false) + + {:ok, activity} = + CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"}) + + {:ok, conversation} = Conversation.create_or_bump_for(activity) + + assert length(conversation.participations) == 2 + + {:ok, activity} = + CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "public"}) + + assert {:error, _} = Conversation.create_or_bump_for(activity) + end end diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 047270a2a..1e056b7ee 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -22,6 +22,28 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do :ok end + describe "streaming out participations" do + test "it streams them out" do + user = insert(:user) + {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"}) + + {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity) + + participations = + conversation.participations + |> Repo.preload(:user) + + with_mock Pleroma.Web.Streamer, + stream: fn _, _ -> nil end do + ActivityPub.stream_out_participations(conversation.participations) + + Enum.each(participations, fn participation -> + assert called(Pleroma.Web.Streamer.stream("participation", participation)) + end) + end + end + end + describe "fetching restricted by visibility" do test "it restricts by the appropriate visibility" do user = insert(:user) -- cgit v1.2.3 From 85b5c60694e07d3bfb1f885d5fda14be6b7bade9 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Thu, 7 Feb 2019 16:41:20 +0100 Subject: Pleroma.Formatter: width/height to class=emoji --- test/formatter_test.exs | 4 ++-- test/web/twitter_api/views/activity_view_test.exs | 2 +- test/web/twitter_api/views/user_view_test.exs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/formatter_test.exs b/test/formatter_test.exs index fdaf29742..06f4f6e50 100644 --- a/test/formatter_test.exs +++ b/test/formatter_test.exs @@ -248,7 +248,7 @@ defmodule Pleroma.FormatterTest do text = "I love :firefox:" expected_result = - "I love \"firefox\"" + "I love \"firefox\"" assert Formatter.emojify(text) == expected_result end @@ -263,7 +263,7 @@ defmodule Pleroma.FormatterTest do } expected_result = - "I love \"\"" + "I love \"\"" assert Formatter.emojify(text, custom_emoji) == expected_result end diff --git a/test/web/twitter_api/views/activity_view_test.exs b/test/web/twitter_api/views/activity_view_test.exs index 85815ba7e..1aa533b48 100644 --- a/test/web/twitter_api/views/activity_view_test.exs +++ b/test/web/twitter_api/views/activity_view_test.exs @@ -100,7 +100,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do expected = ":firefox: meow" expected_html = - "\"firefox\" meow" + "\"firefox\" meow" assert result["summary"] == expected assert result["summary_html"] == expected_html diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index c99dbddeb..74526673c 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -32,7 +32,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do test "A user with emoji in username" do expected = - "\"karjalanpiirakka\" man" + "\"karjalanpiirakka\" man" user = insert(:user, %{ -- cgit v1.2.3 From d70af32127bda1431bacad58e7c6516a2e652fcd Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Mon, 11 Feb 2019 23:47:32 +0100 Subject: Pleroma.User: remove emojify on parse_bio --- test/user_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/user_test.exs b/test/user_test.exs index 67266cb7a..6d21b56f7 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -1103,7 +1103,7 @@ defmodule Pleroma.UserTest do expected_text = "A.k.a. " <> "@nick@domain.com" + }'>@nick@domain.com" assert expected_text == User.parse_bio(bio, user) end -- cgit v1.2.3 From 2f76a40d028c45e99425b061298a1b05e4b59923 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Tue, 12 Feb 2019 14:59:34 +0100 Subject: formatter.ex: Add get_emoji_map/1 --- .../twitter_api/twitter_api_controller_test.exs | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'test') diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 43ad71a16..90718cfb4 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1611,6 +1611,34 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) end + + # Broken before the change to class="emoji" and non- in the DB + @tag :skip + test "it formats emojos", %{conn: conn} do + user = insert(:user) + + conn = + conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{ + "bio" => "I love our :moominmamma:​" + }) + + assert response = json_response(conn, 200) + + assert %{ + "description" => "I love our :moominmamma:", + "description_html" => + ~s{I love our moominmamma Date: Sat, 23 Feb 2019 00:57:42 +0100 Subject: MastoAPI: profile update with emoji_map --- .../mastodon_api/mastodon_api_controller_test.exs | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'test') diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index c2a12d3c7..610aa486e 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -2351,6 +2351,33 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do end end end + + test "updates profile emojos", %{conn: conn} do + user = insert(:user) + + note = "*sips :blank:*" + name = "I am :firefox:" + + conn = + conn + |> assign(:user, user) + |> patch("/api/v1/accounts/update_credentials", %{ + "note" => note, + "display_name" => name + }) + + assert json_response(conn, 200) + + conn = + conn + |> get("/api/v1/accounts/#{user.id}") + + assert user = json_response(conn, 200) + + assert user["note"] == note + assert user["display_name"] == name + assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = user["emojis"] + end end test "get instance information", %{conn: conn} do -- cgit v1.2.3 From 4c76f49e60287e6618f87a2bae3e0d4e8c444895 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 4 May 2019 15:06:18 +0200 Subject: BBS: small fixes. --- test/bbs/handler_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs index a22c6d64d..1386a2d16 100644 --- a/test/bbs/handler_test.exs +++ b/test/bbs/handler_test.exs @@ -1,10 +1,10 @@ defmodule Pleroma.BBS.HandlerTest do use Pleroma.DataCase + alias Pleroma.Activity alias Pleroma.BBS.Handler alias Pleroma.Web.CommonAPI - alias Pleroma.User alias Pleroma.Repo - alias Pleroma.Activity + alias Pleroma.User import ExUnit.CaptureIO import Pleroma.Factory -- cgit v1.2.3 From c58fd4c038da8305d8840c38f525ceb9f13a644d Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 4 May 2019 15:36:48 +0200 Subject: BBS: Fix tests. --- test/bbs/handler_test.exs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs index 1386a2d16..148df6ddd 100644 --- a/test/bbs/handler_test.exs +++ b/test/bbs/handler_test.exs @@ -3,6 +3,7 @@ defmodule Pleroma.BBS.HandlerTest do alias Pleroma.Activity alias Pleroma.BBS.Handler alias Pleroma.Web.CommonAPI + alias Pleroma.Object alias Pleroma.Repo alias Pleroma.User @@ -49,7 +50,8 @@ defmodule Pleroma.BBS.HandlerTest do ) assert activity.actor == user.ap_id - assert activity.data["object"]["content"] == "this is a test post" + object = Object.normalize(activity) + assert object.data["content"] == "this is a test post" end test "replying" do @@ -74,7 +76,8 @@ defmodule Pleroma.BBS.HandlerTest do ) assert reply.actor == user.ap_id - assert reply.data["object"]["content"] == "this is a reply" - assert reply.data["object"]["inReplyTo"] == activity.data["object"]["id"] + object = Object.normalize(reply) + assert object.data["content"] == "this is a reply" + assert object.data["inReplyTo"] == activity.data["object"] end end -- cgit v1.2.3 From eb0fb73ddbed109ca4dcd758b60a25ff0dafc883 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 4 May 2019 15:47:50 +0200 Subject: BBS: Credo fixes. --- test/bbs/handler_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs index 148df6ddd..7d5d68d11 100644 --- a/test/bbs/handler_test.exs +++ b/test/bbs/handler_test.exs @@ -2,10 +2,10 @@ defmodule Pleroma.BBS.HandlerTest do use Pleroma.DataCase alias Pleroma.Activity alias Pleroma.BBS.Handler - alias Pleroma.Web.CommonAPI alias Pleroma.Object alias Pleroma.Repo alias Pleroma.User + alias Pleroma.Web.CommonAPI import ExUnit.CaptureIO import Pleroma.Factory -- cgit v1.2.3 From ce6ca0fefe7feb1c31447287aec117d40233652a Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Mon, 6 May 2019 16:45:22 +0000 Subject: Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into feature/845-improve-status-deletion --- test/user_test.exs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/user_test.exs b/test/user_test.exs index 6d21b56f7..adc77a264 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -829,10 +829,12 @@ defmodule Pleroma.UserTest do user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"}) - {:ok, _} = User.delete_user_activities(user) - # TODO: Remove favorites, repeats, delete activities. - refute Activity.get_by_id(activity.id) + Ecto.Adapters.SQL.Sandbox.unboxed_run(Repo, fn -> + {:ok, _} = User.delete_user_activities(user) + # TODO: Remove favorites, repeats, delete activities. + refute Activity.get_by_id(activity.id) + end) end test ".delete deactivates a user, all follow relationships and all create activities" do -- cgit v1.2.3 From 1040caf096347b638b9fda5b23fcccde87b32ede Mon Sep 17 00:00:00 2001 From: Maksim Date: Mon, 6 May 2019 17:51:03 +0000 Subject: fix format Modified-by: Maksim Pechnikov --- test/repo_test.exs | 44 +++++++ test/web/oauth/oauth_controller_test.exs | 196 +++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 test/repo_test.exs (limited to 'test') diff --git a/test/repo_test.exs b/test/repo_test.exs new file mode 100644 index 000000000..5382289c7 --- /dev/null +++ b/test/repo_test.exs @@ -0,0 +1,44 @@ +defmodule Pleroma.RepoTest do + use Pleroma.DataCase + import Pleroma.Factory + + describe "find_resource/1" do + test "returns user" do + user = insert(:user) + query = from(t in Pleroma.User, where: t.id == ^user.id) + assert Repo.find_resource(query) == {:ok, user} + end + + test "returns not_found" do + query = from(t in Pleroma.User, where: t.id == ^"9gBuXNpD2NyDmmxxdw") + assert Repo.find_resource(query) == {:error, :not_found} + end + end + + describe "get_assoc/2" do + test "get assoc from preloaded data" do + user = %Pleroma.User{name: "Agent Smith"} + token = %Pleroma.Web.OAuth.Token{insert(:oauth_token) | user: user} + assert Repo.get_assoc(token, :user) == {:ok, user} + end + + test "get one-to-one assoc from repo" do + user = insert(:user, name: "Jimi Hendrix") + token = refresh_record(insert(:oauth_token, user: user)) + + assert Repo.get_assoc(token, :user) == {:ok, user} + end + + test "get one-to-many assoc from repo" do + user = insert(:user) + notification = refresh_record(insert(:notification, user: user)) + + assert Repo.get_assoc(user, :notifications) == {:ok, [notification]} + end + + test "return error if has not assoc " do + token = insert(:oauth_token, user: nil) + assert Repo.get_assoc(token, :user) == {:error, :not_found} + end + end +end diff --git a/test/web/oauth/oauth_controller_test.exs b/test/web/oauth/oauth_controller_test.exs index 6e96537ec..cb6836983 100644 --- a/test/web/oauth/oauth_controller_test.exs +++ b/test/web/oauth/oauth_controller_test.exs @@ -12,6 +12,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do alias Pleroma.Web.OAuth.Authorization alias Pleroma.Web.OAuth.Token + @oauth_config_path [:oauth2, :issue_new_refresh_token] @session_opts [ store: :cookie, key: "_test", @@ -714,4 +715,199 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do refute Map.has_key?(resp, "access_token") end end + + describe "POST /oauth/token - refresh token" do + setup do + oauth_token_config = Pleroma.Config.get(@oauth_config_path) + + on_exit(fn -> + Pleroma.Config.get(@oauth_config_path, oauth_token_config) + end) + end + + test "issues a new access token with keep fresh token" do + Pleroma.Config.put(@oauth_config_path, true) + user = insert(:user) + app = insert(:oauth_app, scopes: ["read", "write"]) + + {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) + {:ok, token} = Token.exchange_token(app, auth) + + response = + build_conn() + |> post("/oauth/token", %{ + "grant_type" => "refresh_token", + "refresh_token" => token.refresh_token, + "client_id" => app.client_id, + "client_secret" => app.client_secret + }) + |> json_response(200) + + ap_id = user.ap_id + + assert match?( + %{ + "scope" => "write", + "token_type" => "Bearer", + "expires_in" => 600, + "access_token" => _, + "refresh_token" => _, + "me" => ^ap_id + }, + response + ) + + refute Repo.get_by(Token, token: token.token) + new_token = Repo.get_by(Token, token: response["access_token"]) + assert new_token.refresh_token == token.refresh_token + assert new_token.scopes == auth.scopes + assert new_token.user_id == user.id + assert new_token.app_id == app.id + end + + test "issues a new access token with new fresh token" do + Pleroma.Config.put(@oauth_config_path, false) + user = insert(:user) + app = insert(:oauth_app, scopes: ["read", "write"]) + + {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) + {:ok, token} = Token.exchange_token(app, auth) + + response = + build_conn() + |> post("/oauth/token", %{ + "grant_type" => "refresh_token", + "refresh_token" => token.refresh_token, + "client_id" => app.client_id, + "client_secret" => app.client_secret + }) + |> json_response(200) + + ap_id = user.ap_id + + assert match?( + %{ + "scope" => "write", + "token_type" => "Bearer", + "expires_in" => 600, + "access_token" => _, + "refresh_token" => _, + "me" => ^ap_id + }, + response + ) + + refute Repo.get_by(Token, token: token.token) + new_token = Repo.get_by(Token, token: response["access_token"]) + refute new_token.refresh_token == token.refresh_token + assert new_token.scopes == auth.scopes + assert new_token.user_id == user.id + assert new_token.app_id == app.id + end + + test "returns 400 if we try use access token" do + user = insert(:user) + app = insert(:oauth_app, scopes: ["read", "write"]) + + {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) + {:ok, token} = Token.exchange_token(app, auth) + + response = + build_conn() + |> post("/oauth/token", %{ + "grant_type" => "refresh_token", + "refresh_token" => token.token, + "client_id" => app.client_id, + "client_secret" => app.client_secret + }) + |> json_response(400) + + assert %{"error" => "Invalid credentials"} == response + end + + test "returns 400 if refresh_token invalid" do + app = insert(:oauth_app, scopes: ["read", "write"]) + + response = + build_conn() + |> post("/oauth/token", %{ + "grant_type" => "refresh_token", + "refresh_token" => "token.refresh_token", + "client_id" => app.client_id, + "client_secret" => app.client_secret + }) + |> json_response(400) + + assert %{"error" => "Invalid credentials"} == response + end + + test "issues a new token if token expired" do + user = insert(:user) + app = insert(:oauth_app, scopes: ["read", "write"]) + + {:ok, auth} = Authorization.create_authorization(app, user, ["write"]) + {:ok, token} = Token.exchange_token(app, auth) + + change = + Ecto.Changeset.change( + token, + %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)} + ) + + {:ok, access_token} = Repo.update(change) + + response = + build_conn() + |> post("/oauth/token", %{ + "grant_type" => "refresh_token", + "refresh_token" => access_token.refresh_token, + "client_id" => app.client_id, + "client_secret" => app.client_secret + }) + |> json_response(200) + + ap_id = user.ap_id + + assert match?( + %{ + "scope" => "write", + "token_type" => "Bearer", + "expires_in" => 600, + "access_token" => _, + "refresh_token" => _, + "me" => ^ap_id + }, + response + ) + + refute Repo.get_by(Token, token: token.token) + token = Repo.get_by(Token, token: response["access_token"]) + assert token + assert token.scopes == auth.scopes + assert token.user_id == user.id + assert token.app_id == app.id + end + end + + describe "POST /oauth/token - bad request" do + test "returns 500" do + response = + build_conn() + |> post("/oauth/token", %{}) + |> json_response(500) + + assert %{"error" => "Bad request"} == response + end + end + + describe "POST /oauth/revoke - bad request" do + test "returns 500" do + response = + build_conn() + |> post("/oauth/revoke", %{}) + |> json_response(500) + + assert %{"error" => "Bad request"} == response + end + end end -- cgit v1.2.3 From f841eb7cdb83afc444dfe260581b6be6e690a717 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Sat, 4 May 2019 12:46:42 +0300 Subject: Preload bookmarks wherever the object is preloaded --- test/activity_test.exs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'test') diff --git a/test/activity_test.exs b/test/activity_test.exs index ad889f544..e2a8baada 100644 --- a/test/activity_test.exs +++ b/test/activity_test.exs @@ -5,6 +5,8 @@ defmodule Pleroma.ActivityTest do use Pleroma.DataCase alias Pleroma.Activity + alias Pleroma.Bookmark + alias Pleroma.Object import Pleroma.Factory test "returns an activity by it's AP id" do @@ -28,4 +30,31 @@ defmodule Pleroma.ActivityTest do assert activity == found_activity end + + test "preloading object preloads bookmarks" do + user1 = insert(:user) + user2 = insert(:user) + activity = insert(:note_activity) + {:ok, bookmark1} = Bookmark.create(user1.id, activity.id) + {:ok, bookmark2} = Bookmark.create(user2.id, activity.id) + bookmarks = Enum.sort([bookmark1, bookmark2]) + + queried_activity = + Ecto.Query.from(a in Activity, where: a.id == ^activity.id) + |> Activity.with_preloaded_object() + |> Repo.one() + + assert Enum.sort(queried_activity.bookmarks) == bookmarks + + queried_activity = Activity.get_by_ap_id_with_object(activity.data["id"]) + assert Enum.sort(queried_activity.bookmarks) == bookmarks + + queried_activity = Activity.get_by_id_with_object(activity.id) + assert Enum.sort(queried_activity.bookmarks) == bookmarks + + queried_activity = + Activity.get_create_by_object_ap_id_with_object(Object.normalize(activity).data["id"]) + + assert Enum.sort(queried_activity.bookmarks) == bookmarks + end end -- cgit v1.2.3 From 3a7c14645ed726bd6b7deb6489ec0578c4d8cd79 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Sat, 4 May 2019 13:42:54 +0300 Subject: - Actually use preloaded bookmarks in views - Preload bookmarks in bookmark timeline - Rework bookmark preload tests --- test/activity_test.exs | 60 ++++++++++++++++++++---------- test/web/mastodon_api/status_view_test.exs | 2 + 2 files changed, 43 insertions(+), 19 deletions(-) (limited to 'test') diff --git a/test/activity_test.exs b/test/activity_test.exs index e2a8baada..dd149543c 100644 --- a/test/activity_test.exs +++ b/test/activity_test.exs @@ -31,30 +31,52 @@ defmodule Pleroma.ActivityTest do assert activity == found_activity end - test "preloading object preloads bookmarks" do - user1 = insert(:user) - user2 = insert(:user) - activity = insert(:note_activity) - {:ok, bookmark1} = Bookmark.create(user1.id, activity.id) - {:ok, bookmark2} = Bookmark.create(user2.id, activity.id) - bookmarks = Enum.sort([bookmark1, bookmark2]) + describe "preloading bookmarks" do + setup do + user1 = insert(:user) + user2 = insert(:user) + activity = insert(:note_activity) + {:ok, bookmark1} = Bookmark.create(user1.id, activity.id) + {:ok, bookmark2} = Bookmark.create(user2.id, activity.id) + [activity: activity, bookmarks: Enum.sort([bookmark1, bookmark2])] + end + + test "using with_preloaded_bookmarks", %{activity: activity, bookmarks: bookmarks} do + queried_activity = + Ecto.Query.from(a in Activity, where: a.id == ^activity.id) + |> Activity.with_preloaded_bookmarks() + |> Repo.one() + + assert Enum.sort(queried_activity.bookmarks) == bookmarks + end - queried_activity = - Ecto.Query.from(a in Activity, where: a.id == ^activity.id) - |> Activity.with_preloaded_object() - |> Repo.one() + test "using with_preloaded_object", %{activity: activity, bookmarks: bookmarks} do + queried_activity = + Ecto.Query.from(a in Activity, where: a.id == ^activity.id) + |> Activity.with_preloaded_object() + |> Repo.one() - assert Enum.sort(queried_activity.bookmarks) == bookmarks + assert Enum.sort(queried_activity.bookmarks) == bookmarks + end - queried_activity = Activity.get_by_ap_id_with_object(activity.data["id"]) - assert Enum.sort(queried_activity.bookmarks) == bookmarks + test "using get_by_ap_id_with_object", %{activity: activity, bookmarks: bookmarks} do + queried_activity = Activity.get_by_ap_id_with_object(activity.data["id"]) + assert Enum.sort(queried_activity.bookmarks) == bookmarks + end - queried_activity = Activity.get_by_id_with_object(activity.id) - assert Enum.sort(queried_activity.bookmarks) == bookmarks + test "using get_by_id_with_object", %{activity: activity, bookmarks: bookmarks} do + queried_activity = Activity.get_by_id_with_object(activity.id) + assert Enum.sort(queried_activity.bookmarks) == bookmarks + end - queried_activity = - Activity.get_create_by_object_ap_id_with_object(Object.normalize(activity).data["id"]) + test "using get_create_by_object_ap_id_with_object", %{ + activity: activity, + bookmarks: bookmarks + } do + queried_activity = + Activity.get_create_by_object_ap_id_with_object(Object.normalize(activity).data["id"]) - assert Enum.sort(queried_activity.bookmarks) == bookmarks + assert Enum.sort(queried_activity.bookmarks) == bookmarks + end end end diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs index 5fddc6c58..d7c800e83 100644 --- a/test/web/mastodon_api/status_view_test.exs +++ b/test/web/mastodon_api/status_view_test.exs @@ -168,6 +168,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do {:ok, _bookmark} = Bookmark.create(user.id, activity.id) + activity = Activity.get_by_id_with_object(activity.id) + status = StatusView.render("status.json", %{activity: activity, for: user}) assert status.bookmarked == true -- cgit v1.2.3 From 4c5125dedc429ac021c861c11eb2e41c856ae4e8 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Tue, 7 May 2019 18:00:50 +0300 Subject: Remove `bookmarks` assoc and add a fake `bookmark` assoc instead --- test/activity_test.exs | 66 +++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 36 deletions(-) (limited to 'test') diff --git a/test/activity_test.exs b/test/activity_test.exs index dd149543c..7e91d534b 100644 --- a/test/activity_test.exs +++ b/test/activity_test.exs @@ -6,7 +6,6 @@ defmodule Pleroma.ActivityTest do use Pleroma.DataCase alias Pleroma.Activity alias Pleroma.Bookmark - alias Pleroma.Object import Pleroma.Factory test "returns an activity by it's AP id" do @@ -31,52 +30,47 @@ defmodule Pleroma.ActivityTest do assert activity == found_activity end - describe "preloading bookmarks" do - setup do - user1 = insert(:user) - user2 = insert(:user) - activity = insert(:note_activity) - {:ok, bookmark1} = Bookmark.create(user1.id, activity.id) - {:ok, bookmark2} = Bookmark.create(user2.id, activity.id) - [activity: activity, bookmarks: Enum.sort([bookmark1, bookmark2])] - end + test "preloading a bookmark" do + user = insert(:user) + user2 = insert(:user) + user3 = insert(:user) + activity = insert(:note_activity) + {:ok, _bookmark} = Bookmark.create(user.id, activity.id) + {:ok, _bookmark2} = Bookmark.create(user2.id, activity.id) + {:ok, bookmark3} = Bookmark.create(user3.id, activity.id) - test "using with_preloaded_bookmarks", %{activity: activity, bookmarks: bookmarks} do - queried_activity = - Ecto.Query.from(a in Activity, where: a.id == ^activity.id) - |> Activity.with_preloaded_bookmarks() - |> Repo.one() + queried_activity = + Ecto.Query.from(Pleroma.Activity) + |> Activity.with_preloaded_bookmark(user3) + |> Repo.one() - assert Enum.sort(queried_activity.bookmarks) == bookmarks - end + assert queried_activity.bookmark == bookmark3 + end + + describe "getting a bookmark" do + test "when association is loaded" do + user = insert(:user) + activity = insert(:note_activity) + {:ok, bookmark} = Bookmark.create(user.id, activity.id) - test "using with_preloaded_object", %{activity: activity, bookmarks: bookmarks} do queried_activity = - Ecto.Query.from(a in Activity, where: a.id == ^activity.id) - |> Activity.with_preloaded_object() + Ecto.Query.from(Pleroma.Activity) + |> Activity.with_preloaded_bookmark(user) |> Repo.one() - assert Enum.sort(queried_activity.bookmarks) == bookmarks - end - - test "using get_by_ap_id_with_object", %{activity: activity, bookmarks: bookmarks} do - queried_activity = Activity.get_by_ap_id_with_object(activity.data["id"]) - assert Enum.sort(queried_activity.bookmarks) == bookmarks + assert Activity.get_bookmark(queried_activity, user) == bookmark end - test "using get_by_id_with_object", %{activity: activity, bookmarks: bookmarks} do - queried_activity = Activity.get_by_id_with_object(activity.id) - assert Enum.sort(queried_activity.bookmarks) == bookmarks - end + test "when association is not loaded" do + user = insert(:user) + activity = insert(:note_activity) + {:ok, bookmark} = Bookmark.create(user.id, activity.id) - test "using get_create_by_object_ap_id_with_object", %{ - activity: activity, - bookmarks: bookmarks - } do queried_activity = - Activity.get_create_by_object_ap_id_with_object(Object.normalize(activity).data["id"]) + Ecto.Query.from(Pleroma.Activity) + |> Repo.one() - assert Enum.sort(queried_activity.bookmarks) == bookmarks + assert Activity.get_bookmark(queried_activity, user) == bookmark end end end -- cgit v1.2.3