From ee1b90503c7ff01f7aa9605f4cdb4308b32b773a Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 22 Dec 2017 09:29:35 +0800 Subject: [PATCH] FIX: Error when an invalid date is passed to certificate generator. https://meta.discourse.org/t/broken-image-in-discobot-certificate-with-no-logo-small-url/76594/2 --- .../certificate_generator.rb | 14 +++++++++++++- .../spec/integration/discobot_certificate_spec.rb | 13 ------------- .../spec/lib/certificate_generator_spec.rb | 11 +++++++++++ 3 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 plugins/discourse-narrative-bot/spec/lib/certificate_generator_spec.rb diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb index da4a9435ffe..639aaa70621 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb @@ -2,7 +2,19 @@ module DiscourseNarrativeBot class CertificateGenerator def initialize(user, date) @user = user - @date = I18n.l(Date.parse(date), format: :date_only) + + date = + begin + Date.parse(date) + rescue ArgumentError => e + if e.message == 'invalid date' + Date.parse(Date.today.to_s) + else + raise e + end + end + + @date = I18n.l(date, format: :date_only) @discobot_user = User.find(-2) end diff --git a/plugins/discourse-narrative-bot/spec/integration/discobot_certificate_spec.rb b/plugins/discourse-narrative-bot/spec/integration/discobot_certificate_spec.rb index cf81fce7034..5e2f58d84ee 100644 --- a/plugins/discourse-narrative-bot/spec/integration/discobot_certificate_spec.rb +++ b/plugins/discourse-narrative-bot/spec/integration/discobot_certificate_spec.rb @@ -33,18 +33,5 @@ describe "Discobot Certificate" do end end end - - describe 'when date is invalid' do - it 'should raise the right error' do - expect do - get '/discobot/certificate.svg', params: { - name: user.name, - date: "", - avatar_url: 'https://somesite.com/someavatar', - user_id: user.id - } - end.to raise_error(ArgumentError, 'invalid date') - end - end end end diff --git a/plugins/discourse-narrative-bot/spec/lib/certificate_generator_spec.rb b/plugins/discourse-narrative-bot/spec/lib/certificate_generator_spec.rb new file mode 100644 index 00000000000..2e3536d746b --- /dev/null +++ b/plugins/discourse-narrative-bot/spec/lib/certificate_generator_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe DiscourseNarrativeBot::CertificateGenerator do + let(:user) { Fabricate(:user) } + + describe 'when an invalid date is given' do + it 'should default to the current date' do + expect { described_class.new(user, "2017-00-10") }.to_not raise_error + end + end +end