strip \r and \r from content-disposition filenames
[akkoma] / test / pleroma / web / plugs / set_locale_plug_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
6 use ExUnit.Case, async: true
7 use Plug.Test
8
9 alias Pleroma.Web.Plugs.SetLocalePlug
10 alias Plug.Conn
11
12 test "default locale is `en`" do
13 conn =
14 :get
15 |> conn("/cofe")
16 |> SetLocalePlug.call([])
17
18 assert "en" == Gettext.get_locale()
19 assert %{locale: "en"} = conn.assigns
20 end
21
22 test "use supported locale from `accept-language`" do
23 conn =
24 :get
25 |> conn("/cofe")
26 |> Conn.put_req_header(
27 "accept-language",
28 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
29 )
30 |> SetLocalePlug.call([])
31
32 assert "ru" == Gettext.get_locale()
33 assert %{locale: "ru"} = conn.assigns
34 end
35
36 test "fallback to the general language if a variant is not supported" do
37 conn =
38 :get
39 |> conn("/cofe")
40 |> Conn.put_req_header(
41 "accept-language",
42 "ru-CA;q=0.9, en;q=0.8, *;q=0.5"
43 )
44 |> SetLocalePlug.call([])
45
46 assert "ru" == Gettext.get_locale()
47 assert %{locale: "ru"} = conn.assigns
48 end
49
50 test "use supported locale with specifiers from `accept-language`" do
51 conn =
52 :get
53 |> conn("/cofe")
54 |> Conn.put_req_header(
55 "accept-language",
56 "zh-Hans;q=0.9, en;q=0.8, *;q=0.5"
57 )
58 |> SetLocalePlug.call([])
59
60 assert "zh_Hans" == Gettext.get_locale()
61 assert %{locale: "zh_Hans"} = conn.assigns
62 end
63
64 test "it assigns all supported locales" do
65 conn =
66 :get
67 |> conn("/cofe")
68 |> Conn.put_req_header(
69 "accept-language",
70 "ru, fr-CH, fr;q=0.9, en;q=0.8, x-unsupported;q=0.8, *;q=0.5"
71 )
72 |> SetLocalePlug.call([])
73
74 assert "ru" == Gettext.get_locale()
75 assert %{locale: "ru", locales: ["ru", "fr", "en"]} = conn.assigns
76 end
77
78 test "it assigns all supported locales in cookie" do
79 conn =
80 :get
81 |> conn("/cofe")
82 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans,uk,zh-Hant")
83 |> Conn.put_req_header(
84 "accept-language",
85 "ru, fr-CH, fr;q=0.9, en;q=0.8, x-unsupported;q=0.8, *;q=0.5"
86 )
87 |> SetLocalePlug.call([])
88
89 assert "zh_Hans" == Gettext.get_locale()
90
91 assert %{locale: "zh_Hans", locales: ["zh_Hans", "uk", "zh_Hant", "ru", "fr", "en"]} =
92 conn.assigns
93 end
94
95 test "fallback to some variant of the language if the unqualified language is not supported" do
96 conn =
97 :get
98 |> conn("/cofe")
99 |> Conn.put_req_header(
100 "accept-language",
101 "zh;q=0.9, en;q=0.8, *;q=0.5"
102 )
103 |> SetLocalePlug.call([])
104
105 assert "zh_" <> _ = Gettext.get_locale()
106 assert %{locale: "zh_" <> _} = conn.assigns
107 end
108
109 test "use supported locale from cookie" do
110 conn =
111 :get
112 |> conn("/cofe")
113 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans")
114 |> Conn.put_req_header(
115 "accept-language",
116 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
117 )
118 |> SetLocalePlug.call([])
119
120 assert "zh_Hans" == Gettext.get_locale()
121 assert %{locale: "zh_Hans"} = conn.assigns
122 end
123
124 test "fallback to supported locale from `accept-language` if locale in cookie not supported" do
125 conn =
126 :get
127 |> conn("/cofe")
128 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
129 |> Conn.put_req_header(
130 "accept-language",
131 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
132 )
133 |> SetLocalePlug.call([])
134
135 assert "ru" == Gettext.get_locale()
136 assert %{locale: "ru"} = conn.assigns
137 end
138
139 test "fallback to default if nothing is supported" do
140 conn =
141 :get
142 |> conn("/cofe")
143 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
144 |> Conn.put_req_header(
145 "accept-language",
146 "x-nonexist"
147 )
148 |> SetLocalePlug.call([])
149
150 assert "en" == Gettext.get_locale()
151 assert %{locale: "en"} = conn.assigns
152 end
153
154 test "use default locale if locale from `accept-language` is not supported" do
155 conn =
156 :get
157 |> conn("/cofe")
158 |> Conn.put_req_header("accept-language", "tlh")
159 |> SetLocalePlug.call([])
160
161 assert "en" == Gettext.get_locale()
162 assert %{locale: "en"} = conn.assigns
163 end
164 end