From 90f590788cffd154f9d2b40e5e644ad533883195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sun, 30 Oct 2022 21:06:31 +0100 Subject: [PATCH] Add tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/translation.ex | 3 +- .../web/mastodon_api/views/instance_view.ex | 7 +++- test/pleroma/translation_test.ex | 29 ++++++++++++++ .../controllers/status_controller_test.exs | 40 +++++++++++++++++++ test/support/translation_mock.ex | 22 ++++++++++ 5 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 test/pleroma/translation_test.ex create mode 100644 test/support/translation_mock.ex diff --git a/lib/pleroma/translation.ex b/lib/pleroma/translation.ex index 112f12cab..7efec62a6 100644 --- a/lib/pleroma/translation.ex +++ b/lib/pleroma/translation.ex @@ -3,7 +3,6 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Translation do - @cache_ttl 86_400_000 @cachex Pleroma.Config.get([:cachex, :provider], Cachex) def configured? do @@ -45,7 +44,7 @@ defmodule Pleroma.Translation do end defp store_result({:ok, result}, cache_key) do - @cachex.put(:translations_cache, cache_key, result, ttl: @cache_ttl) + @cachex.put(:translations_cache, cache_key, result) end defp store_result(_, _), do: nil diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex index f48e80fd6..63edd4b30 100644 --- a/lib/pleroma/web/mastodon_api/views/instance_view.ex +++ b/lib/pleroma/web/mastodon_api/views/instance_view.ex @@ -129,7 +129,10 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do "profile_directory" end, "pleroma:get:main/ostatus", - "pleroma:group_actors" + "pleroma:group_actors", + if Pleroma.Translation.configured?() do + "translation" + end ] |> Enum.filter(& &1) end @@ -203,7 +206,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do vapid: %{ public_key: Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key) }, - translation: %{enabled: Pleroma.Translation.configured?} + translation: %{enabled: Pleroma.Translation.configured?()} }) end diff --git a/test/pleroma/translation_test.ex b/test/pleroma/translation_test.ex new file mode 100644 index 000000000..2ae7856ee --- /dev/null +++ b/test/pleroma/translation_test.ex @@ -0,0 +1,29 @@ +defmodule Pleroma.TranslationTest do + use Pleroma.Web.ConnCase + + alias Pleroma.Translation + # use Oban.Testing, repo: Pleroma.Repo + + setup do: clear_config([Pleroma.Translation, :service], TranslationMock) + + test "it translates text" do + assert {:ok, + %{ + content: "txet emos", + detected_source_language: _, + provider: _ + }} = Translation.translate("some text", "en", "uk") + end + + test "it stores translation result in cache" do + Translation.translate("some text", "en", "uk") + + assert {:ok, result} = + Cachex.get( + :translations_cache, + "en/uk/#{:crypto.hash(:sha256, "some text") |> Base.encode64()}" + ) + + assert result.content == "txet emos" + end +end diff --git a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs index f95f15ec3..fd2f3e11c 100644 --- a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs @@ -2550,4 +2550,44 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do |> json_response_and_validate_schema(:not_found) end end + + describe "translating statuses" do + setup do: clear_config([Pleroma.Translation, :service], TranslationMock) + + test "it translates a status to user language" do + user = insert(:user, language: "fr") + %{conn: conn, user: user} = oauth_access(["read:statuses"], user: user) + another_user = insert(:user) + + {:ok, activity} = + CommonAPI.post(another_user, %{ + status: "Cześć!", + visibility: "public", + language: "pl" + }) + + response = + conn + |> post("/api/v1/statuses/#{activity.id}/translate") + |> json_response_and_validate_schema(200) + + assert response == %{"content" => "!ćśezC", "detected_source_language" => "pl", "provider" => "TranslationMock"} + end + + test "it returns an error if no target language provided" do + %{conn: conn, user: user} = oauth_access(["read:statuses"]) + another_user = insert(:user) + + {:ok, activity} = + CommonAPI.post(another_user, %{ + status: "Cześć!", + language: "pl" + }) + + response = + conn + |> post("/api/v1/statuses/#{activity.id}/translate") + |> json_response_and_validate_schema(400) + end + end end diff --git a/test/support/translation_mock.ex b/test/support/translation_mock.ex new file mode 100644 index 000000000..8da2116e8 --- /dev/null +++ b/test/support/translation_mock.ex @@ -0,0 +1,22 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule TranslationMock do + alias Pleroma.Translation.Service + + @behaviour Service + + @impl Service + def configured?, do: true + + @impl Service + def translate(content, source_language, _target_language) do + {:ok, + %{ + content: content |> String.reverse(), + detected_source_language: source_language, + provider: "TranslationMock" + }} + end +end