REFACTOR: notifications controller specs to requests

This commit is contained in:
OsamaSayegh 2018-06-04 06:33:55 +03:00 committed by Sam
parent e4bdafb550
commit 807223deef

View File

@ -2,14 +2,19 @@ require 'rails_helper'
def create_notification(user_id, resp_code, matcher) def create_notification(user_id, resp_code, matcher)
notification_count = Notification.count notification_count = Notification.count
post :create, params: { notification_type: Notification.types[:mentioned], user_id: user_id, data: { message: 'tada' }.to_json }, format: :json post "/notifications.json",
params: {
notification_type: Notification.types[:mentioned],
user_id: user_id,
data: { message: 'tada' }.to_json
}
expect(response.status).to eq(resp_code) expect(response.status).to eq(resp_code)
expect(Notification.count).send(matcher, eq(notification_count)) expect(Notification.count).send(matcher, eq(notification_count))
end end
def update_notification(topic_id, resp_code, matcher) def update_notification(topic_id, resp_code, matcher)
notification = Fabricate(:notification) notification = Fabricate(:notification)
post :update, params: { id: notification.id, topic_id: topic_id }, format: :json put "/notifications/#{notification.id}.json", params: { topic_id: topic_id }
expect(response.status).to eq(resp_code) expect(response.status).to eq(resp_code)
notification.reload notification.reload
expect(notification.topic_id).send(matcher, eq(topic_id)) expect(notification.topic_id).send(matcher, eq(topic_id))
@ -18,67 +23,62 @@ end
def delete_notification(resp_code, matcher) def delete_notification(resp_code, matcher)
notification = Fabricate(:notification) notification = Fabricate(:notification)
notification_count = Notification.count notification_count = Notification.count
delete :destroy, params: { id: notification.id }, format: :json delete "/notifications/#{notification.id}.json"
expect(response.status).to eq(resp_code) expect(response.status).to eq(resp_code)
expect(Notification.count).send(matcher, eq(notification_count)) expect(Notification.count).send(matcher, eq(notification_count))
end end
describe NotificationsController do describe NotificationsController do
context 'when logged in' do context 'when logged in' do
context 'as normal user' do context 'as normal user' do
let!(:user) { sign_in(Fabricate(:user)) }
let!(:user) { log_in }
describe '#index' do describe '#index' do
it 'should succeed for recent' do it 'should succeed for recent' do
get :index, params: { recent: true } get "/notifications", params: { recent: true }
expect(response).to be_success expect(response).to be_success
end end
it 'should succeed for history' do it 'should succeed for history' do
get :index get "/notifications"
expect(response).to be_success expect(response).to be_success
end end
it 'should mark notifications as viewed' do it 'should mark notifications as viewed' do
_notification = Fabricate(:notification, user: user) Fabricate(:notification, user: user)
expect(user.reload.unread_notifications).to eq(1) expect(user.reload.unread_notifications).to eq(1)
expect(user.reload.total_unread_notifications).to eq(1) expect(user.reload.total_unread_notifications).to eq(1)
get :index, params: { recent: true }, format: :json get "/notifications.json", params: { recent: true }
expect(user.reload.unread_notifications).to eq(0) expect(user.reload.unread_notifications).to eq(0)
expect(user.reload.total_unread_notifications).to eq(1) expect(user.reload.total_unread_notifications).to eq(1)
end end
it 'should not mark notifications as viewed if silent param is present' do it 'should not mark notifications as viewed if silent param is present' do
_notification = Fabricate(:notification, user: user) Fabricate(:notification, user: user)
expect(user.reload.unread_notifications).to eq(1) expect(user.reload.unread_notifications).to eq(1)
expect(user.reload.total_unread_notifications).to eq(1) expect(user.reload.total_unread_notifications).to eq(1)
get :index, params: { recent: true, silent: true } get "/notifications", params: { recent: true, silent: true }
expect(user.reload.unread_notifications).to eq(1) expect(user.reload.unread_notifications).to eq(1)
expect(user.reload.total_unread_notifications).to eq(1) expect(user.reload.total_unread_notifications).to eq(1)
end end
context 'when username params is not valid' do context 'when username params is not valid' do
it 'should raise the right error' do it 'should raise the right error' do
get :index, params: { username: 'somedude' }, format: :json get "/notifications.json", params: { username: 'somedude' }
expect(response).to_not be_success
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
end end
end end
it 'should succeed' do it 'should succeed' do
put :mark_read, format: :json put "/notifications/mark-read.json"
expect(response).to be_success expect(response).to be_success
end end
it "can update a single notification" do it "can update a single notification" do
notification = Fabricate(:notification, user: user) notification = Fabricate(:notification, user: user)
notification2 = Fabricate(:notification, user: user) notification2 = Fabricate(:notification, user: user)
put :mark_read, params: { id: notification.id }, format: :json put "/notifications/mark-read.json", params: { id: notification.id }
expect(response).to be_success expect(response).to be_success
notification.reload notification.reload
@ -89,10 +89,10 @@ describe NotificationsController do
end end
it "updates the `read` status" do it "updates the `read` status" do
_notification = Fabricate(:notification, user: user) Fabricate(:notification, user: user)
expect(user.reload.unread_notifications).to eq(1) expect(user.reload.unread_notifications).to eq(1)
expect(user.reload.total_unread_notifications).to eq(1) expect(user.reload.total_unread_notifications).to eq(1)
put :mark_read, format: :json put "/notifications/mark-read.json"
user.reload user.reload
expect(user.reload.unread_notifications).to eq(0) expect(user.reload.unread_notifications).to eq(0)
expect(user.reload.total_unread_notifications).to eq(0) expect(user.reload.total_unread_notifications).to eq(0)
@ -115,12 +115,10 @@ describe NotificationsController do
delete_notification(403, :to) delete_notification(403, :to)
end end
end end
end end
context 'as admin' do context 'as admin' do
let!(:admin) { sign_in(Fabricate(:admin)) }
let!(:admin) { log_in(:admin) }
describe '#create' do describe '#create' do
it "can create notification" do it "can create notification" do
@ -141,16 +139,14 @@ describe NotificationsController do
delete_notification(200, :to_not) delete_notification(200, :to_not)
end end
end end
end end
end end
context 'when not logged in' do context 'when not logged in' do
describe '#index' do describe '#index' do
it 'should raise an error' do it 'should raise an error' do
get :index, params: { recent: true }, format: :json get "/notifications.json", params: { recent: true }
expect(response.status).to eq(403) expect(response.status).to eq(403)
end end
end end
@ -173,7 +169,5 @@ describe NotificationsController do
delete_notification(403, :to) delete_notification(403, :to)
end end
end end
end end
end end