2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2018-02-13 01:38:26 -05:00
|
|
|
def create_notification(user_id, resp_code, matcher)
|
|
|
|
notification_count = Notification.count
|
2018-06-03 23:33:55 -04:00
|
|
|
post "/notifications.json",
|
|
|
|
params: {
|
|
|
|
notification_type: Notification.types[:mentioned],
|
|
|
|
user_id: user_id,
|
|
|
|
data: { message: 'tada' }.to_json
|
|
|
|
}
|
2018-02-13 01:38:26 -05:00
|
|
|
expect(response.status).to eq(resp_code)
|
|
|
|
expect(Notification.count).send(matcher, eq(notification_count))
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_notification(topic_id, resp_code, matcher)
|
|
|
|
notification = Fabricate(:notification)
|
2018-06-03 23:33:55 -04:00
|
|
|
put "/notifications/#{notification.id}.json", params: { topic_id: topic_id }
|
2018-02-13 01:38:26 -05:00
|
|
|
expect(response.status).to eq(resp_code)
|
|
|
|
notification.reload
|
|
|
|
expect(notification.topic_id).send(matcher, eq(topic_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_notification(resp_code, matcher)
|
|
|
|
notification = Fabricate(:notification)
|
|
|
|
notification_count = Notification.count
|
2018-06-03 23:33:55 -04:00
|
|
|
delete "/notifications/#{notification.id}.json"
|
2018-02-13 01:38:26 -05:00
|
|
|
expect(response.status).to eq(resp_code)
|
|
|
|
expect(Notification.count).send(matcher, eq(notification_count))
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
describe NotificationsController do
|
|
|
|
context 'when logged in' do
|
2018-02-13 01:38:26 -05:00
|
|
|
context 'as normal user' do
|
2018-06-03 23:33:55 -04:00
|
|
|
let!(:user) { sign_in(Fabricate(:user)) }
|
2018-02-13 01:38:26 -05:00
|
|
|
|
|
|
|
describe '#index' do
|
|
|
|
it 'should succeed for recent' do
|
2018-06-03 23:33:55 -04:00
|
|
|
get "/notifications", params: { recent: true }
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2018-02-13 01:38:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should succeed for history' do
|
2018-06-03 23:33:55 -04:00
|
|
|
get "/notifications"
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2018-02-13 01:38:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should mark notifications as viewed' do
|
2018-06-03 23:33:55 -04:00
|
|
|
Fabricate(:notification, user: user)
|
2018-02-13 01:38:26 -05:00
|
|
|
expect(user.reload.unread_notifications).to eq(1)
|
|
|
|
expect(user.reload.total_unread_notifications).to eq(1)
|
2018-06-03 23:33:55 -04:00
|
|
|
get "/notifications.json", params: { recent: true }
|
2018-02-13 01:38:26 -05:00
|
|
|
expect(user.reload.unread_notifications).to eq(0)
|
|
|
|
expect(user.reload.total_unread_notifications).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not mark notifications as viewed if silent param is present' do
|
2018-06-03 23:33:55 -04:00
|
|
|
Fabricate(:notification, user: user)
|
2018-02-13 01:38:26 -05:00
|
|
|
expect(user.reload.unread_notifications).to eq(1)
|
|
|
|
expect(user.reload.total_unread_notifications).to eq(1)
|
2018-06-03 23:33:55 -04:00
|
|
|
get "/notifications", params: { recent: true, silent: true }
|
2018-02-13 01:38:26 -05:00
|
|
|
expect(user.reload.unread_notifications).to eq(1)
|
|
|
|
expect(user.reload.total_unread_notifications).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when username params is not valid' do
|
|
|
|
it 'should raise the right error' do
|
2018-06-03 23:33:55 -04:00
|
|
|
get "/notifications.json", params: { username: 'somedude' }
|
2018-02-13 01:38:26 -05:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should succeed' do
|
2018-06-03 23:33:55 -04:00
|
|
|
put "/notifications/mark-read.json"
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-07 05:32:13 -04:00
|
|
|
end
|
2014-09-02 21:32:27 -04:00
|
|
|
|
2018-02-13 01:38:26 -05:00
|
|
|
it "can update a single notification" do
|
|
|
|
notification = Fabricate(:notification, user: user)
|
|
|
|
notification2 = Fabricate(:notification, user: user)
|
2018-06-03 23:33:55 -04:00
|
|
|
put "/notifications/mark-read.json", params: { id: notification.id }
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2018-02-13 01:38:26 -05:00
|
|
|
|
|
|
|
notification.reload
|
|
|
|
notification2.reload
|
|
|
|
|
|
|
|
expect(notification.read).to eq(true)
|
|
|
|
expect(notification2.read).to eq(false)
|
2017-04-07 05:32:13 -04:00
|
|
|
end
|
|
|
|
|
2018-02-13 01:38:26 -05:00
|
|
|
it "updates the `read` status" do
|
2018-06-03 23:33:55 -04:00
|
|
|
Fabricate(:notification, user: user)
|
2017-04-07 05:32:13 -04:00
|
|
|
expect(user.reload.unread_notifications).to eq(1)
|
|
|
|
expect(user.reload.total_unread_notifications).to eq(1)
|
2018-06-03 23:33:55 -04:00
|
|
|
put "/notifications/mark-read.json"
|
2018-02-13 01:38:26 -05:00
|
|
|
user.reload
|
2017-04-07 05:32:13 -04:00
|
|
|
expect(user.reload.unread_notifications).to eq(0)
|
2018-02-13 01:38:26 -05:00
|
|
|
expect(user.reload.total_unread_notifications).to eq(0)
|
2017-04-07 05:32:13 -04:00
|
|
|
end
|
|
|
|
|
2018-02-13 01:38:26 -05:00
|
|
|
describe '#create' do
|
|
|
|
it "can't create notification" do
|
|
|
|
create_notification(user.id, 403, :to)
|
|
|
|
end
|
2017-04-07 05:32:13 -04:00
|
|
|
end
|
|
|
|
|
2018-02-13 01:38:26 -05:00
|
|
|
describe '#update' do
|
|
|
|
it "can't update notification" do
|
2019-01-22 02:19:31 -05:00
|
|
|
update_notification(Fabricate(:topic).id, 403, :to_not)
|
2018-02-13 01:38:26 -05:00
|
|
|
end
|
|
|
|
end
|
2017-04-24 21:56:26 -04:00
|
|
|
|
2018-02-13 01:38:26 -05:00
|
|
|
describe '#destroy' do
|
|
|
|
it "can't delete notification" do
|
|
|
|
delete_notification(403, :to)
|
2017-04-07 05:32:13 -04:00
|
|
|
end
|
|
|
|
end
|
2014-10-13 06:26:30 -04:00
|
|
|
end
|
|
|
|
|
2018-02-13 01:38:26 -05:00
|
|
|
context 'as admin' do
|
2018-06-03 23:33:55 -04:00
|
|
|
let!(:admin) { sign_in(Fabricate(:admin)) }
|
2016-09-16 02:14:00 -04:00
|
|
|
|
2018-02-13 01:38:26 -05:00
|
|
|
describe '#create' do
|
|
|
|
it "can create notification" do
|
|
|
|
create_notification(admin.id, 200, :to_not)
|
|
|
|
expect(::JSON.parse(response.body)["id"]).to_not eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#update' do
|
|
|
|
it "can update notification" do
|
|
|
|
update_notification(8, 200, :to)
|
|
|
|
expect(::JSON.parse(response.body)["topic_id"]).to eq(8)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#destroy' do
|
|
|
|
it "can delete notification" do
|
|
|
|
delete_notification(200, :to_not)
|
|
|
|
end
|
|
|
|
end
|
2014-06-07 06:17:45 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not logged in' do
|
2018-02-13 01:38:26 -05:00
|
|
|
|
|
|
|
describe '#index' do
|
|
|
|
it 'should raise an error' do
|
2018-06-03 23:33:55 -04:00
|
|
|
get "/notifications.json", params: { recent: true }
|
2018-02-13 01:38:26 -05:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#create' do
|
|
|
|
it "can't create notification" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
create_notification(user.id, 403, :to)
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2018-02-13 01:38:26 -05:00
|
|
|
|
|
|
|
describe '#update' do
|
|
|
|
it "can't update notification" do
|
2019-01-22 02:19:31 -05:00
|
|
|
update_notification(Fabricate(:topic).id, 403, :to_not)
|
2018-02-13 01:38:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#destroy' do
|
|
|
|
it "can't delete notification" do
|
|
|
|
delete_notification(403, :to)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|