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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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_relative '../test_helper'
  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 => {:project_id => 1})
  29. assert_response :success
  30. assert_equal 'application/atom+xml', @response.media_type
  31. end
  32. def test_index_with_invalid_query_id
  33. get(
  34. :index,
  35. :params => {
  36. :project_id => 1,
  37. :query_id => 999
  38. }
  39. )
  40. assert_response 404
  41. end
  42. def test_index_should_return_privates_notes_with_permission_only
  43. journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true, :user_id => 1)
  44. @request.session[:user_id] = 2
  45. get(:index, :params => {:project_id => 1})
  46. assert_response :success
  47. assert_select 'entry>id', :text => "http://test.host/issues/2?journal_id=#{journal.id}"
  48. Role.find(1).remove_permission! :view_private_notes
  49. get(:index, :params => {:project_id => 1})
  50. assert_response :success
  51. assert_select 'entry>id', :text => "http://test.host/issues/2?journal_id=#{journal.id}", :count => 0
  52. end
  53. def test_index_should_show_visible_custom_fields_only
  54. set_tmp_attachments_directory
  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(
  82. :index,
  83. :params => {
  84. :format => 'atom',
  85. :key => user.atom_key
  86. }
  87. )
  88. @fields.each_with_index do |field, i|
  89. if visible_fields.include?(field)
  90. assert_select(
  91. "content[type=html]",
  92. {:text => /NewValue#{i}/, :count => 1},
  93. "User #{user.id} was not able to view #{field.name} in API"
  94. )
  95. else
  96. assert_select(
  97. "content[type=html]",
  98. {:text => /NewValue#{i}/, :count => 0},
  99. "User #{user.id} was able to view #{field.name} in API"
  100. )
  101. end
  102. end
  103. end
  104. end
  105. def test_diff_for_description_change
  106. get(:diff, :params => {:id => 3, :detail_id => 4})
  107. assert_response :success
  108. assert_select 'span.diff_out', :text => /removed/
  109. assert_select 'span.diff_in', :text => /added/
  110. end
  111. def test_diff_for_custom_field
  112. field = IssueCustomField.create!(:name => "Long field", :field_format => 'text')
  113. journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Notes', :user_id => 1)
  114. detail = JournalDetail.create!(:journal => journal, :property => 'cf', :prop_key => field.id,
  115. :old_value => 'Foo', :value => 'Bar')
  116. get(
  117. :diff,
  118. :params => {
  119. :id => journal.id,
  120. :detail_id => detail.id
  121. }
  122. )
  123. assert_response :success
  124. assert_select 'span.diff_out', :text => /Foo/
  125. assert_select 'span.diff_in', :text => /Bar/
  126. end
  127. def test_diff_for_custom_field_should_be_denied_if_custom_field_is_not_visible
  128. field = IssueCustomField.create!(:name => "Long field", :field_format => 'text', :visible => false, :role_ids => [1])
  129. journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Notes', :user_id => 1)
  130. detail = JournalDetail.create!(:journal => journal, :property => 'cf', :prop_key => field.id,
  131. :old_value => 'Foo', :value => 'Bar')
  132. get(
  133. :diff,
  134. :params => {
  135. :id => journal.id,
  136. :detail_id => detail.id
  137. }
  138. )
  139. assert_response 302
  140. end
  141. def test_diff_should_default_to_description_diff
  142. get(:diff, :params => {:id => 3})
  143. assert_response :success
  144. assert_select 'span.diff_out', :text => /removed/
  145. assert_select 'span.diff_in', :text => /added/
  146. end
  147. def test_reply_to_issue
  148. @request.session[:user_id] = 2
  149. get(:new, :params => {:id => 6}, :xhr => true)
  150. assert_response :success
  151. assert_equal 'text/javascript', response.media_type
  152. assert_include '> This is an issue', response.body
  153. end
  154. def test_reply_to_issue_without_permission
  155. @request.session[:user_id] = 7
  156. get(:new, :params => {:id => 6}, :xhr => true)
  157. assert_response 403
  158. end
  159. def test_reply_to_note
  160. @request.session[:user_id] = 2
  161. get(
  162. :new,
  163. :params => {
  164. :id => 6,
  165. :journal_id => 4,
  166. :journal_indice => 1
  167. },
  168. :xhr => true
  169. )
  170. assert_response :success
  171. assert_equal 'text/javascript', response.media_type
  172. assert_include 'Redmine Admin wrote in #note-1:', response.body
  173. assert_include '> A comment with a private version', response.body
  174. end
  175. def test_reply_to_private_note_should_fail_without_permission
  176. journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true)
  177. @request.session[:user_id] = 2
  178. get(
  179. :new,
  180. :params => {
  181. :id => 2,
  182. :journal_id => journal.id
  183. },
  184. :xhr => true
  185. )
  186. assert_response :success
  187. assert_equal 'text/javascript', response.media_type
  188. assert_include '> Privates notes', response.body
  189. Role.find(1).remove_permission! :view_private_notes
  190. get(
  191. :new,
  192. :params => {
  193. :id => 2,
  194. :journal_id => journal.id
  195. },
  196. :xhr => true
  197. )
  198. assert_response 404
  199. end
  200. def test_edit_xhr
  201. @request.session[:user_id] = 1
  202. get(:edit, :params => {:id => 2}, :xhr => true)
  203. assert_response :success
  204. assert_equal 'text/javascript', response.media_type
  205. assert_include 'textarea', response.body
  206. end
  207. def test_edit_private_note_should_fail_without_permission
  208. journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true)
  209. @request.session[:user_id] = 2
  210. Role.find(1).add_permission! :edit_issue_notes
  211. get(:edit, :params => {:id => journal.id}, :xhr => true)
  212. assert_response :success
  213. assert_equal 'text/javascript', response.media_type
  214. assert_include 'textarea', response.body
  215. Role.find(1).remove_permission! :view_private_notes
  216. get(:edit, :params => {:id => journal.id}, :xhr => true)
  217. assert_response 404
  218. end
  219. def test_update_xhr
  220. @request.session[:user_id] = 1
  221. post(
  222. :update,
  223. :params => {
  224. :id => 2,
  225. :journal => {
  226. :notes => 'Updated notes'
  227. }
  228. },
  229. :xhr => true
  230. )
  231. assert_response :success
  232. assert_equal 'text/javascript', response.media_type
  233. assert_equal 'Updated notes', Journal.find(2).notes
  234. assert_include 'journal-2-notes', response.body
  235. # response should include journal_indice param for quote link
  236. assert_include 'journal_indice=2', response.body
  237. end
  238. def test_update_xhr_with_private_notes_checked
  239. @request.session[:user_id] = 1
  240. post(
  241. :update,
  242. :params => {
  243. :id => 2,
  244. :journal => {
  245. :private_notes => '1'
  246. }
  247. },
  248. :xhr => true
  249. )
  250. assert_response :success
  251. assert_equal 'text/javascript', response.media_type
  252. assert_equal true, Journal.find(2).private_notes
  253. assert_include 'change-2', response.body
  254. assert_include 'journal-2-private_notes', response.body
  255. end
  256. def test_update_xhr_with_private_notes_unchecked
  257. Journal.find(2).update(:private_notes => true)
  258. @request.session[:user_id] = 1
  259. post(
  260. :update,
  261. :params => {
  262. :id => 2,
  263. :journal => {
  264. :private_notes => '0'
  265. }
  266. },
  267. :xhr => true
  268. )
  269. assert_response :success
  270. assert_equal 'text/javascript', response.media_type
  271. assert_equal false, Journal.find(2).private_notes
  272. assert_include 'change-2', response.body
  273. assert_include 'journal-2-private_notes', response.body
  274. end
  275. def test_update_xhr_without_set_private_notes_permission_should_ignore_private_notes
  276. @request.session[:user_id] = 2
  277. Role.find(1).add_permission! :edit_issue_notes
  278. Role.find(1).add_permission! :view_private_notes
  279. Role.find(1).remove_permission! :set_notes_private
  280. post(
  281. :update,
  282. :params => {
  283. :id => 2,
  284. :journal => {
  285. :private_notes => '1'
  286. }
  287. },
  288. :xhr => true
  289. )
  290. assert_response :success
  291. assert_equal false, Journal.find(2).private_notes
  292. end
  293. def test_update_xhr_with_empty_notes_should_delete_the_journal
  294. @request.session[:user_id] = 1
  295. assert_difference 'Journal.count', -1 do
  296. post(
  297. :update,
  298. :params => {
  299. :id => 2,
  300. :journal => {
  301. :notes => ''
  302. }
  303. },
  304. :xhr => true
  305. )
  306. assert_response :success
  307. assert_equal 'text/javascript', response.media_type
  308. end
  309. assert_nil Journal.find_by_id(2)
  310. assert_include 'change-2', response.body
  311. end
  312. end