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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Profiles::PreferencesController do
let(:user) { create(:user) }
before do
sign_in(user)
allow(subject).to receive(:current_user).and_return(user)
end
describe 'GET show' do
it 'renders' do
get :show
expect(response).to render_template :show
end
it 'assigns user' do
get :show
expect(assigns[:user]).to eq user
end
end
describe 'PATCH update' do
def go(params: {}, format: :json)
params.reverse_merge!(
color_scheme_id: '1',
dashboard: 'stars',
theme_id: '1'
)
patch :update, params: { user: params }, format: format
end
context 'on successful update' do
it 'responds with success' do
go
expect(response).to have_gitlab_http_status(:ok)
expect(response.parsed_body['message']).to eq _('Preferences saved.')
expect(response.parsed_body['type']).to eq('notice')
end
it "changes the user's preferences" do
prefs = {
color_scheme_id: '1',
diffs_deletion_color: '#123456',
diffs_addition_color: '#abcdef',
dashboard: 'stars',
theme_id: '2',
first_day_of_week: '1',
preferred_language: 'jp',
tab_width: '5',
project_shortcut_buttons: 'true',
render_whitespace_in_code: 'true'
}.with_indifferent_access
expect(user).to receive(:assign_attributes).with(ActionController::Parameters.new(prefs).permit!)
expect(user).to receive(:save)
go params: prefs
end
end
context 'on failed update' do
it 'responds with error' do
expect(user).to receive(:save).and_return(false)
go
expect(response).to have_gitlab_http_status(:bad_request)
expect(response.parsed_body['message']).to eq _('Failed to save preferences.')
expect(response.parsed_body['type']).to eq('alert')
end
end
context 'on invalid dashboard setting' do
it 'responds with error' do
prefs = { dashboard: 'invalid' }
go params: prefs
expect(response).to have_gitlab_http_status(:bad_request)
expect(response.parsed_body['message']).to match(/\AFailed to save preferences \(.+\)\.\z/)
expect(response.parsed_body['type']).to eq('alert')
end
end
context 'on invalid diffs colors setting' do
it 'responds with error for diffs_deletion_color' do
prefs = { diffs_deletion_color: '#1234567' }
go params: prefs
expect(response).to have_gitlab_http_status(:bad_request)
expect(response.parsed_body['message']).to eq _('Failed to save preferences.')
expect(response.parsed_body['type']).to eq('alert')
end
it 'responds with error for diffs_addition_color' do
prefs = { diffs_addition_color: '#1234567' }
go params: prefs
expect(response).to have_gitlab_http_status(:bad_request)
expect(response.parsed_body['message']).to eq _('Failed to save preferences.')
expect(response.parsed_body['type']).to eq('alert')
end
end
context 'on disable_follow_users feature flag' do
context 'with feature flag disabled' do
before do
stub_feature_flags(disable_follow_users: false)
end
it 'does not update enabled_following preference of user' do
prefs = { enabled_following: false }
go params: prefs
user.reload
expect(user.enabled_following).to eq(true)
end
end
context 'with feature flag enabled' do
it 'does not update enabled_following preference of user' do
prefs = { enabled_following: false }
go params: prefs
user.reload
expect(user.enabled_following).to eq(false)
end
end
end
end
end
|