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_import_test.rb 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 UserImportTest < ActiveSupport::TestCase
  20. fixtures :users, :auth_sources, :custom_fields
  21. include Redmine::I18n
  22. def setup
  23. set_language_if_valid 'en'
  24. User.current = nil
  25. end
  26. def test_authorized
  27. assert UserImport.authorized?(User.find(1)) # admins
  28. assert !UserImport.authorized?(User.find(2)) # dose not admin
  29. assert !UserImport.authorized?(User.find(6)) # dows not admin
  30. end
  31. def test_maps_login
  32. import = generate_import_with_mapping
  33. first, second, third = new_records(User, 3) {import.run}
  34. assert_equal 'user1', first.login
  35. assert_equal 'user2', second.login
  36. assert_equal 'user3', third.login
  37. end
  38. def test_maps_firstname
  39. import = generate_import_with_mapping
  40. first, second, third = new_records(User, 3) {import.run}
  41. assert_equal 'One', first.firstname
  42. assert_equal 'Two', second.firstname
  43. assert_equal 'Three', third.firstname
  44. end
  45. def test_maps_lastname
  46. import = generate_import_with_mapping
  47. first, second, third = new_records(User, 3) {import.run}
  48. assert_equal 'CSV', first.lastname
  49. assert_equal 'Import', second.lastname
  50. assert_equal 'User', third.lastname
  51. end
  52. def test_maps_mail
  53. import = generate_import_with_mapping
  54. first, second, third = new_records(User, 3) {import.run}
  55. assert_equal 'user1@somenet.foo', first.mail
  56. assert_equal 'user2@somenet.foo', second.mail
  57. assert_equal 'user3@somenet.foo', third.mail
  58. end
  59. def test_maps_language
  60. default_language = 'fr'
  61. with_settings :default_language => default_language do
  62. import = generate_import_with_mapping
  63. first, second, third = new_records(User, 3) {import.run}
  64. assert_equal 'en', first.language
  65. assert_equal 'ja', second.language
  66. assert_equal default_language, third.language
  67. end
  68. end
  69. def test_maps_admin
  70. import = generate_import_with_mapping
  71. first, second, third = new_records(User, 3) {import.run}
  72. assert first.admin?
  73. assert_not second.admin?
  74. assert_not third.admin?
  75. end
  76. def test_maps_auth_information
  77. import = generate_import_with_mapping
  78. first, second, third = new_records(User, 3) {import.run}
  79. # use password
  80. assert User.try_to_login(first.login, 'password', false)
  81. assert User.try_to_login(second.login, 'password', false)
  82. # use auth_source
  83. assert_nil first.auth_source
  84. assert_nil second.auth_source
  85. assert third.auth_source
  86. assert_equal 'LDAP test server', third.auth_source.name
  87. AuthSourceLdap.any_instance.expects(:authenticate).with(third.login, 'ldapassword').returns(true)
  88. assert User.try_to_login(third.login, 'ldapassword', false)
  89. end
  90. def test_map_must_change_password
  91. import = generate_import_with_mapping
  92. first, second, third = new_records(User, 3) {import.run}
  93. assert first.must_change_password?
  94. assert_not second.must_change_password?
  95. assert_not third.must_change_password?
  96. end
  97. def test_maps_status
  98. import = generate_import_with_mapping
  99. first, second, third = new_records(User, 3) {import.run}
  100. assert first.active?
  101. assert second.locked?
  102. assert third.registered?
  103. end
  104. def test_maps_custom_fields
  105. phone_number_cf = UserCustomField.find(4)
  106. import = generate_import_with_mapping
  107. import.mapping["cf_#{phone_number_cf.id}"] = '11'
  108. import.save!
  109. first, second, third = new_records(User, 3) {import.run}
  110. assert_equal '000-1111-2222', first.custom_field_value(phone_number_cf)
  111. assert_equal '333-4444-5555', second.custom_field_value(phone_number_cf)
  112. assert_equal '666-7777-8888', third.custom_field_value(phone_number_cf)
  113. end
  114. def test_deliver_account_information
  115. import = generate_import_with_mapping
  116. import.settings['notifications'] = '1'
  117. %w(admin language auth_source).each do |key|
  118. import.settings['mapping'].delete(key)
  119. end
  120. import.save!
  121. ActionMailer::Base.deliveries.clear
  122. first, = new_records(User, 3){import.run}
  123. assert_equal 3, ActionMailer::Base.deliveries.size
  124. mail = ActionMailer::Base.deliveries.first
  125. assert_equal 'Your Redmine account activation', mail.subject
  126. assert_equal 'user1', first.login
  127. assert_mail_body_match "Login: #{first.login}", mail
  128. end
  129. protected
  130. def generate_import(fixture_name='import_users.csv')
  131. import = UserImport.new
  132. import.user_id = 1
  133. import.file = uploaded_test_file(fixture_name, 'text/csv')
  134. import.save!
  135. import
  136. end
  137. def generate_import_with_mapping(fixture_name='import_users.csv')
  138. import = generate_import(fixture_name)
  139. import.settings = {
  140. 'separator' => ';', 'wrapper' => '"', 'encoding' => 'UTF-8',
  141. 'mapping' => {
  142. 'login' => '1',
  143. 'firstname' => '2',
  144. 'lastname' => '3',
  145. 'mail' => '4',
  146. 'language' => '5',
  147. 'admin' => '6',
  148. 'auth_source' => '7',
  149. 'password' => '8',
  150. 'must_change_passwd' => '9',
  151. 'status' => '10',
  152. }
  153. }
  154. import.save!
  155. import
  156. end
  157. end