cirandas.net

ref: master

plugins/vote/test/functional/vote_plugin_profile_controller_test.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require 'test_helper'
require_relative '../../controllers/vote_plugin_profile_controller'

class VotePluginProfileControllerTest < ActionController::TestCase

  def setup
    @profile = create_user('profile').person
    @article = TextArticle.create!(:profile => @profile, :name => 'An article')
    @comment = Comment.new(:source => @article, :author => @profile, :body => 'test')
    @comment.save!
    login_as(@profile.identifier)
    @environment = Environment.default
    @environment.enable_plugin(VotePlugin)
    self.stubs(:user).returns(@profile)
  end

  attr_reader :profile, :comment, :environment, :article

  should 'do not vote if user is not logged in' do
    logout
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 1
    assert_response 401
  end

  should 'not vote if value is not allowed' do
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 4
    refute profile.voted_on?(comment)
  end

  should 'not vote in a disallowed model' do
    xhr :post, :vote, :profile => profile.identifier, :id => environment.id, :model => 'environment', :vote => 1
    assert profile.votes.empty?
  end

  should 'not vote if a target is archived' do
    article = Article.create!(:profile => profile, :name => 'Archived article', :archived => false)
    comment = Comment.create!(:body => 'Comment test', :source => article, :author => profile)
    xhr :post, :vote, :profile => profile.identifier, :id => article.id, :model => 'article', :vote => 1
    assert !profile.votes.empty?

    article.update_attributes(:archived => true)
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 1

    assert !profile.voted_for?(comment)
  end

  should 'like comment' do
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 1
    assert profile.voted_for?(comment)
  end

  should 'unlike comment' do
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 1
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 1
    refute profile.voted_for?(comment)
  end

  should 'dislike comment' do
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => -1
    assert profile.voted_against?(comment)
  end

  should 'undislike comment' do
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => -1
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => -1
    refute profile.voted_against?(comment)
  end

  should 'dislike a liked comment' do
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 1
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => -1
    assert profile.voted_against?(comment)
  end

  should 'like a disliked comment' do
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => -1
    xhr :post, :vote, :profile => profile.identifier, :id => comment.id, :model => 'comment', :vote => 1
    assert profile.voted_for?(comment)
  end

  should 'like article' do
    xhr :post, :vote, :profile => profile.identifier, :id => article.id, :model => 'article', :vote => 1
    assert profile.voted_for?(article)
  end

  should 'update views with new vote state' do
    xhr :post, :vote, :profile => profile.identifier, :id => article.id, :model => 'article', :vote => 1
    assert_select_rjs :replace do
      assert_select "#vote_article_#{article.id}_1"
      assert_select "#vote_article_#{article.id}_-1"
    end
  end

end