You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

journals_controller_test.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class JournalsControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :users, :members, :member_roles, :roles, :issues, :journals, :journal_details, :enabled_modules,
  21. :trackers, :issue_statuses, :enumerations, :custom_fields, :custom_values, :custom_fields_projects, :projects_trackers
  22. def setup
  23. User.current = nil
  24. end
  25. def test_index
  26. get :index, :params => {
  27. :project_id => 1
  28. }
  29. assert_response :success
  30. assert_equal 'application/atom+xml', @response.content_type
  31. end
  32. def test_index_with_invalid_query_id
  33. get :index, :params => {
  34. :project_id => 1,
  35. :query_id => 999
  36. }
  37. assert_response 404
  38. end
  39. def test_index_should_return_privates_notes_with_permission_only
  40. journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true, :user_id => 1)
  41. @request.session[:user_id] = 2
  42. get :index, :params => {
  43. :project_id => 1
  44. }
  45. assert_response :success
  46. assert_select 'entry>id', :text => "http://test.host/issues/2?journal_id=#{journal.id}"
  47. Role.find(1).remove_permission! :view_private_notes
  48. get :index, :params => {
  49. :project_id => 1
  50. }
  51. assert_response :success
  52. assert_select 'entry>id', :text => "http://test.host/issues/2?journal_id=#{journal.id}", :count => 0
  53. end
  54. def test_index_should_show_visible_custom_fields_only
  55. Issue.destroy_all
  56. Journal.delete_all
  57. field_attributes = {:field_format => 'string', :is_for_all => true, :is_filter => true, :trackers => Tracker.all}
  58. @fields = []
  59. @fields << (@field1 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 1', :visible => true)))
  60. @fields << (@field2 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 2', :visible => false, :role_ids => [1, 2])))
  61. @fields << (@field3 = IssueCustomField.create!(field_attributes.merge(:name => 'Field 3', :visible => false, :role_ids => [1, 3])))
  62. @issue = Issue.generate!(
  63. :author_id => 1,
  64. :project_id => 1,
  65. :tracker_id => 1,
  66. :custom_field_values => {@field1.id => 'Value0', @field2.id => 'Value1', @field3.id => 'Value2'}
  67. )
  68. @issue.init_journal(User.find(1))
  69. @issue.custom_field_values = {@field1.id => 'NewValue0', @field2.id => 'NewValue1', @field3.id => 'NewValue2'}
  70. @issue.save!
  71. user_with_role_on_other_project = User.generate!
  72. User.add_to_project(user_with_role_on_other_project, Project.find(2), Role.find(3))
  73. users_to_test = {
  74. User.find(1) => [@field1, @field2, @field3],
  75. User.find(3) => [@field1, @field2],
  76. user_with_role_on_other_project => [@field1], # should see field1 only on Project 1
  77. User.generate! => [@field1],
  78. User.anonymous => [@field1]
  79. }
  80. users_to_test.each do |user, visible_fields|
  81. get :index, :params => {
  82. :format => 'atom',
  83. :key => user.rss_key
  84. }
  85. @fields.each_with_index do |field, i|
  86. if visible_fields.include?(field)
  87. assert_select "content[type=html]", { :text => /NewValue#{i}/, :count => 1 }, "User #{user.id} was not able to view #{field.name} in API"
  88. else
  89. assert_select "content[type=html]", { :text => /NewValue#{i}/, :count => 0 }, "User #{user.id} was able to view #{field.name} in API"
  90. end
  91. end
  92. end
  93. end
  94. def test_diff_for_description_change
  95. get :diff, :params => {
  96. :id => 3,
  97. :detail_id => 4
  98. }
  99. assert_response :success
  100. assert_select 'span.diff_out', :text => /removed/
  101. assert_select 'span.diff_in', :text => /added/
  102. end
  103. def test_diff_for_custom_field
  104. field = IssueCustomField.create!(:name => "Long field", :field_format => 'text')
  105. journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Notes', :user_id => 1)
  106. detail = JournalDetail.create!(:journal => journal, :property => 'cf', :prop_key => field.id,
  107. :old_value => 'Foo', :value => 'Bar')
  108. get :diff, :params => {
  109. :id => journal.id,
  110. :detail_id => detail.id
  111. }
  112. assert_response :success
  113. assert_select 'span.diff_out', :text => /Foo/
  114. assert_select 'span.diff_in', :text => /Bar/
  115. end
  116. def test_diff_for_custom_field_should_be_denied_if_custom_field_is_not_visible
  117. field = IssueCustomField.create!(:name => "Long field", :field_format => 'text', :visible => false, :role_ids => [1])
  118. journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Notes', :user_id => 1)
  119. detail = JournalDetail.create!(:journal => journal, :property => 'cf', :prop_key => field.id,
  120. :old_value => 'Foo', :value => 'Bar')
  121. get :diff, :params => {
  122. :id => journal.id,
  123. :detail_id => detail.id
  124. }
  125. assert_response 302
  126. end
  127. def test_diff_should_default_to_description_diff
  128. get :diff, :params => {
  129. :id => 3
  130. }
  131. assert_response :success
  132. assert_select 'span.diff_out', :text => /removed/
  133. assert_select 'span.diff_in', :text => /added/
  134. end
  135. def test_reply_to_issue
  136. @request.session[:user_id] = 2
  137. get :new, :params => {
  138. :id => 6
  139. },
  140. :xhr => true
  141. assert_response :success
  142. assert_equal 'text/javascript', response.content_type
  143. assert_include '> This is an issue', response.body
  144. end
  145. def test_reply_to_issue_without_permission
  146. @request.session[:user_id] = 7
  147. get :new, :params => {
  148. :id => 6
  149. },
  150. :xhr => true
  151. assert_response 403
  152. end
  153. def test_reply_to_note
  154. @request.session[:user_id] = 2
  155. get :new, :params => {
  156. :id => 6,
  157. :journal_id => 4,
  158. :journal_indice => 1
  159. },
  160. :xhr => true
  161. assert_response :success
  162. assert_equal 'text/javascript', response.content_type
  163. assert_include 'Redmine Admin wrote in #note-1:', response.body
  164. assert_include '> A comment with a private version', response.body
  165. end
  166. def test_reply_to_private_note_should_fail_without_permission
  167. journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true)
  168. @request.session[:user_id] = 2
  169. get :new, :params => {
  170. :id => 2,
  171. :journal_id => journal.id
  172. },
  173. :xhr => true
  174. assert_response :success
  175. assert_equal 'text/javascript', response.content_type
  176. assert_include '> Privates notes', response.body
  177. Role.find(1).remove_permission! :view_private_notes
  178. get :new, :params => {
  179. :id => 2,
  180. :journal_id => journal.id
  181. },
  182. :xhr => true
  183. assert_response 404
  184. end
  185. def test_edit_xhr
  186. @request.session[:user_id] = 1
  187. get :edit, :params => {
  188. :id => 2
  189. },
  190. :xhr => true
  191. assert_response :success
  192. assert_equal 'text/javascript', response.content_type
  193. assert_include 'textarea', response.body
  194. end
  195. def test_edit_private_note_should_fail_without_permission
  196. journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true)
  197. @request.session[:user_id] = 2
  198. Role.find(1).add_permission! :edit_issue_notes
  199. get :edit, :params => {
  200. :id => journal.id
  201. },
  202. :xhr => true
  203. assert_response :success
  204. assert_equal 'text/javascript', response.content_type
  205. assert_include 'textarea', response.body
  206. Role.find(1).remove_permission! :view_private_notes
  207. get :edit, :params => {
  208. :id => journal.id
  209. },
  210. :xhr => true
  211. assert_response 404
  212. end
  213. def test_update_xhr
  214. @request.session[:user_id] = 1
  215. post :update, :params => {
  216. :id => 2,
  217. :journal => {
  218. :notes => 'Updated notes'
  219. }
  220. },
  221. :xhr => true
  222. assert_response :success
  223. assert_equal 'text/javascript', response.content_type
  224. assert_equal 'Updated notes', Journal.find(2).notes
  225. assert_include 'journal-2-notes', response.body
  226. # response should include journal_indice param for quote link
  227. assert_include 'journal_indice=2', response.body
  228. end
  229. def test_update_xhr_with_private_notes_checked
  230. @request.session[:user_id] = 1
  231. post :update, :params => {
  232. :id => 2,
  233. :journal => {
  234. :private_notes => '1'
  235. }
  236. },
  237. :xhr => true
  238. assert_response :success
  239. assert_equal 'text/javascript', response.content_type
  240. assert_equal true, Journal.find(2).private_notes
  241. assert_include 'change-2', response.body
  242. assert_include 'journal-2-private_notes', response.body
  243. end
  244. def test_update_xhr_with_private_notes_unchecked
  245. Journal.find(2).update_attributes(:private_notes => true)
  246. @request.session[:user_id] = 1
  247. post :update, :params => {
  248. :id => 2,
  249. :journal => {
  250. :private_notes => '0'
  251. }
  252. },
  253. :xhr => true
  254. assert_response :success
  255. assert_equal 'text/javascript', response.content_type
  256. assert_equal false, Journal.find(2).private_notes
  257. assert_include 'change-2', response.body
  258. assert_include 'journal-2-private_notes', response.body
  259. end
  260. def test_update_xhr_without_set_private_notes_permission_should_ignore_private_notes
  261. @request.session[:user_id] = 2
  262. Role.find(1).add_permission! :edit_issue_notes
  263. Role.find(1).add_permission! :view_private_notes
  264. Role.find(1).remove_permission! :set_notes_private
  265. post :update, :params => {
  266. :id => 2,
  267. :journal => {
  268. :private_notes => '1'
  269. }
  270. },
  271. :xhr => true
  272. assert_response :success
  273. assert_equal false, Journal.find(2).private_notes
  274. end
  275. def test_update_xhr_with_empty_notes_should_delete_the_journal
  276. @request.session[:user_id] = 1
  277. assert_difference 'Journal.count', -1 do
  278. post :update, :params => {
  279. :id => 2,
  280. :journal => {
  281. :notes => ''
  282. }
  283. },
  284. :xhr => true
  285. assert_response :success
  286. assert_equal 'text/javascript', response.content_type
  287. end
  288. assert_nil Journal.find_by_id(2)
  289. assert_include 'change-2', response.body
  290. end
  291. end