blob: 1cec62a5f3439f77c476d7d043b95116f1578ba7 (
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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe PreferredLanguageSwitcher, type: :controller do
controller(ActionController::Base) do
include PreferredLanguageSwitcher # rubocop:disable RSpec/DescribedClass
before_action :init_preferred_language, only: :new
def new
render html: 'new page'
end
end
context 'when first visit' do
let(:glm_source) { 'about.gitlab.com' }
let(:accept_language_header) { nil }
before do
request.env['HTTP_ACCEPT_LANGUAGE'] = accept_language_header.dup
get :new, params: { glm_source: glm_source }
end
it 'sets preferred_language to default' do
expect(cookies[:preferred_language]).to eq Gitlab::CurrentSettings.default_preferred_language
end
context 'when language param is valid' do
let(:glm_source) { 'about.gitlab.com/fr-fr/' }
it 'sets preferred_language accordingly' do
expect(cookies[:preferred_language]).to eq 'fr'
end
context 'when language param is invalid' do
let(:glm_source) { 'about.gitlab.com/ko-ko/' }
it 'sets preferred_language to default' do
expect(cookies[:preferred_language]).to eq Gitlab::CurrentSettings.default_preferred_language
end
end
end
context 'when browser preferred language is not english' do
context 'with selectable language' do
let(:accept_language_header) { 'zh-CN,zh;q=0.8,zh-TW;q=0.7' }
it 'sets preferred_language accordingly' do
expect(cookies[:preferred_language]).to eq 'zh_CN'
end
end
context 'with unselectable language' do
let(:accept_language_header) { 'nl-NL;q=0.8' }
it 'sets preferred_language to default' do
expect(cookies[:preferred_language]).to eq Gitlab::CurrentSettings.default_preferred_language
end
end
end
end
context 'when preferred language in cookies has been modified' do
let(:user_preferred_language) { nil }
before do
cookies[:preferred_language] = user_preferred_language
get :new
end
context 'with a valid value' do
let(:user_preferred_language) { 'zh_CN' }
it 'keeps preferred language unchanged' do
expect(cookies[:preferred_language]).to eq user_preferred_language
end
end
context 'with an invalid value' do
let(:user_preferred_language) { 'xxx' }
it 'sets preferred_language to default' do
expect(cookies[:preferred_language]).to eq Gitlab::CurrentSettings.default_preferred_language
end
end
end
end
|