require File.dirname(__FILE__) + '/../test_helper' require 'comment_controller' # Re-raise errors caught by the controller. class CommentController; def rescue_action(e) raise e end; end class CommentControllerTest < ActionController::TestCase def setup @controller = CommentController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @profile = create_user('testinguser').person @environment = @profile.environment end attr_reader :profile, :environment should "not be able to remove other people's comments if not moderator or admin" do create_user('normaluser') profile = create_user('testuser').person article = profile.articles.build(:name => 'test') article.save! commenter = create_user('otheruser').person comment = fast_create(Comment, :source_id => article, :title => 'a comment', :body => 'lalala') login_as 'normaluser' # normaluser cannot remove other people's comments assert_no_difference Comment, :count do post :destroy, :profile => profile.identifier, :id => comment.id end end should "not be able to remove other people's comments if not moderator or admin and return json if is an ajax request" do create_user('normaluser') profile = create_user('testuser').person article = profile.articles.build(:name => 'test') article.save! commenter = create_user('otheruser').person comment = fast_create(Comment, :source_id => article, :author_id => commenter, :title => 'a comment', :body => 'lalala') login_as 'normaluser' # normaluser cannot remove other people's comments assert_no_difference Comment, :count do xhr :post, :destroy, :profile => profile.identifier, :id => comment.id assert_response :success end assert_match /\{\"ok\":false\}/, @response.body end should 'be able to remove comments on their articles' do profile = create_user('testuser').person article = profile.articles.build(:name => 'test') article.save! commenter = create_user('otheruser').person comment = fast_create(Comment, :source_id => article, :author_id => commenter, :title => 'a comment', :body => 'lalala') login_as 'testuser' # testuser must be able to remove comments in his articles assert_difference Comment, :count, -1 do xhr :post, :destroy, :profile => profile.identifier, :id => comment.id assert_response :success end assert_match /\{\"ok\":true\}/, @response.body end should 'be able to remove comments of their images' do profile = create_user('testuser').person image = UploadedFile.create!(:profile => profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) image.save! commenter = create_user('otheruser').person comment = fast_create(Comment, :source_id => image, :author_id => commenter, :title => 'a comment', :body => 'lalala') login_as 'testuser' # testuser must be able to remove comments in his articles assert_difference Comment, :count, -1 do xhr :post, :destroy, :profile => profile.identifier, :id => comment.id assert_response :success end end should 'be able to remove comments if is moderator' do commenter = create_user('commenter_user').person community = Community.create!(:name => 'Community test', :identifier => 'community-test') article = community.articles.create!(:name => 'test', :profile => community) comment = fast_create(Comment, :source_id => article, :author_id => commenter, :title => 'a comment', :body => 'lalala') community.add_moderator(profile) login_as profile.identifier assert_difference Comment, :count, -1 do xhr :post, :destroy, :profile => community.identifier, :id => comment.id assert_response :success end assert_match /\{\"ok\":true\}/, @response.body end should 'be able to remove comment' do profile = create_user('testuser').person article = profile.articles.build(:name => 'test') article.save! comment = fast_create(Comment, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'lalala') login_as 'testuser' assert_difference Comment, :count, -1 do xhr :post, :destroy, :profile => profile.identifier, :id => comment.id assert_response :success end end should 'display not found page if a user should try to make a cross comment' do page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') other_person = create_user('otheruser').person other_page = other_person.articles.create!(:name => 'myarticle', :body => 'the body of the text') assert_no_difference Comment, :count do xhr :post, :create, :profile => profile.identifier, :id => other_page.id, :comment => { :title => 'crap!', :body => 'I think that this article is crap' } end assert_match /not found/, @response.body end should 'not be able to post comment if article do not accept it' do page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text', :accept_comments => false) assert_no_difference Comment, :count do xhr :post, :create, :profile => profile.identifier, :id => page.id, :comment => { :title => 'crap!', :body => 'I think that this article is crap' } end assert_match /Comment not allowed in this article/, @response.body end should "the author's comment be the logged user" do page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') login_as profile.identifier xhr :post, :create, :profile => profile.identifier, :id => page.id, :comment => { :title => 'crap!', :body => 'I think that this article is crap' } assert_equal profile, assigns(:comment).author end should "the articles's comment be the article passed as parameter" do page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') login_as profile.identifier xhr :post, :create, :profile => profile.identifier, :id => page.id, :comment => { :title => 'crap!', :body => 'I think that this article is crap' } assert_equal page, assigns(:comment).article end should 'show comment form opened on error' do login_as profile.identifier page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') xhr :post, :create, :profile => @profile.identifier, :id => page.id, :comment => { :title => '', :body => '' }, :confirm => 'true' response = JSON.parse @response.body assert_match /