From 8648074489b14fc93049935c50affb180b18ab60 Mon Sep 17 00:00:00 2001 From: Stephen Crosby Date: Thu, 10 Dec 2015 11:29:52 -0800 Subject: [PATCH] Refs #1001 truncate long messages on cache --- app/models/notice.rb | 8 ++++++++ app/models/problem.rb | 5 ----- spec/models/notice_spec.rb | 32 ++++++++++++++++++++++++++++++++ spec/models/user_spec.rb | 2 +- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/app/models/notice.rb b/app/models/notice.rb index 8c3e42e..644084e 100644 --- a/app/models/notice.rb +++ b/app/models/notice.rb @@ -1,6 +1,8 @@ require 'recurse' class Notice + MESSAGE_LENGTH_LIMIT = 1000 + include Mongoid::Document include Mongoid::Timestamps @@ -32,6 +34,12 @@ class Notice where(:err_id.in => errs.all.map(&:id)) } + # Overwrite the default setter to make sure the message length is no longer + # than the limit we impose + def message=(m) + super(m.is_a?(String) ? m[0, MESSAGE_LENGTH_LIMIT] : m) + end + def user_agent agent_string = env_vars['HTTP_USER_AGENT'] agent_string.blank? ? nil : UserAgent.parse(agent_string) diff --git a/app/models/problem.rb b/app/models/problem.rb index c8c8d07..55403fa 100644 --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -55,7 +55,6 @@ class Problem validates :last_notice_at, :first_notice_at, presence: true before_create :cache_app_attributes - before_save :truncate_message scope :resolved, -> { where(resolved: true) } scope :unresolved, -> { where(resolved: false) } @@ -223,10 +222,6 @@ class Problem self.app_name = app.name if app end - def truncate_message - self.message = message[0, 1000] if message - end - def issue_type # Return issue_type if configured, but fall back to detecting app's issue tracker attributes['issue_type'] ||= diff --git a/spec/models/notice_spec.rb b/spec/models/notice_spec.rb index 32b5602..0c938f5 100644 --- a/spec/models/notice_spec.rb +++ b/spec/models/notice_spec.rb @@ -19,6 +19,38 @@ describe Notice, type: 'model' do end end + describe '#message=' do + let(:long_message) do + 'Presently I heard a slight groan, and I knew it was the groan of ' \ + 'mortal terror. It was not a groan of pain or of grief --oh, no! ' \ + '--it was the low stifled sound that arises from the bottom of the ' \ + 'soul when overcharged with awe. I knew the sound well. Many a ' \ + 'night, just at midnight, when all the world slept, it has welled ' \ + 'up from my own bosom, deepening, with its dreadful echo, the ' \ + 'terrors that distracted me. I say I knew it well. I knew what the ' \ + 'old man felt, and pitied him, although I chuckled at heart. I ' \ + 'knew that he had been lying awake ever since the first slight ' \ + 'noise, when he had turned in the bed. His fears had been ever ' \ + 'since growing upon him. He had been trying to fancy them ' \ + 'causeless, but could not. He had been saying to himself --"It is ' \ + 'nothing but the wind in the chimney --it is only a mouse crossing ' \ + 'the floor," or "It is merely a cricket which has made a single ' \ + 'chirp." Yes, he had been trying to comfort himself with these ' \ + 'suppositions: but he had found all in vain. All in vain; because ' \ + 'Death, in approaching him had stalked with his black shadow ' \ + 'before him, and enveloped the victim. And it was the mournful ' \ + 'influence of the unperceived shadow that caused him to feel ' \ + '--although he neither saw nor heard --to feel the presence of my ' \ + 'head within the room. ' + end + + it 'truncates the message' do + notice = Fabricate(:notice, message: long_message) + expect(long_message.length).to be > 1000 + expect(notice.message.length).to eq 1000 + end + end + describe "key sanitization" do before do @hash = { "some.key" => { "$nested.key" => { "$Path" => "/", "some$key" => "key" } } } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 5cc66f6..a8edaf6 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -42,7 +42,7 @@ describe User do expect(user.reset_password('Password123', 'Password123')).to be_truthy end - it 'should require a password with minimum of 6 characters' do + it 'should require a password with minimum of 6 characters' do user = Fabricate.build(:user) user.reset_password('12345', '12345') expect(user.errors[:password]).to include("is too short (minimum is 6 characters)", "is too short (minimum is 6 characters)") -- libgit2 0.21.2