Add tests for user_twitter_card and answer_opengraph

This commit is contained in:
Karina Kwiatek 2021-08-06 21:38:51 +02:00
parent 5b05c27091
commit 7249e45989
3 changed files with 54 additions and 4 deletions

View File

@ -185,7 +185,7 @@ module ApplicationHelper
# @param user [User]
def user_opengraph(user)
opengraph_meta_tags({
'og:title': user.display_name,
'og:title': user.safe_name,
'og:type': 'profile',
'og:image': full_profile_picture_url(user),
'og:url': show_user_profile_url(user.screen_name),
@ -201,7 +201,7 @@ module ApplicationHelper
'twitter:card': 'summary',
'twitter:site': '@retrospring',
'twitter:title': user.motivation_header.presence || "Ask me anything!",
'twitter:description': "Ask #{user.display_name} anything on Retrospring",
'twitter:description': "Ask #{user.safe_name} anything on Retrospring",
'twitter:image': full_profile_picture_url(user)
})
end
@ -209,7 +209,7 @@ module ApplicationHelper
# @param answer [Answer]
def answer_opengraph(answer)
opengraph_meta_tags({
'og:title': answer.question.content,
'og:title': "#{answer.user.safe_name} answered: #{answer.question.content}",
'og:type': 'article',
'og:image': full_profile_picture_url(answer.user),
'og:url': show_user_answer_url(answer.user.screen_name, answer.id),

View File

@ -253,6 +253,10 @@ class User < ApplicationRecord
!self.export_processing
end
def safe_name
self.display_name.presence || self.screen_name
end
# %w[admin moderator].each do |m|
# define_method(m) { raise "not allowed: #{m}" }
# define_method(m+??) { raise "not allowed: #{m}?"}

View File

@ -27,7 +27,6 @@ describe ApplicationHelper, :type => :helper do
subject { user_opengraph(user) }
it 'should generate a matching OpenGraph structure for a user' do
allow(APP_CONFIG).to receive(:[]).with('site_name').and_return('pineapplespring')
expect(subject).to eq(<<-EOS.chomp)
@ -42,4 +41,51 @@ EOS
end
end
end
describe "#user_twitter_card" do
context "sample user" do
let(:user) { FactoryBot.create(:user,
display_name: '',
bio: 'A bunch of raccoons in a trenchcoat.',
screen_name: 'raccoons') }
subject { user_twitter_card(user) }
it 'should generate a matching OpenGraph structure for a user' do
expect(subject).to eq(<<-EOS.chomp)
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@retrospring" />
<meta name="twitter:title" content="Ask me anything!" />
<meta name="twitter:description" content="Ask raccoons anything on Retrospring" />
<meta name="twitter:image" content="http://test.host/images/large/no_avatar.png" />
EOS
end
end
end
describe "#answer_opengraph" do
context "sample user and answer" do
let!(:user) { FactoryBot.create(:user,
display_name: '',
bio: 'A bunch of raccoons in a trenchcoat.',
screen_name: 'raccoons') }
let(:answer) { FactoryBot.create(:answer,
user_id: user.id,) }
subject { answer_opengraph(answer) }
it 'should generate a matching OpenGraph structure for a user' do
allow(APP_CONFIG).to receive(:[]).with('site_name').and_return('pineapplespring')
expect(subject).to eq(<<-EOS.chomp)
<meta property="og:title" content="raccoons answered: #{answer.question.content}" />
<meta property="og:type" content="article" />
<meta property="og:image" content="http://test.host/images/large/no_avatar.png" />
<meta property="og:url" content="http://test.host/raccoons/a/#{answer.id}" />
<meta property="og:description" content="#{answer.content}" />
<meta property="og:site_name" content="pineapplespring" />
EOS
end
end
end
end