2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-16 23:18:57 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 14:08:28 -04:00
|
|
|
RSpec.describe Profiles::NotificationsController do
|
2017-03-16 23:18:57 -04:00
|
|
|
let(:user) do
|
|
|
|
create(:user) do |user|
|
2021-04-03 23:08:52 -04:00
|
|
|
user.emails.create!(email: 'original@example.com', confirmed_at: Time.current)
|
|
|
|
user.emails.create!(email: 'new@example.com', confirmed_at: Time.current)
|
2017-03-24 18:10:26 -04:00
|
|
|
user.notification_email = 'original@example.com'
|
2017-03-16 23:18:57 -04:00
|
|
|
user.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET show' do
|
|
|
|
it 'renders' do
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get :show
|
|
|
|
|
|
|
|
expect(response).to render_template :show
|
|
|
|
end
|
2019-10-18 14:06:21 -04:00
|
|
|
|
2021-04-13 14:11:28 -04:00
|
|
|
context 'when personal projects are present', :request_store do
|
|
|
|
let!(:personal_project_1) { create(:project, namespace: user.namespace) }
|
|
|
|
|
|
|
|
context 'N+1 query check' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it 'does not have an N+1' do
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get :show
|
|
|
|
|
|
|
|
control = ActiveRecord::QueryRecorder.new do
|
|
|
|
get :show
|
|
|
|
end
|
|
|
|
|
|
|
|
create_list(:project, 2, namespace: user.namespace)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
get :show
|
|
|
|
end.not_to exceed_query_limit(control)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-18 14:06:21 -04:00
|
|
|
context 'with groups that do not have notification preferences' do
|
2020-02-16 22:09:00 -05:00
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:subgroup) { create(:group, parent: group) }
|
|
|
|
|
2019-10-18 14:06:21 -04:00
|
|
|
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
|
|
|
|
|
2021-04-08 11:09:06 -04:00
|
|
|
context 'N+1 query check' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it 'does not have an N+1' do
|
|
|
|
sign_in(user)
|
2019-10-18 14:06:21 -04:00
|
|
|
|
|
|
|
get :show
|
|
|
|
|
2021-04-08 11:09:06 -04:00
|
|
|
control = ActiveRecord::QueryRecorder.new do
|
|
|
|
get :show
|
|
|
|
end
|
2019-10-18 14:06:21 -04:00
|
|
|
|
2021-04-08 11:09:06 -04:00
|
|
|
create_list(:group, 2, parent: group)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
get :show
|
|
|
|
end.not_to exceed_query_limit(control)
|
|
|
|
end
|
2019-10-18 14:06:21 -04:00
|
|
|
end
|
|
|
|
end
|
2020-01-02 16:07:38 -05:00
|
|
|
|
2020-08-28 11:10:21 -04:00
|
|
|
context 'with group notifications' do
|
2020-09-02 23:08:12 -04:00
|
|
|
let(:notifications_per_page) { 5 }
|
|
|
|
|
2020-08-28 11:10:21 -04:00
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:subgroups) { create_list(:group, 10, parent: group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_developer(user)
|
|
|
|
sign_in(user)
|
2020-09-04 05:08:38 -04:00
|
|
|
allow(Kaminari.config).to receive(:default_per_page).and_return(notifications_per_page)
|
2020-08-28 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'paginates the groups' do
|
2020-09-02 23:08:12 -04:00
|
|
|
get :show
|
|
|
|
|
2020-08-28 11:10:21 -04:00
|
|
|
expect(assigns(:group_notifications).count).to eq(5)
|
|
|
|
end
|
2020-09-02 23:08:12 -04:00
|
|
|
|
|
|
|
context 'when the user is not a member' do
|
|
|
|
let(:notifications_per_page) { 20 }
|
|
|
|
|
|
|
|
let_it_be(:public_group) { create(:group, :public) }
|
|
|
|
|
|
|
|
it 'does not show public groups', :aggregate_failures do
|
|
|
|
get :show
|
|
|
|
|
|
|
|
# Let's make sure we're grabbing all groups in one page, just in case
|
|
|
|
expect(assigns(:user_groups).count).to eq(11)
|
|
|
|
expect(assigns(:user_groups)).not_to include(public_group)
|
|
|
|
end
|
|
|
|
end
|
2020-08-28 11:10:21 -04:00
|
|
|
end
|
|
|
|
|
2020-01-02 16:07:38 -05:00
|
|
|
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-03-16 23:18:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST update' do
|
|
|
|
it 'updates only permitted attributes' do
|
|
|
|
sign_in(user)
|
|
|
|
|
2021-02-15 13:09:09 -05:00
|
|
|
put :update, params: { user: { notification_email: 'new@example.com', email_opted_in: true, notified_of_own_activity: true, admin: true } }
|
2017-03-16 23:18:57 -04:00
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.notification_email).to eq('new@example.com')
|
2021-02-15 13:09:09 -05:00
|
|
|
expect(user.email_opted_in).to eq(true)
|
2017-03-16 23:18:57 -04:00
|
|
|
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)
|
|
|
|
|
2021-08-18 05:10:26 -04:00
|
|
|
put :update, params: { user: { notification_email: 'unverified@example.com' } }
|
2017-03-16 23:18:57 -04:00
|
|
|
|
|
|
|
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
|