2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe PostsController do
|
|
|
|
|
|
|
|
|
2013-04-24 04:05:35 -04:00
|
|
|
describe 'short_link' do
|
|
|
|
it 'logs the incoming link once' do
|
|
|
|
IncomingLink.expects(:add).once.returns(true)
|
|
|
|
p = Fabricate(:post)
|
|
|
|
get :short_link, post_id: p.id, user_id: 999
|
|
|
|
response.should be_redirect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
describe 'show' do
|
|
|
|
|
2013-07-09 15:20:18 -04:00
|
|
|
let(:user) { log_in }
|
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'suceeds' do
|
|
|
|
xhr :get, :show, id: post.id
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
2013-02-08 17:49:15 -05:00
|
|
|
context "deleted post" do
|
|
|
|
|
|
|
|
before do
|
2013-07-09 15:20:18 -04:00
|
|
|
post.trash!(user)
|
2013-02-08 17:49:15 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "can't find deleted posts as an anonymous user" do
|
2013-02-08 17:49:15 -05:00
|
|
|
xhr :get, :show, id: post.id
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "can't find deleted posts as a regular user" do
|
2013-02-08 17:49:15 -05:00
|
|
|
log_in(:user)
|
|
|
|
xhr :get, :show, id: post.id
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "can find posts as a moderator" do
|
2013-02-08 17:49:15 -05:00
|
|
|
log_in(:moderator)
|
|
|
|
xhr :get, :show, id: post.id
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'versions' do
|
|
|
|
|
2013-06-19 16:43:16 -04:00
|
|
|
shared_examples 'posts_controller versions examples' do
|
2013-02-05 14:16:51 -05:00
|
|
|
it "raises an error if the user doesn't have permission to see the post" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(post).returns(false)
|
|
|
|
xhr :get, :versions, post_id: post.id
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders JSON' do
|
|
|
|
xhr :get, :versions, post_id: post.id
|
|
|
|
::JSON.parse(response.body).should be_present
|
|
|
|
end
|
2013-06-19 16:43:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not logged in' do
|
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
include_examples 'posts_controller versions examples'
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-19 16:43:16 -04:00
|
|
|
context 'when logged in' do
|
|
|
|
let(:post) { Fabricate(:post, user: log_in) }
|
|
|
|
include_examples 'posts_controller versions examples'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'delete a post' do
|
|
|
|
it 'raises an exception when not logged in' do
|
|
|
|
lambda { xhr :delete, :destroy, id: 123 }.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when logged in' do
|
|
|
|
|
2013-02-07 15:12:55 -05:00
|
|
|
let(:user) { log_in(:moderator) }
|
|
|
|
let(:post) { Fabricate(:post, user: user, post_number: 2) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "raises an error when the user doesn't have permission to see the post" do
|
|
|
|
Guardian.any_instance.expects(:can_delete?).with(post).returns(false)
|
|
|
|
xhr :delete, :destroy, id: post.id
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
2013-03-18 17:52:29 -04:00
|
|
|
it "uses a PostDestroyer" do
|
|
|
|
destroyer = mock
|
|
|
|
PostDestroyer.expects(:new).with(user, post).returns(destroyer)
|
|
|
|
destroyer.expects(:destroy)
|
2013-02-05 14:16:51 -05:00
|
|
|
xhr :delete, :destroy, id: post.id
|
|
|
|
end
|
|
|
|
|
2013-02-07 15:12:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'recover a post' do
|
|
|
|
it 'raises an exception when not logged in' do
|
|
|
|
lambda { xhr :put, :recover, post_id: 123 }.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when logged in' do
|
|
|
|
|
|
|
|
let(:user) { log_in(:moderator) }
|
|
|
|
let(:post) { Fabricate(:post, user: user, post_number: 2) }
|
|
|
|
|
|
|
|
it "raises an error when the user doesn't have permission to see the post" do
|
|
|
|
Guardian.any_instance.expects(:can_recover_post?).with(post).returns(false)
|
|
|
|
xhr :put, :recover, post_id: post.id
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
2013-07-09 12:15:55 -04:00
|
|
|
it "calls recover and updates the topic's statistics" do
|
2013-05-07 00:39:01 -04:00
|
|
|
Post.any_instance.expects(:recover!)
|
2013-07-09 12:15:55 -04:00
|
|
|
Topic.any_instance.expects(:update_statistics)
|
2013-02-07 15:12:55 -05:00
|
|
|
xhr :put, :recover, post_id: post.id
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-07 15:12:55 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
describe 'destroy_many' do
|
|
|
|
it 'raises an exception when not logged in' do
|
|
|
|
lambda { xhr :delete, :destroy_many, post_ids: [123, 345] }.should raise_error(Discourse::NotLoggedIn)
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe 'when logged in' do
|
|
|
|
|
|
|
|
let!(:poster) { log_in(:moderator) }
|
|
|
|
let!(:post1) { Fabricate(:post, user: poster, post_number: 2) }
|
|
|
|
let!(:post2) { Fabricate(:post, topic_id: post1.topic_id, user: poster, post_number: 3) }
|
|
|
|
|
|
|
|
it "raises invalid parameters no post_ids" do
|
2013-06-06 03:14:32 -04:00
|
|
|
lambda { xhr :delete, :destroy_many }.should raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises invalid parameters with missing ids" do
|
|
|
|
lambda { xhr :delete, :destroy_many, post_ids: [12345] }.should raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when the user doesn't have permission to delete the posts" do
|
|
|
|
Guardian.any_instance.expects(:can_delete?).with(instance_of(Post)).returns(false)
|
|
|
|
xhr :delete, :destroy_many, post_ids: [post1.id, post2.id]
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the post" do
|
|
|
|
Post.any_instance.expects(:destroy).twice
|
|
|
|
xhr :delete, :destroy_many, post_ids: [post1.id, post2.id]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the highest read data for the forum" do
|
|
|
|
Topic.expects(:reset_highest)
|
|
|
|
xhr :delete, :destroy_many, post_ids: [post1.id, post2.id]
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
describe 'edit a post' do
|
|
|
|
|
|
|
|
it 'raises an exception when not logged in' do
|
|
|
|
lambda { xhr :put, :update, id: 2 }.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when logged in' do
|
|
|
|
|
|
|
|
let(:post) { Fabricate(:post, user: log_in) }
|
|
|
|
let(:update_params) do
|
2013-02-25 11:42:20 -05:00
|
|
|
{id: post.id,
|
2013-02-05 14:16:51 -05:00
|
|
|
post: {raw: 'edited body'},
|
|
|
|
image_sizes: {'http://image.com/image.jpg' => {'width' => 123, 'height' => 456}}}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes the image sizes through' do
|
|
|
|
Post.any_instance.expects(:image_sizes=)
|
|
|
|
xhr :put, :update, update_params
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when the post parameter is missing" do
|
|
|
|
update_params.delete(:post)
|
2013-02-25 11:42:20 -05:00
|
|
|
lambda {
|
2013-02-05 14:16:51 -05:00
|
|
|
xhr :put, :update, update_params
|
2013-06-06 03:14:32 -04:00
|
|
|
}.should raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when the user doesn't have permission to see the post" do
|
|
|
|
Guardian.any_instance.expects(:can_edit?).with(post).returns(false)
|
|
|
|
xhr :put, :update, update_params
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls revise with valid parameters" do
|
2013-02-21 18:09:56 -05:00
|
|
|
PostRevisor.any_instance.expects(:revise!).with(post.user, 'edited body')
|
2013-02-05 14:16:51 -05:00
|
|
|
xhr :put, :update, update_params
|
|
|
|
end
|
|
|
|
|
|
|
|
it "extracts links from the new body" do
|
|
|
|
TopicLink.expects(:extract_from).with(post)
|
|
|
|
xhr :put, :update, update_params
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'bookmark a post' do
|
|
|
|
|
|
|
|
it 'raises an exception when not logged in' do
|
|
|
|
lambda { xhr :put, :bookmark, post_id: 2 }.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when logged in' do
|
|
|
|
|
|
|
|
let(:post) { Fabricate(:post, user: log_in) }
|
|
|
|
|
|
|
|
it "raises an error if the user doesn't have permission to see the post" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(post).returns(false)
|
2013-02-25 11:42:20 -05:00
|
|
|
xhr :put, :bookmark, post_id: post.id, bookmarked: 'true'
|
2013-02-05 14:16:51 -05:00
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a bookmark' do
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.expects(:act).with(post.user, post, PostActionType.types[:bookmark])
|
2013-02-05 14:16:51 -05:00
|
|
|
xhr :put, :bookmark, post_id: post.id, bookmarked: 'true'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes a bookmark' do
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.expects(:remove_act).with(post.user, post, PostActionType.types[:bookmark])
|
2013-02-05 14:16:51 -05:00
|
|
|
xhr :put, :bookmark, post_id: post.id
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'creating a post' do
|
|
|
|
|
|
|
|
it 'raises an exception when not logged in' do
|
|
|
|
lambda { xhr :post, :create }.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when logged in' do
|
|
|
|
|
|
|
|
let!(:user) { log_in }
|
|
|
|
let(:new_post) { Fabricate.build(:post, user: user) }
|
|
|
|
|
2013-06-07 03:52:03 -04:00
|
|
|
it "raises an exception without a raw parameter" do
|
|
|
|
lambda { xhr :post, :create }.should raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls the post creator' do
|
|
|
|
PostCreator.any_instance.expects(:create).returns(new_post)
|
2013-06-07 03:52:03 -04:00
|
|
|
xhr :post, :create, {raw: 'test'}
|
2013-02-05 14:16:51 -05:00
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns JSON of the post' do
|
|
|
|
PostCreator.any_instance.expects(:create).returns(new_post)
|
2013-06-07 03:52:03 -04:00
|
|
|
xhr :post, :create, {raw: 'test'}
|
2013-02-05 14:16:51 -05:00
|
|
|
::JSON.parse(response.body).should be_present
|
|
|
|
end
|
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
context "errors" do
|
|
|
|
|
|
|
|
let(:post_with_errors) { Fabricate.build(:post, user: user)}
|
|
|
|
|
|
|
|
before do
|
|
|
|
post_with_errors.errors.add(:base, I18n.t(:spamming_host))
|
|
|
|
PostCreator.any_instance.stubs(:errors).returns(post_with_errors.errors)
|
|
|
|
PostCreator.any_instance.expects(:create).returns(post_with_errors)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not succeed" do
|
2013-06-07 03:52:03 -04:00
|
|
|
xhr :post, :create, {raw: 'test'}
|
2013-05-10 16:58:23 -04:00
|
|
|
User.any_instance.expects(:flag_linked_posts_as_spam).never
|
|
|
|
response.should_not be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "it triggers flag_linked_posts_as_spam when the post creator returns spam" do
|
|
|
|
PostCreator.any_instance.expects(:spam?).returns(true)
|
|
|
|
User.any_instance.expects(:flag_linked_posts_as_spam)
|
2013-06-07 03:52:03 -04:00
|
|
|
xhr :post, :create, {raw: 'test'}
|
2013-05-10 16:58:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context "parameters" do
|
|
|
|
|
|
|
|
let(:post_creator) { mock }
|
|
|
|
|
|
|
|
before do
|
|
|
|
post_creator.expects(:create).returns(new_post)
|
|
|
|
post_creator.stubs(:errors).returns(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes raw through" do
|
2013-06-07 03:52:03 -04:00
|
|
|
PostCreator.expects(:new).with(user, has_entries('raw' => 'hello')).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello'}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes title through" do
|
2013-06-07 03:52:03 -04:00
|
|
|
PostCreator.expects(:new).with(user, has_entries('title' => 'new topic title')).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', title: 'new topic title'}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes topic_id through" do
|
2013-06-07 03:52:03 -04:00
|
|
|
PostCreator.expects(:new).with(user, has_entries('topic_id' => '1234')).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', topic_id: 1234}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes archetype through" do
|
2013-06-07 03:52:03 -04:00
|
|
|
PostCreator.expects(:new).with(user, has_entries('archetype' => 'private_message')).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', archetype: 'private_message'}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes category through" do
|
2013-06-07 03:52:03 -04:00
|
|
|
PostCreator.expects(:new).with(user, has_entries('category' => 'cool')).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', category: 'cool'}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes target_usernames through" do
|
2013-06-07 03:52:03 -04:00
|
|
|
PostCreator.expects(:new).with(user, has_entries('target_usernames' => 'evil,trout')).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', target_usernames: 'evil,trout'}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes reply_to_post_number through" do
|
2013-06-07 03:52:03 -04:00
|
|
|
PostCreator.expects(:new).with(user, has_entries('reply_to_post_number' => '6789')).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', reply_to_post_number: 6789}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes image_sizes through" do
|
2013-06-07 03:52:03 -04:00
|
|
|
PostCreator.expects(:new).with(user, has_entries('image_sizes' => 'test')).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', image_sizes: 'test'}
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "passes meta_data through" do
|
2013-06-07 03:52:03 -04:00
|
|
|
PostCreator.expects(:new).with(user, has_entries('meta_data' => {'xyz' => 'abc'})).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', meta_data: {xyz: 'abc'}}
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|