From e9b2018bc8bc1254075bec597ce30a84f38b456e Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 10 Jan 2019 09:21:28 +0800 Subject: [PATCH] FIX: Partial reply key search in email sent logs. Follow up to c85b9c6ed3a847ce0df7d58340d6680536d92b90 --- app/controllers/admin/email_controller.rb | 2 +- app/models/email_log.rb | 4 --- app/models/post_reply_key.rb | 6 +---- app/serializers/email_log_serializer.rb | 2 +- spec/models/email_log_spec.rb | 3 +-- spec/models/post_reply_key_spec.rb | 5 ++-- spec/requests/admin/email_controller_spec.rb | 26 ++++++++++++++++++++ 7 files changed, 32 insertions(+), 16 deletions(-) diff --git a/app/controllers/admin/email_controller.rb b/app/controllers/admin/email_controller.rb index 476f0aff8da..b1f28d46933 100644 --- a/app/controllers/admin/email_controller.rb +++ b/app/controllers/admin/email_controller.rb @@ -32,7 +32,7 @@ class Admin::EmailController < Admin::AdminController if params[:reply_key].present? email_logs = email_logs.where( - "CAST (post_reply_keys.reply_key AS VARCHAR) ILIKE ?", "%#{params[:reply_key]}%" + "post_reply_keys.reply_key::TEXT ILIKE ?", "%#{params[:reply_key]}%" ) end diff --git a/app/models/email_log.rb b/app/models/email_log.rb index 769436ea017..0aeae600ff1 100644 --- a/app/models/email_log.rb +++ b/app/models/email_log.rb @@ -73,10 +73,6 @@ class EmailLog < ActiveRecord::Base .first end - def bounce_key - super&.delete('-') - end - end # == Schema Information diff --git a/app/models/post_reply_key.rb b/app/models/post_reply_key.rb index 3f1479f6a01..2c85058556d 100644 --- a/app/models/post_reply_key.rb +++ b/app/models/post_reply_key.rb @@ -8,12 +8,8 @@ class PostReplyKey < ActiveRecord::Base validates :user_id, presence: true validates :reply_key, presence: true - def reply_key - super&.delete('-') - end - def self.generate_reply_key - SecureRandom.hex(16) + SecureRandom.uuid end end diff --git a/app/serializers/email_log_serializer.rb b/app/serializers/email_log_serializer.rb index 75c7010298c..e4d120a9f29 100644 --- a/app/serializers/email_log_serializer.rb +++ b/app/serializers/email_log_serializer.rb @@ -12,6 +12,6 @@ class EmailLogSerializer < ApplicationSerializer end def reply_key - @options[:reply_keys][[object.post_id, object.user_id]].delete("-") + @options[:reply_keys][[object.post_id, object.user_id]] end end diff --git a/spec/models/email_log_spec.rb b/spec/models/email_log_spec.rb index f0f60dabb0c..6124d320712 100644 --- a/spec/models/email_log_spec.rb +++ b/spec/models/email_log_spec.rb @@ -103,8 +103,7 @@ describe EmailLog do .pluck("bounce_key::text") .first - expect(raw_key).to_not eq(hex) - expect(raw_key.delete('-')).to eq(hex) + expect(raw_key).to eq(hex) expect(EmailLog.find(email_log.id).bounce_key).to eq(hex) end end diff --git a/spec/models/post_reply_key_spec.rb b/spec/models/post_reply_key_spec.rb index 631e6cd6a2e..45f6d5ee146 100644 --- a/spec/models/post_reply_key_spec.rb +++ b/spec/models/post_reply_key_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' RSpec.describe PostReplyKey do describe "#reply_key" do it "should format the reply_key correctly" do - hex = SecureRandom.hex + hex = SecureRandom.uuid post_reply_key = Fabricate(:post_reply_key, reply_key: hex ) @@ -12,8 +12,7 @@ RSpec.describe PostReplyKey do .pluck("reply_key::text") .first - expect(raw_key).to_not eq(hex) - expect(raw_key.delete('-')).to eq(hex) + expect(raw_key).to eq(hex) expect(PostReplyKey.find(post_reply_key.id).reply_key).to eq(hex) end end diff --git a/spec/requests/admin/email_controller_spec.rb b/spec/requests/admin/email_controller_spec.rb index 34b2faff5b6..8bee246ae17 100644 --- a/spec/requests/admin/email_controller_spec.rb +++ b/spec/requests/admin/email_controller_spec.rb @@ -58,6 +58,32 @@ describe Admin::EmailController do expect(log["id"]).to eq(email_log.id) expect(log["reply_key"]).to eq(post_reply_key.reply_key) end + + it 'should be able to filter by reply key' do + email_log_2 = Fabricate(:email_log, post: post) + + post_reply_key_2 = Fabricate(:post_reply_key, + post: post, + user: email_log_2.user, + reply_key: "2d447423-c625-4fb9-8717-ff04ac60eee8" + ) + + [ + "17-ff04", + "2d447423-c625-4fb9-8717-ff04ac60eee8" + ].each do |reply_key| + get "/admin/email/sent.json", params: { + reply_key: reply_key + } + + expect(response.status).to eq(200) + + logs = JSON.parse(response.body) + + expect(logs.size).to eq(1) + expect(logs.first["reply_key"]).to eq(post_reply_key_2.reply_key) + end + end end describe '#skipped' do