Finish port to fabrication
[shapado:piglops-shapado.git] / spec / controllers / votes_controller_spec.rb
1 require 'spec_helper'
2
3 describe VotesController do
4   include Devise::TestHelpers
5
6   before (:each) do
7     stub_group
8     @group = Fabricate(:group)
9     @user = Fabricate(:user)
10     @user.join!(@group)
11     @user.update_reputation(120, @group)
12     @user.reload
13     stub_authentication @user
14     @question = Fabricate(:question, :group => @group)
15     @voteable = Fabricate(:answer, :group => @group, :question => @question)
16   end
17
18   describe "GET 'index'" do
19     it "should redirect to root path" do
20       get 'index'
21       response.should redirect_to root_path
22     end
23   end
24
25   describe "POST 'create'" do
26     before(:each) do
27       stub_group(@voteable.group)
28       @vote_attrs = {"vote_up" => 1, :question_id => @voteable.question_id}
29     end
30
31     it "should be successful" do
32       @vote_attrs.merge!(:answer_id => @voteable.id)
33       post 'create', @vote_attrs
34       response.should redirect_to root_path
35     end
36
37     it "should be successful for js format" do
38       @vote_attrs.merge!(:answer_id => @voteable.id, :format => "js")
39       post 'create', @vote_attrs
40       body = JSON.load(response.body)
41       body["average"].should == 1
42
43       other_user = Fabricate(:user)
44       other_user.join!(@group)
45       other_user.update_reputation(60, @group)
46       stub_authentication(other_user)
47       @vote_attrs.merge!(:answer_id => @voteable.id, :format => "js")
48       post 'create', @vote_attrs
49       body = JSON.load(response.body)
50       body["average"].should == 2
51     end
52
53     it "should revoke the vote" do
54       @vote_attrs.merge!(:answer_id => @voteable.id, :format => "js")
55       post 'create', @vote_attrs
56
57       @vote_attrs.merge!(:answer_id => @voteable.id, :format => "js")
58       post 'create', @vote_attrs
59       body = JSON.load(response.body)
60       body["average"].should == 0
61     end
62
63     it "should change the vote" do
64       @vote_attrs.merge!(:answer_id => @voteable.id, :format => "js")
65       post 'create', @vote_attrs
66
67       @vote_attrs.delete("vote_up")
68       @vote_attrs["vote_down"] = 1
69       post 'create', @vote_attrs
70       body = JSON.load(response.body)
71       body["average"].should == -1
72
73       @vote_attrs.delete("vote_down")
74       @vote_attrs["vote_up"] = 1
75       post 'create', @vote_attrs
76       body = JSON.load(response.body)
77       body["average"].should == 1
78     end
79   end
80 end