mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-11-12 01:11:24 +08:00
fixes bug with password confirmation
This commit is contained in:
parent
c20f0c2d8a
commit
dea69014d0
4 changed files with 43 additions and 9 deletions
|
|
@ -55,7 +55,8 @@ module ClientApi
|
||||||
bypass_sign_in(current_user)
|
bypass_sign_in(current_user)
|
||||||
success_response
|
success_response
|
||||||
else
|
else
|
||||||
unsuccess_response(current_user.errors.full_messages, :unauthorized)
|
unsuccess_response(current_user.errors.full_messages,
|
||||||
|
:unprocessable_entity)
|
||||||
end
|
end
|
||||||
rescue CustomUserError => error
|
rescue CustomUserError => error
|
||||||
unsuccess_response(error.to_s)
|
unsuccess_response(error.to_s)
|
||||||
|
|
|
||||||
|
|
@ -2,19 +2,26 @@ module ClientApi
|
||||||
class UserService < BaseService
|
class UserService < BaseService
|
||||||
def update_user!
|
def update_user!
|
||||||
error = I18n.t('client_api.user.passwords_dont_match')
|
error = I18n.t('client_api.user.passwords_dont_match')
|
||||||
raise CustomUserError, error unless check_password_confirmation
|
raise CustomUserError, error unless check_current_password
|
||||||
@params.delete(:current_password) # removes unneeded element
|
@params.delete(:current_password) # removes unneeded element
|
||||||
@current_user.update(@params)
|
@current_user.update(@params)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def check_password_confirmation
|
def check_current_password
|
||||||
return true unless @params[:email] || @params[:password]
|
return true unless @params[:email] || @params[:password]
|
||||||
error = I18n.t('client_api.user.blank_password_error')
|
pass_blank_err = I18n.t('client_api.user.blank_password_error')
|
||||||
password_confirmation = @params[:current_password]
|
pass_match_err = I18n.t('client_api.user.passwords_dont_match')
|
||||||
raise CustomUserError, error unless password_confirmation
|
current_password = @params[:current_password]
|
||||||
@current_user.valid_password? password_confirmation
|
raise CustomUserError, pass_blank_err unless current_password
|
||||||
|
raise CustomUserError, pass_match_err unless check_password_confirmation
|
||||||
|
@current_user.valid_password? current_password
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_password_confirmation
|
||||||
|
return true if @params[:email]
|
||||||
|
@params[:password] == @params[:password_confirmation]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
CustomUserError = Class.new(StandardError)
|
CustomUserError = Class.new(StandardError)
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ describe ClientApi::Users::UsersController, type: :controller do
|
||||||
|
|
||||||
describe 'POST update' do
|
describe 'POST update' do
|
||||||
let(:new_password) { 'secretPassword' }
|
let(:new_password) { 'secretPassword' }
|
||||||
|
let(:new_email) { 'banana@fruit.com' }
|
||||||
|
|
||||||
it 'responds successfully if all password params are set' do
|
it 'responds successfully if all password params are set' do
|
||||||
post :update,
|
post :update,
|
||||||
|
|
@ -52,6 +53,29 @@ describe ClientApi::Users::UsersController, type: :controller do
|
||||||
expect(response).to have_http_status(:ok)
|
expect(response).to have_http_status(:ok)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'responds successfully if email is updated' do
|
||||||
|
post :update, params: { user: { email: new_email,
|
||||||
|
current_password: 'asdf1243' } },
|
||||||
|
format: :json
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(@user.reload.email).to eq(new_email)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'responds unsuccessfully if email is updated without password' do
|
||||||
|
post :update, params: { user: { email: new_email } },
|
||||||
|
format: :json
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
expect(@user.reload.email).to_not eq(new_email)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'responds unsuccessfully if email is updated with invalid email' do
|
||||||
|
post :update, params: { user: { email: 'bananafruit.com',
|
||||||
|
current_password: 'asdf1243' } },
|
||||||
|
format: :json
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
expect(@user.reload.email).to_not eq(new_email)
|
||||||
|
end
|
||||||
|
|
||||||
it 'changes timezone' do
|
it 'changes timezone' do
|
||||||
user = User.first
|
user = User.first
|
||||||
expect(user.time_zone).to eq('UTC')
|
expect(user.time_zone).to eq('UTC')
|
||||||
|
|
|
||||||
|
|
@ -59,8 +59,10 @@ describe ClientApi::UserService do
|
||||||
password_confirmation: 'hello1234567890',
|
password_confirmation: 'hello1234567890',
|
||||||
current_password: 'asdf1234' }
|
current_password: 'asdf1234' }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
expect {
|
||||||
user_service.update_user!
|
user_service.update_user!
|
||||||
expect(user).to_not be_valid
|
}.to raise_error(ClientApi::CustomUserError, 'Passwords don\'t match')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should update the password' do
|
it 'should update the password' do
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue