From 02122343c3d2872372e94949e374f08f1989f586 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 1 Mar 2017 19:51:48 -0600 Subject: [PATCH] dmails: don't default IP addr to 127.0.0.1 in database (fixes #2908). Bug introduced in 1400f64; that commit changed dmails so that creator_ip_addr defaulted to CurrentUser.ip_addr like this: after_initialize :initialize_attributes, if: :new_record? def initialize_attributes self.from_id ||= CurrentUser.id self.creator_ip_addr ||= CurrentUser.ip_addr end ...but creator_ip_addr already defaulted to 127.0.0.1 from the database, so the ||= assignment didn't work. Remove the database default so we always default to CurrentUser.ip_addr. --- .../20170302014435_remove_default_ip_addr_from_dmails.rb | 9 +++++++++ db/structure.sql | 4 +++- test/unit/dmail_test.rb | 7 ++++++- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20170302014435_remove_default_ip_addr_from_dmails.rb diff --git a/db/migrate/20170302014435_remove_default_ip_addr_from_dmails.rb b/db/migrate/20170302014435_remove_default_ip_addr_from_dmails.rb new file mode 100644 index 000000000..1bccb2e38 --- /dev/null +++ b/db/migrate/20170302014435_remove_default_ip_addr_from_dmails.rb @@ -0,0 +1,9 @@ +class RemoveDefaultIpAddrFromDmails < ActiveRecord::Migration + def up + change_column_default(:dmails, :creator_ip_addr, nil) + end + + def down + change_column_default(:dmails, :creator_ip_addr, "127.0.0.1") + end +end diff --git a/db/structure.sql b/db/structure.sql index 240c02f73..7cd5193a5 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1007,7 +1007,7 @@ CREATE TABLE dmails ( is_deleted boolean DEFAULT false NOT NULL, created_at timestamp without time zone, updated_at timestamp without time zone, - creator_ip_addr inet DEFAULT '127.0.0.1'::inet NOT NULL + creator_ip_addr inet NOT NULL ); @@ -7466,3 +7466,5 @@ INSERT INTO schema_migrations (version) VALUES ('20170117233040'); INSERT INTO schema_migrations (version) VALUES ('20170218104710'); +INSERT INTO schema_migrations (version) VALUES ('20170302014435'); + diff --git a/test/unit/dmail_test.rb b/test/unit/dmail_test.rb index 05644707c..1b775cc81 100644 --- a/test/unit/dmail_test.rb +++ b/test/unit/dmail_test.rb @@ -6,7 +6,7 @@ class DmailTest < ActiveSupport::TestCase MEMCACHE.flush_all @user = FactoryGirl.create(:user) CurrentUser.user = @user - CurrentUser.ip_addr = "127.0.0.1" + CurrentUser.ip_addr = "1.2.3.4" ActionMailer::Base.delivery_method = :test ActionMailer::Base.perform_deliveries = true ActionMailer::Base.deliveries = [] @@ -112,6 +112,11 @@ class DmailTest < ActiveSupport::TestCase end end + should "record the creator's ip addr" do + dmail = FactoryGirl.create(:dmail, owner: @user) + assert_equal(CurrentUser.ip_addr, dmail.creator_ip_addr.to_s) + end + should "send an email if the user wants it" do user = FactoryGirl.create(:user, :receive_email_notifications => true) assert_difference("ActionMailer::Base.deliveries.size", 1) do