Fallback to a variant if the language in general is not supported
[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 "use supported locale with specifiers from `accept-language`" do
37 conn =
38 :get
39 |> conn("/cofe")
40 |> Conn.put_req_header(
41 "accept-language",
42 "zh-Hans;q=0.9, en;q=0.8, *;q=0.5"
43 )
44 |> SetLocalePlug.call([])
45
46 assert "zh_Hans" == Gettext.get_locale()
47 assert %{locale: "zh_Hans"} == conn.assigns
48 end
49
50 test "fallback to some variant of the language if the unqualified language is not supported" do
51 conn =
52 :get
53 |> conn("/cofe")
54 |> Conn.put_req_header(
55 "accept-language",
56 "zh;q=0.9, en;q=0.8, *;q=0.5"
57 )
58 |> SetLocalePlug.call([])
59
60 assert "zh_" <> _ = Gettext.get_locale()
61 assert %{locale: "zh_" <> _} = conn.assigns
62 end
63
64 test "use supported locale from cookie" do
65 conn =
66 :get
67 |> conn("/cofe")
68 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans")
69 |> Conn.put_req_header(
70 "accept-language",
71 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
72 )
73 |> SetLocalePlug.call([])
74
75 assert "zh_Hans" == Gettext.get_locale()
76 assert %{locale: "zh_Hans"} == conn.assigns
77 end
78
79 test "fallback to supported locale from `accept-language` if locale in cookie not supported" do
80 conn =
81 :get
82 |> conn("/cofe")
83 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
84 |> Conn.put_req_header(
85 "accept-language",
86 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
87 )
88 |> SetLocalePlug.call([])
89
90 assert "ru" == Gettext.get_locale()
91 assert %{locale: "ru"} == conn.assigns
92 end
93
94 test "fallback to default if nothing is supported" do
95 conn =
96 :get
97 |> conn("/cofe")
98 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
99 |> Conn.put_req_header(
100 "accept-language",
101 "x-nonexist"
102 )
103 |> SetLocalePlug.call([])
104
105 assert "en" == Gettext.get_locale()
106 assert %{locale: "en"} == conn.assigns
107 end
108
109 test "use default locale if locale from `accept-language` is not supported" do
110 conn =
111 :get
112 |> conn("/cofe")
113 |> Conn.put_req_header("accept-language", "tlh")
114 |> SetLocalePlug.call([])
115
116 assert "en" == Gettext.get_locale()
117 assert %{locale: "en"} == conn.assigns
118 end
119 end