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.

user_test.rb 33KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 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 UserTest < ActiveSupport::TestCase
  19. fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources,
  20. :trackers, :issue_statuses,
  21. :projects_trackers,
  22. :watchers,
  23. :issue_categories, :enumerations, :issues,
  24. :journals, :journal_details,
  25. :groups_users,
  26. :enabled_modules,
  27. :workflows
  28. def setup
  29. @admin = User.find(1)
  30. @jsmith = User.find(2)
  31. @dlopper = User.find(3)
  32. end
  33. test 'object_daddy creation' do
  34. User.generate_with_protected!(:firstname => 'Testing connection')
  35. User.generate_with_protected!(:firstname => 'Testing connection')
  36. assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
  37. end
  38. def test_truth
  39. assert_kind_of User, @jsmith
  40. end
  41. def test_mail_should_be_stripped
  42. u = User.new
  43. u.mail = " foo@bar.com "
  44. assert_equal "foo@bar.com", u.mail
  45. end
  46. def test_mail_validation
  47. u = User.new
  48. u.mail = ''
  49. assert !u.valid?
  50. assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail]
  51. end
  52. def test_login_length_validation
  53. user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
  54. user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
  55. assert !user.valid?
  56. user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
  57. assert user.valid?
  58. assert user.save
  59. end
  60. def test_create
  61. user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
  62. user.login = "jsmith"
  63. user.password, user.password_confirmation = "password", "password"
  64. # login uniqueness
  65. assert !user.save
  66. assert_equal 1, user.errors.count
  67. user.login = "newuser"
  68. user.password, user.password_confirmation = "passwd", "password"
  69. # password confirmation
  70. assert !user.save
  71. assert_equal 1, user.errors.count
  72. user.password, user.password_confirmation = "password", "password"
  73. assert user.save
  74. end
  75. context "User#before_create" do
  76. should "set the mail_notification to the default Setting" do
  77. @user1 = User.generate_with_protected!
  78. assert_equal 'only_my_events', @user1.mail_notification
  79. with_settings :default_notification_option => 'all' do
  80. @user2 = User.generate_with_protected!
  81. assert_equal 'all', @user2.mail_notification
  82. end
  83. end
  84. end
  85. context "User.login" do
  86. should "be case-insensitive." do
  87. u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
  88. u.login = 'newuser'
  89. u.password, u.password_confirmation = "password", "password"
  90. assert u.save
  91. u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
  92. u.login = 'NewUser'
  93. u.password, u.password_confirmation = "password", "password"
  94. assert !u.save
  95. assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login]
  96. end
  97. end
  98. def test_mail_uniqueness_should_not_be_case_sensitive
  99. u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
  100. u.login = 'newuser1'
  101. u.password, u.password_confirmation = "password", "password"
  102. assert u.save
  103. u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
  104. u.login = 'newuser2'
  105. u.password, u.password_confirmation = "password", "password"
  106. assert !u.save
  107. assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail]
  108. end
  109. def test_update
  110. assert_equal "admin", @admin.login
  111. @admin.login = "john"
  112. assert @admin.save, @admin.errors.full_messages.join("; ")
  113. @admin.reload
  114. assert_equal "john", @admin.login
  115. end
  116. def test_update_should_not_fail_for_legacy_user_with_different_case_logins
  117. u1 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser1@somenet.foo")
  118. u1.login = 'newuser1'
  119. assert u1.save
  120. u2 = User.new(:firstname => "new", :lastname => "user", :mail => "newuser2@somenet.foo")
  121. u2.login = 'newuser1'
  122. assert u2.save(false)
  123. user = User.find(u2.id)
  124. user.firstname = "firstname"
  125. assert user.save, "Save failed"
  126. end
  127. def test_destroy_should_delete_members_and_roles
  128. members = Member.find_all_by_user_id(2)
  129. ms = members.size
  130. rs = members.collect(&:roles).flatten.size
  131. assert_difference 'Member.count', - ms do
  132. assert_difference 'MemberRole.count', - rs do
  133. User.find(2).destroy
  134. end
  135. end
  136. assert_nil User.find_by_id(2)
  137. assert Member.find_all_by_user_id(2).empty?
  138. end
  139. def test_destroy_should_update_attachments
  140. attachment = Attachment.create!(:container => Project.find(1),
  141. :file => uploaded_test_file("testfile.txt", "text/plain"),
  142. :author_id => 2)
  143. User.find(2).destroy
  144. assert_nil User.find_by_id(2)
  145. assert_equal User.anonymous, attachment.reload.author
  146. end
  147. def test_destroy_should_update_comments
  148. comment = Comment.create!(
  149. :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
  150. :author => User.find(2),
  151. :comments => 'foo'
  152. )
  153. User.find(2).destroy
  154. assert_nil User.find_by_id(2)
  155. assert_equal User.anonymous, comment.reload.author
  156. end
  157. def test_destroy_should_update_issues
  158. issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
  159. User.find(2).destroy
  160. assert_nil User.find_by_id(2)
  161. assert_equal User.anonymous, issue.reload.author
  162. end
  163. def test_destroy_should_unassign_issues
  164. issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
  165. User.find(2).destroy
  166. assert_nil User.find_by_id(2)
  167. assert_nil issue.reload.assigned_to
  168. end
  169. def test_destroy_should_update_journals
  170. issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
  171. issue.init_journal(User.find(2), "update")
  172. issue.save!
  173. User.find(2).destroy
  174. assert_nil User.find_by_id(2)
  175. assert_equal User.anonymous, issue.journals.first.reload.user
  176. end
  177. def test_destroy_should_update_journal_details_old_value
  178. issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
  179. issue.init_journal(User.find(1), "update")
  180. issue.assigned_to_id = nil
  181. assert_difference 'JournalDetail.count' do
  182. issue.save!
  183. end
  184. journal_detail = JournalDetail.first(:order => 'id DESC')
  185. assert_equal '2', journal_detail.old_value
  186. User.find(2).destroy
  187. assert_nil User.find_by_id(2)
  188. assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
  189. end
  190. def test_destroy_should_update_journal_details_value
  191. issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
  192. issue.init_journal(User.find(1), "update")
  193. issue.assigned_to_id = 2
  194. assert_difference 'JournalDetail.count' do
  195. issue.save!
  196. end
  197. journal_detail = JournalDetail.first(:order => 'id DESC')
  198. assert_equal '2', journal_detail.value
  199. User.find(2).destroy
  200. assert_nil User.find_by_id(2)
  201. assert_equal User.anonymous.id.to_s, journal_detail.reload.value
  202. end
  203. def test_destroy_should_update_messages
  204. board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
  205. message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
  206. User.find(2).destroy
  207. assert_nil User.find_by_id(2)
  208. assert_equal User.anonymous, message.reload.author
  209. end
  210. def test_destroy_should_update_news
  211. news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
  212. User.find(2).destroy
  213. assert_nil User.find_by_id(2)
  214. assert_equal User.anonymous, news.reload.author
  215. end
  216. def test_destroy_should_delete_private_queries
  217. query = Query.new(:name => 'foo', :is_public => false)
  218. query.project_id = 1
  219. query.user_id = 2
  220. query.save!
  221. User.find(2).destroy
  222. assert_nil User.find_by_id(2)
  223. assert_nil Query.find_by_id(query.id)
  224. end
  225. def test_destroy_should_update_public_queries
  226. query = Query.new(:name => 'foo', :is_public => true)
  227. query.project_id = 1
  228. query.user_id = 2
  229. query.save!
  230. User.find(2).destroy
  231. assert_nil User.find_by_id(2)
  232. assert_equal User.anonymous, query.reload.user
  233. end
  234. def test_destroy_should_update_time_entries
  235. entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
  236. entry.project_id = 1
  237. entry.user_id = 2
  238. entry.save!
  239. User.find(2).destroy
  240. assert_nil User.find_by_id(2)
  241. assert_equal User.anonymous, entry.reload.user
  242. end
  243. def test_destroy_should_delete_tokens
  244. token = Token.create!(:user_id => 2, :value => 'foo')
  245. User.find(2).destroy
  246. assert_nil User.find_by_id(2)
  247. assert_nil Token.find_by_id(token.id)
  248. end
  249. def test_destroy_should_delete_watchers
  250. issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
  251. watcher = Watcher.create!(:user_id => 2, :watchable => issue)
  252. User.find(2).destroy
  253. assert_nil User.find_by_id(2)
  254. assert_nil Watcher.find_by_id(watcher.id)
  255. end
  256. def test_destroy_should_update_wiki_contents
  257. wiki_content = WikiContent.create!(
  258. :text => 'foo',
  259. :author_id => 2,
  260. :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
  261. )
  262. wiki_content.text = 'bar'
  263. assert_difference 'WikiContent::Version.count' do
  264. wiki_content.save!
  265. end
  266. User.find(2).destroy
  267. assert_nil User.find_by_id(2)
  268. assert_equal User.anonymous, wiki_content.reload.author
  269. wiki_content.versions.each do |version|
  270. assert_equal User.anonymous, version.reload.author
  271. end
  272. end
  273. def test_destroy_should_nullify_issue_categories
  274. category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
  275. User.find(2).destroy
  276. assert_nil User.find_by_id(2)
  277. assert_nil category.reload.assigned_to_id
  278. end
  279. def test_destroy_should_nullify_changesets
  280. changeset = Changeset.create!(
  281. :repository => Repository::Subversion.generate!(
  282. :project_id => 1
  283. ),
  284. :revision => '12',
  285. :committed_on => Time.now,
  286. :committer => 'jsmith'
  287. )
  288. assert_equal 2, changeset.user_id
  289. User.find(2).destroy
  290. assert_nil User.find_by_id(2)
  291. assert_nil changeset.reload.user_id
  292. end
  293. def test_anonymous_user_should_not_be_destroyable
  294. assert_no_difference 'User.count' do
  295. assert_equal false, User.anonymous.destroy
  296. end
  297. end
  298. def test_validate_login_presence
  299. @admin.login = ""
  300. assert !@admin.save
  301. assert_equal 1, @admin.errors.count
  302. end
  303. def test_validate_mail_notification_inclusion
  304. u = User.new
  305. u.mail_notification = 'foo'
  306. u.save
  307. assert_not_nil u.errors[:mail_notification]
  308. end
  309. context "User#try_to_login" do
  310. should "fall-back to case-insensitive if user login is not found as-typed." do
  311. user = User.try_to_login("AdMin", "admin")
  312. assert_kind_of User, user
  313. assert_equal "admin", user.login
  314. end
  315. should "select the exact matching user first" do
  316. case_sensitive_user = User.generate_with_protected!(
  317. :login => 'changed', :password => 'admin',
  318. :password_confirmation => 'admin')
  319. # bypass validations to make it appear like existing data
  320. case_sensitive_user.update_attribute(:login, 'ADMIN')
  321. user = User.try_to_login("ADMIN", "admin")
  322. assert_kind_of User, user
  323. assert_equal "ADMIN", user.login
  324. end
  325. end
  326. def test_password
  327. user = User.try_to_login("admin", "admin")
  328. assert_kind_of User, user
  329. assert_equal "admin", user.login
  330. user.password = "hello"
  331. assert user.save
  332. user = User.try_to_login("admin", "hello")
  333. assert_kind_of User, user
  334. assert_equal "admin", user.login
  335. end
  336. def test_validate_password_length
  337. with_settings :password_min_length => '100' do
  338. user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
  339. user.login = "newuser100"
  340. user.password, user.password_confirmation = "password100", "password100"
  341. assert !user.save
  342. assert_equal 1, user.errors.count
  343. end
  344. end
  345. def test_name_format
  346. assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
  347. with_settings :user_format => :firstname_lastname do
  348. assert_equal 'John Smith', @jsmith.reload.name
  349. end
  350. with_settings :user_format => :username do
  351. assert_equal 'jsmith', @jsmith.reload.name
  352. end
  353. end
  354. def test_fields_for_order_statement_should_return_fields_according_user_format_setting
  355. with_settings :user_format => 'lastname_coma_firstname' do
  356. assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement
  357. end
  358. end
  359. def test_fields_for_order_statement_width_table_name_should_prepend_table_name
  360. with_settings :user_format => 'lastname_firstname' do
  361. assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], User.fields_for_order_statement('authors')
  362. end
  363. end
  364. def test_fields_for_order_statement_with_blank_format_should_return_default
  365. with_settings :user_format => '' do
  366. assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
  367. end
  368. end
  369. def test_fields_for_order_statement_with_invalid_format_should_return_default
  370. with_settings :user_format => 'foo' do
  371. assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
  372. end
  373. end
  374. def test_lock
  375. user = User.try_to_login("jsmith", "jsmith")
  376. assert_equal @jsmith, user
  377. @jsmith.status = User::STATUS_LOCKED
  378. assert @jsmith.save
  379. user = User.try_to_login("jsmith", "jsmith")
  380. assert_equal nil, user
  381. end
  382. context ".try_to_login" do
  383. context "with good credentials" do
  384. should "return the user" do
  385. user = User.try_to_login("admin", "admin")
  386. assert_kind_of User, user
  387. assert_equal "admin", user.login
  388. end
  389. end
  390. context "with wrong credentials" do
  391. should "return nil" do
  392. assert_nil User.try_to_login("admin", "foo")
  393. end
  394. end
  395. end
  396. if ldap_configured?
  397. context "#try_to_login using LDAP" do
  398. context "with failed connection to the LDAP server" do
  399. should "return nil" do
  400. @auth_source = AuthSourceLdap.find(1)
  401. AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
  402. assert_equal nil, User.try_to_login('edavis', 'wrong')
  403. end
  404. end
  405. context "with an unsuccessful authentication" do
  406. should "return nil" do
  407. assert_equal nil, User.try_to_login('edavis', 'wrong')
  408. end
  409. end
  410. context "binding with user's account" do
  411. setup do
  412. @auth_source = AuthSourceLdap.find(1)
  413. @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
  414. @auth_source.account_password = ''
  415. @auth_source.save!
  416. @ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1)
  417. @ldap_user.login = 'example1'
  418. @ldap_user.save!
  419. end
  420. context "with a successful authentication" do
  421. should "return the user" do
  422. assert_equal @ldap_user, User.try_to_login('example1', '123456')
  423. end
  424. end
  425. context "with an unsuccessful authentication" do
  426. should "return nil" do
  427. assert_nil User.try_to_login('example1', '11111')
  428. end
  429. end
  430. end
  431. context "on the fly registration" do
  432. setup do
  433. @auth_source = AuthSourceLdap.find(1)
  434. @auth_source.update_attribute :onthefly_register, true
  435. end
  436. context "with a successful authentication" do
  437. should "create a new user account if it doesn't exist" do
  438. assert_difference('User.count') do
  439. user = User.try_to_login('edavis', '123456')
  440. assert !user.admin?
  441. end
  442. end
  443. should "retrieve existing user" do
  444. user = User.try_to_login('edavis', '123456')
  445. user.admin = true
  446. user.save!
  447. assert_no_difference('User.count') do
  448. user = User.try_to_login('edavis', '123456')
  449. assert user.admin?
  450. end
  451. end
  452. end
  453. context "binding with user's account" do
  454. setup do
  455. @auth_source = AuthSourceLdap.find(1)
  456. @auth_source.account = "uid=$login,ou=Person,dc=redmine,dc=org"
  457. @auth_source.account_password = ''
  458. @auth_source.save!
  459. end
  460. context "with a successful authentication" do
  461. should "create a new user account if it doesn't exist" do
  462. assert_difference('User.count') do
  463. user = User.try_to_login('example1', '123456')
  464. assert_kind_of User, user
  465. end
  466. end
  467. end
  468. context "with an unsuccessful authentication" do
  469. should "return nil" do
  470. assert_nil User.try_to_login('example1', '11111')
  471. end
  472. end
  473. end
  474. end
  475. end
  476. else
  477. puts "Skipping LDAP tests."
  478. end
  479. def test_create_anonymous
  480. AnonymousUser.delete_all
  481. anon = User.anonymous
  482. assert !anon.new_record?
  483. assert_kind_of AnonymousUser, anon
  484. end
  485. def test_ensure_single_anonymous_user
  486. AnonymousUser.delete_all
  487. anon1 = User.anonymous
  488. assert !anon1.new_record?
  489. assert_kind_of AnonymousUser, anon1
  490. anon2 = AnonymousUser.create(
  491. :lastname => 'Anonymous', :firstname => '',
  492. :mail => '', :login => '', :status => 0)
  493. assert_equal 1, anon2.errors.count
  494. end
  495. def test_rss_key
  496. assert_nil @jsmith.rss_token
  497. key = @jsmith.rss_key
  498. assert_equal 40, key.length
  499. @jsmith.reload
  500. assert_equal key, @jsmith.rss_key
  501. end
  502. def test_rss_key_should_not_be_generated_twice
  503. assert_difference 'Token.count', 1 do
  504. key1 = @jsmith.rss_key
  505. key2 = @jsmith.rss_key
  506. assert_equal key1, key2
  507. end
  508. end
  509. def test_api_key_should_not_be_generated_twice
  510. assert_difference 'Token.count', 1 do
  511. key1 = @jsmith.api_key
  512. key2 = @jsmith.api_key
  513. assert_equal key1, key2
  514. end
  515. end
  516. context "User#api_key" do
  517. should "generate a new one if the user doesn't have one" do
  518. user = User.generate_with_protected!(:api_token => nil)
  519. assert_nil user.api_token
  520. key = user.api_key
  521. assert_equal 40, key.length
  522. user.reload
  523. assert_equal key, user.api_key
  524. end
  525. should "return the existing api token value" do
  526. user = User.generate_with_protected!
  527. token = Token.generate!(:action => 'api')
  528. user.api_token = token
  529. assert user.save
  530. assert_equal token.value, user.api_key
  531. end
  532. end
  533. context "User#find_by_api_key" do
  534. should "return nil if no matching key is found" do
  535. assert_nil User.find_by_api_key('zzzzzzzzz')
  536. end
  537. should "return nil if the key is found for an inactive user" do
  538. user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
  539. token = Token.generate!(:action => 'api')
  540. user.api_token = token
  541. user.save
  542. assert_nil User.find_by_api_key(token.value)
  543. end
  544. should "return the user if the key is found for an active user" do
  545. user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
  546. token = Token.generate!(:action => 'api')
  547. user.api_token = token
  548. user.save
  549. assert_equal user, User.find_by_api_key(token.value)
  550. end
  551. end
  552. def test_default_admin_account_changed_should_return_false_if_account_was_not_changed
  553. user = User.find_by_login("admin")
  554. user.password = "admin"
  555. user.save!
  556. assert_equal false, User.default_admin_account_changed?
  557. end
  558. def test_default_admin_account_changed_should_return_true_if_password_was_changed
  559. user = User.find_by_login("admin")
  560. user.password = "newpassword"
  561. user.save!
  562. assert_equal true, User.default_admin_account_changed?
  563. end
  564. def test_default_admin_account_changed_should_return_true_if_account_is_disabled
  565. user = User.find_by_login("admin")
  566. user.password = "admin"
  567. user.status = User::STATUS_LOCKED
  568. user.save!
  569. assert_equal true, User.default_admin_account_changed?
  570. end
  571. def test_default_admin_account_changed_should_return_true_if_account_does_not_exist
  572. user = User.find_by_login("admin")
  573. user.destroy
  574. assert_equal true, User.default_admin_account_changed?
  575. end
  576. def test_roles_for_project
  577. # user with a role
  578. roles = @jsmith.roles_for_project(Project.find(1))
  579. assert_kind_of Role, roles.first
  580. assert_equal "Manager", roles.first.name
  581. # user with no role
  582. assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
  583. end
  584. def test_projects_by_role_for_user_with_role
  585. user = User.find(2)
  586. assert_kind_of Hash, user.projects_by_role
  587. assert_equal 2, user.projects_by_role.size
  588. assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
  589. assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
  590. end
  591. def test_projects_by_role_for_user_with_no_role
  592. user = User.generate!
  593. assert_equal({}, user.projects_by_role)
  594. end
  595. def test_projects_by_role_for_anonymous
  596. assert_equal({}, User.anonymous.projects_by_role)
  597. end
  598. def test_valid_notification_options
  599. # without memberships
  600. assert_equal 5, User.find(7).valid_notification_options.size
  601. # with memberships
  602. assert_equal 6, User.find(2).valid_notification_options.size
  603. end
  604. def test_valid_notification_options_class_method
  605. assert_equal 5, User.valid_notification_options.size
  606. assert_equal 5, User.valid_notification_options(User.find(7)).size
  607. assert_equal 6, User.valid_notification_options(User.find(2)).size
  608. end
  609. def test_mail_notification_all
  610. @jsmith.mail_notification = 'all'
  611. @jsmith.notified_project_ids = []
  612. @jsmith.save
  613. @jsmith.reload
  614. assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
  615. end
  616. def test_mail_notification_selected
  617. @jsmith.mail_notification = 'selected'
  618. @jsmith.notified_project_ids = [1]
  619. @jsmith.save
  620. @jsmith.reload
  621. assert Project.find(1).recipients.include?(@jsmith.mail)
  622. end
  623. def test_mail_notification_only_my_events
  624. @jsmith.mail_notification = 'only_my_events'
  625. @jsmith.notified_project_ids = []
  626. @jsmith.save
  627. @jsmith.reload
  628. assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
  629. end
  630. def test_comments_sorting_preference
  631. assert !@jsmith.wants_comments_in_reverse_order?
  632. @jsmith.pref.comments_sorting = 'asc'
  633. assert !@jsmith.wants_comments_in_reverse_order?
  634. @jsmith.pref.comments_sorting = 'desc'
  635. assert @jsmith.wants_comments_in_reverse_order?
  636. end
  637. def test_find_by_mail_should_be_case_insensitive
  638. u = User.find_by_mail('JSmith@somenet.foo')
  639. assert_not_nil u
  640. assert_equal 'jsmith@somenet.foo', u.mail
  641. end
  642. def test_random_password
  643. u = User.new
  644. u.random_password
  645. assert !u.password.blank?
  646. assert !u.password_confirmation.blank?
  647. end
  648. context "#change_password_allowed?" do
  649. should "be allowed if no auth source is set" do
  650. user = User.generate_with_protected!
  651. assert user.change_password_allowed?
  652. end
  653. should "delegate to the auth source" do
  654. user = User.generate_with_protected!
  655. allowed_auth_source = AuthSource.generate!
  656. def allowed_auth_source.allow_password_changes?; true; end
  657. denied_auth_source = AuthSource.generate!
  658. def denied_auth_source.allow_password_changes?; false; end
  659. assert user.change_password_allowed?
  660. user.auth_source = allowed_auth_source
  661. assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
  662. user.auth_source = denied_auth_source
  663. assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
  664. end
  665. end
  666. def test_own_account_deletable_should_be_true_with_unsubscrive_enabled
  667. with_settings :unsubscribe => '1' do
  668. assert_equal true, User.find(2).own_account_deletable?
  669. end
  670. end
  671. def test_own_account_deletable_should_be_false_with_unsubscrive_disabled
  672. with_settings :unsubscribe => '0' do
  673. assert_equal false, User.find(2).own_account_deletable?
  674. end
  675. end
  676. def test_own_account_deletable_should_be_false_for_a_single_admin
  677. User.delete_all(["admin = ? AND id <> ?", true, 1])
  678. with_settings :unsubscribe => '1' do
  679. assert_equal false, User.find(1).own_account_deletable?
  680. end
  681. end
  682. def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists
  683. User.generate_with_protected(:admin => true)
  684. with_settings :unsubscribe => '1' do
  685. assert_equal true, User.find(1).own_account_deletable?
  686. end
  687. end
  688. context "#allowed_to?" do
  689. context "with a unique project" do
  690. should "return false if project is archived" do
  691. project = Project.find(1)
  692. Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
  693. assert ! @admin.allowed_to?(:view_issues, Project.find(1))
  694. end
  695. should "return false if related module is disabled" do
  696. project = Project.find(1)
  697. project.enabled_module_names = ["issue_tracking"]
  698. assert @admin.allowed_to?(:add_issues, project)
  699. assert ! @admin.allowed_to?(:view_wiki_pages, project)
  700. end
  701. should "authorize nearly everything for admin users" do
  702. project = Project.find(1)
  703. assert ! @admin.member_of?(project)
  704. %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p|
  705. assert @admin.allowed_to?(p.to_sym, project)
  706. end
  707. end
  708. should "authorize normal users depending on their roles" do
  709. project = Project.find(1)
  710. assert @jsmith.allowed_to?(:delete_messages, project) #Manager
  711. assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper
  712. end
  713. end
  714. context "with multiple projects" do
  715. should "return false if array is empty" do
  716. assert ! @admin.allowed_to?(:view_project, [])
  717. end
  718. should "return true only if user has permission on all these projects" do
  719. assert @admin.allowed_to?(:view_project, Project.all)
  720. assert ! @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
  721. assert @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
  722. assert ! @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
  723. end
  724. should "behave correctly with arrays of 1 project" do
  725. assert ! User.anonymous.allowed_to?(:delete_issues, [Project.first])
  726. end
  727. end
  728. context "with options[:global]" do
  729. should "authorize if user has at least one role that has this permission" do
  730. @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
  731. @anonymous = User.find(6)
  732. assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
  733. assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
  734. assert @dlopper2.allowed_to?(:add_issues, nil, :global => true)
  735. assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true)
  736. assert @anonymous.allowed_to?(:view_issues, nil, :global => true)
  737. end
  738. end
  739. end
  740. context "User#notify_about?" do
  741. context "Issues" do
  742. setup do
  743. @project = Project.find(1)
  744. @author = User.generate_with_protected!
  745. @assignee = User.generate_with_protected!
  746. @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
  747. end
  748. should "be true for a user with :all" do
  749. @author.update_attribute(:mail_notification, 'all')
  750. assert @author.notify_about?(@issue)
  751. end
  752. should "be false for a user with :none" do
  753. @author.update_attribute(:mail_notification, 'none')
  754. assert ! @author.notify_about?(@issue)
  755. end
  756. should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
  757. @user = User.generate_with_protected!(:mail_notification => 'only_my_events')
  758. Member.create!(:user => @user, :project => @project, :role_ids => [1])
  759. assert ! @user.notify_about?(@issue)
  760. end
  761. should "be true for a user with :only_my_events and is the author" do
  762. @author.update_attribute(:mail_notification, 'only_my_events')
  763. assert @author.notify_about?(@issue)
  764. end
  765. should "be true for a user with :only_my_events and is the assignee" do
  766. @assignee.update_attribute(:mail_notification, 'only_my_events')
  767. assert @assignee.notify_about?(@issue)
  768. end
  769. should "be true for a user with :only_assigned and is the assignee" do
  770. @assignee.update_attribute(:mail_notification, 'only_assigned')
  771. assert @assignee.notify_about?(@issue)
  772. end
  773. should "be false for a user with :only_assigned and is not the assignee" do
  774. @author.update_attribute(:mail_notification, 'only_assigned')
  775. assert ! @author.notify_about?(@issue)
  776. end
  777. should "be true for a user with :only_owner and is the author" do
  778. @author.update_attribute(:mail_notification, 'only_owner')
  779. assert @author.notify_about?(@issue)
  780. end
  781. should "be false for a user with :only_owner and is not the author" do
  782. @assignee.update_attribute(:mail_notification, 'only_owner')
  783. assert ! @assignee.notify_about?(@issue)
  784. end
  785. should "be true for a user with :selected and is the author" do
  786. @author.update_attribute(:mail_notification, 'selected')
  787. assert @author.notify_about?(@issue)
  788. end
  789. should "be true for a user with :selected and is the assignee" do
  790. @assignee.update_attribute(:mail_notification, 'selected')
  791. assert @assignee.notify_about?(@issue)
  792. end
  793. should "be false for a user with :selected and is not the author or assignee" do
  794. @user = User.generate_with_protected!(:mail_notification => 'selected')
  795. Member.create!(:user => @user, :project => @project, :role_ids => [1])
  796. assert ! @user.notify_about?(@issue)
  797. end
  798. end
  799. context "other events" do
  800. should 'be added and tested'
  801. end
  802. end
  803. def test_salt_unsalted_passwords
  804. # Restore a user with an unsalted password
  805. user = User.find(1)
  806. user.salt = nil
  807. user.hashed_password = User.hash_password("unsalted")
  808. user.save!
  809. User.salt_unsalted_passwords!
  810. user.reload
  811. # Salt added
  812. assert !user.salt.blank?
  813. # Password still valid
  814. assert user.check_password?("unsalted")
  815. assert_equal user, User.try_to_login(user.login, "unsalted")
  816. end
  817. if Object.const_defined?(:OpenID)
  818. def test_setting_identity_url
  819. normalized_open_id_url = 'http://example.com/'
  820. u = User.new( :identity_url => 'http://example.com/' )
  821. assert_equal normalized_open_id_url, u.identity_url
  822. end
  823. def test_setting_identity_url_without_trailing_slash
  824. normalized_open_id_url = 'http://example.com/'
  825. u = User.new( :identity_url => 'http://example.com' )
  826. assert_equal normalized_open_id_url, u.identity_url
  827. end
  828. def test_setting_identity_url_without_protocol
  829. normalized_open_id_url = 'http://example.com/'
  830. u = User.new( :identity_url => 'example.com' )
  831. assert_equal normalized_open_id_url, u.identity_url
  832. end
  833. def test_setting_blank_identity_url
  834. u = User.new( :identity_url => 'example.com' )
  835. u.identity_url = ''
  836. assert u.identity_url.blank?
  837. end
  838. def test_setting_invalid_identity_url
  839. u = User.new( :identity_url => 'this is not an openid url' )
  840. assert u.identity_url.blank?
  841. end
  842. else
  843. puts "Skipping openid tests."
  844. end
  845. end