Merge pull request #1978 from velesin/posts_controller__by_number_tests

Adds specs for PostsController#by_number.
This commit is contained in:
Robin Ward 2014-02-19 12:26:57 -05:00
commit ef18e415cf
2 changed files with 52 additions and 36 deletions

View File

@ -119,7 +119,9 @@ class PostsController < ApplicationController
end
def by_number
@post = Post.where(topic_id: params[:topic_id], post_number: params[:post_number]).first
finder = Post.where(topic_id: params[:topic_id], post_number: params[:post_number])
finder = finder.with_deleted if current_user.try(:staff?)
@post = finder.first
guardian.ensure_can_see!(@post)
@post.revert_to(params[:version].to_i) if params[:version].present?
render_post_json(@post)

View File

@ -1,5 +1,46 @@
require 'spec_helper'
shared_examples 'finding and showing post' do
let(:user) { log_in }
let(:post) { Fabricate(:post, user: user) }
it 'ensures the user can see the post' do
Guardian.any_instance.expects(:can_see?).with(post).returns(false)
xhr :get, action, params
response.should be_forbidden
end
it 'succeeds' do
xhr :get, action, params
response.should be_success
end
context "deleted post" do
before do
post.trash!(user)
end
it "can't find deleted posts as an anonymous user" do
xhr :get, action, params
response.should be_forbidden
end
it "can't find deleted posts as a regular user" do
log_in(:user)
xhr :get, action, params
response.should be_forbidden
end
it "can find posts as a moderator" do
log_in(:moderator)
xhr :get, action, params
response.should be_success
end
end
end
describe PostsController do
describe 'short_link' do
@ -12,43 +53,16 @@ describe PostsController do
end
describe 'show' do
let(:user) { log_in }
let(:post) { Fabricate(:post, user: user) }
it 'ensures the user can see the post' do
Guardian.any_instance.expects(:can_see?).with(post).returns(false)
xhr :get, :show, id: post.id
response.should be_forbidden
include_examples 'finding and showing post' do
let(:action) { :show }
let(:params) { {id: post.id} }
end
end
it 'succeeds' do
xhr :get, :show, id: post.id
response.should be_success
end
context "deleted post" do
before do
post.trash!(user)
end
it "can't find deleted posts as an anonymous user" do
xhr :get, :show, id: post.id
response.should be_forbidden
end
it "can't find deleted posts as a regular user" do
log_in(:user)
xhr :get, :show, id: post.id
response.should be_forbidden
end
it "can find posts as a moderator" do
log_in(:moderator)
xhr :get, :show, id: post.id
response.should be_success
end
describe 'by_number' do
include_examples 'finding and showing post' do
let(:action) { :by_number }
let(:params) { {topic_id: post.topic_id, post_number: post.post_number} }
end
end