Refactor tumblr service spec

closes #6386
This commit is contained in:
Jonne Haß 2015-09-07 13:20:27 +02:00
parent 2ace9b47b1
commit 0e64d8de2a
3 changed files with 45 additions and 29 deletions

View file

@ -7,6 +7,7 @@
## Features ## Features
* Show spinner on initial stream load [#6384](https://github.com/diaspora/diaspora/pull/6384) * Show spinner on initial stream load [#6384](https://github.com/diaspora/diaspora/pull/6384)
* Add new moderator role. Moderators can view and act on reported posts [#6351](https://github.com/diaspora/diaspora/pull/6351) * Add new moderator role. Moderators can view and act on reported posts [#6351](https://github.com/diaspora/diaspora/pull/6351)
* Only post to the primary tumblr blog [#6386](https://github.com/diaspora/diaspora/pull/6386)
# 0.5.3.0 # 0.5.3.0

View file

@ -17,7 +17,7 @@ class Services::Tumblr < Service
body = build_tumblr_post(post, url) body = build_tumblr_post(post, url)
user_info = JSON.parse(client.get("/v2/user/info").body) user_info = JSON.parse(client.get("/v2/user/info").body)
blogs = user_info["response"]["user"]["blogs"] blogs = user_info["response"]["user"]["blogs"]
primaryblog = blogs.find(blogs[0]) {|blog| blog["primary"] } primaryblog = blogs.find {|blog| blog["primary"] } || blogs[0]
tumblr_ids = {} tumblr_ids = {}
blogurl = URI.parse(primaryblog["url"]) blogurl = URI.parse(primaryblog["url"])

View file

@ -1,41 +1,56 @@
require 'spec_helper' require "spec_helper"
describe Services::Tumblr, :type => :model do describe Services::Tumblr, type: :model do
let(:user) { alice }
let(:post) { user.post(:status_message, text: "hello", to: user.aspects.first.id) }
let(:service) { Services::Tumblr.new(access_token: "yeah", access_secret: "foobar") }
describe "#post" do
let(:post_id) { "bla" }
let(:post_request) { {body: service.build_tumblr_post(post, "")} }
let(:post_response) { {status: 201, body: {response: {id: post_id}}.to_json} }
before do before do
@user = alice user.services << service
@post = @user.post(:status_message, :text => "hello", :to =>@user.aspects.first.id) stub_request(:get, "http://api.tumblr.com/v2/user/info").to_return(status: 200, body: user_info)
@service = Services::Tumblr.new(:access_token => "yeah", :access_secret => "foobar")
@user.services << @service
end end
describe '#post' do context "with multiple blogs" do
it 'posts a status message to tumblr and saves the returned ids' do let(:user_info) {
response = double(body: '{"response": {"user": {"blogs": {response: {user: {blogs: [
[{"primary": false, "url": "http://foo.tumblr.com"}, {primary: false, url: "http://foo.tumblr.com"},
{"primary": true, "url": "http://bar.tumblr.com"}]}}}') {primary: true, url: "http://bar.tumblr.com"}
expect_any_instance_of(OAuth::AccessToken).to receive(:get) ]}}}.to_json
.with("/v2/user/info") }
.and_return(response)
response = double(code: "201", body: '{"response": {"id": "bla"}}') it "posts a status message to the primary blog and stores the id" do
expect_any_instance_of(OAuth::AccessToken).to receive(:post) stub_request(:post, "http://api.tumblr.com/v2/blog/bar.tumblr.com/post")
.with("/v2/blog/bar.tumblr.com/post", @service.build_tumblr_post(@post, "")) .with(post_request).to_return(post_response)
.and_return(response)
expect(@post).to receive(:tumblr_ids=).with({"bar.tumblr.com" => "bla"}.to_json) expect(post).to receive(:tumblr_ids=).with({"bar.tumblr.com" => post_id}.to_json)
@service.post(@post) service.post(post)
end end
end end
describe '#delete_post' do context "with a single blog" do
it 'removes posts from tumblr' do let(:user_info) { {response: {user: {blogs: [{url: "http://foo.tumblr.com"}]}}}.to_json }
stub_request(:post, "http://api.tumblr.com/v2/blog/foodbar.tumblr.com/post/delete").
to_return(:status => 200)
@service.delete_post(@post) it "posts a status message to the returned blog" do
stub_request(:post, "http://api.tumblr.com/v2/blog/foo.tumblr.com/post")
.with(post_request).to_return(post_response)
service.post(post)
end
end
end
describe "#delete_post" do
it "removes posts from tumblr" do
stub_request(:post, "http://api.tumblr.com/v2/blog/foodbar.tumblr.com/post/delete")
.to_return(status: 200)
service.delete_post(post)
end end
end end
end end