b0e7afffdc1fbc54cd0a2727c6e71a6775b9cb0c
[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 "fallback to some variant of the language if the unqualified language is not supported" do
79 conn =
80 :get
81 |> conn("/cofe")
82 |> Conn.put_req_header(
83 "accept-language",
84 "zh;q=0.9, en;q=0.8, *;q=0.5"
85 )
86 |> SetLocalePlug.call([])
87
88 assert "zh_" <> _ = Gettext.get_locale()
89 assert %{locale: "zh_" <> _} = conn.assigns
90 end
91
92 test "use supported locale from cookie" do
93 conn =
94 :get
95 |> conn("/cofe")
96 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans")
97 |> Conn.put_req_header(
98 "accept-language",
99 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
100 )
101 |> SetLocalePlug.call([])
102
103 assert "zh_Hans" == Gettext.get_locale()
104 assert %{locale: "zh_Hans"} = conn.assigns
105 end
106
107 test "fallback to supported locale from `accept-language` if locale in cookie not supported" do
108 conn =
109 :get
110 |> conn("/cofe")
111 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
112 |> Conn.put_req_header(
113 "accept-language",
114 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
115 )
116 |> SetLocalePlug.call([])
117
118 assert "ru" == Gettext.get_locale()
119 assert %{locale: "ru"} = conn.assigns
120 end
121
122 test "fallback to default if nothing is supported" do
123 conn =
124 :get
125 |> conn("/cofe")
126 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
127 |> Conn.put_req_header(
128 "accept-language",
129 "x-nonexist"
130 )
131 |> SetLocalePlug.call([])
132
133 assert "en" == Gettext.get_locale()
134 assert %{locale: "en"} = conn.assigns
135 end
136
137 test "use default locale if locale from `accept-language` is not supported" do
138 conn =
139 :get
140 |> conn("/cofe")
141 |> Conn.put_req_header("accept-language", "tlh")
142 |> SetLocalePlug.call([])
143
144 assert "en" == Gettext.get_locale()
145 assert %{locale: "en"} = conn.assigns
146 end
147 end