Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Tumblr sharing not working #237

Merged
merged 4 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/services/tumblr.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Services::Tumblr < Service
include Rails.application.routes.url_helpers
include SocialHelper::TumblrMethods

def provider
Expand Down
18 changes: 16 additions & 2 deletions spec/helpers/social_helper/tumblr_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
content: 'aaaa',
question_content: 'q') }

before do
stub_const("APP_CONFIG", {
'hostname' => 'example.com',
'anonymous_name' => 'Anonymous',
'https' => true,
'items_per_page' => 5,
'sharing' => {
'tumblr' => {
'consumer_key' => 'AAA',
}
}
})
end

describe '#tumblr_title' do
context 'Asker is anonymous' do
subject { tumblr_title(answer) }
Expand Down Expand Up @@ -36,15 +50,15 @@
subject { tumblr_body(answer) }

it 'should return a proper body' do
expect(subject).to eq("aaaa\n\n[Smile or comment on the answer here](https://justask.rrerr.net/#{answer.user.screen_name}/a/#{answer.id})")
expect(subject).to eq("aaaa\n\n[Smile or comment on the answer here](https://example.com/#{answer.user.screen_name}/a/#{answer.id})")
end
end

describe '#tumblr_share_url' do
subject { tumblr_share_url(answer) }

it 'should return a proper share link' do
expect(subject).to eq("https://www.tumblr.com/widgets/share/tool?shareSource=legacy&posttype=text&title=#{CGI.escape(tumblr_title(answer))}&url=#{CGI.escape("https://justask.rrerr.net/#{answer.user.screen_name}/a/#{answer.id}")}&caption=&content=#{CGI.escape(tumblr_body(answer))}")
expect(subject).to eq("https://www.tumblr.com/widgets/share/tool?shareSource=legacy&posttype=text&title=#{CGI.escape(tumblr_title(answer))}&url=#{CGI.escape("https://example.com/#{answer.user.screen_name}/a/#{answer.id}")}&caption=&content=#{CGI.escape(tumblr_body(answer))}")
end
end
end
12 changes: 10 additions & 2 deletions spec/helpers/social_helper/twitter_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@
content: 'a' * 255,
question_content: 'q' * 255) }

before do
stub_const("APP_CONFIG", {
'hostname' => 'example.com',
'https' => true,
'items_per_page' => 5
})
end

describe '#prepare_tweet' do
context 'when the question and answer need to be shortened' do
subject { prepare_tweet(answer) }

it 'should return a properly formatted tweet' do
expect(subject).to eq("#{'q' * 123}… — #{'a' * 124}… https://justask.rrerr.net/#{user.screen_name}/a/#{answer.id}")
expect(subject).to eq("#{'q' * 123}… — #{'a' * 124}… https://example.com/#{user.screen_name}/a/#{answer.id}")
end
end

Expand All @@ -28,7 +36,7 @@
subject { prepare_tweet(answer) }

it 'should return a properly formatted tweet' do
expect(subject).to eq("#{answer.question.content} — #{answer.content} https://justask.rrerr.net/#{user.screen_name}/a/#{answer.id}")
expect(subject).to eq("#{answer.question.content} — #{answer.content} https://example.com/#{user.screen_name}/a/#{answer.id}")
end
end
end
Expand Down
48 changes: 48 additions & 0 deletions spec/models/services/tumblr_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require 'rails_helper'

describe Services::Tumblr do
describe "#post" do
let(:user) { FactoryBot.create(:user) }
let(:service) { Services::Tumblr.create(user: user) }
let(:answer) { FactoryBot.create(:answer, user: user,
content: 'a' * 255,
question_content: 'q' * 255) }
let(:tumblr_client) { instance_double(Tumblr::Client) }

before do
allow(Tumblr::Client).to receive(:new).and_return(tumblr_client)
allow(tumblr_client).to receive(:text)
stub_const("APP_CONFIG", {
'hostname' => 'example.com',
'anonymous_name' => 'Anonymous',
'https' => true,
'items_per_page' => 5,
'sharing' => {
'tumblr' => {
'consumer_key' => 'AAA',
}
}
})
end

it "posts a text-post" do
answer.question.content = 'Why are raccoons so good?'
answer.question.author_is_anonymous = true
answer.question.save!
answer.content = 'Because they are good cunes.'
answer.save!

service.post(answer)

expect(tumblr_client).to have_received(:text).with(
service.uid,
title: 'Anonymous asked: Why are raccoons so good?',
body: "Because they are good cunes.\n\n[Smile or comment on the answer here](https://example.com/#{user.screen_name}/a/#{answer.id})",
format: 'markdown',
tweet: 'off'
)
end
end
end