From cb99f59ec3769227bd890c633c768ec4f8356853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 20 Feb 2017 10:37:01 +0100 Subject: [PATCH] reset bounce score when email is successfully changed --- app/controllers/admin/users_controller.rb | 2 +- app/controllers/users_email_controller.rb | 6 ++++-- app/models/user.rb | 1 - app/models/user_stat.rb | 4 ++++ spec/controllers/users_controller_spec.rb | 1 - spec/controllers/users_email_controller_spec.rb | 8 ++++++++ spec/models/user_spec.rb | 2 ++ 7 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 9f72667af07..8fdd4b95368 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -363,7 +363,7 @@ class Admin::UsersController < Admin::AdminController def reset_bounce_score guardian.ensure_can_reset_bounce_score!(@user) - @user.user_stat.update_columns(bounce_score: 0, reset_bounce_score_after: nil) + @user.user_stat&.reset_bounce_score! render json: success_json end diff --git a/app/controllers/users_email_controller.rb b/app/controllers/users_email_controller.rb index da4ce692340..a61fd57819f 100644 --- a/app/controllers/users_email_controller.rb +++ b/app/controllers/users_email_controller.rb @@ -36,8 +36,10 @@ class UsersEmailController < ApplicationController updater = EmailUpdater.new @update_result = updater.confirm(params[:token]) - # Log in the user if the process is complete (and they're not logged in) - log_on_user(updater.user) if @update_result == :complete + if @update_result == :complete + updater.user.user_stat.reset_bounce_score! + log_on_user(updater.user) + end render layout: 'no_ember' end diff --git a/app/models/user.rb b/app/models/user.rb index 2841be1f5d4..e117a6dbc0b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -985,7 +985,6 @@ class User < ActiveRecord::Base end end - def hash_password(password, salt) raise StandardError.new("password is too long") if password.size > User.max_password_length Pbkdf2.hash_password(password, salt, Rails.configuration.pbkdf2_iterations, Rails.configuration.pbkdf2_algorithm) diff --git a/app/models/user_stat.rb b/app/models/user_stat.rb index 7898a2e57be..50761737e3f 100644 --- a/app/models/user_stat.rb +++ b/app/models/user_stat.rb @@ -77,6 +77,10 @@ class UserStat < ActiveRecord::Base cache_last_seen(Time.now.to_f) end + def reset_bounce_score! + update_columns(reset_bounce_score_after: nil, bounce_score: 0) + end + protected def trigger_badges diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 47a3246704b..746d26a0712 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -255,7 +255,6 @@ describe UsersController do user.reload expect(session["password-#{token}"]).to be_blank - expect(UserAuthToken.where(id: user_auth_token.id).count).to eq(0) end diff --git a/spec/controllers/users_email_controller_spec.rb b/spec/controllers/users_email_controller_spec.rb index 0a2bfe99ce3..e43ad3befea 100644 --- a/spec/controllers/users_email_controller_spec.rb +++ b/spec/controllers/users_email_controller_spec.rb @@ -33,9 +33,17 @@ describe UsersEmailController do end it 'confirms with a correct token' do + user.user_stat.update_columns(bounce_score: 42, reset_bounce_score_after: 1.week.from_now) + get :confirm, token: user.email_tokens.last.token + expect(response).to be_success expect(assigns(:update_result)).to eq(:complete) + + user.reload + + expect(user.user_stat.bounce_score).to eq(0) + expect(user.user_stat.reset_bounce_score_after).to eq(nil) end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d4bb2149b02..cf12bbab690 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -190,6 +190,8 @@ describe User do it "has correct settings" do expect(subject.email_tokens).to be_present + expect(subject.user_stat).to be_present + expect(subject.user_profile).to be_present expect(subject.user_option.email_private_messages).to eq(true) expect(subject.user_option.email_direct).to eq(true) end