user_spec.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Auth::User do
  4. let(:user) { create(:user) }
  5. let(:instance) { described_class.new(user.login) }
  6. describe '.can_login?' do
  7. it 'responds to can_login?' do
  8. expect(instance).to respond_to(:can_login?)
  9. end
  10. shared_examples 'check loginable' do
  11. it 'checks if users can login' do
  12. expect(instance.can_login?).to be true
  13. end
  14. end
  15. shared_examples 'check not loginable' do
  16. it 'check that user can not login' do
  17. expect(instance.can_login?).to be false
  18. end
  19. end
  20. context 'with valid user login' do
  21. include_examples 'check loginable'
  22. end
  23. context 'with to many failed logins' do
  24. let(:user) { create(:user, login_failed: 999) }
  25. include_examples 'check not loginable'
  26. end
  27. context 'with not active user' do
  28. let(:user) { create(:user, active: false) }
  29. include_examples 'check not loginable'
  30. end
  31. context 'with invalid instance username parameter' do
  32. let(:instance) { described_class.new('not_existing') }
  33. include_examples 'check not loginable'
  34. end
  35. context 'with empty instance username parameter' do
  36. let(:instance) { described_class.new('') }
  37. include_examples 'check not loginable'
  38. end
  39. context 'with given default password_max_login_failed' do
  40. context 'with 5 attempts' do
  41. let(:user) { create(:user, login_failed: 5) }
  42. include_examples 'check loginable'
  43. end
  44. context 'with 6 attempts' do
  45. let(:user) { create(:user, login_failed: 6) }
  46. include_examples 'check not loginable'
  47. end
  48. end
  49. context 'when "password_max_login_failed" Setting is changed' do
  50. context 'when changed to lower value' do
  51. before do
  52. Setting.set('password_max_login_failed', 5)
  53. user.update(login_failed: 6)
  54. end
  55. include_examples 'check not loginable'
  56. end
  57. context 'when changed to nil' do
  58. before do
  59. Setting.set('password_max_login_failed', nil)
  60. end
  61. include_examples 'check loginable'
  62. context 'when User login failed once' do
  63. before do
  64. user.update(login_failed: 1)
  65. end
  66. include_examples 'check not loginable'
  67. end
  68. end
  69. end
  70. end
  71. end