summaryrefslogtreecommitdiff
path: root/spec/models/namespace
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-07-20 12:26:25 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-07-20 12:26:25 +0000
commita09983ae35713f5a2bbb100981116d31ce99826e (patch)
tree2ee2af7bd104d57086db360a7e6d8c9d5d43667a /spec/models/namespace
parent18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff)
downloadgitlab-ce-a09983ae35713f5a2bbb100981116d31ce99826e.tar.gz
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'spec/models/namespace')
-rw-r--r--spec/models/namespace/root_storage_size_spec.rb67
-rw-r--r--spec/models/namespace/root_storage_statistics_spec.rb47
-rw-r--r--spec/models/namespace/traversal_hierarchy_spec.rb63
3 files changed, 109 insertions, 68 deletions
diff --git a/spec/models/namespace/root_storage_size_spec.rb b/spec/models/namespace/root_storage_size_spec.rb
deleted file mode 100644
index a8048b7f637..00000000000
--- a/spec/models/namespace/root_storage_size_spec.rb
+++ /dev/null
@@ -1,67 +0,0 @@
-# frozen_string_literal: true
-
-require 'spec_helper'
-
-RSpec.describe Namespace::RootStorageSize, type: :model do
- let(:namespace) { create(:namespace) }
- let(:current_size) { 50.megabytes }
- let(:limit) { 100 }
- let(:model) { described_class.new(namespace) }
- let(:create_statistics) { create(:namespace_root_storage_statistics, namespace: namespace, storage_size: current_size)}
-
- before do
- create_statistics
-
- stub_application_setting(namespace_storage_size_limit: limit)
- end
-
- describe '#above_size_limit?' do
- subject { model.above_size_limit? }
-
- context 'when limit is 0' do
- let(:limit) { 0 }
-
- it { is_expected.to eq(false) }
- end
-
- context 'when below limit' do
- it { is_expected.to eq(false) }
- end
-
- context 'when above limit' do
- let(:current_size) { 101.megabytes }
-
- it { is_expected.to eq(true) }
- end
- end
-
- describe '#usage_ratio' do
- subject { model.usage_ratio }
-
- it { is_expected.to eq(0.5) }
-
- context 'when limit is 0' do
- let(:limit) { 0 }
-
- it { is_expected.to eq(0) }
- end
-
- context 'when there are no root_storage_statistics' do
- let(:create_statistics) { nil }
-
- it { is_expected.to eq(0) }
- end
- end
-
- describe '#current_size' do
- subject { model.current_size }
-
- it { is_expected.to eq(current_size) }
- end
-
- describe '#limit' do
- subject { model.limit }
-
- it { is_expected.to eq(limit.megabytes) }
- end
-end
diff --git a/spec/models/namespace/root_storage_statistics_spec.rb b/spec/models/namespace/root_storage_statistics_spec.rb
index 9e12831a704..ce6f875ee09 100644
--- a/spec/models/namespace/root_storage_statistics_spec.rb
+++ b/spec/models/namespace/root_storage_statistics_spec.rb
@@ -43,6 +43,7 @@ RSpec.describe Namespace::RootStorageStatistics, type: :model do
total_build_artifacts_size = stat1.build_artifacts_size + stat2.build_artifacts_size
total_packages_size = stat1.packages_size + stat2.packages_size
total_storage_size = stat1.storage_size + stat2.storage_size
+ total_snippets_size = stat1.snippets_size + stat2.snippets_size
expect(root_storage_statistics.repository_size).to eq(total_repository_size)
expect(root_storage_statistics.wiki_size).to eq(total_wiki_size)
@@ -50,6 +51,7 @@ RSpec.describe Namespace::RootStorageStatistics, type: :model do
expect(root_storage_statistics.build_artifacts_size).to eq(total_build_artifacts_size)
expect(root_storage_statistics.packages_size).to eq(total_packages_size)
expect(root_storage_statistics.storage_size).to eq(total_storage_size)
+ expect(root_storage_statistics.snippets_size).to eq(total_snippets_size)
end
it 'works when there are no projects' do
@@ -64,10 +66,20 @@ RSpec.describe Namespace::RootStorageStatistics, type: :model do
expect(root_storage_statistics.build_artifacts_size).to eq(0)
expect(root_storage_statistics.packages_size).to eq(0)
expect(root_storage_statistics.storage_size).to eq(0)
+ expect(root_storage_statistics.snippets_size).to eq(0)
+ end
+ end
+
+ shared_examples 'does not include personal snippets' do
+ specify do
+ expect(root_storage_statistics).not_to receive(:from_personal_snippets)
+
+ root_storage_statistics.recalculate!
end
end
it_behaves_like 'data refresh'
+ it_behaves_like 'does not include personal snippets'
context 'with subgroups' do
let(:subgroup1) { create(:group, parent: namespace)}
@@ -77,12 +89,45 @@ RSpec.describe Namespace::RootStorageStatistics, type: :model do
let(:project2) { create(:project, namespace: subgroup2) }
it_behaves_like 'data refresh'
+ it_behaves_like 'does not include personal snippets'
end
context 'with a personal namespace' do
- let(:namespace) { create(:user).namespace }
+ let_it_be(:user) { create(:user) }
+ let(:namespace) { user.namespace }
it_behaves_like 'data refresh'
+
+ context 'when user has personal snippets' do
+ let(:total_project_snippets_size) { stat1.snippets_size + stat2.snippets_size }
+
+ it 'aggregates personal and project snippets size' do
+ # This is just a a snippet authored by other user
+ # to ensure we only pick snippets from the namespace
+ # user
+ create(:personal_snippet, :repository).statistics.refresh!
+
+ snippets = create_list(:personal_snippet, 3, :repository, author: user)
+ snippets.each { |s| s.statistics.refresh! }
+
+ total_personal_snippets_size = snippets.map { |s| s.statistics.repository_size }.sum
+
+ root_storage_statistics.recalculate!
+
+ expect(root_storage_statistics.snippets_size).to eq(total_personal_snippets_size + total_project_snippets_size)
+ end
+
+ context 'when personal snippets do not have statistics' do
+ it 'does not raise any error' do
+ snippets = create_list(:personal_snippet, 2, :repository, author: user)
+ snippets.last.statistics.refresh!
+
+ root_storage_statistics.recalculate!
+
+ expect(root_storage_statistics.snippets_size).to eq(total_project_snippets_size + snippets.last.statistics.repository_size)
+ end
+ end
+ end
end
end
end
diff --git a/spec/models/namespace/traversal_hierarchy_spec.rb b/spec/models/namespace/traversal_hierarchy_spec.rb
new file mode 100644
index 00000000000..71b0e974106
--- /dev/null
+++ b/spec/models/namespace/traversal_hierarchy_spec.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Namespace::TraversalHierarchy, type: :model do
+ let_it_be(:root, reload: true) { create(:namespace, :with_hierarchy) }
+
+ describe '.for_namespace' do
+ let(:hierarchy) { described_class.for_namespace(namespace) }
+
+ context 'with root group' do
+ let(:namespace) { root }
+
+ it { expect(hierarchy.root).to eq root }
+ end
+
+ context 'with child group' do
+ let(:namespace) { root.children.first.children.first }
+
+ it { expect(hierarchy.root).to eq root }
+ end
+
+ context 'with group outside of hierarchy' do
+ let(:namespace) { create(:namespace) }
+
+ it { expect(hierarchy.root).not_to eq root }
+ end
+ end
+
+ describe '.new' do
+ let(:hierarchy) { described_class.new(namespace) }
+
+ context 'with root group' do
+ let(:namespace) { root }
+
+ it { expect(hierarchy.root).to eq root }
+ end
+
+ context 'with child group' do
+ let(:namespace) { root.children.first }
+
+ it { expect { hierarchy }.to raise_error(StandardError, 'Must specify a root node') }
+ end
+ end
+
+ describe '#incorrect_traversal_ids' do
+ subject { described_class.new(root).incorrect_traversal_ids }
+
+ it { is_expected.to match_array Namespace.all }
+ end
+
+ describe '#sync_traversal_ids!' do
+ let(:hierarchy) { described_class.new(root) }
+
+ before do
+ hierarchy.sync_traversal_ids!
+ root.reload
+ end
+
+ it_behaves_like 'hierarchy with traversal_ids'
+ it { expect(hierarchy.incorrect_traversal_ids).to be_empty }
+ end
+end