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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# frozen_string_literal: true
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
describe CommentsController, :type => :controller do
before do
sign_in alice, scope: :user
end
describe '#create' do
let(:comment_hash) {
{:text =>"facebook, is that you?",
:post_id =>"#{@post.id}"}
}
context "on my own post" do
before do
aspect_to_post = alice.aspects.where(:name => "generic").first
@post = alice.post :status_message, :text => 'GIANTS', :to => aspect_to_post
end
it 'responds to format json' do
post :create, params: comment_hash, format: :json
expect(response.code).to eq('201')
expect(response.body).to match comment_hash[:text]
end
it 'responds to format mobile' do
post :create, params: comment_hash, format: :mobile
expect(response).to be_successful
end
end
context "on a post from a contact" do
before do
aspect_to_post = bob.aspects.where(:name => "generic").first
@post = bob.post :status_message, :text => 'GIANTS', :to => aspect_to_post
end
it 'comments' do
post :create, params: comment_hash
expect(response.code).to eq('201')
end
it "doesn't overwrite author_id" do
new_user = FactoryGirl.create(:user)
comment_hash[:author_id] = new_user.person.id.to_s
post :create, params: comment_hash
expect(Comment.find_by_text(comment_hash[:text]).author_id).to eq(alice.person.id)
end
it "doesn't overwrite id" do
old_comment = alice.comment!(@post, "hello")
comment_hash[:id] = old_comment.id
post :create, params: comment_hash
expect(old_comment.reload.text).to eq('hello')
end
end
it 'posts no comment on a post from a stranger' do
aspect_to_post = eve.aspects.where(:name => "generic").first
@post = eve.post :status_message, :text => 'GIANTS', :to => aspect_to_post
allow(@controller).to receive(:current_user).and_return(alice)
expect(alice).not_to receive(:comment)
post :create, params: comment_hash
expect(response.code).to eq("404")
expect(response.body).to eq(I18n.t("comments.create.error"))
end
end
describe '#destroy' do
before do
aspect_to_post = bob.aspects.where(:name => "generic").first
@message = bob.post(:status_message, :text => "hey", :to => aspect_to_post)
end
context 'your post' do
before do
allow(@controller).to receive(:current_user).and_return(bob)
sign_in bob, scope: :user
end
it "lets the user delete their comment" do
comment = bob.comment!(@message, "hey")
expect(bob).to receive(:retract).with(comment)
delete :destroy, params: {post_id: 1, id: comment.id}, format: :js
expect(response.status).to eq(204)
end
it "lets the user destroy other people's comments" do
comment = alice.comment!(@message, "hey")
expect(bob).to receive(:retract).with(comment)
delete :destroy, params: {post_id: 1, id: comment.id}, format: :js
expect(response.status).to eq(204)
end
end
context "another user's post" do
it "lets the user delete their comment" do
comment = alice.comment!(@message, "hey")
allow(@controller).to receive(:current_user).and_return(alice)
expect(alice).to receive(:retract).with(comment)
delete :destroy, params: {post_id: 1, id: comment.id}, format: :js
expect(response.status).to eq(204)
end
it "does not let the user destroy comments they do not own" do
comment1 = bob.comment!(@message, "hey")
comment2 = eve.comment!(@message, "hey")
allow(@controller).to receive(:current_user).and_return(alice)
expect(alice).not_to receive(:retract).with(comment1)
delete :destroy, params: {post_id: 1, id: comment2.id}, format: :js
expect(response.status).to eq(403)
end
end
it 'renders nothing and 404 on a nonexistent comment' do
delete :destroy, params: {post_id: 1, id: 343_415}
expect(response.status).to eq(404)
expect(response.body.strip).to be_empty
end
end
describe '#index' do
before do
aspect_to_post = bob.aspects.where(:name => "generic").first
@message = bob.post(:status_message, :text => "hey", :to => aspect_to_post.id)
end
it 'works for mobile' do
get :index, params: {post_id: @message.id}, format: :mobile
expect(response).to be_successful
end
it 'returns all the comments for a post' do
comments = [alice, bob, eve].map{ |u| u.comment!(@message, "hey") }
get :index, params: {post_id: @message.id}, format: :json
expect(JSON.parse(response.body).map {|comment| comment["id"] }).to match_array(comments.map(&:id))
end
it 'returns a 404 on a nonexistent post' do
get :index, params: {post_id: 235_236}, format: :json
expect(response.status).to eq(404)
end
it 'returns a 404 on a post that is not visible to the signed in user' do
aspect_to_post = eve.aspects.where(:name => "generic").first
message = eve.post(:status_message, :text => "hey", :to => aspect_to_post.id)
bob.comment!(@message, "hey")
get :index, params: {post_id: message.id}, format: :json
expect(response.status).to eq(404)
end
it "returns a 401 for a private post when logged out" do
bob.comment!(@message, "hey")
sign_out :user
get :index, params: {post_id: @message.id}, format: :json
expect(response.status).to eq(401)
end
end
end
|