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.

my_controller_test.rb 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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 MyControllerTest < Redmine::ControllerTest
  20. fixtures :users, :email_addresses, :user_preferences,
  21. :roles, :projects, :members, :member_roles,
  22. :issues, :issue_statuses, :trackers, :enumerations,
  23. :custom_fields, :auth_sources, :queries, :enabled_modules,
  24. :journals, :projects_trackers
  25. def setup
  26. @request.session[:user_id] = 2
  27. end
  28. def test_index
  29. get :index
  30. assert_response :success
  31. assert_select 'h2', 'My page'
  32. end
  33. def test_page
  34. get :page
  35. assert_response :success
  36. assert_select 'h2', 'My page'
  37. end
  38. def test_page_with_timelog_block
  39. preferences = User.find(2).pref
  40. preferences[:my_page_layout] = {'top' => ['timelog']}
  41. preferences.save!
  42. with_issue =
  43. TimeEntry.create!(
  44. :user => User.find(2), :spent_on => Date.yesterday,
  45. :hours => 2.5, :activity_id => 10, :issue_id => 1
  46. )
  47. without_issue =
  48. TimeEntry.create!(
  49. :user => User.find(2), :spent_on => Date.yesterday,
  50. :hours => 3.5, :activity_id => 10, :project_id => 1
  51. )
  52. get :page
  53. assert_response :success
  54. assert_select "tr#time-entry-#{with_issue.id}" do
  55. assert_select 'td.subject a[href="/issues/1"]'
  56. assert_select 'td.hours', :text => '2:30'
  57. end
  58. assert_select "tr#time-entry-#{without_issue.id}" do
  59. assert_select 'td.hours', :text => '3:30'
  60. end
  61. end
  62. def test_page_with_assigned_issues_block_and_no_custom_settings
  63. preferences = User.find(2).pref
  64. preferences.my_page_layout = {'top' => ['issuesassignedtome']}
  65. preferences.my_page_settings = nil
  66. preferences.save!
  67. get :page
  68. assert_select '#block-issuesassignedtome' do
  69. assert_select 'table.issues' do
  70. assert_select 'th a[data-remote=true][data-method=post]', :text => 'Tracker'
  71. end
  72. assert_select '#issuesassignedtome-settings' do
  73. assert_select 'select[name=?]', 'settings[issuesassignedtome][columns][]'
  74. end
  75. end
  76. end
  77. def test_page_with_assigned_issues_block_and_custom_columns
  78. preferences = User.find(2).pref
  79. preferences.my_page_layout = {'top' => ['issuesassignedtome']}
  80. preferences.my_page_settings =
  81. {'issuesassignedtome' => {:columns => ['tracker', 'subject', 'due_date']}}
  82. preferences.save!
  83. get :page
  84. assert_select '#block-issuesassignedtome' do
  85. assert_select 'table.issues td.due_date'
  86. end
  87. end
  88. def test_page_with_assigned_issues_block_and_custom_sort
  89. preferences = User.find(2).pref
  90. preferences.my_page_layout = {'top' => ['issuesassignedtome']}
  91. preferences.my_page_settings = {'issuesassignedtome' => {:sort => 'due_date'}}
  92. preferences.save!
  93. get :page
  94. assert_select '#block-issuesassignedtome' do
  95. assert_select 'table.issues.sort-by-due-date'
  96. end
  97. end
  98. def test_page_with_issuequery_block_and_no_settings
  99. user = User.find(2)
  100. user.pref.my_page_layout = {'top' => ['issuequery']}
  101. user.pref.save!
  102. get :page
  103. assert_response :success
  104. assert_select '#block-issuequery' do
  105. assert_select 'h3', :text => 'Issues'
  106. assert_select 'select[name=?]', 'settings[issuequery][query_id]' do
  107. assert_select 'option[value="5"]', :text => 'Open issues by priority and tracker'
  108. end
  109. end
  110. end
  111. def test_page_with_issuequery_block_and_global_query
  112. user = User.find(2)
  113. query =
  114. IssueQuery.create!(
  115. :name => 'All issues', :user => user,
  116. :column_names => [:tracker, :subject, :status, :assigned_to]
  117. )
  118. user.pref.my_page_layout = {'top' => ['issuequery']}
  119. user.pref.my_page_settings = {'issuequery' => {:query_id => query.id}}
  120. user.pref.save!
  121. get :page
  122. assert_response :success
  123. assert_select '#block-issuequery' do
  124. assert_select 'a[href=?]', "/issues?query_id=#{query.id}"
  125. # assert number of columns (columns from query + id column + checkbox column)
  126. assert_select 'table.issues th', 7
  127. # assert results limit
  128. assert_select 'table.issues tr.issue', 10
  129. assert_select 'table.issues td.assigned_to'
  130. end
  131. end
  132. def test_page_with_issuequery_block_and_project_query
  133. user = User.find(2)
  134. query =
  135. IssueQuery.create!(
  136. :name => 'All issues', :project => Project.find(1),
  137. :user => user,
  138. :column_names => [:tracker, :subject, :status, :assigned_to]
  139. )
  140. user.pref.my_page_layout = {'top' => ['issuequery']}
  141. user.pref.my_page_settings = {'issuequery' => {:query_id => query.id}}
  142. user.pref.save!
  143. get :page
  144. assert_response :success
  145. assert_select '#block-issuequery' do
  146. assert_select 'a[href=?]', "/projects/ecookbook/issues?query_id=#{query.id}"
  147. # assert number of columns (columns from query + id column + checkbox column)
  148. assert_select 'table.issues th', 7
  149. # assert results limit
  150. assert_select 'table.issues tr.issue', 10
  151. assert_select 'table.issues td.assigned_to'
  152. end
  153. end
  154. def test_page_with_issuequery_block_and_query_should_display_custom_columns
  155. user = User.find(2)
  156. query =
  157. IssueQuery.create!(
  158. :name => 'All issues', :user => user,
  159. :column_names => [:tracker, :subject, :status, :assigned_to]
  160. )
  161. user.pref.my_page_layout = {'top' => ['issuequery']}
  162. user.pref.my_page_settings = {
  163. 'issuequery' => {:query_id => query.id, :columns => [:subject, :due_date]}
  164. }
  165. user.pref.save!
  166. get :page
  167. assert_response :success
  168. assert_select '#block-issuequery' do
  169. # assert number of columns (columns from query + id column + checkbox column)
  170. assert_select 'table.issues th', 5
  171. assert_select 'table.issues th', :text => 'Due date'
  172. end
  173. end
  174. def test_page_with_multiple_issuequery_blocks
  175. user = User.find(2)
  176. query1 =
  177. IssueQuery.create!(:name => 'All issues', :user => user,
  178. :column_names => [:tracker, :subject, :status, :assigned_to])
  179. query2 =
  180. IssueQuery.create!(:name => 'Other issues', :user => user,
  181. :column_names => [:tracker, :subject, :priority])
  182. user.pref.my_page_layout = {'top' => ['issuequery__1', 'issuequery']}
  183. user.pref.my_page_settings = {
  184. 'issuequery' => {:query_id => query1.id, :columns => [:subject, :due_date]},
  185. 'issuequery__1' => {:query_id => query2.id}
  186. }
  187. user.pref.save!
  188. get :page
  189. assert_response :success
  190. assert_select '#block-issuequery' do
  191. assert_select 'h3', :text => /All issues/
  192. assert_select 'table.issues th', :text => 'Due date'
  193. end
  194. assert_select '#block-issuequery__1' do
  195. assert_select 'h3', :text => /Other issues/
  196. assert_select 'table.issues th', :text => 'Priority'
  197. end
  198. assert_select '#block-select' do
  199. assert_select 'option[value=?]:not([disabled])', 'issuequery__2', :text => 'Issues'
  200. end
  201. end
  202. def test_page_with_activity
  203. user = User.find(2)
  204. user.pref.my_page_layout = {'top' => ['activity']}
  205. user.pref.time_zone = 'UTC'
  206. user.pref.save!
  207. get :page
  208. assert_response :success
  209. assert_select 'div#block-activity' do
  210. assert_select 'h3' do
  211. assert_select(
  212. 'a[href=?]', activity_path(from: User.current.today, user_id: user.id),
  213. :text => 'Activity'
  214. )
  215. end
  216. assert_select 'div#activity' do
  217. assert_select 'dt', 10
  218. end
  219. end
  220. end
  221. def test_page_with_updated_issues_block
  222. preferences = User.find(2).pref
  223. preferences.my_page_layout = {'top' => ['issuesupdatedbyme']}
  224. preferences.my_page_settings = {'issuesupdatedbyme' => {}}
  225. preferences.save!
  226. project = Project.find(3)
  227. project.close
  228. get :page
  229. assert_response :success
  230. assert_select '#block-issuesupdatedbyme' do
  231. report_url = CGI.unescape(css_select('h3 a').first.attr(:href))
  232. assert_match 'f[]=project.status', report_url
  233. assert_match 'v[project.status][]=1', report_url
  234. assert_match 'f[]=updated_by', report_url
  235. assert_match 'v[updated_by][]=me', report_url
  236. assert_select 'table.issues tbody tr', 2
  237. assert_select 'table.issues tbody tr[id=?]', 'issue-1', 1, :title => 'Cannot print recipes'
  238. assert_select 'table.issues tbody tr[id=?]', 'issue-14', 0
  239. end
  240. end
  241. def test_page_with_all_blocks
  242. blocks = Redmine::MyPage.blocks.keys
  243. preferences = User.find(2).pref
  244. preferences[:my_page_layout] = {'top' => blocks}
  245. preferences.save!
  246. get :page
  247. assert_response :success
  248. assert_select 'div.mypage-box', blocks.size
  249. end
  250. def test_page_with_assigned_issues_block_should_not_show_issues_from_closed_projects
  251. preferences = User.find(2).pref
  252. preferences.my_page_layout = {'top' => ['issuesassignedtome']}
  253. preferences.my_page_settings = {'issuesassignedtome' => {}}
  254. preferences.save!
  255. issue = Issue.find(1)
  256. issue.assigned_to = User.find(2)
  257. issue.save!
  258. project = Project.find(2)
  259. project.close
  260. project.save
  261. get :page
  262. assert_response :success
  263. assert_select '#block-issuesassignedtome table.issues tbody' do
  264. report_url = css_select('h3 a').map {|e| e.attr(:href)}.first
  265. assert_match 'f%5B%5D=project.status', report_url
  266. assert_match 'v%5Bproject.status%5D%5B%5D=1', report_url
  267. assert_select 'tr', 1
  268. assert_select 'tr[id=?]', 'issue-1', 1, :title => 'Cannot print recipes'
  269. assert_select 'tr[id=?]', 'issue-4', 0
  270. end
  271. end
  272. def test_page_with_reported_issues_block_should_not_show_issues_from_closed_projects
  273. preferences = User.find(2).pref
  274. preferences.my_page_layout = {'top' => ['issuesreportedbyme']}
  275. preferences.my_page_settings = {'issuesreportedbyme' => {}}
  276. preferences.save!
  277. issue = Issue.find(1)
  278. issue.assigned_to = User.find(2)
  279. issue.save!
  280. project = Project.find(2)
  281. project.close
  282. project.save
  283. get :page
  284. assert_response :success
  285. assert_select '#block-issuesreportedbyme' do
  286. report_url = css_select('h3 a').map {|e| e.attr(:href)}.first
  287. assert_match 'f%5B%5D=project.status', report_url
  288. assert_match 'v%5Bproject.status%5D%5B%5D=1', report_url
  289. assert_select 'table.issues tbody tr', 10
  290. assert_select 'table.issues tbody tr[id=?]', 'issue-1', 1, :title => 'Cannot print recipes'
  291. assert_select 'table.issues tbody tr[id=?]', 'issue-4', 0
  292. end
  293. end
  294. def test_page_with_watched_issues_block_should_not_show_issues_from_closed_projects
  295. preferences = User.find(2).pref
  296. preferences.my_page_layout = {'top' => ['issueswatched']}
  297. preferences.my_page_settings = {'issueswatched' => {}}
  298. preferences.save!
  299. issue = Issue.find(1)
  300. issue.watcher_user_ids = ['1', '2']
  301. issue.save!
  302. issue2 = Issue.find(4)
  303. issue2.watcher_user_ids = ['2']
  304. issue2.save!
  305. project = Project.find(2)
  306. project.close
  307. project.save
  308. get :page
  309. assert_response :success
  310. assert_select '#block-issueswatched table.issues tbody' do
  311. report_url = css_select('h3 a').map {|e| e.attr(:href)}.first
  312. assert_match 'f%5B%5D=project.status', report_url
  313. assert_match 'v%5Bproject.status%5D%5B%5D=1', report_url
  314. assert_select 'tr', 1
  315. assert_select 'tr[id=?]', 'issue-1', 1, :title => 'Cannot print recipes'
  316. assert_select 'tr[id=?]', 'issue-4', 0
  317. end
  318. end
  319. def test_my_account_should_show_editable_custom_fields
  320. get :account
  321. assert_response :success
  322. assert_select 'input[name=?]', 'user[custom_field_values][4]'
  323. end
  324. def test_my_account_should_not_show_non_editable_custom_fields
  325. UserCustomField.find(4).update_attribute :editable, false
  326. get :account
  327. assert_response :success
  328. assert_select 'input[name=?]', 'user[custom_field_values][4]', 0
  329. end
  330. def test_my_account_should_show_language_select
  331. get :account
  332. assert_response :success
  333. assert_select 'select[name=?]', 'user[language]'
  334. end
  335. def test_my_account_with_avatar_enabled_should_link_to_edit_avatar
  336. with_settings :gravatar_enabled => '1' do
  337. Redmine::Configuration.with 'avatar_server_url' => 'https://gravatar.com' do
  338. get :account
  339. assert_response :success
  340. assert_select 'a[href=?] img.gravatar', 'https://gravatar.com'
  341. end
  342. end
  343. end
  344. def test_my_account_should_not_show_language_select_with_force_default_language_for_loggedin
  345. with_settings :force_default_language_for_loggedin => '1' do
  346. get :account
  347. assert_response :success
  348. assert_select 'select[name=?]', 'user[language]', 0
  349. end
  350. end
  351. def test_page_with_calendar
  352. date = '2020-10-21'
  353. subject = 'calendar on my page'
  354. issue = Issue.generate!(:start_date => date,
  355. :due_date => date,
  356. :project_id => 1,
  357. :tracker_id => 1,
  358. :subject => subject)
  359. travel_to date
  360. preferences = User.find(2).pref
  361. preferences[:my_page_layout] = {'top' => ['calendar']}
  362. preferences.save!
  363. with_settings :start_of_week => 7 do
  364. get :page
  365. end
  366. assert_response :success
  367. assert_select 'form[data-cm-url=?]', '/issues/context_menu'
  368. assert_select 'table.cal' do
  369. assert_select 'tr' do
  370. assert_select 'td' do
  371. assert_select(
  372. 'div.issue.hascontextmenu.tooltip.starting.ending',
  373. :text => /eCookbook.*#{subject}/m
  374. ) do
  375. assert_select(
  376. 'a.issue[href=?]', "/issues/#{issue.id}",
  377. :text => "Bug ##{issue.id}"
  378. )
  379. assert_select(
  380. 'input[name=?][type=?][value=?]',
  381. 'ids[]',
  382. 'checkbox',
  383. issue.id.to_s
  384. )
  385. end
  386. end
  387. end
  388. end
  389. end
  390. def test_update_account
  391. put(
  392. :account,
  393. :params => {
  394. :user => {
  395. :firstname => "Joe",
  396. :login => "root",
  397. :admin => 1,
  398. :group_ids => ['10'],
  399. :custom_field_values => {
  400. "4" => "0100562500"
  401. }
  402. }
  403. }
  404. )
  405. assert_redirected_to '/my/account'
  406. user = User.find(2)
  407. assert_equal "Joe", user.firstname
  408. assert_equal "jsmith", user.login
  409. assert_equal "0100562500", user.custom_value_for(4).value
  410. # ignored
  411. assert !user.admin?
  412. assert user.groups.empty?
  413. end
  414. def test_update_account_should_send_security_notification
  415. ActionMailer::Base.deliveries.clear
  416. put(
  417. :account,
  418. :params => {
  419. :user => {
  420. :mail => 'foobar@example.com'
  421. }
  422. }
  423. )
  424. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  425. assert_mail_body_match '0.0.0.0', mail
  426. assert_mail_body_match(
  427. I18n.t(:mail_body_security_notification_change_to,
  428. :field => I18n.t(:field_mail), :value => 'foobar@example.com'),
  429. mail
  430. )
  431. assert_select_email do
  432. assert_select 'a[href^=?]', 'http://localhost:3000/my/account', :text => 'My account'
  433. end
  434. # The old email address should be notified about the change for security purposes
  435. assert mail.to.include?(User.find(2).mail)
  436. assert mail.to.include?('foobar@example.com')
  437. end
  438. def test_my_account_notify_about_high_priority_issues_preference
  439. # normally, preference should be shown
  440. get :account
  441. assert_select 'label[for="pref_notify_about_high_priority_issues"]'
  442. # preference should be persisted
  443. put(
  444. :account,
  445. :params => {
  446. :pref => {
  447. notify_about_high_priority_issues: '1'
  448. }
  449. }
  450. )
  451. assert User.find(2).notify_about_high_priority_issues?
  452. # preference should be hidden if there aren't any priorities
  453. Issue.destroy_all
  454. IssuePriority.destroy_all
  455. get :account
  456. assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
  457. # preference should be hidden if there isn't a "high" priority
  458. a = IssuePriority.create! name: 'A'
  459. get :account
  460. assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
  461. # preference should be shown if there are at least two priorities (one low, one high)
  462. b = IssuePriority.create! name: 'B'
  463. get :account
  464. assert_select 'label[for="pref_notify_about_high_priority_issues"]'
  465. # preference should be hidden if the highest priority is the default one,
  466. # because that means that there is no "high" priority
  467. b.update! is_default: true
  468. get :account
  469. assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
  470. end
  471. def test_my_account_should_show_destroy_link
  472. get :account
  473. assert_select 'a[href="/my/account/destroy"]'
  474. end
  475. def test_get_destroy_should_display_the_destroy_confirmation
  476. get :destroy
  477. assert_response :success
  478. assert_select 'form[action="/my/account/destroy"]' do
  479. assert_select 'input[name=confirm]'
  480. end
  481. end
  482. def test_post_destroy_without_confirmation_should_not_destroy_account
  483. assert_no_difference 'User.count' do
  484. post :destroy
  485. end
  486. assert_response :success
  487. end
  488. def test_post_destroy_without_confirmation_should_destroy_account
  489. assert_difference 'User.count', -1 do
  490. post(
  491. :destroy,
  492. :params => {
  493. :confirm => '1'
  494. }
  495. )
  496. end
  497. assert_redirected_to '/'
  498. assert_match /deleted/i, flash[:notice]
  499. end
  500. def test_post_destroy_with_unsubscribe_not_allowed_should_not_destroy_account
  501. User.any_instance.stubs(:own_account_deletable?).returns(false)
  502. assert_no_difference 'User.count' do
  503. post(
  504. :destroy,
  505. :params => {
  506. :confirm => '1'
  507. }
  508. )
  509. end
  510. assert_redirected_to '/my/account'
  511. end
  512. def test_change_password
  513. get :password
  514. assert_response :success
  515. assert_select 'input[type=password][name=password]'
  516. assert_select 'input[type=password][name=new_password]'
  517. assert_select 'input[type=password][name=new_password_confirmation]'
  518. end
  519. def test_update_password
  520. post(
  521. :password,
  522. :params => {
  523. :password => 'jsmith',
  524. :new_password => 'secret123',
  525. :new_password_confirmation => 'secret123'
  526. }
  527. )
  528. assert_redirected_to '/my/account'
  529. assert User.try_to_login('jsmith', 'secret123')
  530. end
  531. def test_update_password_with_non_matching_confirmation
  532. post(
  533. :password,
  534. :params => {
  535. :password => 'jsmith',
  536. :new_password => 'secret123',
  537. :new_password_confirmation => 'secret1234'
  538. }
  539. )
  540. assert_response :success
  541. assert_select_error /Password doesn.*t match confirmation/
  542. assert User.try_to_login('jsmith', 'jsmith')
  543. end
  544. def test_update_password_with_wrong_password
  545. # wrong password
  546. post(
  547. :password,
  548. :params => {
  549. :password => 'wrongpassword',
  550. :new_password => 'secret123',
  551. :new_password_confirmation => 'secret123'
  552. }
  553. )
  554. assert_response :success
  555. assert_equal 'Wrong password', flash[:error]
  556. assert User.try_to_login('jsmith', 'jsmith')
  557. end
  558. def test_change_password_should_redirect_if_user_cannot_change_its_password
  559. User.find(2).update_attribute(:auth_source_id, 1)
  560. get :password
  561. assert_not_nil flash[:error]
  562. assert_redirected_to '/my/account'
  563. end
  564. def test_update_password_should_send_security_notification
  565. ActionMailer::Base.deliveries.clear
  566. post(
  567. :password,
  568. :params => {
  569. :password => 'jsmith',
  570. :new_password => 'secret123',
  571. :new_password_confirmation => 'secret123'
  572. }
  573. )
  574. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  575. assert_mail_body_no_match 'secret123', mail # just to be sure: pw should never be sent!
  576. assert_select_email do
  577. assert_select 'a[href^=?]', 'http://localhost:3000/my/password', :text => 'Change password'
  578. end
  579. end
  580. def test_update_page_with_blank_preferences
  581. user = User.generate!(:language => 'en')
  582. @request.session[:user_id] = user.id
  583. post(
  584. :update_page,
  585. :params => {
  586. :settings => {
  587. 'issuesassignedtome' => {
  588. 'columns' => ['subject', 'due_date']
  589. }
  590. }
  591. },
  592. :xhr => true
  593. )
  594. assert_response :success
  595. assert_include '$("#block-issuesassignedtome").replaceWith(', response.body
  596. assert_include 'Due date', response.body
  597. assert_equal({:columns => ['subject', 'due_date']},
  598. user.reload.pref.my_page_settings('issuesassignedtome'))
  599. end
  600. def test_add_block
  601. post(
  602. :add_block,
  603. :params => {
  604. :block => 'issueswatched'
  605. }
  606. )
  607. assert_redirected_to '/my/page'
  608. assert User.find(2).pref[:my_page_layout]['top'].include?('issueswatched')
  609. end
  610. def test_add_block_xhr
  611. post(
  612. :add_block,
  613. :params => {
  614. :block => 'issueswatched'
  615. },
  616. :xhr => true
  617. )
  618. assert_response :success
  619. assert_include 'issueswatched', User.find(2).pref[:my_page_layout]['top']
  620. end
  621. def test_add_invalid_block_should_error
  622. post(
  623. :add_block,
  624. :params => {
  625. :block => 'invalid'
  626. }
  627. )
  628. assert_response 422
  629. end
  630. def test_remove_block
  631. post(
  632. :remove_block,
  633. :params => {
  634. :block => 'issuesassignedtome'
  635. }
  636. )
  637. assert_redirected_to '/my/page'
  638. assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome')
  639. end
  640. def test_remove_block_xhr
  641. post(
  642. :remove_block,
  643. :params => {
  644. :block => 'issuesassignedtome'
  645. },
  646. :xhr => true
  647. )
  648. assert_response :success
  649. assert_include '$("#block-issuesassignedtome").remove();', response.body
  650. assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome')
  651. end
  652. def test_order_blocks
  653. pref = User.find(2).pref
  654. pref.my_page_layout = {'left' => ['news', 'calendar', 'documents']}
  655. pref.save!
  656. post(
  657. :order_blocks,
  658. :params => {
  659. :group => 'left',
  660. :blocks => ['documents', 'calendar', 'news']
  661. },
  662. :xhr => true
  663. )
  664. assert_response :success
  665. assert_equal ['documents', 'calendar', 'news'], User.find(2).pref.my_page_layout['left']
  666. end
  667. def test_move_block
  668. pref = User.find(2).pref
  669. pref.my_page_layout = {'left' => ['news', 'documents'], 'right' => ['calendar']}
  670. pref.save!
  671. post(
  672. :order_blocks,
  673. :params => {
  674. :group => 'left',
  675. :blocks => ['news', 'calendar', 'documents']
  676. },
  677. :xhr => true
  678. )
  679. assert_response :success
  680. assert_equal({'left' => ['news', 'calendar', 'documents'],
  681. 'right' => []},
  682. User.find(2).pref.my_page_layout)
  683. end
  684. def test_reset_rss_key_with_existing_key
  685. @previous_token_value = User.find(2).rss_key # Will generate one if it's missing
  686. post :reset_rss_key
  687. assert_not_equal @previous_token_value, User.find(2).rss_key
  688. assert User.find(2).rss_token
  689. assert_match /reset/, flash[:notice]
  690. assert_redirected_to '/my/account'
  691. end
  692. def test_reset_rss_key_without_existing_key
  693. Token.delete_all
  694. assert_nil User.find(2).rss_token
  695. post :reset_rss_key
  696. assert User.find(2).rss_token
  697. assert_match /reset/, flash[:notice]
  698. assert_redirected_to '/my/account'
  699. end
  700. def test_show_api_key
  701. get :show_api_key
  702. assert_response :success
  703. assert_select 'pre', User.find(2).api_key
  704. end
  705. def test_reset_api_key_with_existing_key
  706. @previous_token_value = User.find(2).api_key # Will generate one if it's missing
  707. post :reset_api_key
  708. assert_not_equal @previous_token_value, User.find(2).api_key
  709. assert User.find(2).api_token
  710. assert_match /reset/, flash[:notice]
  711. assert_redirected_to '/my/account'
  712. end
  713. def test_reset_api_key_without_existing_key
  714. assert_nil User.find(2).api_token
  715. post :reset_api_key
  716. assert User.find(2).api_token
  717. assert_match /reset/, flash[:notice]
  718. assert_redirected_to '/my/account'
  719. end
  720. end