summaryrefslogtreecommitdiff
path: root/test/pleroma/web/plugs/set_locale_plug_test.exs
blob: 4f664f84e167159954e8dca655349ae81b37198a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
  use ExUnit.Case, async: true
  use Plug.Test

  alias Pleroma.Web.Plugs.SetLocalePlug
  alias Plug.Conn

  test "default locale is `en`" do
    conn =
      :get
      |> conn("/cofe")
      |> SetLocalePlug.call([])

    assert "en" == Gettext.get_locale()
    assert %{locale: "en"} = conn.assigns
  end

  test "use supported locale from `accept-language`" do
    conn =
      :get
      |> conn("/cofe")
      |> Conn.put_req_header(
        "accept-language",
        "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
      )
      |> SetLocalePlug.call([])

    assert "ru" == Gettext.get_locale()
    assert %{locale: "ru"} = conn.assigns
  end

  test "fallback to the general language if a variant is not supported" do
    conn =
      :get
      |> conn("/cofe")
      |> Conn.put_req_header(
        "accept-language",
        "ru-CA;q=0.9, en;q=0.8, *;q=0.5"
      )
      |> SetLocalePlug.call([])

    assert "ru" == Gettext.get_locale()
    assert %{locale: "ru"} = conn.assigns
  end

  test "use supported locale with specifiers from `accept-language`" do
    conn =
      :get
      |> conn("/cofe")
      |> Conn.put_req_header(
        "accept-language",
        "zh-Hans;q=0.9, en;q=0.8, *;q=0.5"
      )
      |> SetLocalePlug.call([])

    assert "zh_Hans" == Gettext.get_locale()
    assert %{locale: "zh_Hans"} = conn.assigns
  end

  test "it assigns all supported locales" do
    conn =
      :get
      |> conn("/cofe")
      |> Conn.put_req_header(
        "accept-language",
        "ru, fr-CH, fr;q=0.9, en;q=0.8, x-unsupported;q=0.8, *;q=0.5"
      )
      |> SetLocalePlug.call([])

    assert "ru" == Gettext.get_locale()
    assert %{locale: "ru", locales: ["ru", "fr", "en"]} = conn.assigns
  end

  test "it assigns all supported locales in cookie" do
    conn =
      :get
      |> conn("/cofe")
      |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans,uk,zh-Hant")
      |> Conn.put_req_header(
        "accept-language",
        "ru, fr-CH, fr;q=0.9, en;q=0.8, x-unsupported;q=0.8, *;q=0.5"
      )
      |> SetLocalePlug.call([])

    assert "zh_Hans" == Gettext.get_locale()

    assert %{locale: "zh_Hans", locales: ["zh_Hans", "uk", "zh_Hant", "ru", "fr", "en"]} =
             conn.assigns
  end

  test "fallback to some variant of the language if the unqualified language is not supported" do
    conn =
      :get
      |> conn("/cofe")
      |> Conn.put_req_header(
        "accept-language",
        "zh;q=0.9, en;q=0.8, *;q=0.5"
      )
      |> SetLocalePlug.call([])

    assert "zh_" <> _ = Gettext.get_locale()
    assert %{locale: "zh_" <> _} = conn.assigns
  end

  test "use supported locale from cookie" do
    conn =
      :get
      |> conn("/cofe")
      |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans")
      |> Conn.put_req_header(
        "accept-language",
        "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
      )
      |> SetLocalePlug.call([])

    assert "zh_Hans" == Gettext.get_locale()
    assert %{locale: "zh_Hans"} = conn.assigns
  end

  test "fallback to supported locale from `accept-language` if locale in cookie not supported" do
    conn =
      :get
      |> conn("/cofe")
      |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
      |> Conn.put_req_header(
        "accept-language",
        "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
      )
      |> SetLocalePlug.call([])

    assert "ru" == Gettext.get_locale()
    assert %{locale: "ru"} = conn.assigns
  end

  test "fallback to default if nothing is supported" do
    conn =
      :get
      |> conn("/cofe")
      |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
      |> Conn.put_req_header(
        "accept-language",
        "x-nonexist"
      )
      |> SetLocalePlug.call([])

    assert "en" == Gettext.get_locale()
    assert %{locale: "en"} = conn.assigns
  end

  test "use default locale if locale from `accept-language` is not supported" do
    conn =
      :get
      |> conn("/cofe")
      |> Conn.put_req_header("accept-language", "tlh")
      |> SetLocalePlug.call([])

    assert "en" == Gettext.get_locale()
    assert %{locale: "en"} = conn.assigns
  end
end