debian-mirror-gitlab/spec/controllers/notification_settings_controller_spec.rb

167 lines
4.6 KiB
Ruby
Raw Normal View History

2016-06-22 15:30:34 +05:30
require 'spec_helper'
describe NotificationSettingsController do
let(:project) { create(:empty_project) }
2016-08-24 12:49:21 +05:30
let(:group) { create(:group, :internal) }
2016-06-22 15:30:34 +05:30
let(:user) { create(:user) }
before do
project.team << [user, :developer]
end
describe '#create' do
context 'when not authorized' do
it 'redirects to sign in page' do
post :create,
2016-08-24 12:49:21 +05:30
project_id: project.id,
2016-06-22 15:30:34 +05:30
notification_setting: { level: :participating }
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when authorized' do
2016-08-24 12:49:21 +05:30
let(:custom_events) do
events = {}
NotificationSetting::EMAIL_EVENTS.each do |event|
events[event.to_s] = true
end
events
end
2016-06-22 15:30:34 +05:30
before do
sign_in(user)
end
2016-08-24 12:49:21 +05:30
context 'for projects' do
let(:notification_setting) { user.notification_settings_for(project) }
2016-06-22 15:30:34 +05:30
2016-08-24 12:49:21 +05:30
it 'creates notification setting' do
post :create,
project_id: project.id,
notification_setting: { level: :participating }
2016-06-22 15:30:34 +05:30
2016-08-24 12:49:21 +05:30
expect(response.status).to eq 200
expect(notification_setting.level).to eq("participating")
expect(notification_setting.user_id).to eq(user.id)
expect(notification_setting.source_id).to eq(project.id)
expect(notification_setting.source_type).to eq("Project")
end
2016-06-22 15:30:34 +05:30
2016-08-24 12:49:21 +05:30
context 'with custom settings' do
it 'creates notification setting' do
post :create,
project_id: project.id,
notification_setting: { level: :custom }.merge(custom_events)
expect(response.status).to eq 200
expect(notification_setting.level).to eq("custom")
expect(notification_setting.events).to eq(custom_events)
2016-06-22 15:30:34 +05:30
end
end
2016-08-24 12:49:21 +05:30
end
2016-06-22 15:30:34 +05:30
2016-08-24 12:49:21 +05:30
context 'for groups' do
let(:notification_setting) { user.notification_settings_for(group) }
it 'creates notification setting' do
2016-06-22 15:30:34 +05:30
post :create,
2016-08-24 12:49:21 +05:30
namespace_id: group.id,
notification_setting: { level: :watch }
2016-06-22 15:30:34 +05:30
expect(response.status).to eq 200
2016-08-24 12:49:21 +05:30
expect(notification_setting.level).to eq("watch")
expect(notification_setting.user_id).to eq(user.id)
expect(notification_setting.source_id).to eq(group.id)
expect(notification_setting.source_type).to eq("Namespace")
end
context 'with custom settings' do
it 'creates notification setting' do
post :create,
namespace_id: group.id,
notification_setting: { level: :custom }.merge(custom_events)
expect(response.status).to eq 200
expect(notification_setting.level).to eq("custom")
expect(notification_setting.events).to eq(custom_events)
end
2016-06-22 15:30:34 +05:30
end
end
end
context 'not authorized' do
let(:private_project) { create(:project, :private) }
before { sign_in(user) }
it 'returns 404' do
post :create,
2016-08-24 12:49:21 +05:30
project_id: private_project.id,
2016-06-22 15:30:34 +05:30
notification_setting: { level: :participating }
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
2016-06-22 15:30:34 +05:30
end
end
end
describe '#update' do
let(:notification_setting) { user.global_notification_setting }
context 'when not authorized' do
it 'redirects to sign in page' do
put :update,
id: notification_setting,
notification_setting: { level: :participating }
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when authorized' do
before{ sign_in(user) }
it 'returns success' do
put :update,
id: notification_setting,
notification_setting: { level: :participating }
expect(response.status).to eq 200
end
context 'and setting custom notification setting' do
let(:custom_events) do
events = {}
NotificationSetting::EMAIL_EVENTS.each do |event|
events[event] = "true"
end
end
it 'returns success' do
put :update,
id: notification_setting,
notification_setting: { level: :participating, events: custom_events }
expect(response.status).to eq 200
end
end
end
context 'not authorized' do
let(:other_user) { create(:user) }
before { sign_in(other_user) }
it 'returns 404' do
put :update,
id: notification_setting,
notification_setting: { level: :participating }
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
2016-06-22 15:30:34 +05:30
end
end
end
end