Fix rate_limiter_test.exs test "it restricts based on config values" (#233)
[akkoma] / test / pleroma / translators / deepl_test.exs
1 defmodule Pleroma.Akkoma.Translators.DeepLTest do
2 use Pleroma.DataCase, async: true
3
4 alias Pleroma.Akkoma.Translators.DeepL
5
6 describe "translating with deepl" do
7 setup do
8 clear_config([:deepl, :api_key], "deepl_api_key")
9 end
10
11 test "should list supported languages" do
12 clear_config([:deepl, :tier], :free)
13
14 Tesla.Mock.mock(fn
15 %{method: :get, url: "https://api-free.deepl.com/v2/languages?type=target"} = env ->
16 auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
17 assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
18
19 %Tesla.Env{
20 status: 200,
21 body:
22 Jason.encode!([
23 %{
24 "language" => "BG",
25 "name" => "Bulgarian",
26 "supports_formality" => false
27 },
28 %{
29 "language" => "CS",
30 "name" => "Czech",
31 "supports_formality" => false
32 }
33 ])
34 }
35
36 %{method: :get, url: "https://api-free.deepl.com/v2/languages?type=source"} ->
37 %Tesla.Env{
38 status: 200,
39 body:
40 Jason.encode!([
41 %{
42 "language" => "JA",
43 "name" => "Japanese",
44 "supports_formality" => false
45 }
46 ])
47 }
48 end)
49
50 assert {:ok, [%{code: "JA", name: "Japanese"}],
51 [%{code: "BG", name: "Bulgarian"}, %{code: "CS", name: "Czech"}]} =
52 DeepL.languages()
53 end
54
55 test "should work with the free tier" do
56 clear_config([:deepl, :tier], :free)
57
58 Tesla.Mock.mock(fn
59 %{method: :post, url: "https://api-free.deepl.com/v2/translate"} = env ->
60 auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
61 assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
62
63 %Tesla.Env{
64 status: 200,
65 body:
66 Jason.encode!(%{
67 translations: [
68 %{
69 "text" => "I will crush you",
70 "detected_source_language" => "ja"
71 }
72 ]
73 })
74 }
75 end)
76
77 assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
78 end
79
80 test "should work with the pro tier" do
81 clear_config([:deepl, :tier], :pro)
82
83 Tesla.Mock.mock(fn
84 %{method: :post, url: "https://api.deepl.com/v2/translate"} = env ->
85 auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
86 assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
87
88 %Tesla.Env{
89 status: 200,
90 body:
91 Jason.encode!(%{
92 translations: [
93 %{
94 "text" => "I will crush you",
95 "detected_source_language" => "ja"
96 }
97 ]
98 })
99 }
100 end)
101
102 assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
103 end
104
105 test "should assign source language if set" do
106 clear_config([:deepl, :tier], :pro)
107
108 Tesla.Mock.mock(fn
109 %{method: :post, url: "https://api.deepl.com/v2/translate"} = env ->
110 auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
111 assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
112 assert String.contains?(env.body, "source_lang=ja")
113
114 %Tesla.Env{
115 status: 200,
116 body:
117 Jason.encode!(%{
118 translations: [
119 %{
120 "text" => "I will crush you",
121 "detected_source_language" => "ja"
122 }
123 ]
124 })
125 }
126 end)
127
128 assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", "ja", "en")
129 end
130
131 test "should gracefully fail if the API errors" do
132 clear_config([:deepl, :tier], :free)
133
134 Tesla.Mock.mock(fn
135 %{method: :post, url: "https://api-free.deepl.com/v2/translate"} ->
136 %Tesla.Env{
137 status: 403,
138 body: ""
139 }
140 end)
141
142 assert {:error, "DeepL request failed (code 403)"} =
143 DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
144 end
145 end
146 end