您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

journals_controller_test.rb 10KB

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