|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'updated_ajax request', type: :request do |
| 6 | + init_site |
| 7 | + |
| 8 | + let(:current_site) { Cama::Site.first.decorate } |
| 9 | + let(:current_user) { create(:user_admin, site: current_site, password: 'secret', password_confirmation: 'secret') } |
| 10 | + |
| 11 | + before do |
| 12 | + allow_any_instance_of(CamaleonCms::AdminController).to receive(:cama_authenticate) |
| 13 | + allow_any_instance_of(CamaleonCms::Admin::UsersController).to receive(:validate_role).and_return(true) |
| 14 | + end |
| 15 | + |
| 16 | + context 'when receiving correct params' do |
| 17 | + it "updates user's password" do |
| 18 | + expect(current_user.authenticate('secret')).to be_truthy |
| 19 | + expect(current_user.authenticate('new password')).to be_falsey |
| 20 | + |
| 21 | + patch "/admin/users/#{current_user.id}/updated_ajax", |
| 22 | + params: { password: { password: 'new password', password_confirmation: 'new password' } } |
| 23 | + |
| 24 | + expect(response.status).to eql(204) |
| 25 | + expect(response.body).to eql('') |
| 26 | + expect(current_user.reload.authenticate('secret')).to be_falsey |
| 27 | + expect(current_user.reload.authenticate('new password')).to be_truthy |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + context 'when receiving incorrect params' do |
| 32 | + context 'when wrong password confirmation' do |
| 33 | + it "doesn't update user's password and return error" do |
| 34 | + expect(current_user.authenticate('secret')).to be_truthy |
| 35 | + |
| 36 | + patch "/admin/users/#{current_user.id}/updated_ajax", |
| 37 | + params: { password: { password: 'new password', password_confirmation: 'old password' } } |
| 38 | + |
| 39 | + expect(response.status).to eql(422) |
| 40 | + expect(response.body).to eql("Password confirmation doesn't match Password") |
| 41 | + expect(current_user.reload.authenticate('secret')).to be_truthy |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + context 'when missing password confirmation' do |
| 46 | + it "doesn't update user's password" do |
| 47 | + expect(current_user.authenticate('secret')).to be_truthy |
| 48 | + |
| 49 | + patch "/admin/users/#{current_user.id}/updated_ajax", params: { password: { password: 'new password' } } |
| 50 | + |
| 51 | + expect(response.status).to eql(400) |
| 52 | + expect(response.body).to start_with( |
| 53 | + 'ERROR: ActionController::ParameterMissing, param is missing or the value is empty' |
| 54 | + ) |
| 55 | + expect(response.body).to include('password_confirmation') |
| 56 | + expect(current_user.reload.authenticate('secret')).to be_truthy |
| 57 | + expect(current_user.reload.authenticate('new password')).to be_falsey |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context 'when passing unpermitted params' do |
| 62 | + it 'ignores the unpermitted param' do |
| 63 | + expect(current_user.authenticate('secret')).to be_truthy |
| 64 | + |
| 65 | + # Changing this to false, because the receiver is not only yielded to the blocks, but also passed as an |
| 66 | + # unexpected additional argument to the `originall.call` |
| 67 | + RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = false |
| 68 | + |
| 69 | + allow_any_instance_of(CamaleonCms::User).to receive(:update).and_call_original |
| 70 | + |
| 71 | + expect_any_instance_of(CamaleonCms::User) |
| 72 | + .to receive(:update).with(password: 'new password', password_confirmation: 'new password') |
| 73 | + |
| 74 | + patch "/admin/users/#{current_user.id}/updated_ajax", |
| 75 | + params: { password: { password: 'new password', password_confirmation: 'new password', role: 'admin' } } |
| 76 | + |
| 77 | + expect(response.status).to eql(204) |
| 78 | + expect(response.body).to eql('') |
| 79 | + expect(current_user.reload.authenticate('secret')).to be_falsey |
| 80 | + expect(current_user.reload.authenticate('new password')).to be_truthy |
| 81 | + |
| 82 | + # returning the default configuration |
| 83 | + RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = false |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments