logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/
commit: 90f590788cffd154f9d2b40e5e644ad533883195
parent 90f91168f7ed9af6a4141fafa11417a6419a0c83
Author: marcin mikołajczak <git@mkljczk.pl>
Date:   Sun, 30 Oct 2022 21:06:31 +0100

Add tests

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>

Diffstat:

Mlib/pleroma/translation.ex3+--
Mlib/pleroma/web/mastodon_api/views/instance_view.ex7+++++--
Atest/pleroma/translation_test.ex29+++++++++++++++++++++++++++++
Mtest/pleroma/web/mastodon_api/controllers/status_controller_test.exs40++++++++++++++++++++++++++++++++++++++++
Atest/support/translation_mock.ex22++++++++++++++++++++++
5 files changed, 97 insertions(+), 4 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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 @@ -0,0 +1,22 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/> +# 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