2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2014-02-19 11:41:17 -05:00
|
|
|
shared_examples 'finding and showing post' do
|
|
|
|
let(:user) { log_in }
|
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-02-19 11:41:17 -05:00
|
|
|
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
|
2013-04-24 04:05:35 -04:00
|
|
|
end
|
|
|
|
|
2014-02-19 11:41:17 -05:00
|
|
|
it 'succeeds' do
|
|
|
|
xhr :get, action, params
|
|
|
|
response.should be_success
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-02-19 11:41:17 -05:00
|
|
|
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
|
2014-09-10 21:31:31 -04:00
|
|
|
response.status.should == 404
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-02-19 11:41:17 -05:00
|
|
|
it "can't find deleted posts as a regular user" do
|
|
|
|
log_in(:user)
|
|
|
|
xhr :get, action, params
|
2014-09-10 21:31:31 -04:00
|
|
|
response.status.should == 404
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-02-19 11:41:17 -05:00
|
|
|
it "can find posts as a moderator" do
|
|
|
|
log_in(:moderator)
|
|
|
|
xhr :get, action, params
|
|
|
|
response.should be_success
|
|
|
|
end
|
2014-08-07 13:12:35 -04:00
|
|
|
|
|
|
|
it "can find posts as a admin" do
|
|
|
|
log_in(:admin)
|
|
|
|
xhr :get, action, params
|
|
|
|
response.should be_success
|
|
|
|
end
|
2014-02-19 11:41:17 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-08 17:49:15 -05:00
|
|
|
|
2014-02-24 12:14:35 -05:00
|
|
|
shared_examples 'action requires login' do |method, action, params|
|
|
|
|
it 'raises an exception when not logged in' do
|
|
|
|
lambda { xhr method, action, params }.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-19 11:41:17 -05:00
|
|
|
describe PostsController do
|
2013-02-08 17:49:15 -05:00
|
|
|
|
2014-06-20 17:06:44 -04:00
|
|
|
describe 'cooked' do
|
|
|
|
before do
|
|
|
|
post = Post.new(cooked: 'wat')
|
|
|
|
PostsController.any_instance.expects(:find_post_from_params).returns(post)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the cooked conent' do
|
|
|
|
xhr :get, :cooked, id: 1234
|
|
|
|
response.should be_success
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
json.should be_present
|
|
|
|
json['cooked'].should == 'wat'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-19 11:41:17 -05:00
|
|
|
describe 'show' do
|
|
|
|
include_examples 'finding and showing post' do
|
|
|
|
let(:action) { :show }
|
|
|
|
let(:params) { {id: post.id} }
|
|
|
|
end
|
2014-04-23 22:42:04 -04:00
|
|
|
|
|
|
|
it 'gets all the expected fields' do
|
|
|
|
# non fabricated test
|
|
|
|
new_post = create_post
|
|
|
|
xhr :get, :show, {id: new_post.id}
|
|
|
|
parsed = JSON.parse(response.body)
|
|
|
|
parsed["topic_slug"].should == new_post.topic.slug
|
|
|
|
parsed["moderator"].should == false
|
|
|
|
parsed["username"].should == new_post.user.username
|
|
|
|
parsed["cooked"].should == new_post.cooked
|
|
|
|
end
|
2014-02-19 11:41:17 -05:00
|
|
|
end
|
2013-02-08 17:49:15 -05:00
|
|
|
|
2014-02-19 11:41:17 -05:00
|
|
|
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} }
|
2013-02-08 17:49:15 -05:00
|
|
|
end
|
2013-08-06 17:42:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'reply_history' do
|
2014-02-20 11:38:13 -05:00
|
|
|
include_examples 'finding and showing post' do
|
|
|
|
let(:action) { :reply_history }
|
|
|
|
let(:params) { {id: post.id} }
|
2013-08-06 17:42:36 -04:00
|
|
|
end
|
2013-02-08 17:49:15 -05:00
|
|
|
|
2014-02-20 11:38:13 -05:00
|
|
|
it 'asks post for reply history' do
|
2013-08-06 17:42:36 -04:00
|
|
|
Post.any_instance.expects(:reply_history)
|
|
|
|
xhr :get, :reply_history, id: post.id
|
2014-02-20 11:38:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'replies' do
|
|
|
|
include_examples 'finding and showing post' do
|
|
|
|
let(:action) { :replies }
|
|
|
|
let(:params) { {post_id: post.id} }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'asks post for replies' do
|
|
|
|
Post.any_instance.expects(:replies)
|
|
|
|
xhr :get, :replies, post_id: post.id
|
2013-08-06 17:42:36 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'delete a post' do
|
2014-02-24 12:14:35 -05:00
|
|
|
include_examples 'action requires login', :delete, :destroy, id: 123
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-02-18 11:19:38 -05:00
|
|
|
it 'does not allow to destroy when edit time limit expired' do
|
|
|
|
Guardian.any_instance.stubs(:can_delete_post?).with(post).returns(false)
|
|
|
|
Post.any_instance.stubs(:edit_time_limit_expired?).returns(true)
|
|
|
|
|
|
|
|
xhr :delete, :destroy, id: post.id
|
|
|
|
|
|
|
|
response.status.should == 422
|
|
|
|
JSON.parse(response.body)['errors'].should include(I18n.t('too_late_to_edit'))
|
|
|
|
end
|
|
|
|
|
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
|
2014-10-01 11:40:13 -04:00
|
|
|
PostDestroyer.expects(:new).returns(destroyer)
|
2013-03-18 17:52:29 -04:00
|
|
|
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
|
2014-02-24 12:14:35 -05:00
|
|
|
include_examples 'action requires login', :put, :recover, post_id: 123
|
2013-02-07 15:12:55 -05:00
|
|
|
|
|
|
|
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-22 03:48:24 -04:00
|
|
|
it "recovers a post correctly" do
|
|
|
|
topic_id = create_post.topic_id
|
|
|
|
post = create_post(topic_id: topic_id)
|
|
|
|
|
|
|
|
PostDestroyer.new(user, post).destroy
|
2013-02-07 15:12:55 -05:00
|
|
|
xhr :put, :recover, post_id: post.id
|
2013-07-22 03:48:24 -04:00
|
|
|
post.reload
|
|
|
|
post.deleted_at.should == nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'destroy_many' do
|
2014-02-24 12:14:35 -05:00
|
|
|
include_examples 'action requires login', :delete, :destroy_many, post_ids: [123, 345]
|
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) }
|
2013-09-04 11:53:00 -04:00
|
|
|
let!(:post2) { Fabricate(:post, topic_id: post1.topic_id, user: poster, post_number: 3, reply_to_post_number: post1.post_number) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "raises invalid parameters no post_ids" do
|
2013-09-04 11:53:00 -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
|
2013-09-04 20:50:58 -04:00
|
|
|
PostDestroyer.any_instance.expects(:destroy).twice
|
2013-02-05 14:16:51 -05:00
|
|
|
xhr :delete, :destroy_many, post_ids: [post1.id, post2.id]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the highest read data for the forum" do
|
2013-09-04 20:50:58 -04:00
|
|
|
Topic.expects(:reset_highest).twice
|
2013-02-05 14:16:51 -05:00
|
|
|
xhr :delete, :destroy_many, post_ids: [post1.id, post2.id]
|
|
|
|
end
|
|
|
|
|
2013-09-04 11:53:00 -04:00
|
|
|
describe "can delete replies" do
|
|
|
|
|
|
|
|
before do
|
|
|
|
PostReply.create(post_id: post1.id, reply_id: post2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the post and the reply to it" do
|
2013-09-04 20:50:58 -04:00
|
|
|
PostDestroyer.any_instance.expects(:destroy).twice
|
2013-09-04 11:53:00 -04:00
|
|
|
xhr :delete, :destroy_many, post_ids: [post1.id], reply_post_ids: [post1.id]
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'edit a post' do
|
|
|
|
|
2014-02-24 12:14:35 -05:00
|
|
|
include_examples 'action requires login', :put, :update, id: 2
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe 'when logged in' do
|
|
|
|
|
|
|
|
let(:post) { Fabricate(:post, user: log_in) }
|
|
|
|
let(:update_params) do
|
2013-11-15 17:28:16 -05:00
|
|
|
{
|
|
|
|
id: post.id,
|
|
|
|
post: { raw: 'edited body', edit_reason: 'typo' },
|
|
|
|
image_sizes: { 'http://image.com/image.jpg' => {'width' => 123, 'height' => 456} },
|
|
|
|
}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-02-18 11:19:38 -05:00
|
|
|
it 'does not allow to update when edit time limit expired' do
|
|
|
|
Guardian.any_instance.stubs(:can_edit?).with(post).returns(false)
|
|
|
|
Post.any_instance.stubs(:edit_time_limit_expired?).returns(true)
|
|
|
|
|
|
|
|
xhr :put, :update, update_params
|
|
|
|
|
|
|
|
response.status.should == 422
|
|
|
|
JSON.parse(response.body)['errors'].should include(I18n.t('too_late_to_edit'))
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'passes the image sizes through' do
|
|
|
|
Post.any_instance.expects(:image_sizes=)
|
|
|
|
xhr :put, :update, update_params
|
|
|
|
end
|
|
|
|
|
2013-11-15 17:28:16 -05:00
|
|
|
it 'passes the edit reason through' do
|
|
|
|
Post.any_instance.expects(:edit_reason=)
|
|
|
|
xhr :put, :update, update_params
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
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-10-02 10:59:57 -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
|
2014-01-07 10:32:09 -05:00
|
|
|
Guardian.any_instance.expects(:can_edit?).with(post).at_least_once.returns(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
xhr :put, :update, update_params
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls revise with valid parameters" do
|
2013-11-15 17:28:16 -05:00
|
|
|
PostRevisor.any_instance.expects(:revise!).with(post.user, 'edited body', edit_reason: 'typo')
|
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
|
|
|
|
|
2014-02-24 12:14:35 -05:00
|
|
|
include_examples 'action requires login', :put, :bookmark, post_id: 2
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
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
|
2014-06-04 11:41:11 -04:00
|
|
|
Guardian.any_instance.expects(:can_see?).with(post).returns(false).once
|
2014-05-09 11:49:39 -04:00
|
|
|
|
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
|
|
|
|
|
2014-05-13 08:53:11 -04:00
|
|
|
describe "wiki" do
|
|
|
|
|
|
|
|
include_examples "action requires login", :put, :wiki, post_id: 2
|
|
|
|
|
|
|
|
describe "when logged in" do
|
|
|
|
let(:user) {log_in}
|
|
|
|
let(:post) {Fabricate(:post, user: user)}
|
|
|
|
|
2014-09-11 10:04:40 -04:00
|
|
|
it "raises an error if the user doesn't have permission to wiki the post" do
|
2014-05-13 08:53:11 -04:00
|
|
|
Guardian.any_instance.expects(:can_wiki?).returns(false)
|
|
|
|
|
|
|
|
xhr :put, :wiki, post_id: post.id, wiki: 'true'
|
|
|
|
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can wiki a post" do
|
|
|
|
Guardian.any_instance.expects(:can_wiki?).returns(true)
|
|
|
|
|
|
|
|
xhr :put, :wiki, post_id: post.id, wiki: 'true'
|
|
|
|
|
|
|
|
post.reload
|
2014-09-25 11:44:48 -04:00
|
|
|
post.wiki.should == true
|
2014-05-13 08:53:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can unwiki a post" do
|
|
|
|
wikied_post = Fabricate(:post, user: user, wiki: true)
|
|
|
|
Guardian.any_instance.expects(:can_wiki?).returns(true)
|
|
|
|
|
|
|
|
xhr :put, :wiki, post_id: wikied_post.id, wiki: 'false'
|
|
|
|
|
|
|
|
wikied_post.reload
|
2014-09-25 11:44:48 -04:00
|
|
|
wikied_post.wiki.should == false
|
2014-05-13 08:53:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-09-11 10:04:40 -04:00
|
|
|
describe "post_type" do
|
|
|
|
|
|
|
|
include_examples "action requires login", :put, :post_type, post_id: 2
|
|
|
|
|
|
|
|
describe "when logged in" do
|
|
|
|
let(:user) {log_in}
|
|
|
|
let(:post) {Fabricate(:post, user: user)}
|
|
|
|
|
|
|
|
it "raises an error if the user doesn't have permission to change the post type" do
|
|
|
|
Guardian.any_instance.expects(:can_change_post_type?).returns(false)
|
|
|
|
|
|
|
|
xhr :put, :post_type, post_id: post.id, post_type: 2
|
|
|
|
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can change the post type" do
|
|
|
|
Guardian.any_instance.expects(:can_change_post_type?).returns(true)
|
|
|
|
|
|
|
|
xhr :put, :post_type, post_id: post.id, post_type: 2
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
post.post_type.should == 2
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "rebake" do
|
|
|
|
|
|
|
|
include_examples "action requires login", :put, :rebake, post_id: 2
|
|
|
|
|
|
|
|
describe "when logged in" do
|
|
|
|
let(:user) {log_in}
|
|
|
|
let(:post) {Fabricate(:post, user: user)}
|
|
|
|
|
|
|
|
it "raises an error if the user doesn't have permission to rebake the post" do
|
|
|
|
Guardian.any_instance.expects(:can_rebake?).returns(false)
|
|
|
|
|
|
|
|
xhr :put, :rebake, post_id: post.id
|
|
|
|
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can rebake the post" do
|
|
|
|
Guardian.any_instance.expects(:can_rebake?).returns(true)
|
|
|
|
|
|
|
|
xhr :put, :rebake, post_id: post.id
|
|
|
|
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
describe 'creating a post' do
|
|
|
|
|
2014-02-24 12:14:35 -05:00
|
|
|
include_examples 'action requires login', :post, :create
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-07-14 01:59:58 -04:00
|
|
|
context 'api' do
|
|
|
|
it 'allows dupes through' do
|
|
|
|
raw = "this is a test post 123 #{SecureRandom.hash}"
|
|
|
|
title = "this is a title #{SecureRandom.hash}"
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
master_key = ApiKey.create_master_key.key
|
|
|
|
|
|
|
|
xhr :post, :create, {api_username: user.username, api_key: master_key, raw: raw, title: title, wpid: 1}
|
|
|
|
response.should be_success
|
|
|
|
original = response.body
|
|
|
|
|
|
|
|
xhr :post, :create, {api_username: user.username_lower, api_key: master_key, raw: raw, title: title, wpid: 2}
|
|
|
|
response.should be_success
|
|
|
|
|
|
|
|
response.body.should == original
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
describe 'when logged in' do
|
|
|
|
|
|
|
|
let!(:user) { log_in }
|
2014-09-08 11:11:56 -04:00
|
|
|
let(:moderator) { log_in(:moderator) }
|
2013-02-05 14:16:51 -05:00
|
|
|
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-07-28 22:25:19 -04:00
|
|
|
it 'protects against dupes' do
|
2014-07-14 01:59:58 -04:00
|
|
|
raw = "this is a test post 123 #{SecureRandom.hash}"
|
|
|
|
title = "this is a title #{SecureRandom.hash}"
|
2013-07-28 22:25:19 -04:00
|
|
|
|
2014-07-14 01:59:58 -04:00
|
|
|
xhr :post, :create, {raw: raw, title: title, wpid: 1}
|
2013-07-28 22:25:19 -04:00
|
|
|
response.should be_success
|
|
|
|
|
2014-07-14 01:59:58 -04:00
|
|
|
xhr :post, :create, {raw: raw, title: title, wpid: 2}
|
|
|
|
response.should_not be_success
|
2013-07-28 22:25:19 -04:00
|
|
|
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-09-27 04:55:50 -04:00
|
|
|
PostCreator.expects(:new).with(user, has_entries('image_sizes' => {'width' => '100', 'height' => '200'})).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', image_sizes: {width: '100', height: '200'}}
|
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
|
|
|
|
2014-09-08 11:11:56 -04:00
|
|
|
context "is_warning" do
|
|
|
|
it "doesn't pass `is_warning` through if you're not staff" do
|
|
|
|
PostCreator.expects(:new).with(user, Not(has_entries('is_warning' => true))).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', archetype: 'private_message', is_warning: 'true'}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes `is_warning` through if you're staff" do
|
|
|
|
PostCreator.expects(:new).with(moderator, has_entries('is_warning' => true)).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', archetype: 'private_message', is_warning: 'true'}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes `is_warning` as false through if you're staff" do
|
|
|
|
PostCreator.expects(:new).with(moderator, has_entries('is_warning' => false)).returns(post_creator)
|
|
|
|
xhr :post, :create, {raw: 'hello', archetype: 'private_message', is_warning: 'false'}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-04 14:05:50 -05:00
|
|
|
describe "revisions" do
|
|
|
|
|
|
|
|
let(:post_revision) { Fabricate(:post_revision) }
|
|
|
|
|
|
|
|
it "throws an exception when revision is < 2" do
|
|
|
|
expect {
|
|
|
|
xhr :get, :revisions, post_id: post_revision.post_id, revision: 1
|
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when edit history is not visible to the public" do
|
|
|
|
|
|
|
|
before { SiteSetting.stubs(:edit_history_visible_to_public).returns(false) }
|
|
|
|
|
2014-03-13 10:47:37 -04:00
|
|
|
it "ensures anonymous cannot see the revisions" do
|
|
|
|
xhr :get, :revisions, post_id: post_revision.post_id, revision: post_revision.number
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ensures regular user cannot see the revisions" do
|
|
|
|
u = log_in(:user)
|
2014-02-04 14:05:50 -05:00
|
|
|
xhr :get, :revisions, post_id: post_revision.post_id, revision: post_revision.number
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ensures staff can see the revisions" do
|
|
|
|
log_in(:admin)
|
|
|
|
xhr :get, :revisions, post_id: post_revision.post_id, revision: post_revision.number
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ensures poster can see the revisions" do
|
|
|
|
user = log_in(:active_user)
|
2014-03-13 10:47:37 -04:00
|
|
|
post = Fabricate(:post, user: user)
|
|
|
|
pr = Fabricate(:post_revision, user: user, post: post)
|
2014-02-04 14:05:50 -05:00
|
|
|
xhr :get, :revisions, post_id: pr.post_id, revision: pr.number
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
2014-03-13 10:47:37 -04:00
|
|
|
it "ensures trust level 4 can see the revisions" do
|
2014-09-05 02:52:40 -04:00
|
|
|
log_in(:trust_level_4)
|
2014-03-13 10:47:37 -04:00
|
|
|
xhr :get, :revisions, post_id: post_revision.post_id, revision: post_revision.number
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
2014-02-04 14:05:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when edit history is visible to everyone" do
|
|
|
|
|
|
|
|
before { SiteSetting.stubs(:edit_history_visible_to_public).returns(true) }
|
|
|
|
|
|
|
|
it "ensures anyone can see the revisions" do
|
|
|
|
xhr :get, :revisions, post_id: post_revision.post_id, revision: post_revision.number
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "deleted post" do
|
|
|
|
let(:admin) { log_in(:admin) }
|
|
|
|
let(:deleted_post) { Fabricate(:post, user: admin) }
|
|
|
|
let(:deleted_post_revision) { Fabricate(:post_revision, user: admin, post: deleted_post) }
|
|
|
|
|
|
|
|
before { deleted_post.trash!(admin) }
|
|
|
|
|
|
|
|
it "also work on deleted post" do
|
|
|
|
xhr :get, :revisions, post_id: deleted_post_revision.post_id, revision: deleted_post_revision.number
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-12 10:30:10 -04:00
|
|
|
context "deleted topic" do
|
|
|
|
let(:admin) { log_in(:admin) }
|
|
|
|
let(:deleted_topic) { Fabricate(:topic, user: admin) }
|
|
|
|
let(:post) { Fabricate(:post, user: admin, topic: deleted_topic) }
|
|
|
|
let(:post_revision) { Fabricate(:post_revision, user: admin, post: post) }
|
|
|
|
|
|
|
|
before { deleted_topic.trash!(admin) }
|
|
|
|
|
|
|
|
it "also work on deleted topic" do
|
|
|
|
xhr :get, :revisions, post_id: post_revision.post_id, revision: post_revision.number
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-04 14:05:50 -05:00
|
|
|
end
|
|
|
|
|
2014-04-03 11:30:43 -04:00
|
|
|
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
|
2014-07-16 15:04:55 -04:00
|
|
|
|
|
|
|
describe "flagged posts" do
|
|
|
|
|
|
|
|
include_examples "action requires login", :get, :flagged_posts, username: "system"
|
|
|
|
|
|
|
|
describe "when logged in" do
|
|
|
|
before { log_in }
|
|
|
|
|
|
|
|
it "raises an error if the user doesn't have permission to see the flagged posts" do
|
|
|
|
Guardian.any_instance.expects(:can_see_flagged_posts?).returns(false)
|
|
|
|
xhr :get, :flagged_posts, username: "system"
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can see the flagged posts when authorized" do
|
|
|
|
Guardian.any_instance.expects(:can_see_flagged_posts?).returns(true)
|
|
|
|
xhr :get, :flagged_posts, username: "system"
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
2014-10-09 10:10:16 -04:00
|
|
|
it "only shows agreed and deferred flags" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
post_agreed = create_post(user: user)
|
|
|
|
post_deferred = create_post(user: user)
|
|
|
|
post_disagreed = create_post(user: user)
|
|
|
|
|
|
|
|
moderator = Fabricate(:moderator)
|
|
|
|
PostAction.act(moderator, post_agreed, PostActionType.types[:spam])
|
|
|
|
PostAction.act(moderator, post_deferred, PostActionType.types[:off_topic])
|
|
|
|
PostAction.act(moderator, post_disagreed, PostActionType.types[:inappropriate])
|
|
|
|
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
PostAction.agree_flags!(post_agreed, admin)
|
|
|
|
PostAction.defer_flags!(post_deferred, admin)
|
|
|
|
PostAction.clear_flags!(post_disagreed, admin)
|
|
|
|
|
|
|
|
Guardian.any_instance.expects(:can_see_flagged_posts?).returns(true)
|
|
|
|
xhr :get, :flagged_posts, username: user.username
|
|
|
|
response.should be_success
|
|
|
|
|
|
|
|
JSON.parse(response.body).length.should == 2
|
|
|
|
end
|
|
|
|
|
2014-07-16 15:04:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "deleted posts" do
|
|
|
|
|
|
|
|
include_examples "action requires login", :get, :deleted_posts, username: "system"
|
|
|
|
|
|
|
|
describe "when logged in" do
|
|
|
|
before { log_in }
|
|
|
|
|
|
|
|
it "raises an error if the user doesn't have permission to see the deleted posts" do
|
|
|
|
Guardian.any_instance.expects(:can_see_deleted_posts?).returns(false)
|
|
|
|
xhr :get, :deleted_posts, username: "system"
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can see the deleted posts when authorized" do
|
|
|
|
Guardian.any_instance.expects(:can_see_deleted_posts?).returns(true)
|
|
|
|
xhr :get, :deleted_posts, username: "system"
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
2014-10-09 10:10:16 -04:00
|
|
|
it "only shows posts deleted by other users" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
|
|
|
|
post_not_deleted = create_post(user: user)
|
|
|
|
post_deleted_by_user = create_post(user: user)
|
|
|
|
post_deleted_by_admin = create_post(user: user)
|
|
|
|
|
|
|
|
PostDestroyer.new(user, post_deleted_by_user).destroy
|
|
|
|
PostDestroyer.new(admin, post_deleted_by_admin).destroy
|
|
|
|
|
|
|
|
Guardian.any_instance.expects(:can_see_deleted_posts?).returns(true)
|
|
|
|
xhr :get, :deleted_posts, username: user.username
|
|
|
|
response.should be_success
|
|
|
|
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
data.length.should == 1
|
|
|
|
data[0]["id"].should == post_deleted_by_admin.id
|
|
|
|
data[0]["deleted_by"]["id"].should == admin.id
|
|
|
|
end
|
|
|
|
|
2014-07-16 15:04:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|