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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# 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 NotificationsController, :type => :controller do
before do
sign_in alice, scope: :user
end
describe '#update' do
it 'marks a notification as read if it gets no other information' do
note = FactoryGirl.create(:notification)
expect(Notification).to receive(:where).and_return([note])
expect(note).to receive(:set_read_state).with(true)
get :update, params: {id: note.id}, format: :json
end
it 'marks a notification as read if it is told to' do
note = FactoryGirl.create(:notification)
expect(Notification).to receive(:where).and_return([note])
expect(note).to receive(:set_read_state).with(true)
get :update, params: {id: note.id, set_unread: "false"}, format: :json
end
it 'marks a notification as unread if it is told to' do
note = FactoryGirl.create(:notification)
expect(Notification).to receive(:where).and_return([note])
expect(note).to receive(:set_read_state).with(false)
get :update, params: {id: note.id, set_unread: "true"}, format: :json
end
it "marks a notification as unread without timestamping" do
note = Timecop.travel(1.hour.ago) do
FactoryGirl.create(:notification, recipient: alice, unread: false)
end
get :update, params: {id: note.id, set_unread: "true"}, format: :json
expect(response).to be_success
updated_note = Notification.find(note.id)
expect(updated_note.unread).to eq(true)
expect(updated_note.updated_at.iso8601).to eq(note.updated_at.iso8601)
end
it 'only lets you read your own notifications' do
user2 = bob
FactoryGirl.create(:notification, :recipient => alice)
note = FactoryGirl.create(:notification, :recipient => user2)
get :update, params: {id: note.id, set_unread: "false"}, format: :json
expect(Notification.find(note.id).unread).to eq(true)
end
end
describe '#index' do
before do
@post = FactoryGirl.create(:status_message)
@notification = FactoryGirl.create(:notification, recipient: alice, target: @post)
end
it 'succeeds' do
get :index
expect(response).to be_success
expect(assigns[:notifications].count).to eq(1)
end
it "succeeds for notification dropdown" do
Timecop.travel(6.seconds.ago) do
@notification.touch
end
get :index, format: :json
expect(response).to be_success
response_json = JSON.parse(response.body)
note_html = Nokogiri::HTML(response_json["notification_list"][0]["also_commented"]["note_html"])
timeago_content = note_html.css("time")[0]["data-time-ago"]
expect(response_json["unread_count"]).to be(1)
expect(response_json["unread_count_by_type"]).to eq(
"also_commented" => 1,
"comment_on_post" => 0,
"contacts_birthday" => 0,
"liked" => 0,
"mentioned" => 0,
"mentioned_in_comment" => 0,
"reshared" => 0,
"started_sharing" => 0
)
expect(timeago_content).to include(@notification.updated_at.iso8601)
expect(response.body).to match(/note_html/)
end
it 'succeeds on mobile' do
get :index, format: :mobile
expect(response).to be_success
end
it 'paginates the notifications' do
25.times { FactoryGirl.create(:notification, :recipient => alice, :target => @post) }
get :index
expect(assigns[:notifications].count).to eq(25)
get :index, params: {page: 2}
expect(assigns[:notifications].count).to eq(1)
end
it "supports a limit per_page parameter" do
2.times { FactoryGirl.create(:notification, :recipient => alice, :target => @post) }
get :index, params: {per_page: 2}
expect(assigns[:notifications].count).to eq(2)
end
describe "special case for start sharing notifications" do
it "should not provide a contacts menu for standard notifications" do
FactoryGirl.create(:notification, :recipient => alice, :target => @post)
get :index, params: {per_page: 5}
expect(Nokogiri(response.body).css('.aspect_membership')).to be_empty
end
it "should provide a contacts menu for start sharing notifications" do
eve.share_with(alice.person, eve.aspects.first)
get :index, params: {per_page: 5}
expect(Nokogiri(response.body).css(".aspect-membership-dropdown")).not_to be_empty
end
it 'succeeds on mobile' do
eve.share_with(alice.person, eve.aspects.first)
get :index, format: :mobile
expect(response).to be_success
end
end
describe "filter notifications" do
it "supports filtering by notification type" do
FactoryGirl.create(:notification, :recipient => alice, :type => "Notifications::StartedSharing")
get :index, params: {type: "started_sharing"}
expect(assigns[:notifications].count).to eq(1)
end
it "supports filtering by read/unread" do
FactoryGirl.create(:notification, :recipient => alice, :target => @post)
get :read_all
FactoryGirl.create(:notification, :recipient => alice, :target => @post)
get :index, params: {show: "unread"}
expect(assigns[:notifications].count).to eq(1)
end
end
context "after deleting a person" do
before do
user = FactoryGirl.create(:user_with_aspect)
user.share_with(alice.person, user.aspects.first)
user.person.delete
end
it "succeeds" do
get :index
expect(response).to be_success
end
it "succeeds on mobile" do
get :index, format: :mobile
expect(response).to be_success
end
end
end
describe "#read_all" do
let(:post) { FactoryGirl.create(:status_message) }
it "marks all notifications as read" do
request.env["HTTP_REFERER"] = "I wish I were spelled right"
FactoryGirl.create(:notification, recipient: alice, target: post)
FactoryGirl.create(:notification, recipient: alice, target: post)
expect(Notification.where(unread: true).count).to eq(2)
get :read_all
expect(Notification.where(unread: true).count).to eq(0)
end
it "marks all notifications in the current filter as read" do
request.env["HTTP_REFERER"] = "I wish I were spelled right"
FactoryGirl.create(:notification, recipient: alice, target: post)
FactoryGirl.create(:notification, recipient: alice, type: "Notifications::StartedSharing")
expect(Notification.where(unread: true).count).to eq(2)
get :read_all, params: {type: "started_sharing"}
expect(Notification.where(unread: true).count).to eq(1)
end
it "should redirect back in the html version if it has > 0 notifications" do
FactoryGirl.create(:notification, recipient: alice, type: "Notifications::StartedSharing")
get :read_all, params: {type: "liked"}, format: :html
expect(response).to redirect_to(notifications_path)
end
it "should redirect back in the mobile version if it has > 0 notifications" do
FactoryGirl.create(:notification, recipient: alice, type: "Notifications::StartedSharing")
get :read_all, params: {type: "liked"}, format: :mobile
expect(response).to redirect_to(notifications_path)
end
it "should redirect to stream in the html version if it has 0 notifications" do
FactoryGirl.create(:notification, recipient: alice, type: "Notifications::StartedSharing")
get :read_all, params: {type: "started_sharing"}, format: :html
expect(response).to redirect_to(stream_path)
end
it "should redirect back in the mobile version if it has 0 notifications" do
FactoryGirl.create(:notification, recipient: alice, type: "Notifications::StartedSharing")
get :read_all, params: {type: "started_sharing"}, format: :mobile
expect(response).to redirect_to(stream_path)
end
it "should return a dummy value in the json version" do
FactoryGirl.create(:notification, recipient: alice, target: post)
get :read_all, format: :json
expect(response).not_to be_redirect
end
end
end
|