Handle links with no href in VerifyLinkService (#20741)

Before this change, the following error would cause VerifyAccountLinksWorker to fail:

NoMethodError: undefined method `downcase' for nil:NilClass
  [PROJECT_ROOT]/app/services/verify_link_service.rb:31 :in `block in link_back_present?`
This commit is contained in:
Joshua Wood 2022-11-17 01:59:35 -08:00 committed by GitHub
parent cbb0153bd0
commit daf6f3453e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -28,7 +28,7 @@ class VerifyLinkService < BaseService
links = Nokogiri::HTML(@body).xpath('//a[contains(concat(" ", normalize-space(@rel), " "), " me ")]|//link[contains(concat(" ", normalize-space(@rel), " "), " me ")]')
if links.any? { |link| link['href'].downcase == @link_back.downcase }
if links.any? { |link| link['href']&.downcase == @link_back.downcase }
true
elsif links.empty?
false
@ -38,6 +38,8 @@ class VerifyLinkService < BaseService
end
def link_redirects_back?(test_url)
return false if test_url.blank?
redirect_to_url = Request.new(:head, test_url, follow: false).perform do |res|
res.headers['Location']
end

View File

@ -76,7 +76,25 @@ RSpec.describe VerifyLinkService, type: :service do
context 'when a link does not contain a link back' do
let(:html) { '' }
it 'marks the field as verified' do
it 'does not mark the field as verified' do
expect(field.verified?).to be false
end
end
context 'when link has no `href` attribute' do
let(:html) do
<<-HTML
<!doctype html>
<head>
<link type="text/html" rel="me" />
</head>
<body>
<a rel="me" target="_blank">Follow me on Mastodon</a>
</body>
HTML
end
it 'does not mark the field as verified' do
expect(field.verified?).to be false
end
end