auth_test.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # encoding: utf-8
  2. require 'test_helper'
  3. Setting.create_or_update(
  4. :title => 'Authentication via LDAP',
  5. :name => 'auth_ldap',
  6. :area => 'Security::Authentication',
  7. :description => 'Enables user authentication via LDAP.',
  8. :state => {
  9. :adapter => 'Auth::Ldap',
  10. :host => 'localhost',
  11. :port => 389,
  12. :bind_dn => 'cn=Manager,dc=example,dc=org',
  13. :bind_pw => 'example',
  14. :uid => 'mail',
  15. :base => 'dc=example,dc=org',
  16. :always_filter => '',
  17. :always_roles => ['Admin', 'Agent'],
  18. :always_groups => ['Users'],
  19. :sync_params => {
  20. :firstname => 'sn',
  21. :lastname => 'givenName',
  22. :email => 'mail',
  23. :login => 'mail',
  24. },
  25. },
  26. :frontend => false
  27. )
  28. user = User.lookup( :login => 'nicole.braun@zammad.org' )
  29. if user
  30. user.update_attributes(
  31. :password => 'some_pass',
  32. :active => true,
  33. )
  34. else
  35. user = User.create_if_not_exists(
  36. :login => 'nicole.braun@zammad.org',
  37. :firstname => 'Nicole',
  38. :lastname => 'Braun',
  39. :email => 'nicole.braun@zammad.org',
  40. :password => 'some_pass',
  41. :active => true,
  42. :updated_by_id => 1,
  43. :created_by_id => 1
  44. )
  45. end
  46. class AuthTest < ActiveSupport::TestCase
  47. test 'auth' do
  48. tests = [
  49. # test 1
  50. {
  51. :username => 'not_existing',
  52. :password => 'password',
  53. :result => nil,
  54. },
  55. # test 2
  56. {
  57. :username => 'paige.chen@example.org',
  58. :password => 'password',
  59. :result => true,
  60. :verify => {
  61. :firstname => 'Chen',
  62. :lastname => 'Paige',
  63. :email => 'paige.chen@example.org',
  64. }
  65. },
  66. # test 3
  67. {
  68. :username => 'nicole.braun@zammad.org',
  69. :password => 'some_pass',
  70. :result => true,
  71. :verify => {
  72. :firstname => 'Nicole',
  73. :lastname => 'Braun',
  74. :email => 'nicole.braun@zammad.org',
  75. }
  76. },
  77. ]
  78. tests.each { |test|
  79. user = User.authenticate( test[:username], test[:password] )
  80. if test[:result] == true
  81. if !user
  82. assert( false, 'auth faild' )
  83. else
  84. test[:verify].each {|key, value|
  85. assert_equal( user[key], value, 'verify' )
  86. }
  87. end
  88. else
  89. assert_equal( test[:result], user, 'faild or not existing' )
  90. end
  91. }
  92. end
  93. end