-
Notifications
You must be signed in to change notification settings - Fork 290
Fix updated_ajax action to permit only legit params #1109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+95
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'updated_ajax request', type: :request do | ||
| init_site | ||
|
|
||
| let(:current_site) { Cama::Site.first.decorate } | ||
| let(:current_user) { create(:user_admin, site: current_site, password: 'secret', password_confirmation: 'secret') } | ||
|
|
||
| before do | ||
| allow_any_instance_of(CamaleonCms::AdminController).to receive(:cama_authenticate) | ||
| allow_any_instance_of(CamaleonCms::Admin::UsersController).to receive(:validate_role).and_return(true) | ||
| end | ||
|
|
||
| context 'when receiving correct params' do | ||
| it "updates user's password" do | ||
| expect(current_user.authenticate('secret')).to be_truthy | ||
| expect(current_user.authenticate('new password')).to be_falsey | ||
|
|
||
| patch "/admin/users/#{current_user.id}/updated_ajax", | ||
| params: { password: { password: 'new password', password_confirmation: 'new password' } } | ||
|
|
||
| expect(response.status).to eql(204) | ||
| expect(response.body).to eql('') | ||
| expect(current_user.reload.authenticate('secret')).to be_falsey | ||
| expect(current_user.reload.authenticate('new password')).to be_truthy | ||
| end | ||
| end | ||
|
|
||
| context 'when receiving incorrect params' do | ||
| context 'when wrong password confirmation' do | ||
| it "doesn't update user's password and return error" do | ||
| expect(current_user.authenticate('secret')).to be_truthy | ||
|
|
||
| patch "/admin/users/#{current_user.id}/updated_ajax", | ||
| params: { password: { password: 'new password', password_confirmation: 'old password' } } | ||
|
|
||
| expect(response.status).to eql(422) | ||
| expect(response.body).to eql("Password confirmation doesn't match Password") | ||
| expect(current_user.reload.authenticate('secret')).to be_truthy | ||
| end | ||
| end | ||
|
|
||
| context 'when missing password confirmation' do | ||
| it "doesn't update user's password" do | ||
| expect(current_user.authenticate('secret')).to be_truthy | ||
|
|
||
| patch "/admin/users/#{current_user.id}/updated_ajax", params: { password: { password: 'new password' } } | ||
|
|
||
| expect(response.status).to eql(400) | ||
| expect(response.body).to start_with( | ||
| 'ERROR: ActionController::ParameterMissing, param is missing or the value is empty' | ||
| ) | ||
| expect(response.body).to include('password_confirmation') | ||
| expect(current_user.reload.authenticate('secret')).to be_truthy | ||
| expect(current_user.reload.authenticate('new password')).to be_falsey | ||
| end | ||
| end | ||
|
|
||
| context 'when passing unpermitted params' do | ||
| it 'ignores the unpermitted param' do | ||
| expect(current_user.authenticate('secret')).to be_truthy | ||
|
|
||
| # Changing this to false, because the receiver is not only yielded to the blocks, but also passed as an | ||
| # unexpected additional argument to the `originall.call` | ||
| RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = false | ||
|
|
||
| allow_any_instance_of(CamaleonCms::User).to receive(:update).and_call_original | ||
|
|
||
| expect_any_instance_of(CamaleonCms::User) | ||
| .to receive(:update).with(password: 'new password', password_confirmation: 'new password') | ||
|
|
||
| patch "/admin/users/#{current_user.id}/updated_ajax", | ||
| params: { password: { password: 'new password', password_confirmation: 'new password', role: 'admin' } } | ||
|
|
||
| expect(response.status).to eql(204) | ||
| expect(response.body).to eql('') | ||
| expect(current_user.reload.authenticate('secret')).to be_falsey | ||
| expect(current_user.reload.authenticate('new password')).to be_truthy | ||
|
|
||
| # returning the default configuration | ||
| RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = false | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
password[role]=admin