summaryrefslogtreecommitdiffstats
path: root/test/functional/issues_custom_fields_visibility_test.rb
blob: 5eafc623289fe46e12318e2f15cd7b8bfb53f35c (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Redmine - project management software
# Copyright (C) 2006-2016  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require File.expand_path('../../test_helper', __FILE__)

class IssuesCustomFieldsVisibilityTest < Redmine::ControllerTest
  tests IssuesController
  fixtures :projects,
           :users, :email_addresses, :user_preferences,
           :roles,
           :members,
           :member_roles,
           :issue_statuses,
           :trackers,
           :projects_trackers,
           :enabled_modules,
           :enumerations,
           :workflows

  def setup
    CustomField.delete_all
    Issue.delete_all
    field_attributes = {:field_format => 'string', :is_for_all => true, :is_filter => true, :trackers => Tracker.all}
    @fields = []
    @fields << (@field1 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 1', :visible => true)))
    @fields << (@field2 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 2', :visible => false, :role_ids => [1, 2])))
    @fields << (@field3 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 3', :visible => false, :role_ids => [1, 3])))
    @issue = Issue.generate!(
      :author_id => 1,
      :project_id => 1,
      :tracker_id => 1,
      :custom_field_values => {@field1.id => 'Value0', @field2.id => 'Value1', @field3.id => 'Value2'}
    )

    @user_with_role_on_other_project = User.generate!
    User.add_to_project(@user_with_role_on_other_project, Project.find(2), Role.find(3))

    @users_to_test = {
      User.find(1) => [@field1, @field2, @field3],
      User.find(3) => [@field1, @field2],
      @user_with_role_on_other_project => [@field1], # should see field1 only on Project 1
      User.generate! => [@field1],
      User.anonymous => [@field1]
    }

    Member.where(:project_id => 1).each do |member|
      member.destroy unless @users_to_test.keys.include?(member.principal)
    end
  end

  def test_show_should_show_visible_custom_fields_only
    @users_to_test.each do |user, fields|
      @request.session[:user_id] = user.id
      get :show, :id => @issue.id
      @fields.each_with_index do |field, i|
        if fields.include?(field)
          assert_select '.value', {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name}"
        else
          assert_select '.value', {:text => "Value#{i}", :count => 0}, "User #{user.id} was able to view #{field.name}"
        end
      end
    end
  end

  def test_show_should_show_visible_custom_fields_only_in_api
    @users_to_test.each do |user, fields|
      with_settings :rest_api_enabled => '1' do
        get :show, :id => @issue.id, :format => 'xml', :include => 'custom_fields', :key => user.api_key
      end
      @fields.each_with_index do |field, i|
        if fields.include?(field)
          assert_select "custom_field[id=?] value", field.id.to_s, {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name} in API"
        else
          assert_select "custom_field[id=?] value", field.id.to_s, {:text => "Value#{i}", :count => 0}, "User #{user.id} was not able to view #{field.name} in API"
        end
      end
    end
  end

  def test_show_should_show_visible_custom_fields_only_in_history
    @issue.init_journal(User.find(1))
    @issue.custom_field_values = {@field1.id => 'NewValue0', @field2.id => 'NewValue1', @field3.id => 'NewValue2'}
    @issue.save!

    @users_to_test.each do |user, fields|
      @request.session[:user_id] = user.id
      get :show, :id => @issue.id
      @fields.each_with_index do |field, i|
        if fields.include?(field)
          assert_select 'ul.details i', {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name} change"
        else
          assert_select 'ul.details i', {:text => "Value#{i}", :count => 0}, "User #{user.id} was able to view #{field.name} change"
        end
      end
    end
  end

  def test_show_should_show_visible_custom_fields_only_in_history_api
    @issue.init_journal(User.find(1))
    @issue.custom_field_values = {@field1.id => 'NewValue0', @field2.id => 'NewValue1', @field3.id => 'NewValue2'}
    @issue.save!

    @users_to_test.each do |user, fields|
      with_settings :rest_api_enabled => '1' do
        get :show, :id => @issue.id, :format => 'xml', :include => 'journals', :key => user.api_key
      end
      @fields.each_with_index do |field, i|
        if fields.include?(field)
          assert_select 'details old_value', {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name} change in API"
        else
          assert_select 'details old_value', {:text => "Value#{i}", :count => 0}, "User #{user.id} was able to view #{field.name} change in API"
        end
      end
    end
  end

  def test_edit_should_show_visible_custom_fields_only
    Role.anonymous.add_permission! :edit_issues

    @users_to_test.each do |user, fields|
      @request.session[:user_id] = user.id
      get :edit, :id => @issue.id
      @fields.each_with_index do |field, i|
        if fields.include?(field)
          assert_select 'input[value=?]', "Value#{i}", 1, "User #{user.id} was not able to edit #{field.name}"
        else
          assert_select 'input[value=?]', "Value#{i}", 0, "User #{user.id} was able to edit #{field.name}"
        end
      end
    end
  end

  def test_update_should_update_visible_custom_fields_only
    Role.anonymous.add_permission! :edit_issues

    @users_to_test.each do |user, fields|
      @request.session[:user_id] = user.id
      put :update, :id => @issue.id,
        :issue => {:custom_field_values => {
          @field1.id.to_s => "User#{user.id}Value0",
          @field2.id.to_s => "User#{user.id}Value1",
          @field3.id.to_s => "User#{user.id}Value2",
        }}
      @issue.reload
      @fields.each_with_index do |field, i|
        if fields.include?(field)
          assert_equal "User#{user.id}Value#{i}", @issue.custom_field_value(field), "User #{user.id} was not able to update #{field.name}"
        else
          assert_not_equal "User#{user.id}Value#{i}", @issue.custom_field_value(field), "User #{user.id} was able to update #{field.name}"
        end
      end
    end
  end

  def test_index_should_show_visible_custom_fields_only
    @users_to_test.each do |user, fields|
      @request.session[:user_id] = user.id
      get :index, :c => (["subject"] + @fields.map{|f| "cf_#{f.id}"})
      @fields.each_with_index do |field, i|
        if fields.include?(field)
          assert_select 'td', {:text => "Value#{i}", :count => 1}, "User #{user.id} was not able to view #{field.name}"
        else
          assert_select 'td', {:text => "Value#{i}", :count => 0}, "User #{user.id} was able to view #{field.name}"
        end
      end
    end
  end

  def test_index_as_csv_should_show_visible_custom_fields_only
    @users_to_test.each do |user, fields|
      @request.session[:user_id] = user.id
      get :index, :c => (["subject"] + @fields.map{|f| "cf_#{f.id}"}), :format => 'csv'
      @fields.each_with_index do |field, i|
        if fields.include?(field)
          assert_include "Value#{i}", response.body, "User #{user.id} was not able to view #{field.name} in CSV"
        else
          assert_not_include "Value#{i}", response.body, "User #{user.id} was able to view #{field.name} in CSV"
        end
      end
    end
  end

  def test_index_with_partial_custom_field_visibility
    Issue.delete_all
    p1 = Project.generate!
    p2 = Project.generate!
    user = User.generate!
    User.add_to_project(user, p1, Role.where(:id => [1, 3]).to_a)
    User.add_to_project(user, p2, Role.where(:id => 3).to_a)
    Issue.generate!(:project => p1, :tracker_id => 1, :custom_field_values => {@field2.id => 'ValueA'})
    Issue.generate!(:project => p2, :tracker_id => 1, :custom_field_values => {@field2.id => 'ValueB'})
    Issue.generate!(:project => p1, :tracker_id => 1, :custom_field_values => {@field2.id => 'ValueC'})

    @request.session[:user_id] = user.id
    get :index, :c => ["subject", "cf_#{@field2.id}"]
    assert_select 'td', :text => 'ValueA'
    assert_select 'td', :text => 'ValueB', :count => 0
    assert_select 'td', :text => 'ValueC'

    get :index, :sort => "cf_#{@field2.id}"
    # ValueB is not visible to user and ignored while sorting
    assert_equal %w(ValueB ValueA ValueC), assigns(:issues).map{|i| i.custom_field_value(@field2)}

    get :index, :set_filter => '1', "cf_#{@field2.id}" => '*'
    assert_equal %w(ValueA ValueC), assigns(:issues).map{|i| i.custom_field_value(@field2)}

    CustomField.update_all(:field_format => 'list')
    get :index, :group => "cf_#{@field2.id}"
    assert_equal %w(ValueA ValueC), assigns(:issues).map{|i| i.custom_field_value(@field2)}
  end

  def test_create_should_send_notifications_according_custom_fields_visibility
    # anonymous user is never notified
    users_to_test = @users_to_test.reject {|k,v| k.anonymous?}

    ActionMailer::Base.deliveries.clear
    @request.session[:user_id] = 1
    with_settings :bcc_recipients => '1' do
      assert_difference 'Issue.count' do
        post :create,
          :project_id => 1,
          :issue => {
            :tracker_id => 1,
            :status_id => 1,
            :subject => 'New issue',
            :priority_id => 5,
            :custom_field_values => {@field1.id.to_s => 'Value0', @field2.id.to_s => 'Value1', @field3.id.to_s => 'Value2'},
            :watcher_user_ids => users_to_test.keys.map(&:id)
          }
        assert_response 302
      end
    end
    assert_equal users_to_test.values.uniq.size, ActionMailer::Base.deliveries.size
    # tests that each user receives 1 email with the custom fields he is allowed to see only
    users_to_test.each do |user, fields|
      mails = ActionMailer::Base.deliveries.select {|m| m.bcc.include? user.mail}
      assert_equal 1, mails.size
      mail = mails.first
      @fields.each_with_index do |field, i|
        if fields.include?(field)
          assert_mail_body_match "Value#{i}", mail, "User #{user.id} was not able to view #{field.name} in notification"
        else
          assert_mail_body_no_match "Value#{i}", mail, "User #{user.id} was able to view #{field.name} in notification"
        end
      end
    end
  end

  def test_update_should_send_notifications_according_custom_fields_visibility
    # anonymous user is never notified
    users_to_test = @users_to_test.reject {|k,v| k.anonymous?}

    users_to_test.keys.each do |user|
      Watcher.create!(:user => user, :watchable => @issue)
    end
    ActionMailer::Base.deliveries.clear
    @request.session[:user_id] = 1
    with_settings :bcc_recipients => '1' do
      put :update,
        :id => @issue.id,
        :issue => {
          :custom_field_values => {@field1.id.to_s => 'NewValue0', @field2.id.to_s => 'NewValue1', @field3.id.to_s => 'NewValue2'}
        }
      assert_response 302
    end
    assert_equal users_to_test.values.uniq.size, ActionMailer::Base.deliveries.size
    # tests that each user receives 1 email with the custom fields he is allowed to see only
    users_to_test.each do |user, fields|
      mails = ActionMailer::Base.deliveries.select {|m| m.bcc.include? user.mail}
      assert_equal 1, mails.size
      mail = mails.first
      @fields.each_with_index do |field, i|
        if fields.include?(field)
          assert_mail_body_match "Value#{i}", mail, "User #{user.id} was not able to view #{field.name} in notification"
        else
          assert_mail_body_no_match "Value#{i}", mail, "User #{user.id} was able to view #{field.name} in notification"
        end
      end
    end
  end

  def test_updating_hidden_custom_fields_only_should_not_notifiy_user
    # anonymous user is never notified
    users_to_test = @users_to_test.reject {|k,v| k.anonymous?}

    users_to_test.keys.each do |user|
      Watcher.create!(:user => user, :watchable => @issue)
    end
    ActionMailer::Base.deliveries.clear
    @request.session[:user_id] = 1
    with_settings :bcc_recipients => '1' do
      put :update,
        :id => @issue.id,
        :issue => {
          :custom_field_values => {@field2.id.to_s => 'NewValue1', @field3.id.to_s => 'NewValue2'}
        }
      assert_response 302
    end
    users_to_test.each do |user, fields|
      mails = ActionMailer::Base.deliveries.select {|m| m.bcc.include? user.mail}
      if (fields & [@field2, @field3]).any?
        assert_equal 1, mails.size, "User #{user.id} was not notified"
      else
        assert_equal 0, mails.size, "User #{user.id} was notified"
      end
    end
  end
end