2016-06-14 14:36:36 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe NotificationSettingsController do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2016-06-21 15:50:13 -04:00
|
|
|
let(:group) { create(:group, :internal) }
|
2016-06-14 14:36:36 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_developer(user)
|
2016-06-14 14:36:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#create' do
|
|
|
|
context 'when not authorized' do
|
|
|
|
it 'redirects to sign in page' do
|
|
|
|
post :create,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
project_id: project.id,
|
|
|
|
notification_setting: { level: :participating }
|
|
|
|
}
|
2016-06-14 14:36:36 -04:00
|
|
|
|
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authorized' do
|
2018-07-22 22:42:19 -04:00
|
|
|
let(:notification_setting) { user.notification_settings_for(source) }
|
2016-06-21 15:50:13 -04:00
|
|
|
let(:custom_events) do
|
|
|
|
events = {}
|
|
|
|
|
2018-07-22 22:42:19 -04:00
|
|
|
NotificationSetting.email_events(source).each do |event|
|
2016-06-21 15:50:13 -04:00
|
|
|
events[event.to_s] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
events
|
|
|
|
end
|
|
|
|
|
2016-06-14 14:36:36 -04:00
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2016-06-21 15:50:13 -04:00
|
|
|
context 'for projects' do
|
2018-07-22 22:42:19 -04:00
|
|
|
let(:source) { project }
|
2016-06-14 14:36:36 -04:00
|
|
|
|
2016-06-21 15:50:13 -04:00
|
|
|
it 'creates notification setting' do
|
|
|
|
post :create,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
project_id: project.id,
|
|
|
|
notification_setting: { level: :participating }
|
|
|
|
}
|
2016-06-14 14:36:36 -04:00
|
|
|
|
2016-06-21 15:50:13 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
context 'with custom settings' do
|
|
|
|
it 'creates notification setting' do
|
|
|
|
post :create,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
project_id: project.id,
|
|
|
|
notification_setting: { level: :custom }.merge(custom_events)
|
|
|
|
}
|
2016-06-14 14:36:36 -04:00
|
|
|
|
2016-06-21 15:50:13 -04:00
|
|
|
expect(response.status).to eq 200
|
|
|
|
expect(notification_setting.level).to eq("custom")
|
2017-06-06 12:37:15 -04:00
|
|
|
|
|
|
|
custom_events.each do |event, value|
|
2017-06-08 12:10:35 -04:00
|
|
|
expect(notification_setting.event_enabled?(event)).to eq(value)
|
2017-06-06 12:37:15 -04:00
|
|
|
end
|
2016-06-14 14:36:36 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-21 15:50:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for groups' do
|
2018-07-22 22:42:19 -04:00
|
|
|
let(:source) { group }
|
2016-06-14 14:36:36 -04:00
|
|
|
|
2016-06-21 15:50:13 -04:00
|
|
|
it 'creates notification setting' do
|
2016-06-14 14:36:36 -04:00
|
|
|
post :create,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
namespace_id: group.id,
|
|
|
|
notification_setting: { level: :watch }
|
|
|
|
}
|
2016-06-14 14:36:36 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq 200
|
2016-06-21 15:50:13 -04:00
|
|
|
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,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
namespace_id: group.id,
|
|
|
|
notification_setting: { level: :custom }.merge(custom_events)
|
|
|
|
}
|
2016-06-21 15:50:13 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
expect(notification_setting.level).to eq("custom")
|
2017-06-06 12:37:15 -04:00
|
|
|
|
|
|
|
custom_events.each do |event, value|
|
2017-06-08 12:10:35 -04:00
|
|
|
expect(notification_setting.event_enabled?(event)).to eq(value)
|
2017-06-06 12:37:15 -04:00
|
|
|
end
|
2016-06-21 15:50:13 -04:00
|
|
|
end
|
2016-06-14 14:36:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'not authorized' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:private_project) { create(:project, :private) }
|
2017-06-14 14:18:56 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2016-06-14 14:36:36 -04:00
|
|
|
|
|
|
|
it 'returns 404' do
|
|
|
|
post :create,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
project_id: private_project.id,
|
|
|
|
notification_setting: { level: :participating }
|
|
|
|
}
|
2016-06-14 14:36:36 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2016-06-14 14:36:36 -04:00
|
|
|
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,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
id: notification_setting,
|
|
|
|
notification_setting: { level: :participating }
|
|
|
|
}
|
2016-06-14 14:36:36 -04:00
|
|
|
|
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authorized' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2016-06-14 14:36:36 -04:00
|
|
|
|
|
|
|
it 'returns success' do
|
|
|
|
put :update,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
id: notification_setting,
|
|
|
|
notification_setting: { level: :participating }
|
|
|
|
}
|
2016-06-14 14:36:36 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and setting custom notification setting' do
|
|
|
|
let(:custom_events) do
|
|
|
|
events = {}
|
|
|
|
|
2018-07-22 22:42:19 -04:00
|
|
|
notification_setting.email_events.each do |event|
|
2016-06-14 14:36:36 -04:00
|
|
|
events[event] = "true"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns success' do
|
|
|
|
put :update,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
id: notification_setting,
|
|
|
|
notification_setting: { level: :participating, events: custom_events }
|
|
|
|
}
|
2016-06-14 14:36:36 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'not authorized' do
|
|
|
|
let(:other_user) { create(:user) }
|
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
sign_in(other_user)
|
|
|
|
end
|
2016-06-14 14:36:36 -04:00
|
|
|
|
|
|
|
it 'returns 404' do
|
|
|
|
put :update,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
id: notification_setting,
|
|
|
|
notification_setting: { level: :participating }
|
|
|
|
}
|
2016-06-14 14:36:36 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2016-06-14 14:36:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|