diff --git a/app/helpers/license_helper.rb b/app/helpers/license_helper.rb index e94d8c0ca32a7094dfe8578edbc6a75190590b5c..01968d085c91d806c6b7be6d19efcd9352824f18 100644 --- a/app/helpers/license_helper.rb +++ b/app/helpers/license_helper.rb @@ -12,66 +12,60 @@ def max_historical_user_count HistoricalData.max_historical_user_count end - # in_html is set to false from an initializer, which shouldn't try to render - # HTML links. - # - def license_message(signed_in: signed_in?, is_admin: (current_user && current_user.admin?), in_html: true) - @license_message = - if License.current - yes_license_message(signed_in, is_admin) - else - no_license_message(is_admin, in_html: in_html) - end + def license_message(signed_in: signed_in?, is_admin: (current_user && current_user.admin?)) + yes_license_message(signed_in, is_admin) if current_license end - private + def trial_license_message + if signed_in? && current_license&.trial? + status = current_license.expired? ? :expired : :active - def no_license_message(is_admin, in_html: true) - upload_a_license = - if in_html - link_to('Upload a license', new_admin_license_path) - else - 'Upload a license' - end + message = + if current_user.admin? + buy_now_link = link_to('Buy now!', 'https://customers.gitlab.com/plans', target: '_blank') - message = [] - message << 'No GitLab Enterprise Edition license has been provided yet.' - message << 'Pushing code and creation of issues and merge requests has been disabled.' - message << - if is_admin - "#{upload_a_license} in the admin area to activate this functionality." - else - 'Ask an admin to upload a license to activate this functionality.' - end - - if in_html - content_tag(:p, message.join(' ').html_safe) - else - message.join(' ') + if status == :active + remaining_days = (current_license.expires_at - Date.today).to_i + + "Your GitLab Enterprise Edition trial license remains #{pluralize(remaining_days, 'day')}. #{buy_now_link}".html_safe + else + "Your GitLab Enterprise Edition trial license expired. #{buy_now_link}".html_safe + end + elsif status == :expired + "Your GitLab Enterprise Edition trial license expired. Please contact your administrator." + end end + + message end - def yes_license_message(signed_in, is_admin) - license = License.current + private - return unless signed_in + def current_license + return @current_license if defined?(@current_license) + + @current_license = License.current + end - return unless (is_admin && license.notify_admins?) || license.notify_users? + def yes_license_message(signed_in, is_admin) + return if current_license.trial? + return unless signed_in + return unless (is_admin && current_license.notify_admins?) || current_license.notify_users? message = [] message << 'The GitLab Enterprise Edition license' - message << (license.expired? ? 'expired' : 'will expire') - message << "on #{license.expires_at}." + message << (current_license.expired? ? 'expired' : 'will expire') + message << "on #{current_license.expires_at}." - if license.expired? && license.will_block_changes? + if current_license.expired? && current_license.will_block_changes? message << 'Pushing code and creation of issues and merge requests' message << - if license.block_changes? + if current_license.block_changes? 'has been disabled.' else - "will be disabled on #{license.block_changes_at}." + "will be disabled on #{current_license.block_changes_at}." end message << @@ -82,7 +76,7 @@ def yes_license_message(signed_in, is_admin) end message << 'to' - message << (license.block_changes? ? 'restore' : 'ensure uninterrupted') + message << (current_license.block_changes? ? 'restore' : 'ensure uninterrupted') message << 'service.' end diff --git a/app/models/license.rb b/app/models/license.rb index 4035f04e89e20344efb3f58222c8228f1a382188..8e3b753cc25ff94118b1d215ee7d3194976deb56 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -110,7 +110,10 @@ def plan_includes_feature?(plan, code) end def block_changes? - !current || current.block_changes? + return false if current.nil? + return false if current&.trial? + + current.block_changes? end def load_license @@ -170,6 +173,8 @@ def respond_to_missing?(method_name, include_private = false) # keep `add_ons`, therefore this method needs to be backward-compatible in that sense. # See https://gitlab.com/gitlab-org/gitlab-ee/issues/2019 def add_ons + return {} if trial? && expired? + explicit_add_ons = restricted_attr(:add_ons, {}) plan_features = self.class.features_for_plan(plan) diff --git a/app/views/layouts/header/_default.html.haml b/app/views/layouts/header/_default.html.haml index 1cb8c42716faf9674a8fd64f26e26010e03cb916..4f38758a583f7697842e4108042774e9e1d876e2 100644 --- a/app/views/layouts/header/_default.html.haml +++ b/app/views/layouts/header/_default.html.haml @@ -2,6 +2,8 @@ .navbar-border %a.sr-only.gl-accessibility{ href: "#content-body", tabindex: "1" } Skip to content .container-fluid + - if trial_license_message.present? + %p.text-center= trial_license_message .header-content .dropdown.global-dropdown %button.global-dropdown-toggle{ type: 'button', 'data-toggle' => 'dropdown' } diff --git a/features/admin/license.feature b/features/admin/license.feature index 7588d09cf823d735658ad31c137d6654af71f9f0..edf350568f547d116ca0bcaa77969870c1516bb2 100644 --- a/features/admin/license.feature +++ b/features/admin/license.feature @@ -8,12 +8,6 @@ Feature: Admin license And I visit admin license page Then I should see to whom the license is licensed - Scenario: Viewing license when there is none - Given There is no license - And I visit admin license page - Then I should see a warning telling me there is no license - And I should be redirected to the license upload page - Scenario: Viewing expired license Given there is a license And the current license is expired diff --git a/features/steps/admin/license.rb b/features/steps/admin/license.rb index a5d0ae98b81914f695ac8d4f580bf68c7286d733..32a480661447040815c1aac87d07fa79c91d9336 100644 --- a/features/steps/admin/license.rb +++ b/features/steps/admin/license.rb @@ -14,10 +14,6 @@ class Spinach::Features::AdminLicense < Spinach::FeatureSteps License.destroy_all end - step 'I should see a warning telling me there is no license' do - expect(page).to have_content "No GitLab Enterprise Edition license has been provided yet." - end - step 'I should be redirected to the license upload page' do expect(current_path).to eq(new_admin_license_path) end diff --git a/spec/factories/licenses.rb b/spec/factories/licenses.rb index 0dcbfb779915e0ce8f3657eb1f9f8d034870476a..082278b2905319284a62e695a247224d91dfbbb7 100644 --- a/spec/factories/licenses.rb +++ b/spec/factories/licenses.rb @@ -1,10 +1,25 @@ FactoryGirl.define do factory :gitlab_license, class: "Gitlab::License" do + trait :trial do + restrictions do + { trial: true } + end + end + + trait :expired do + expires_at { 3.weeks.ago.to_date } + end + starts_at { Date.today - 1.month } expires_at { Date.today + 11.months } + block_changes_at { expires_at + 2.weeks } + notify_users_at { expires_at } + notify_admins_at { expires_at } + licensee do { "Name" => generate(:name) } end + restrictions do { add_ons: { @@ -13,21 +28,23 @@ } } end - notify_users_at { |l| l.expires_at } - notify_admins_at { |l| l.expires_at } - - trait :trial do - restrictions do - { trial: true } - end - end end factory :license do - data { build(:gitlab_license).export } - end + transient do + expired false + trial false + end + + data do + attrs = [:gitlab_license] + attrs << :trial if trial + attrs << :expired if expired + + build(*attrs).export + end - factory :trial_license, class: License do - data { build(:gitlab_license, :trial).export } + # Disable validations when creating an expired license key + to_create {|instance| instance.save(validate: !expired) } end end diff --git a/spec/helpers/license_helper_spec.rb b/spec/helpers/license_helper_spec.rb index a029dd12069e04e263d49964a1c83c948134d948..5b966a549b87461bbd6a09e7d4473f911ad50aad 100644 --- a/spec/helpers/license_helper_spec.rb +++ b/spec/helpers/license_helper_spec.rb @@ -11,18 +11,14 @@ let(:is_admin) { true } it 'displays correct error message for admin user' do - admin_msg = '

