summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Kozono <mkozono@gmail.com>2017-08-07 20:19:43 -0700
committerMichael Kozono <mkozono@gmail.com>2017-08-07 20:19:43 -0700
commit767af283487288c00007ace0521500e639bb9eb2 (patch)
tree7db67d70f01821c1fa98100496bb336af5b87f8b
parente6a7058fd40dc7f5267a84c576fc4eebf0a657e3 (diff)
downloadgitlab-ce-mk-add-opt-in-email-checkbox.tar.gz
Capture email opt-in IPmk-add-opt-in-email-checkbox
-rw-r--r--app/models/user.rb5
-rw-r--r--spec/models/user_spec.rb49
2 files changed, 54 insertions, 0 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index 5148886eed7..c4eb31e7df5 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -158,6 +158,7 @@ class User < ActiveRecord::Base
before_validation :sanitize_attrs
before_validation :set_notification_email, if: :email_changed?
before_validation :set_public_email, if: :public_email_changed?
+ before_validation :set_email_opted_in_ip, if: :email_opted_in_changed?
after_update :update_emails_with_primary_email, if: :email_changed?
before_save :ensure_authentication_token, :ensure_incoming_email_token
@@ -736,6 +737,10 @@ class User < ActiveRecord::Base
end
end
+ def set_email_opted_in_ip
+ self.email_opted_in_ip = email_opted_in? ? current_sign_in_ip : nil
+ end
+
def update_secondary_emails!
set_notification_email
set_public_email
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 0103fb6040e..238bbef1f50 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -2000,4 +2000,53 @@ describe User do
expect(user.projects_limit_left).to eq(5)
end
end
+
+ describe '#set_email_opted_in_ip' do
+ context 'when the user did not change email_opted_in' do
+ context 'when email_opted_in is falsey' do
+ let!(:user) { create(:user, email_opted_in: nil, current_sign_in_ip: '1.1.1.1') }
+
+ it 'does not change email_opted_in_ip' do
+ user.touch
+ expect(user.email_opted_in_ip).to be_nil
+ end
+ end
+
+ context 'when email_opted_in is truthy' do
+ let(:existing_ip) { '1.1.1.1' }
+ let!(:user) { create(:user, email_opted_in: true, current_sign_in_ip: existing_ip) }
+
+ it 'does not change email_opted_in_ip' do
+ user.update_attribute(:current_sign_in_ip, '2.2.2.2')
+ expect(user.email_opted_in_ip).to eq(existing_ip)
+ end
+ end
+
+ end
+
+ context 'when the user just changed to opted-in' do
+ let!(:user) { create(:user, email_opted_in: nil, current_sign_in_ip: '1.1.1.1') }
+ let(:new_ip) { '2.2.2.2' }
+
+ before do
+ user.update_attributes(email_opted_in: true, current_sign_in_ip: new_ip)
+ end
+
+ it 'sets email_opted_in_ip to current_sign_in_ip' do
+ expect(user.email_opted_in_ip).to eq(new_ip)
+ end
+ end
+
+ context 'when the user just changed to opted-out' do
+ let!(:user) { create(:user, email_opted_in: true, current_sign_in_ip: '1.1.1.1') }
+
+ before do
+ user.update_attributes(email_opted_in: false, current_sign_in_ip: '2.2.2.2')
+ end
+
+ it 'sets email_opted_in_ip to nil' do
+ expect(user.email_opted_in_ip).to be_nil
+ end
+ end
+ end
end