user_spec.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. require 'rails_helper'
  2. RSpec.describe User do
  3. let(:new_password) { 'N3W54V3PW!' }
  4. context 'password' do
  5. it 'resets login_failed on password change' do
  6. user = create(:user_login_failed)
  7. expect {
  8. user.password = new_password
  9. user.save
  10. }.to change { user.login_failed }.to(0)
  11. end
  12. end
  13. context '#by_reset_token' do
  14. it 'returns a User instance for existing tokens' do
  15. token = create(:token_password_reset)
  16. expect(described_class.by_reset_token(token.name)).to be_instance_of(described_class)
  17. end
  18. it 'returns nil for not existing tokens' do
  19. expect(described_class.by_reset_token('not-existing')).to be nil
  20. end
  21. end
  22. context '#password_reset_via_token' do
  23. it 'changes the password of the token user and destroys the token' do
  24. token = create(:token_password_reset)
  25. user = User.find(token.user_id)
  26. expect {
  27. described_class.password_reset_via_token(token.name, new_password)
  28. user.reload
  29. }.to change {
  30. user.password
  31. }.and change {
  32. Token.count
  33. }.by(-1)
  34. end
  35. end
  36. end