about summary refs log tree commit diff
path: root/spec/controllers/auth/passwords_controller_spec.rb
blob: 1c6874f08c47b19f802fe5363e24f525444097c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true

require 'rails_helper'

describe Auth::PasswordsController, type: :controller do
  include Devise::Test::ControllerHelpers

  describe 'GET #new' do
    it 'returns http success' do
      @request.env['devise.mapping'] = Devise.mappings[:user]
      get :new
      expect(response).to have_http_status(200)
    end
  end

  describe 'GET #edit' do
    let(:user) { Fabricate(:user) }

    before do
      request.env['devise.mapping'] = Devise.mappings[:user]
      @token = user.send_reset_password_instructions
    end

    context 'with valid reset_password_token' do
      it 'returns http success' do
        get :edit, params: { reset_password_token: @token }
        expect(response).to have_http_status(200)
      end
    end

    context 'with invalid reset_password_token' do
      it 'redirects to #new' do
        get :edit, params: { reset_password_token: 'some_invalid_value' }
        expect(response).to redirect_to subject.new_password_path(subject.send(:resource_name))
      end
    end
  end

  describe 'POST #update' do
    let(:user) { Fabricate(:user) }

    before do
      @password = 'reset0password'
      request.env['devise.mapping'] = Devise.mappings[:user]
    end

    context 'with valid reset_password_token' do
      let!(:session_activation) { Fabricate(:session_activation, user: user) }
      let!(:access_token) { Fabricate(:access_token, resource_owner_id: user.id) }
      let!(:web_push_subscription) { Fabricate(:web_push_subscription, access_token: access_token) }

      before do
        @token = user.send_reset_password_instructions

        post :update, params: { user: { password: @password, password_confirmation: @password, reset_password_token: @token } }
      end

      it 'redirect to sign in' do
        expect(response).to redirect_to '/auth/sign_in'
      end

      it 'changes password' do
        this_user = User.find(user.id)

        expect(this_user).to_not be_nil
        expect(this_user.valid_password?(@password)).to be true
      end

      it 'deactivates all sessions' do
        expect(user.session_activations.count).to eq 0
      end

      it 'revokes all access tokens' do
        expect(Doorkeeper::AccessToken.active_for(user).count).to eq 0
      end

      it 'removes push subscriptions' do
        expect(Web::PushSubscription.where(user: user).or(Web::PushSubscription.where(access_token: access_token)).count).to eq 0
      end
    end

    context 'with invalid reset_password_token' do
      before do
        post :update, params: { user: { password: @password, password_confirmation: @password, reset_password_token: 'some_invalid_value' } }
      end

      it 'renders reset password' do
        expect(response).to render_template(:new)
      end

      it 'retains password' do
        this_user = User.find(user.id)

        expect(this_user).to_not be_nil
        expect(this_user.external_or_valid_password?(user.password)).to be true
      end
    end
  end
end