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