Welcome to mirror list, hosted at ThFree Co, Russian Federation.

likes_controller_spec.rb « controllers « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8b27516b9f73665c74f3693ccaea5901a9299a59 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  #   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

require 'spec_helper'

describe LikesController do
  before do
    @alices_aspect = alice.aspects.where(:name => "generic").first
    @bobs_aspect = bob.aspects.where(:name => "generic").first

    sign_in :user, alice
  end

  [Comment, Post].each do |class_const|
    context class_const.to_s do
        let(:id_field){
          "#{class_const.to_s.underscore}_id"
        }

      describe '#create' do
        let(:like_hash) {
          {:positive => 1,
           id_field => "#{@target.id}"}
        }
        let(:dislike_hash) {
          {:positive => 0,
           id_field => "#{@target.id}"}
        }

        context "on my own post" do
          it 'succeeds' do
            @target = alice.post :status_message, :text => "AWESOME", :to => @alices_aspect.id
            @target = alice.comment!(@target, "hey") if class_const == Comment
            post :create, like_hash.merge(:format => :json)
            response.code.should == '201'
          end
        end

        context "on a post from a contact" do
          before do
            @target = bob.post(:status_message, :text => "AWESOME", :to => @bobs_aspect.id)
            @target = bob.comment!(@target, "hey") if class_const == Comment
          end

          it 'likes' do
            post :create, like_hash
            response.code.should == '201'
          end

          it 'dislikes' do
            post :create, dislike_hash
            response.code.should == '201'
          end

          it "doesn't post multiple times" do
            alice.like!(@target)
            post :create, dislike_hash
            response.code.should == '422'
          end
        end

        context "on a post from a stranger" do
          before do
            @target = eve.post :status_message, :text => "AWESOME", :to => eve.aspects.first.id
            @target = eve.comment!(@target, "hey") if class_const == Comment
          end

          it "doesn't post" do
            alice.should_not_receive(:like!)
            post :create, like_hash
            response.code.should == '422'
          end
        end
      end

      describe '#index' do
        before do
          @message = alice.post(:status_message, :text => "hey", :to => @alices_aspect.id)
          @message = alice.comment!(@message, "hey") if class_const == Comment
        end

        it 'generates a jasmine fixture', :fixture => true do
          get :index, id_field => @message.id

          save_fixture(response.body, "ajax_likes_on_#{class_const.to_s.underscore}")
        end

        it 'returns a 404 for a post not visible to the user' do
          sign_in eve
          expect{get :index, id_field => @message.id}.to raise_error(ActiveRecord::RecordNotFound)
        end

        it 'returns an array of likes for a post' do
          like = bob.like!(@message)
          get :index, id_field => @message.id
          assigns[:likes].map(&:id).should == @message.likes.map(&:id)
        end

        it 'returns an empty array for a post with no likes' do
          get :index, id_field => @message.id
          assigns[:likes].should == []
        end
      end

      describe '#destroy' do
        before do
          @message = bob.post(:status_message, :text => "hey", :to => @alices_aspect.id)
          @message = bob.comment!(@message, "hey") if class_const == Comment
          @like = alice.like!(@message)
        end

        it 'lets a user destroy their like' do
          expect {
            delete :destroy, :format => :json, id_field => @like.target_id, :id => @like.id
          }.should change(Like, :count).by(-1)
          response.status.should == 204
        end

        it 'does not let a user destroy other likes' do
          like2 = eve.like!(@message)

          like_count = Like.count
          expect {
            delete :destroy, :format => :json, id_field => like2.target_id, :id => like2.id
          }.should raise_error(ActiveRecord::RecordNotFound)

          Like.count.should == like_count

        end
      end
    end
  end
end