passwords_controller_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Auth::PasswordsController do
  4. include Devise::Test::ControllerHelpers
  5. describe 'GET #new' do
  6. it 'returns http success' do
  7. request.env['devise.mapping'] = Devise.mappings[:user]
  8. get :new
  9. expect(response).to have_http_status(200)
  10. end
  11. end
  12. describe 'GET #edit' do
  13. let(:user) { Fabricate(:user) }
  14. before do
  15. request.env['devise.mapping'] = Devise.mappings[:user]
  16. end
  17. context 'with valid reset_password_token' do
  18. it 'returns http success' do
  19. token = user.send_reset_password_instructions
  20. get :edit, params: { reset_password_token: token }
  21. expect(response).to have_http_status(200)
  22. end
  23. end
  24. context 'with invalid reset_password_token' do
  25. it 'redirects to #new' do
  26. get :edit, params: { reset_password_token: 'some_invalid_value' }
  27. expect(response).to redirect_to subject.new_password_path(subject.send(:resource_name))
  28. end
  29. end
  30. end
  31. describe 'POST #update' do
  32. let(:user) { Fabricate(:user) }
  33. let(:password) { 'reset0password' }
  34. before do
  35. request.env['devise.mapping'] = Devise.mappings[:user]
  36. end
  37. context 'with valid reset_password_token' do
  38. let!(:session_activation) { Fabricate(:session_activation, user: user) }
  39. let!(:access_token) { Fabricate(:access_token, resource_owner_id: user.id) }
  40. let!(:web_push_subscription) { Fabricate(:web_push_subscription, access_token: access_token) }
  41. before do
  42. token = user.send_reset_password_instructions
  43. post :update, params: { user: { password: password, password_confirmation: password, reset_password_token: token } }
  44. end
  45. it 'resets the password' do
  46. expect(response)
  47. .to redirect_to '/auth/sign_in'
  48. # Change password
  49. expect(User.find(user.id))
  50. .to be_present
  51. .and be_valid_password(password)
  52. # Deactivate session
  53. expect(user.session_activations.count)
  54. .to eq 0
  55. expect { session_activation.reload }
  56. .to raise_error(ActiveRecord::RecordNotFound)
  57. # Revoke tokens
  58. expect(Doorkeeper::AccessToken.active_for(user).count)
  59. .to eq 0
  60. # Remove push subs
  61. expect(Web::PushSubscription.where(user: user).or(Web::PushSubscription.where(access_token: access_token)).count)
  62. .to eq 0
  63. expect { web_push_subscription.reload }
  64. .to raise_error(ActiveRecord::RecordNotFound)
  65. end
  66. end
  67. context 'with invalid reset_password_token' do
  68. before do
  69. post :update, params: { user: { password: password, password_confirmation: password, reset_password_token: 'some_invalid_value' } }
  70. end
  71. it 'renders reset password and retains password' do
  72. expect(response)
  73. .to render_template(:new)
  74. expect(User.find(user.id))
  75. .to be_present
  76. .and be_external_or_valid_password(user.password)
  77. end
  78. end
  79. end
  80. end