2019-09-30 08:06:01 -04:00
# frozen_string_literal: true
2018-06-13 04:39:47 -04:00
require " spec_helper "
2016-11-22 11:58:10 -05:00
2020-06-24 05:08:32 -04:00
RSpec . describe StorageHelper do
2018-06-13 04:39:47 -04:00
describe " # storage_counter " do
it " formats bytes to one decimal place " do
expect ( helper . storage_counter ( 1 . 23 . megabytes ) ) . to eq ( " 1.2 MB " )
2016-11-22 11:58:10 -05:00
end
2018-06-13 04:39:47 -04:00
it " does not add decimals for sizes < 1 MB " do
expect ( helper . storage_counter ( 23 . 5 . kilobytes ) ) . to eq ( " 24 KB " )
2016-11-22 11:58:10 -05:00
end
2018-06-13 04:39:47 -04:00
it " does not add decimals for zeroes " do
expect ( helper . storage_counter ( 2 . megabytes ) ) . to eq ( " 2 MB " )
2016-11-22 11:58:10 -05:00
end
2018-06-13 04:39:47 -04:00
it " uses commas as thousands separator " do
2018-12-15 04:06:56 -05:00
expect ( helper . storage_counter ( 100_000_000_000_000_000_000_000 ) ) . to eq ( " 86,736.2 EB " )
2016-11-22 11:58:10 -05:00
end
end
2019-05-02 12:04:15 -04:00
describe " # storage_counters_details " do
2020-09-08 14:08:48 -04:00
let_it_be ( :namespace ) { create ( :namespace ) }
let_it_be ( :project ) do
2019-05-02 12:04:15 -04:00
create ( :project ,
namespace : namespace ,
statistics : build ( :project_statistics ,
2021-11-09 13:13:13 -05:00
namespace : namespace ,
repository_size : 10 . kilobytes ,
wiki_size : 10 . bytes ,
lfs_objects_size : 20 . gigabytes ,
build_artifacts_size : 30 . megabytes ,
pipeline_artifacts_size : 11 . megabytes ,
snippets_size : 40 . megabytes ,
packages_size : 12 . megabytes ,
uploads_size : 15 . megabytes ) )
2019-05-02 12:04:15 -04:00
end
2021-11-09 13:13:13 -05:00
let ( :message ) { 'Repository: 10 KB / Wikis: 10 Bytes / Build Artifacts: 30 MB / Pipeline Artifacts: 11 MB / LFS: 20 GB / Snippets: 40 MB / Packages: 12 MB / Uploads: 15 MB' }
2019-05-02 12:04:15 -04:00
it 'works on ProjectStatistics' do
expect ( helper . storage_counters_details ( project . statistics ) ) . to eq ( message )
end
it 'works on Namespace.with_statistics' do
namespace_stats = Namespace . with_statistics . find ( project . namespace . id )
expect ( helper . storage_counters_details ( namespace_stats ) ) . to eq ( message )
end
end
2022-02-15 16:12:52 -05:00
2022-07-20 02:10:04 -04:00
describe " storage_enforcement_banner " do
2022-02-15 16:12:52 -05:00
let_it_be_with_refind ( :current_user ) { create ( :user ) }
2022-08-19 05:11:29 -04:00
let_it_be ( :free_group ) { create ( :group , :with_root_storage_statistics ) }
let_it_be ( :paid_group ) { create ( :group , :with_root_storage_statistics ) }
2022-02-15 16:12:52 -05:00
before do
2022-07-27 20:09:28 -04:00
allow ( helper ) . to receive ( :can? ) . with ( current_user , :maintainer_access , free_group ) . and_return ( true )
allow ( helper ) . to receive ( :can? ) . with ( current_user , :maintainer_access , paid_group ) . and_return ( true )
2022-02-15 16:12:52 -05:00
allow ( helper ) . to receive ( :current_user ) { current_user }
allow ( paid_group ) . to receive ( :paid? ) . and_return ( true )
2022-06-08 11:08:15 -04:00
2022-08-19 05:11:29 -04:00
free_group . root_storage_statistics . update! ( storage_size : :: Namespace :: MIN_STORAGE_ENFORCEMENT_USAGE )
paid_group . root_storage_statistics . update! ( storage_size : :: Namespace :: MIN_STORAGE_ENFORCEMENT_USAGE )
2022-06-08 11:08:15 -04:00
stub_feature_flags ( namespace_storage_limit_bypass_date_check : false )
2022-02-15 16:12:52 -05:00
end
describe " # storage_enforcement_banner_info " do
it 'returns nil when namespace is not free' do
2022-03-11 07:07:56 -05:00
expect ( helper . storage_enforcement_banner_info ( paid_group ) ) . to be ( nil )
2022-02-15 16:12:52 -05:00
end
it 'returns nil when storage_enforcement_date is not set' do
allow ( free_group ) . to receive ( :storage_enforcement_date ) . and_return ( nil )
2022-03-11 07:07:56 -05:00
expect ( helper . storage_enforcement_banner_info ( free_group ) ) . to be ( nil )
2022-02-15 16:12:52 -05:00
end
2022-03-11 07:07:56 -05:00
describe 'when storage_enforcement_date is set' do
let_it_be ( :storage_enforcement_date ) { Date . today + 30 }
before do
allow ( free_group ) . to receive ( :storage_enforcement_date ) . and_return ( storage_enforcement_date )
end
it 'returns nil when current_user do not have access usage quotas page' do
2022-07-27 20:09:28 -04:00
allow ( helper ) . to receive ( :can? ) . with ( current_user , :maintainer_access , free_group ) . and_return ( false )
2022-03-11 07:07:56 -05:00
expect ( helper . storage_enforcement_banner_info ( free_group ) ) . to be ( nil )
end
2022-06-30 02:09:06 -04:00
it 'returns nil when namespace_storage_limit_show_preenforcement_banner FF is disabled' do
stub_feature_flags ( namespace_storage_limit_show_preenforcement_banner : false )
expect ( helper . storage_enforcement_banner_info ( free_group ) ) . to be ( nil )
end
2022-04-27 11:10:01 -04:00
context 'when current_user can access the usage quotas page' do
it 'returns a hash' do
2022-07-27 20:09:28 -04:00
used_storage = helper . storage_counter ( free_group . root_storage_statistics & . storage_size || 0 )
2022-04-27 11:10:01 -04:00
expect ( helper . storage_enforcement_banner_info ( free_group ) ) . to eql ( {
2022-08-11 08:09:19 -04:00
text_paragraph_1 : " Effective #{ storage_enforcement_date } , namespace storage limits will apply to the <strong> #{ free_group . name } </strong> namespace. View the <a href= \" /help/user/usage_quotas # namespace-storage-limit-enforcement-schedule \" >rollout schedule for this change</a>. " ,
text_paragraph_2 : " The namespace is currently using <strong> #{ used_storage } </strong> of namespace storage. Group owners can view namespace storage usage and purchase more from <strong>Group settings > Usage quotas</strong>. <a href= \" /help/user/usage_quotas # manage-your-storage-usage \" >Learn more.</a> " ,
text_paragraph_3 : " See our <a href= \" https://about.gitlab.com/pricing/faq-efficient-free-tier/ # storage-limits-on-gitlab-saas-free-tier \" >FAQ</a> for more information. " ,
2022-04-27 11:10:01 -04:00
variant : 'warning' ,
2022-07-27 20:09:28 -04:00
namespace_id : free_group . id ,
2022-04-27 11:10:01 -04:00
callouts_feature_name : 'storage_enforcement_banner_second_enforcement_threshold' ,
2022-07-27 20:09:28 -04:00
callouts_path : '/-/users/group_callouts'
2022-04-27 11:10:01 -04:00
} )
end
2022-08-19 05:11:29 -04:00
context 'when namespace is under MIN_STORAGE_ENFORCEMENT_USAGE limit' do
2022-04-27 11:10:01 -04:00
before do
2022-08-19 05:11:29 -04:00
free_group . root_storage_statistics . update! ( storage_size : :: Namespace :: MIN_STORAGE_ENFORCEMENT_USAGE - 1 )
2022-04-27 11:10:01 -04:00
end
2022-08-19 05:11:29 -04:00
it 'returns nil' do
expect ( helper . storage_enforcement_banner_info ( free_group ) ) . to be ( nil )
2022-04-27 11:10:01 -04:00
end
end
2022-06-08 11:08:15 -04:00
context 'when the given group is a sub-group' do
let_it_be ( :sub_group ) { build ( :group ) }
before do
2022-07-27 20:09:28 -04:00
allow ( helper ) . to receive ( :can? ) . with ( current_user , :maintainer_access , sub_group ) . and_return ( true )
2022-06-08 11:08:15 -04:00
allow ( sub_group ) . to receive ( :root_ancestor ) . and_return ( free_group )
end
it 'returns the banner hash' do
2022-07-27 20:09:28 -04:00
expect ( helper . storage_enforcement_banner_info ( sub_group ) . keys ) . to match_array ( % i ( text_paragraph_1 text_paragraph_2 text_paragraph_3 variant namespace_id callouts_feature_name callouts_path ) )
2022-06-08 11:08:15 -04:00
end
end
end
end
context 'when the :storage_banner_bypass_date_check is enabled' , :freeze_time do
before do
stub_feature_flags ( namespace_storage_limit_bypass_date_check : true )
end
it 'returns the enforcement info' do
2022-08-11 08:09:19 -04:00
expect ( helper . storage_enforcement_banner_info ( free_group ) [ :text_paragraph_1 ] ) . to include ( " Effective #{ Date . current } , namespace storage limits will apply " )
2022-03-11 07:07:56 -05:00
end
2022-02-15 16:12:52 -05:00
end
context 'when storage_enforcement_date is set and dismissed callout exists' do
before do
create ( :group_callout ,
user : current_user ,
group_id : free_group . id ,
feature_name : 'storage_enforcement_banner_second_enforcement_threshold' )
storage_enforcement_date = Date . today + 30
allow ( free_group ) . to receive ( :storage_enforcement_date ) . and_return ( storage_enforcement_date )
end
2022-03-11 07:07:56 -05:00
it { expect ( helper . storage_enforcement_banner_info ( free_group ) ) . to be ( nil ) }
2022-02-15 16:12:52 -05:00
end
context 'callouts_feature_name' do
let ( :days_from_now ) { 45 }
subject do
storage_enforcement_date = Date . today + days_from_now
allow ( free_group ) . to receive ( :storage_enforcement_date ) . and_return ( storage_enforcement_date )
2022-03-11 07:07:56 -05:00
helper . storage_enforcement_banner_info ( free_group ) [ :callouts_feature_name ]
2022-02-15 16:12:52 -05:00
end
it 'returns first callouts_feature_name' do
is_expected . to eq ( 'storage_enforcement_banner_first_enforcement_threshold' )
end
context 'returns second callouts_feature_name' do
let ( :days_from_now ) { 20 }
it { is_expected . to eq ( 'storage_enforcement_banner_second_enforcement_threshold' ) }
end
context 'returns third callouts_feature_name' do
let ( :days_from_now ) { 13 }
it { is_expected . to eq ( 'storage_enforcement_banner_third_enforcement_threshold' ) }
end
context 'returns fourth callouts_feature_name' do
let ( :days_from_now ) { 3 }
it { is_expected . to eq ( 'storage_enforcement_banner_fourth_enforcement_threshold' ) }
end
end
end
end
2016-11-22 11:58:10 -05:00
end