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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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 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, :issue_categories
  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' do
  238. assert_select 'td.subject a', :text => 'Cannot print recipes', :count => 1
  239. end
  240. assert_select 'table.issues tbody tr[id=?]', 'issue-14', 0
  241. end
  242. end
  243. def test_page_with_all_blocks
  244. blocks = Redmine::MyPage.blocks.keys
  245. preferences = User.find(2).pref
  246. preferences[:my_page_layout] = {'top' => blocks}
  247. preferences.save!
  248. get :page
  249. assert_response :success
  250. assert_select 'div.mypage-box', blocks.size
  251. end
  252. def test_page_with_assigned_issues_block_should_not_show_issues_from_closed_projects
  253. preferences = User.find(2).pref
  254. preferences.my_page_layout = {'top' => ['issuesassignedtome']}
  255. preferences.my_page_settings = {'issuesassignedtome' => {}}
  256. preferences.save!
  257. issue = Issue.find(1)
  258. issue.assigned_to = User.find(2)
  259. issue.save!
  260. project = Project.find(2)
  261. project.close
  262. project.save
  263. get :page
  264. assert_response :success
  265. assert_select '#block-issuesassignedtome table.issues tbody' do
  266. report_url = css_select('h3 a').map {|e| e.attr(:href)}.first
  267. assert_match 'f%5B%5D=project.status', report_url
  268. assert_match 'v%5Bproject.status%5D%5B%5D=1', report_url
  269. assert_select 'tr', 1
  270. assert_select 'tr[id=?]', 'issue-1' do
  271. assert_select 'td.subject a', :text => 'Cannot print recipes', :count => 1
  272. end
  273. assert_select 'tr[id=?]', 'issue-4', 0
  274. end
  275. end
  276. def test_page_with_reported_issues_block_should_not_show_issues_from_closed_projects
  277. preferences = User.find(2).pref
  278. preferences.my_page_layout = {'top' => ['issuesreportedbyme']}
  279. preferences.my_page_settings = {'issuesreportedbyme' => {}}
  280. preferences.save!
  281. issue = Issue.find(1)
  282. issue.assigned_to = User.find(2)
  283. issue.save!
  284. project = Project.find(2)
  285. project.close
  286. project.save
  287. get :page
  288. assert_response :success
  289. assert_select '#block-issuesreportedbyme' do
  290. report_url = css_select('h3 a').map {|e| e.attr(:href)}.first
  291. assert_match 'f%5B%5D=project.status', report_url
  292. assert_match 'v%5Bproject.status%5D%5B%5D=1', report_url
  293. assert_select 'table.issues tbody tr', 10
  294. assert_select 'table.issues tbody tr[id=?]', 'issue-1' do
  295. assert_select 'td.subject a', :text => 'Cannot print recipes', :count => 1
  296. end
  297. assert_select 'table.issues tbody tr[id=?]', 'issue-4', 0
  298. end
  299. end
  300. def test_page_with_watched_issues_block_should_not_show_issues_from_closed_projects
  301. preferences = User.find(2).pref
  302. preferences.my_page_layout = {'top' => ['issueswatched']}
  303. preferences.my_page_settings = {'issueswatched' => {}}
  304. preferences.save!
  305. issue = Issue.find(1)
  306. issue.watcher_user_ids = ['1', '2']
  307. issue.save!
  308. issue2 = Issue.find(4)
  309. issue2.watcher_user_ids = ['2']
  310. issue2.save!
  311. project = Project.find(2)
  312. project.close
  313. project.save
  314. get :page
  315. assert_response :success
  316. assert_select '#block-issueswatched table.issues tbody' do
  317. report_url = css_select('h3 a').map {|e| e.attr(:href)}.first
  318. assert_match 'f%5B%5D=project.status', report_url
  319. assert_match 'v%5Bproject.status%5D%5B%5D=1', report_url
  320. assert_select 'tr', 1
  321. assert_select 'tr[id=?]', 'issue-1' do
  322. assert_select 'td.subject a', :text => 'Cannot print recipes', :count => 1
  323. end
  324. assert_select 'tr[id=?]', 'issue-4', 0
  325. end
  326. end
  327. def test_my_account_should_show_editable_custom_fields
  328. get :account
  329. assert_response :success
  330. assert_select 'input[name=?]', 'user[custom_field_values][4]'
  331. end
  332. def test_my_account_should_not_show_non_editable_custom_fields
  333. UserCustomField.find(4).update_attribute :editable, false
  334. get :account
  335. assert_response :success
  336. assert_select 'input[name=?]', 'user[custom_field_values][4]', 0
  337. end
  338. def test_my_account_should_show_language_select
  339. get :account
  340. assert_response :success
  341. assert_select 'select[name=?]', 'user[language]'
  342. end
  343. def test_my_account_with_avatar_enabled_should_link_to_edit_avatar
  344. with_settings :gravatar_enabled => '1' do
  345. Redmine::Configuration.with 'avatar_server_url' => 'https://gravatar.com' do
  346. get :account
  347. assert_response :success
  348. assert_select 'a[href=?] img.gravatar', 'https://gravatar.com'
  349. end
  350. end
  351. end
  352. def test_my_account_should_not_show_language_select_with_force_default_language_for_loggedin
  353. with_settings :force_default_language_for_loggedin => '1' do
  354. get :account
  355. assert_response :success
  356. assert_select 'select[name=?]', 'user[language]', 0
  357. end
  358. end
  359. def test_page_with_calendar
  360. date = '2020-10-21'
  361. subject = 'calendar on my page'
  362. issue = Issue.generate!(:start_date => date,
  363. :due_date => date,
  364. :project_id => 1,
  365. :tracker_id => 1,
  366. :subject => subject)
  367. travel_to date
  368. preferences = User.find(2).pref
  369. preferences[:my_page_layout] = {'top' => ['calendar']}
  370. preferences.save!
  371. with_settings :start_of_week => 7 do
  372. get :page
  373. end
  374. assert_response :success
  375. assert_select 'form[data-cm-url=?]', '/issues/context_menu'
  376. assert_select 'ul.cal' do
  377. assert_select 'li' do
  378. assert_select(
  379. 'div.issue.hascontextmenu.tooltip.starting.ending',
  380. :text => /eCookbook.*#{subject}/m
  381. ) do
  382. assert_select(
  383. 'a.issue[href=?]', "/issues/#{issue.id}",
  384. :text => "Bug ##{issue.id}"
  385. )
  386. assert_select(
  387. 'input[name=?][type=?][value=?]',
  388. 'ids[]',
  389. 'checkbox',
  390. issue.id.to_s
  391. )
  392. end
  393. end
  394. end
  395. end
  396. def test_update_account
  397. put(
  398. :account,
  399. :params => {
  400. :user => {
  401. :firstname => "Joe",
  402. :login => "root",
  403. :admin => 1,
  404. :group_ids => ['10'],
  405. :custom_field_values => {
  406. "4" => "0100562500"
  407. }
  408. }
  409. }
  410. )
  411. assert_redirected_to '/my/account'
  412. user = User.find(2)
  413. assert_equal "Joe", user.firstname
  414. assert_equal "jsmith", user.login
  415. assert_equal "0100562500", user.custom_value_for(4).value
  416. # ignored
  417. assert !user.admin?
  418. assert user.groups.empty?
  419. end
  420. def test_update_account_should_send_security_notification
  421. ActionMailer::Base.deliveries.clear
  422. put(
  423. :account,
  424. :params => {
  425. :user => {
  426. :mail => 'foobar@example.com'
  427. }
  428. }
  429. )
  430. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  431. assert_mail_body_match '0.0.0.0', mail
  432. assert_mail_body_match(
  433. I18n.t(:mail_body_security_notification_change_to,
  434. :field => I18n.t(:field_mail), :value => 'foobar@example.com'),
  435. mail
  436. )
  437. assert_select_email do
  438. assert_select 'a[href^=?]', 'http://localhost:3000/my/account', :text => 'My account'
  439. end
  440. # The old email address should be notified about the change for security purposes
  441. assert mail.to.include?(User.find(2).mail)
  442. assert mail.to.include?('foobar@example.com')
  443. end
  444. def test_my_account_notify_about_high_priority_issues_preference
  445. # normally, preference should be shown
  446. get :account
  447. assert_select 'label[for="pref_notify_about_high_priority_issues"]'
  448. # preference should be persisted
  449. put(
  450. :account,
  451. :params => {
  452. :pref => {
  453. notify_about_high_priority_issues: '1'
  454. }
  455. }
  456. )
  457. assert User.find(2).notify_about_high_priority_issues?
  458. # preference should be hidden if there aren't any priorities
  459. Issue.destroy_all
  460. IssuePriority.destroy_all
  461. get :account
  462. assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
  463. # preference should be hidden if there isn't a "high" priority
  464. a = IssuePriority.create! name: 'A'
  465. get :account
  466. assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
  467. # preference should be shown if there are at least two priorities (one low, one high)
  468. b = IssuePriority.create! name: 'B'
  469. get :account
  470. assert_select 'label[for="pref_notify_about_high_priority_issues"]'
  471. # preference should be hidden if the highest priority is the default one,
  472. # because that means that there is no "high" priority
  473. b.update! is_default: true
  474. get :account
  475. assert_select 'label[for="pref_notify_about_high_priority_issues"]', false
  476. end
  477. def test_my_account_should_show_destroy_link
  478. get :account
  479. assert_select 'a[href="/my/account/destroy"]'
  480. end
  481. def test_get_destroy_should_display_the_destroy_confirmation
  482. get :destroy
  483. assert_response :success
  484. assert_select 'form[action="/my/account/destroy"]' do
  485. assert_select 'input[name=confirm]'
  486. end
  487. end
  488. def test_post_destroy_without_confirmation_should_not_destroy_account
  489. assert_no_difference 'User.count' do
  490. post :destroy
  491. end
  492. assert_response :success
  493. end
  494. def test_post_destroy_without_confirmation_should_destroy_account
  495. assert_difference 'User.count', -1 do
  496. post(
  497. :destroy,
  498. :params => {
  499. :confirm => '1'
  500. }
  501. )
  502. end
  503. assert_redirected_to '/'
  504. assert_match /deleted/i, flash[:notice]
  505. end
  506. def test_post_destroy_with_unsubscribe_not_allowed_should_not_destroy_account
  507. User.any_instance.stubs(:own_account_deletable?).returns(false)
  508. assert_no_difference 'User.count' do
  509. post(
  510. :destroy,
  511. :params => {
  512. :confirm => '1'
  513. }
  514. )
  515. end
  516. assert_redirected_to '/my/account'
  517. end
  518. def test_change_password
  519. get :password
  520. assert_response :success
  521. assert_select 'input[type=password][name=password]'
  522. assert_select 'input[type=password][name=new_password]'
  523. assert_select 'input[type=password][name=new_password_confirmation]'
  524. end
  525. def test_update_password
  526. post(
  527. :password,
  528. :params => {
  529. :password => 'jsmith',
  530. :new_password => 'secret123',
  531. :new_password_confirmation => 'secret123'
  532. }
  533. )
  534. assert_redirected_to '/my/account'
  535. assert User.try_to_login('jsmith', 'secret123')
  536. end
  537. def test_update_password_with_non_matching_confirmation
  538. post(
  539. :password,
  540. :params => {
  541. :password => 'jsmith',
  542. :new_password => 'secret123',
  543. :new_password_confirmation => 'secret1234'
  544. }
  545. )
  546. assert_response :success
  547. assert_select_error /Password doesn.*t match confirmation/
  548. assert User.try_to_login('jsmith', 'jsmith')
  549. end
  550. def test_update_password_with_wrong_password
  551. # wrong password
  552. post(
  553. :password,
  554. :params => {
  555. :password => 'wrongpassword',
  556. :new_password => 'secret123',
  557. :new_password_confirmation => 'secret123'
  558. }
  559. )
  560. assert_response :success
  561. assert_equal 'Wrong password', flash[:error]
  562. assert User.try_to_login('jsmith', 'jsmith')
  563. end
  564. def test_change_password_should_redirect_if_user_cannot_change_its_password
  565. User.find(2).update_attribute(:auth_source_id, 1)
  566. get :password
  567. assert_not_nil flash[:error]
  568. assert_redirected_to '/my/account'
  569. end
  570. def test_update_password_should_send_security_notification
  571. ActionMailer::Base.deliveries.clear
  572. post(
  573. :password,
  574. :params => {
  575. :password => 'jsmith',
  576. :new_password => 'secret123',
  577. :new_password_confirmation => 'secret123'
  578. }
  579. )
  580. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  581. assert_mail_body_no_match 'secret123', mail # just to be sure: pw should never be sent!
  582. assert_select_email do
  583. assert_select 'a[href^=?]', 'http://localhost:3000/my/password', :text => 'Change password'
  584. end
  585. end
  586. def test_update_page_with_blank_preferences
  587. user = User.generate!(:language => 'en')
  588. @request.session[:user_id] = user.id
  589. post(
  590. :update_page,
  591. :params => {
  592. :settings => {
  593. 'issuesassignedtome' => {
  594. 'columns' => ['subject', 'due_date']
  595. }
  596. }
  597. },
  598. :xhr => true
  599. )
  600. assert_response :success
  601. assert_include '$("#block-issuesassignedtome").replaceWith(', response.body
  602. assert_include 'Due date', response.body
  603. assert_equal({:columns => ['subject', 'due_date']},
  604. user.reload.pref.my_page_settings('issuesassignedtome'))
  605. end
  606. def test_add_block
  607. post(
  608. :add_block,
  609. :params => {
  610. :block => 'issueswatched'
  611. }
  612. )
  613. assert_redirected_to '/my/page'
  614. assert User.find(2).pref[:my_page_layout]['top'].include?('issueswatched')
  615. end
  616. def test_add_block_xhr
  617. post(
  618. :add_block,
  619. :params => {
  620. :block => 'issueswatched'
  621. },
  622. :xhr => true
  623. )
  624. assert_response :success
  625. assert_include 'issueswatched', User.find(2).pref[:my_page_layout]['top']
  626. end
  627. def test_add_invalid_block_should_error
  628. post(
  629. :add_block,
  630. :params => {
  631. :block => 'invalid'
  632. }
  633. )
  634. assert_response 422
  635. end
  636. def test_remove_block
  637. post(
  638. :remove_block,
  639. :params => {
  640. :block => 'issuesassignedtome'
  641. }
  642. )
  643. assert_redirected_to '/my/page'
  644. assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome')
  645. end
  646. def test_remove_block_xhr
  647. post(
  648. :remove_block,
  649. :params => {
  650. :block => 'issuesassignedtome'
  651. },
  652. :xhr => true
  653. )
  654. assert_response :success
  655. assert_include '$("#block-issuesassignedtome").remove();', response.body
  656. assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome')
  657. end
  658. def test_order_blocks
  659. pref = User.find(2).pref
  660. pref.my_page_layout = {'left' => ['news', 'calendar', 'documents']}
  661. pref.save!
  662. post(
  663. :order_blocks,
  664. :params => {
  665. :group => 'left',
  666. :blocks => ['documents', 'calendar', 'news']
  667. },
  668. :xhr => true
  669. )
  670. assert_response :success
  671. assert_equal ['documents', 'calendar', 'news'], User.find(2).pref.my_page_layout['left']
  672. end
  673. def test_move_block
  674. pref = User.find(2).pref
  675. pref.my_page_layout = {'left' => ['news', 'documents'], 'right' => ['calendar']}
  676. pref.save!
  677. post(
  678. :order_blocks,
  679. :params => {
  680. :group => 'left',
  681. :blocks => ['news', 'calendar', 'documents']
  682. },
  683. :xhr => true
  684. )
  685. assert_response :success
  686. assert_equal({'left' => ['news', 'calendar', 'documents'],
  687. 'right' => []},
  688. User.find(2).pref.my_page_layout)
  689. end
  690. def test_reset_atom_key_with_existing_key
  691. @previous_token_value = User.find(2).atom_key # Will generate one if it's missing
  692. post :reset_atom_key
  693. assert_not_equal @previous_token_value, User.find(2).atom_key
  694. assert User.find(2).atom_token
  695. assert_match /reset/, flash[:notice]
  696. assert_redirected_to '/my/account'
  697. end
  698. def test_reset_atom_key_without_existing_key
  699. Token.delete_all
  700. assert_nil User.find(2).atom_token
  701. post :reset_atom_key
  702. assert User.find(2).atom_token
  703. assert_match /reset/, flash[:notice]
  704. assert_redirected_to '/my/account'
  705. end
  706. def test_show_api_key
  707. get :show_api_key
  708. assert_response :success
  709. assert_select 'pre', User.find(2).api_key
  710. end
  711. def test_reset_api_key_with_existing_key
  712. @previous_token_value = User.find(2).api_key # Will generate one if it's missing
  713. post :reset_api_key
  714. assert_not_equal @previous_token_value, User.find(2).api_key
  715. assert User.find(2).api_token
  716. assert_match /reset/, flash[:notice]
  717. assert_redirected_to '/my/account'
  718. end
  719. def test_reset_api_key_without_existing_key
  720. assert_nil User.find(2).api_token
  721. post :reset_api_key
  722. assert User.find(2).api_token
  723. assert_match /reset/, flash[:notice]
  724. assert_redirected_to '/my/account'
  725. end
  726. end