2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Profiles::NotificationsController do
|
|
|
|
let(:user) do
|
|
|
|
create(:user) do |user|
|
|
|
|
user.emails.create(email: 'original@example.com')
|
|
|
|
user.emails.create(email: 'new@example.com')
|
|
|
|
user.notification_email = 'original@example.com'
|
|
|
|
user.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET show' do
|
|
|
|
it 'renders' do
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get :show
|
|
|
|
|
|
|
|
expect(response).to render_template :show
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
context 'with groups that do not have notification preferences' do
|
2020-03-09 13:42:32 +05:30
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:subgroup) { create(:group, parent: group) }
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'still shows up in the list' do
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get :show
|
|
|
|
|
|
|
|
expect(assigns(:group_notifications).map(&:source_id)).to include(subgroup.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has an N+1 (but should not)' do
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
control = ActiveRecord::QueryRecorder.new do
|
|
|
|
get :show
|
|
|
|
end
|
|
|
|
|
|
|
|
create_list(:group, 2, parent: group)
|
|
|
|
|
|
|
|
# We currently have an N + 1, switch to `not_to` once fixed
|
|
|
|
expect do
|
|
|
|
get :show
|
|
|
|
end.to exceed_query_limit(control)
|
|
|
|
end
|
|
|
|
end
|
2020-01-03 18:37:03 +05:30
|
|
|
|
|
|
|
context 'with project notifications' do
|
|
|
|
let!(:notification_setting) { create(:notification_setting, source: project, user: user, level: :watch) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
get :show
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is public' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
|
|
|
|
it 'shows notification setting for project' do
|
|
|
|
expect(assigns(:project_notifications).map(&:source_id)).to include(project.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is public' do
|
|
|
|
let(:project) { create(:project, :private) }
|
|
|
|
|
|
|
|
it 'shows notification setting for project' do
|
|
|
|
# notification settings for given project were created before project was set to private
|
|
|
|
expect(user.notification_settings.for_projects.map(&:source_id)).to include(project.id)
|
|
|
|
|
|
|
|
# check that notification settings for project where user does not have access are filtered
|
|
|
|
expect(assigns(:project_notifications)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST update' do
|
|
|
|
it 'updates only permitted attributes' do
|
|
|
|
sign_in(user)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
put :update, params: { user: { notification_email: 'new@example.com', notified_of_own_activity: true, admin: true } }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.notification_email).to eq('new@example.com')
|
|
|
|
expect(user.notified_of_own_activity).to eq(true)
|
|
|
|
expect(user.admin).to eq(false)
|
|
|
|
expect(controller).to set_flash[:notice].to('Notification settings saved')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows an error message if the params are invalid' do
|
|
|
|
sign_in(user)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
put :update, params: { user: { notification_email: '' } }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(user.reload.notification_email).to eq('original@example.com')
|
|
|
|
expect(controller).to set_flash[:alert].to('Failed to save new settings')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|