diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/emails/admin_email_test.exs | 4 | ||||
| -rw-r--r-- | test/fixtures/tesla_mock/poll_modified.json | 1 | ||||
| -rw-r--r-- | test/fixtures/tesla_mock/poll_original.json | 1 | ||||
| -rw-r--r-- | test/fixtures/tesla_mock/rin.json | 1 | ||||
| -rw-r--r-- | test/object_test.exs | 109 | ||||
| -rw-r--r-- | test/support/http_request_mock.ex | 4 | 
6 files changed, 118 insertions, 2 deletions
| diff --git a/test/emails/admin_email_test.exs b/test/emails/admin_email_test.exs index 9e83c73c6..594f03959 100644 --- a/test/emails/admin_email_test.exs +++ b/test/emails/admin_email_test.exs @@ -19,8 +19,8 @@ defmodule Pleroma.Emails.AdminEmailTest do        AdminEmail.report(to_user, reporter, account, [%{name: "Test", id: "12"}], "Test comment")      status_url = Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, "12") -    reporter_url = Helpers.o_status_url(Pleroma.Web.Endpoint, :feed_redirect, reporter.nickname) -    account_url = Helpers.o_status_url(Pleroma.Web.Endpoint, :feed_redirect, account.nickname) +    reporter_url = Helpers.o_status_url(Pleroma.Web.Endpoint, :feed_redirect, reporter.id) +    account_url = Helpers.o_status_url(Pleroma.Web.Endpoint, :feed_redirect, account.id)      assert res.to == [{to_user.name, to_user.email}]      assert res.from == {config[:name], config[:notify_email]} diff --git a/test/fixtures/tesla_mock/poll_modified.json b/test/fixtures/tesla_mock/poll_modified.json new file mode 100644 index 000000000..1d026b592 --- /dev/null +++ b/test/fixtures/tesla_mock/poll_modified.json @@ -0,0 +1 @@ +{"@context":["https://www.w3.org/ns/activitystreams","https://patch.cx/schemas/litepub-0.1.jsonld",{"@language":"und"}],"actor":"https://patch.cx/users/rin","attachment":[],"attributedTo":"https://patch.cx/users/rin","cc":["https://patch.cx/users/rin/followers"],"closed":"2019-09-19T00:32:36.785333","content":"can you vote on this poll?","context":"https://patch.cx/contexts/626ecafd-3377-46c4-b908-3721a4d4373c","conversation":"https://patch.cx/contexts/626ecafd-3377-46c4-b908-3721a4d4373c","id":"https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d","oneOf":[{"name":"yes","replies":{"totalItems":8,"type":"Collection"},"type":"Note"},{"name":"no","replies":{"totalItems":3,"type":"Collection"},"type":"Note"}],"published":"2019-09-18T14:32:36.802152Z","sensitive":false,"summary":"","tag":[],"to":["https://www.w3.org/ns/activitystreams#Public"],"type":"Question"}
\ No newline at end of file diff --git a/test/fixtures/tesla_mock/poll_original.json b/test/fixtures/tesla_mock/poll_original.json new file mode 100644 index 000000000..267876b3c --- /dev/null +++ b/test/fixtures/tesla_mock/poll_original.json @@ -0,0 +1 @@ +{"@context":["https://www.w3.org/ns/activitystreams","https://patch.cx/schemas/litepub-0.1.jsonld",{"@language":"und"}],"actor":"https://patch.cx/users/rin","attachment":[],"attributedTo":"https://patch.cx/users/rin","cc":["https://patch.cx/users/rin/followers"],"closed":"2019-09-19T00:32:36.785333","content":"can you vote on this poll?","context":"https://patch.cx/contexts/626ecafd-3377-46c4-b908-3721a4d4373c","conversation":"https://patch.cx/contexts/626ecafd-3377-46c4-b908-3721a4d4373c","id":"https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d","oneOf":[{"name":"yes","replies":{"totalItems":4,"type":"Collection"},"type":"Note"},{"name":"no","replies":{"totalItems":0,"type":"Collection"},"type":"Note"}],"published":"2019-09-18T14:32:36.802152Z","sensitive":false,"summary":"","tag":[],"to":["https://www.w3.org/ns/activitystreams#Public"],"type":"Question"}
\ No newline at end of file diff --git a/test/fixtures/tesla_mock/rin.json b/test/fixtures/tesla_mock/rin.json new file mode 100644 index 000000000..2cf623764 --- /dev/null +++ b/test/fixtures/tesla_mock/rin.json @@ -0,0 +1 @@ +{"@context":["https://www.w3.org/ns/activitystreams","https://patch.cx/schemas/litepub-0.1.jsonld",{"@language":"und"}],"attachment":[],"endpoints":{"oauthAuthorizationEndpoint":"https://patch.cx/oauth/authorize","oauthRegistrationEndpoint":"https://patch.cx/api/v1/apps","oauthTokenEndpoint":"https://patch.cx/oauth/token","sharedInbox":"https://patch.cx/inbox"},"followers":"https://patch.cx/users/rin/followers","following":"https://patch.cx/users/rin/following","icon":{"type":"Image","url":"https://patch.cx/media/4e914f5b84e4a259a3f6c2d2edc9ab642f2ab05f3e3d9c52c81fc2d984b3d51e.jpg"},"id":"https://patch.cx/users/rin","image":{"type":"Image","url":"https://patch.cx/media/f739efddefeee49c6e67e947c4811fdc911785c16ae43da4c3684051fbf8da6a.jpg?name=f739efddefeee49c6e67e947c4811fdc911785c16ae43da4c3684051fbf8da6a.jpg"},"inbox":"https://patch.cx/users/rin/inbox","manuallyApprovesFollowers":false,"name":"rinpatch","outbox":"https://patch.cx/users/rin/outbox","preferredUsername":"rin","publicKey":{"id":"https://patch.cx/users/rin#main-key","owner":"https://patch.cx/users/rin","publicKeyPem":"-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5DLtwGXNZElJyxFGfcVc\nXANhaMadj/iYYQwZjOJTV9QsbtiNBeIK54PJrYuU0/0YIdrvS1iqheX5IwXRhcwa\nhm3ZyLz7XeN9st7FBni4BmZMBtMpxAuYuu5p/jbWy13qAiYOhPreCx0wrWgm/lBD\n9mkgaxIxPooBE0S4ZWEJIDIV1Vft3AWcRUyWW1vIBK0uZzs6GYshbQZB952S0yo4\nFzI1hABGHncH8UvuFauh4EZ8tY7/X5I0pGRnDOcRN1dAht5w5yTA+6r5kebiFQjP\nIzN/eCO/a9Flrj9YGW7HDNtjSOH0A31PLRGlJtJO3yK57dnf5ppyCZGfL4emShQo\ncQIDAQAB\n-----END PUBLIC KEY-----\n\n"},"summary":"your friendly neighborhood pleroma developer<br>I like cute things and distributed systems, and really hate delete and redrafts","tag":[],"type":"Person","url":"https://patch.cx/users/rin"}
\ No newline at end of file diff --git a/test/object_test.exs b/test/object_test.exs index ba96aeea4..3d64fdb49 100644 --- a/test/object_test.exs +++ b/test/object_test.exs @@ -4,10 +4,13 @@  defmodule Pleroma.ObjectTest do    use Pleroma.DataCase +  import ExUnit.CaptureLog    import Pleroma.Factory    import Tesla.Mock +  alias Pleroma.Activity    alias Pleroma.Object    alias Pleroma.Repo +  alias Pleroma.Web.CommonAPI    setup do      mock(fn env -> apply(HttpRequestMock, :request, [env]) end) @@ -89,4 +92,110 @@ defmodule Pleroma.ObjectTest do               )      end    end + +  describe "get_by_id_and_maybe_refetch" do +    setup do +      mock(fn +        %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} -> +          %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/poll_original.json")} + +        env -> +          apply(HttpRequestMock, :request, [env]) +      end) + +      mock_modified = fn resp -> +        mock(fn +          %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} -> +            resp + +          env -> +            apply(HttpRequestMock, :request, [env]) +        end) +      end + +      on_exit(fn -> mock(fn env -> apply(HttpRequestMock, :request, [env]) end) end) + +      [mock_modified: mock_modified] +    end + +    test "refetches if the time since the last refetch is greater than the interval", %{ +      mock_modified: mock_modified +    } do +      %Object{} = +        object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d") + +      assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +      assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0 + +      mock_modified.(%Tesla.Env{ +        status: 200, +        body: File.read!("test/fixtures/tesla_mock/poll_modified.json") +      }) + +      updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1) +      assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 8 +      assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 3 +    end + +    test "returns the old object if refetch fails", %{mock_modified: mock_modified} do +      %Object{} = +        object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d") + +      assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +      assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0 + +      assert capture_log(fn -> +               mock_modified.(%Tesla.Env{status: 404, body: ""}) + +               updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1) +               assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +               assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 0 +             end) =~ +               "[error] Couldn't refresh https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d" +    end + +    test "does not refetch if the time since the last refetch is greater than the interval", %{ +      mock_modified: mock_modified +    } do +      %Object{} = +        object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d") + +      assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +      assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0 + +      mock_modified.(%Tesla.Env{ +        status: 200, +        body: File.read!("test/fixtures/tesla_mock/poll_modified.json") +      }) + +      updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: 100) +      assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +      assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 0 +    end + +    test "preserves internal fields on refetch", %{mock_modified: mock_modified} do +      %Object{} = +        object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d") + +      assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4 +      assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0 + +      user = insert(:user) +      activity = Activity.get_create_by_object_ap_id(object.data["id"]) +      {:ok, _activity, object} = CommonAPI.favorite(activity.id, user) + +      assert object.data["like_count"] == 1 + +      mock_modified.(%Tesla.Env{ +        status: 200, +        body: File.read!("test/fixtures/tesla_mock/poll_modified.json") +      }) + +      updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1) +      assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 8 +      assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 3 + +      assert updated_object.data["like_count"] == 1 +    end +  end  end diff --git a/test/support/http_request_mock.ex b/test/support/http_request_mock.ex index 231e7c498..833162a61 100644 --- a/test/support/http_request_mock.ex +++ b/test/support/http_request_mock.ex @@ -1004,6 +1004,10 @@ defmodule HttpRequestMock do      {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/sjw.json")}}    end +  def get("https://patch.cx/users/rin", _, _, _) do +    {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/rin.json")}} +  end +    def get(url, query, body, headers) do      {:error,       "Mock response not implemented for GET #{inspect(url)}, #{query}, #{inspect(body)}, #{ | 