No GitLab Enterprise Edition license has been provided yet. Pushing code and creation of issues and merge requests has been disabled. Upload a license in the admin area to activate this functionality.

' - - expect(license_message(signed_in: true, is_admin: is_admin)).to eq(admin_msg) + expect(license_message(signed_in: true, is_admin: is_admin)).to be_blank end end context 'normal user' do let(:is_admin) { false } it 'displays correct error message for normal user' do - user_msg = '

No GitLab Enterprise Edition license has been provided yet. Pushing code and creation of issues and merge requests has been disabled. Ask an admin to upload a license to activate this functionality.

' - - expect(license_message(signed_in: true, is_admin: is_admin)).to eq(user_msg) + expect(license_message(signed_in: true, is_admin: is_admin)).to be_blank end end end diff --git a/spec/models/license_spec.rb b/spec/models/license_spec.rb index ccdfa3d3e4937957eaae1a9456fc489cce2e4d70..a56b9f66f78449d4e7ff29258c1fa64f6f9cf039 100644 --- a/spec/models/license_spec.rb +++ b/spec/models/license_spec.rb @@ -292,8 +292,24 @@ allow(described_class).to receive(:current).and_return(nil) end - it "returns true" do - expect(described_class.block_changes?).to be_truthy + it "returns false" do + expect(described_class.block_changes?).to eq(false) + end + end + + context 'with an expired trial license' do + let!(:license) { create(:license, trial: true) } + + it 'returns false' do + expect(described_class.block_changes?).to eq(false) + end + end + + context 'with an expired normal license' do + let!(:license) { create(:license, expired: true) } + + it 'returns true' do + expect(described_class.block_changes?).to eq(true) end end @@ -389,6 +405,15 @@ expect(license.add_ons.keys).to include(License::DEPLOY_BOARD_FEATURE, *eep_features) end end + + context 'with an expired trial license' do + it 'returns an empty Hash' do + described_class.destroy_all + create(:license, trial: true, expired: true) + + expect(described_class.current.add_ons).to be_empty + end + end end describe '#feature_available?' do diff --git a/spec/requests/git_http_spec.rb b/spec/requests/git_http_spec.rb index b21a27a5971d2b0d31289c01282ad8bdfc2a78d3..3484c5cba7cf9dcda9abc9b37fcd04339c0d6d87 100644 --- a/spec/requests/git_http_spec.rb +++ b/spec/requests/git_http_spec.rb @@ -733,18 +733,13 @@ def attempt_login(include_password) let(:env) { { user: user.username, password: user.password } } before do - project.team << [user, :master] - end - - it 'responds with status 403 Forbidden' do - msg = 'No GitLab Enterprise Edition license has been provided yet. Pushing code and creation of issues and merge requests has been disabled. Ask an admin to upload a license to activate this functionality.' allow(License).to receive(:current).and_return(nil) - upload(path, env) do |response| - expect(response).to have_http_status(:forbidden) - expect(response.body).to eq(msg) - end + project.team << [user, :master] end + + it_behaves_like 'pushes are allowed' + it_behaves_like 'pushes are allowed' end end diff --git a/spec/workers/historical_data_worker_spec.rb b/spec/workers/historical_data_worker_spec.rb index 769cb8ac8b6380ceccd59ebf10ef31441ba17ee7..c32441082aafefa901338669909580427078307e 100644 --- a/spec/workers/historical_data_worker_spec.rb +++ b/spec/workers/historical_data_worker_spec.rb @@ -6,7 +6,7 @@ describe '#perform' do context 'with a trial license' do before do - FactoryGirl.create(:trial_license) + FactoryGirl.create(:license, trial: true) end it 'does not track historical data' do