lab.nexedi.com will be down from Thursday, 20 March 2025, 07:30:00 UTC for a duration of approximately 2 hours

  • mlunoe's avatar
    Feat(Licesing): update upload to activate · 36b4b17e
    mlunoe authored
    Since the user both kan upload a license file and
    paste a license key, we are updating the copy to
    reflect this.
    
    This change also goes through and updates the docs
    that were a bit out-dated in this area
    
    Changelog: added
    EE: true
    36b4b17e
admin_views_subscription_spec.rb 7.97 KB
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Admin views Subscription', :js do
  let_it_be(:admin) { create(:admin) }

  before do
    stub_feature_flags(bootstrap_confirmation_modals: false)
    sign_in(admin)
    gitlab_enable_admin_mode_sign_in(admin)
  end

  shared_examples 'an "Export license usage file" button' do
    it 'displays the Export License Usage File button' do
      expect(page).to have_link('Export license usage file', href: admin_license_usage_export_path(format: :csv))
    end
  end

  shared_examples 'license removal' do
    context 'when removing a license file' do
      before do
        accept_alert do
          click_on 'Remove license'
        end
      end

      it 'shows a message saying the license was correctly removed' do
        page.within(find('#content-body', match: :first)) do
          expect(page).to have_content('The license was removed.')
        end
      end
    end
  end

  shared_examples 'no active license' do
    it 'displays a message signaling there is not active subscription' do
      page.within(find('#content-body', match: :first)) do
        expect(page).to have_content('You do not have an active subscription')
      end
    end
  end

  context 'with a cloud license' do
    let!(:license) { create_current_license(cloud_licensing_enabled: true, plan: License::ULTIMATE_PLAN) }

    context 'with a cloud license only' do
      before do
        visit(admin_subscription_path)
      end

      it 'displays the subscription details' do
        page.within(find('#content-body', match: :first)) do
          expect(page).to have_content('Subscription details')
          expect(all("[data-testid='details-label']")[1]).to have_content('Plan:')
          expect(all("[data-testid='details-content']")[1]).to have_content('Ultimate')
        end
      end

      it 'succeeds to sync the subscription' do
        page.within(find('#content-body', match: :first)) do
          click_button('Sync subscription details')

          expect(page).to have_content('Your subscription details will sync shortly.')
        end
      end

      it 'fails to sync the subscription' do
        create_current_license(cloud_licensing_enabled: true, plan: License::ULTIMATE_PLAN, expires_at: nil)

        page.within(find('#content-body', match: :first)) do
          click_button('Sync subscription details')

          expect(page).to have_content('You can no longer sync your subscription details with GitLab. Get help for the most common connectivity issues by troubleshooting the activation code')
        end
      end

      it_behaves_like 'an "Export license usage file" button'
      it_behaves_like 'license removal'
    end
  end

  context 'with license file' do
    let!(:license) { create_current_license(cloud_licensing_enabled: false, plan: License::ULTIMATE_PLAN) }

    before do
      visit(admin_subscription_path)
    end

    it_behaves_like 'an "Export license usage file" button'
    it_behaves_like 'license removal'

    context 'when activating another subscription' do
      before do
        page.within(find('[data-testid="subscription-details"]', match: :first)) do
          click_button('Enter activation code')
        end
      end

      it 'shows the activation modal' do
        page.within(find('#subscription-activation-modal', match: :first)) do
          expect(page).to have_content('Activate subscription')
        end
      end

      it 'displays an error when the activation fails' do
        stub_request(:post, EE::SUBSCRIPTIONS_GRAPHQL_URL).to_return(status: 422, body: '', headers: {})

        page.within(find('#subscription-activation-modal', match: :first)) do
          fill_activation_form

          expect(page).to have_content('An error occurred while activating your subscription.')
        end
      end

      it 'displays a connectivity error' do
        stub_request(:post, EE::SUBSCRIPTIONS_GRAPHQL_URL)
          .to_return(status: 500, body: '', headers: {})

        page.within(find('#subscription-activation-modal', match: :first)) do
          fill_activation_form

          expect(page).to have_content('There is a connectivity issue.')
        end
      end
    end
  end

  context 'with no active subscription' do
    let_it_be(:license_to_be_created) { nil }

    before do
      License.current.destroy!

      visit(admin_subscription_path)
    end

    it_behaves_like 'no active license'

    it 'does not display the Export License Usage File button' do
      expect(page).not_to have_link('Export license usage file', href: admin_license_usage_export_path(format: :csv))
    end

    context 'when activating a subscription fails' do
      before do
        stub_request(:post, EE::SUBSCRIPTIONS_GRAPHQL_URL)
          .to_return(status: 200, body: {
            "data": {
              "cloudActivationActivate": {
                "errors": ["invalid activation code"],
                "license": license_to_be_created
              },
              "success": "true"
            }
          }.to_json, headers: { 'Content-Type' => 'application/json' })

        page.within(find('#content-body', match: :first)) do
          fill_activation_form
        end
      end

      it 'shows an error message' do
        expect(page).to have_content('An error occurred while activating your subscription.')
      end
    end

    context 'when activating a future-dated subscription' do
      let_it_be(:license_to_be_created) { build(:license, data: build(:gitlab_license, { starts_at: Date.current + 1.month, cloud_licensing_enabled: true, plan: License::ULTIMATE_PLAN }).export) }

      before do
        stub_request(:post, EE::SUBSCRIPTIONS_GRAPHQL_URL)
          .to_return(status: 200, body: {
            "data": {
              "cloudActivationActivate": {
                "licenseKey": license_to_be_created.data
              }
            }
          }.to_json, headers: { 'Content-Type' => 'application/json' })

        page.within(find('#content-body', match: :first)) do
          fill_activation_form
        end
      end

      it 'shows a successful future-dated activation message' do
        expect(page).to have_content('Your future dated license was successfully added')
      end

      it_behaves_like 'no active license'
    end

    context 'when activating a new subscription' do
      let_it_be(:license_to_be_created) { build(:license, data: build(:gitlab_license, { starts_at: Date.current, cloud_licensing_enabled: true, plan: License::ULTIMATE_PLAN }).export) }

      before do
        stub_request(:post, EE::SUBSCRIPTIONS_GRAPHQL_URL)
          .to_return(status: 200, body: {
            "data": {
              "cloudActivationActivate": {
                "licenseKey": license_to_be_created.data
              }
            }
          }.to_json, headers: { 'Content-Type' => 'application/json' })

        page.within(find('#content-body', match: :first)) do
          fill_activation_form
        end
      end

      it 'shows a successful activation message' do
        expect(page).to have_content('Your subscription was successfully activated.')
      end

      it 'shows the subscription details' do
        expect(page).to have_content('Subscription details')
      end

      it 'shows the appropriate license type' do
        page.within(find('[data-testid="subscription-cell-type"]', match: :first)) do
          expect(page).to have_content('Cloud license')
        end
      end
    end

    context 'when uploading a license file' do
      it 'does not show a link to activate a license file' do
        page.within(find('#content-body', match: :first)) do
          expect(page).not_to have_link('Activate a license', href: new_admin_license_path)
        end
      end
    end
  end

  include_examples 'manual renewal banner', path_to_visit: :admin_subscription_path
  include_examples 'manual quarterly co-term banner', path_to_visit: :admin_subscription_path

  private

  def fill_activation_form
    fill_in 'activationCode', with: '00112233aaaassssddddffff'
    check 'subscription-form-terms-check'
    click_button 'Activate'
  end
end