From 10d0320532846bb81e8b3a004b6b212b86d96bfc Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 3 Apr 2014 11:30:43 -0400 Subject: [PATCH] FIX: Allow expanding posts when anonymous, add specs --- app/controllers/posts_controller.rb | 11 ++--------- app/models/topic_embed.rb | 10 ++++++++++ spec/controllers/posts_controller_spec.rb | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 317637922..9a2bb4cdc 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -5,7 +5,7 @@ require_dependency 'distributed_memoizer' class PostsController < ApplicationController # Need to be logged in for all actions here - before_filter :ensure_logged_in, except: [:show, :replies, :by_number, :short_link, :reply_history, :revisions] + before_filter :ensure_logged_in, except: [:show, :replies, :by_number, :short_link, :reply_history, :revisions, :expand_embed] skip_before_filter :store_incoming_links, only: [:short_link] skip_before_filter :check_xhr, only: [:markdown,:short_link] @@ -144,14 +144,7 @@ class PostsController < ApplicationController end def expand_embed - post = find_post_from_params - content = Rails.cache.fetch("embed-topic:#{post.topic_id}", expires_in: 10.minutes) do - url = TopicEmbed.where(topic_id: post.topic_id).pluck(:embed_url).first - title, body = TopicEmbed.find_remote(url) - body << TopicEmbed.imported_from_html(url) - body - end - render json: {cooked: content} + render json: {cooked: TopicEmbed.expanded_for(find_post_from_params) } rescue render_json_error I18n.t('errors.embed.load_from_remote') end diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb index 8b7ffd09b..a1ea9f5a0 100644 --- a/app/models/topic_embed.rb +++ b/app/models/topic_embed.rb @@ -137,6 +137,16 @@ class TopicEmbed < ActiveRecord::Base # If there is no first paragaph, return the first div (onebox) doc.css('div').first end + + def self.expanded_for(post) + Rails.cache.fetch("embed-topic:#{post.topic_id}", expires_in: 10.minutes) do + url = TopicEmbed.where(topic_id: post.topic_id).pluck(:embed_url).first + title, body = TopicEmbed.find_remote(url) + body << TopicEmbed.imported_from_html(url) + body + end + end + end # == Schema Information diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index 459253ccf..735a88f98 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -490,4 +490,21 @@ describe PostsController do end + describe 'expandable embedded posts' do + let(:post) { Fabricate(:post) } + + it "raises an error when you can't see the post" do + Guardian.any_instance.expects(:can_see?).with(post).returns(false) + xhr :get, :expand_embed, id: post.id + response.should_not be_success + end + + it "retrieves the body when you can see the post" do + Guardian.any_instance.expects(:can_see?).with(post).returns(true) + TopicEmbed.expects(:expanded_for).with(post).returns("full content") + xhr :get, :expand_embed, id: post.id + response.should be_success + ::JSON.parse(response.body)['cooked'].should == "full content" + end + end end