mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 15:32:26 +08:00
Merge pull request #1971 from velesin/posts_controller_refactoring
Refactors PostsController and adds unit tests.
This commit is contained in:
commit
43612e9fde
|
@ -64,7 +64,7 @@ class PostsController < ApplicationController
|
||||||
post = post.first
|
post = post.first
|
||||||
post.image_sizes = params[:image_sizes] if params[:image_sizes].present?
|
post.image_sizes = params[:image_sizes] if params[:image_sizes].present?
|
||||||
|
|
||||||
if !guardian.can_edit?(post) && post.user_id == current_user.id && post.edit_time_limit_expired?
|
if too_late_to(:edit, post)
|
||||||
render json: {errors: [I18n.t('too_late_to_edit')]}, status: 422
|
render json: {errors: [I18n.t('too_late_to_edit')]}, status: 422
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -134,7 +134,7 @@ class PostsController < ApplicationController
|
||||||
def destroy
|
def destroy
|
||||||
post = find_post_from_params
|
post = find_post_from_params
|
||||||
|
|
||||||
if !guardian.can_delete_post?(post) && post.user_id == current_user.id && post.edit_time_limit_expired?
|
if too_late_to(:delete_post, post)
|
||||||
render json: {errors: [I18n.t('too_late_to_edit')]}, status: 422
|
render json: {errors: [I18n.t('too_late_to_edit')]}, status: 422
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -269,4 +269,8 @@ class PostsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def too_late_to(action, post)
|
||||||
|
!guardian.send("can_#{action}?", post) && post.user_id == current_user.id && post.edit_time_limit_expired?
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -79,6 +79,16 @@ describe PostsController do
|
||||||
let(:user) { log_in(:moderator) }
|
let(:user) { log_in(:moderator) }
|
||||||
let(:post) { Fabricate(:post, user: user, post_number: 2) }
|
let(:post) { Fabricate(:post, user: user, post_number: 2) }
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
it "raises an error when the user doesn't have permission to see the post" do
|
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)
|
Guardian.any_instance.expects(:can_delete?).with(post).returns(false)
|
||||||
xhr :delete, :destroy, id: post.id
|
xhr :delete, :destroy, id: post.id
|
||||||
|
@ -195,6 +205,16 @@ describe PostsController do
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
it 'passes the image sizes through' do
|
it 'passes the image sizes through' do
|
||||||
Post.any_instance.expects(:image_sizes=)
|
Post.any_instance.expects(:image_sizes=)
|
||||||
xhr :put, :update, update_params
|
xhr :put, :update, update_params
|
||||||
|
|
Loading…
Reference in New Issue
Block a user