diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
commit | 9dc93a4519d9d5d7be48ff274127136236a3adb3 (patch) | |
tree | 70467ae3692a0e35e5ea56bcb803eb512a10bedb /spec/services/users | |
parent | 4b0f34b6d759d6299322b3a54453e930c6121ff0 (diff) | |
download | gitlab-ce-9dc93a4519d9d5d7be48ff274127136236a3adb3.tar.gz |
Add latest changes from gitlab-org/gitlab@13-11-stable-eev13.11.0-rc43
Diffstat (limited to 'spec/services/users')
-rw-r--r-- | spec/services/users/refresh_authorized_projects_service_spec.rb | 164 | ||||
-rw-r--r-- | spec/services/users/update_todo_count_cache_service_spec.rb | 61 |
2 files changed, 61 insertions, 164 deletions
diff --git a/spec/services/users/refresh_authorized_projects_service_spec.rb b/spec/services/users/refresh_authorized_projects_service_spec.rb index 1e74ff3d9eb..a8ad0d02f60 100644 --- a/spec/services/users/refresh_authorized_projects_service_spec.rb +++ b/spec/services/users/refresh_authorized_projects_service_spec.rb @@ -163,168 +163,4 @@ RSpec.describe Users::RefreshAuthorizedProjectsService do service.update_authorizations([], [[user.id, project.id, Gitlab::Access::MAINTAINER]]) end end - - describe '#fresh_access_levels_per_project' do - let(:hash) { service.fresh_access_levels_per_project } - - it 'returns a Hash' do - expect(hash).to be_an_instance_of(Hash) - end - - it 'sets the keys to the project IDs' do - expect(hash.keys).to eq([project.id]) - end - - it 'sets the values to the access levels' do - expect(hash.values).to eq([Gitlab::Access::MAINTAINER]) - end - - context 'personal projects' do - it 'includes the project with the right access level' do - expect(hash[project.id]).to eq(Gitlab::Access::MAINTAINER) - end - end - - context 'projects the user is a member of' do - let!(:other_project) { create(:project) } - - before do - other_project.team.add_reporter(user) - end - - it 'includes the project with the right access level' do - expect(hash[other_project.id]).to eq(Gitlab::Access::REPORTER) - end - end - - context 'projects of groups the user is a member of' do - let(:group) { create(:group) } - let!(:other_project) { create(:project, group: group) } - - before do - group.add_owner(user) - end - - it 'includes the project with the right access level' do - expect(hash[other_project.id]).to eq(Gitlab::Access::OWNER) - end - end - - context 'projects of subgroups of groups the user is a member of' do - let(:group) { create(:group) } - let(:nested_group) { create(:group, parent: group) } - let!(:other_project) { create(:project, group: nested_group) } - - before do - group.add_maintainer(user) - end - - it 'includes the project with the right access level' do - expect(hash[other_project.id]).to eq(Gitlab::Access::MAINTAINER) - end - end - - context 'projects shared with groups the user is a member of' do - let(:group) { create(:group) } - let(:other_project) { create(:project) } - let!(:project_group_link) { create(:project_group_link, project: other_project, group: group, group_access: Gitlab::Access::GUEST) } - - before do - group.add_maintainer(user) - end - - it 'includes the project with the right access level' do - expect(hash[other_project.id]).to eq(Gitlab::Access::GUEST) - end - end - - context 'projects shared with subgroups of groups the user is a member of' do - let(:group) { create(:group) } - let(:nested_group) { create(:group, parent: group) } - let(:other_project) { create(:project) } - let!(:project_group_link) { create(:project_group_link, project: other_project, group: nested_group, group_access: Gitlab::Access::DEVELOPER) } - - before do - group.add_maintainer(user) - end - - it 'includes the project with the right access level' do - expect(hash[other_project.id]).to eq(Gitlab::Access::DEVELOPER) - end - end - end - - describe '#current_authorizations_per_project' do - let(:hash) { service.current_authorizations_per_project } - - it 'returns a Hash' do - expect(hash).to be_an_instance_of(Hash) - end - - it 'sets the keys to the project IDs' do - expect(hash.keys).to eq([project.id]) - end - - it 'sets the values to the project authorization rows' do - expect(hash.values.length).to eq(1) - - value = hash.values[0] - - expect(value.project_id).to eq(project.id) - expect(value.access_level).to eq(Gitlab::Access::MAINTAINER) - end - end - - describe '#current_authorizations' do - context 'without authorizations' do - it 'returns an empty list' do - user.project_authorizations.delete_all - - expect(service.current_authorizations.empty?).to eq(true) - end - end - - context 'with an authorization' do - let(:row) { service.current_authorizations.take } - - it 'returns the currently authorized projects' do - expect(service.current_authorizations.length).to eq(1) - end - - it 'includes the project ID for every row' do - expect(row.project_id).to eq(project.id) - end - - it 'includes the access level for every row' do - expect(row.access_level).to eq(Gitlab::Access::MAINTAINER) - end - end - end - - describe '#fresh_authorizations' do - it 'returns the new authorized projects' do - expect(service.fresh_authorizations.length).to eq(1) - end - - it 'returns the highest access level' do - project.team.add_guest(user) - - rows = service.fresh_authorizations.to_a - - expect(rows.length).to eq(1) - expect(rows.first.access_level).to eq(Gitlab::Access::MAINTAINER) - end - - context 'every returned row' do - let(:row) { service.fresh_authorizations.take } - - it 'includes the project ID' do - expect(row.project_id).to eq(project.id) - end - - it 'includes the access level' do - expect(row.access_level).to eq(Gitlab::Access::MAINTAINER) - end - end - end end diff --git a/spec/services/users/update_todo_count_cache_service_spec.rb b/spec/services/users/update_todo_count_cache_service_spec.rb new file mode 100644 index 00000000000..3e3618b1291 --- /dev/null +++ b/spec/services/users/update_todo_count_cache_service_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Users::UpdateTodoCountCacheService do + describe '#execute' do + let_it_be(:user1) { create(:user) } + let_it_be(:user2) { create(:user) } + + let_it_be(:todo1) { create(:todo, user: user1, state: :done) } + let_it_be(:todo2) { create(:todo, user: user1, state: :done) } + let_it_be(:todo3) { create(:todo, user: user1, state: :pending) } + let_it_be(:todo4) { create(:todo, user: user2, state: :done) } + let_it_be(:todo5) { create(:todo, user: user2, state: :pending) } + let_it_be(:todo6) { create(:todo, user: user2, state: :pending) } + + it 'updates the todos_counts for users', :use_clean_rails_memory_store_caching do + Rails.cache.write(['users', user1.id, 'todos_done_count'], 0) + Rails.cache.write(['users', user1.id, 'todos_pending_count'], 0) + Rails.cache.write(['users', user2.id, 'todos_done_count'], 0) + Rails.cache.write(['users', user2.id, 'todos_pending_count'], 0) + + expect { described_class.new([user1, user2]).execute } + .to change(user1, :todos_done_count).from(0).to(2) + .and change(user1, :todos_pending_count).from(0).to(1) + .and change(user2, :todos_done_count).from(0).to(1) + .and change(user2, :todos_pending_count).from(0).to(2) + + Todo.delete_all + + expect { described_class.new([user1, user2]).execute } + .to change(user1, :todos_done_count).from(2).to(0) + .and change(user1, :todos_pending_count).from(1).to(0) + .and change(user2, :todos_done_count).from(1).to(0) + .and change(user2, :todos_pending_count).from(2).to(0) + end + + it 'avoids N+1 queries' do + control_count = ActiveRecord::QueryRecorder.new { described_class.new([user1]).execute }.count + + expect { described_class.new([user1, user2]).execute }.not_to exceed_query_limit(control_count) + end + + it 'executes one query per batch of users' do + stub_const("#{described_class}::QUERY_BATCH_SIZE", 1) + + expect(ActiveRecord::QueryRecorder.new { described_class.new([user1]).execute }.count).to eq(1) + expect(ActiveRecord::QueryRecorder.new { described_class.new([user1, user2]).execute }.count).to eq(2) + end + + it 'sets the cache expire time to the users count_cache_validity_period' do + allow(user1).to receive(:count_cache_validity_period).and_return(1.minute) + allow(user2).to receive(:count_cache_validity_period).and_return(1.hour) + + expect(Rails.cache).to receive(:write).with(['users', user1.id, anything], anything, expires_in: 1.minute).twice + expect(Rails.cache).to receive(:write).with(['users', user2.id, anything], anything, expires_in: 1.hour).twice + + described_class.new([user1, user2]).execute + end + end +end |