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
This commit is contained in:
Guo Xiang Tan 2017-12-22 09:29:35 +08:00
parent 6f89db4c24
commit ee1b90503c
3 changed files with 24 additions and 14 deletions

View File

@ -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

View File

@ -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: "<script type=\"text/javascript\">alert('This app is probably vulnerable to XSS attacks!');</script>",
avatar_url: 'https://somesite.com/someavatar',
user_id: user.id
}
end.to raise_error(ArgumentError, 'invalid date')
end
end
end
end

View File

@ -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