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

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