Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

user_import_test.rb 5.1KB

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