Commit 55d1a072 authored by Tyler Amos's avatar Tyler Amos

Prevent manual upload of cloud license

Updates the LicenseController#create action to prevent cloud licenses
from being created.  Presents an alert to the user to upload a valid
license.

Improve LicenseController specs with expectations on the creation of
License records.
parent 795d3b34
...@@ -27,16 +27,12 @@ class Admin::LicensesController < Admin::ApplicationController ...@@ -27,16 +27,12 @@ class Admin::LicensesController < Admin::ApplicationController
end end
def create def create
unless params[:license][:data].present? || params[:license][:data_file].present? return upload_license_error if license_params[:data].blank? && license_params[:data_file].blank?
flash[:alert] = _('Please enter or upload a license.')
@license = License.new
redirect_to new_admin_license_path
return
end
@license = License.new(license_params) @license = License.new(license_params)
return upload_license_error if @license.cloud?
respond_with(@license, location: admin_license_path) do respond_with(@license, location: admin_license_path) do
if @license.save if @license.save
notice = if @license.started? notice = if @license.started?
...@@ -85,4 +81,10 @@ class Admin::LicensesController < Admin::ApplicationController ...@@ -85,4 +81,10 @@ class Admin::LicensesController < Admin::ApplicationController
license_params.delete(:data) if license_params[:data_file] license_params.delete(:data) if license_params[:data_file]
license_params license_params
end end
def upload_license_error
flash[:alert] = _('Please enter or upload a valid license.')
@license = License.new
redirect_to new_admin_license_path
end
end end
...@@ -13,28 +13,42 @@ RSpec.describe Admin::LicensesController do ...@@ -13,28 +13,42 @@ RSpec.describe Admin::LicensesController do
render_views render_views
it 'redirects back when no license is entered/uploaded' do it 'redirects back when no license is entered/uploaded' do
expect do
post :create, params: { license: { data: '' } } post :create, params: { license: { data: '' } }
end.not_to change(License, :count)
expect(response).to redirect_to new_admin_license_path expect(response).to redirect_to new_admin_license_path
expect(flash[:alert]).to include 'Please enter or upload a license.' expect(flash[:alert]).to include 'Please enter or upload a valid license.'
end
context 'when the license is for a cloud license' do
it 'redirects back' do
license = build_license(type: 'cloud')
expect do
post :create, params: { license: { data: license.data } }
end.not_to change(License, :count)
expect(response).to redirect_to new_admin_license_path
expect(flash[:alert]).to include 'Please enter or upload a valid license.'
end
end end
it 'renders new with an alert when an invalid license is entered/uploaded' do it 'renders new with an alert when an invalid license is entered/uploaded' do
expect do
post :create, params: { license: { data: 'GA!89-)GaRBAGE' } } post :create, params: { license: { data: 'GA!89-)GaRBAGE' } }
end.not_to change(License, :count)
expect(response).to render_template(:new) expect(response).to render_template(:new)
expect(response.body).to include('The license key is invalid. Make sure it is exactly as you received it from GitLab Inc.') expect(response.body).to include('The license key is invalid. Make sure it is exactly as you received it from GitLab Inc.')
end end
it 'redirects to show when a valid license is entered/uploaded' do it 'redirects to show when a valid license is entered/uploaded' do
gl_license = build(:gitlab_license, restrictions: { license = build_license
trial: false,
plan: License::PREMIUM_PLAN,
active_user_count: 1,
previous_user_count: 1
})
license = build(:license, data: gl_license.export)
expect do
post :create, params: { license: { data: license.data } } post :create, params: { license: { data: license.data } }
end.to change(License, :count).by(1)
expect(response).to redirect_to(admin_license_path) expect(response).to redirect_to(admin_license_path)
end end
...@@ -45,21 +59,27 @@ RSpec.describe Admin::LicensesController do ...@@ -45,21 +59,27 @@ RSpec.describe Admin::LicensesController do
end end
it 'redirects to show when a valid trial license is entered/uploaded' do it 'redirects to show when a valid trial license is entered/uploaded' do
gl_license = build(:gitlab_license, license = build_license(restrictions: { trial: true })
expires_at: Date.tomorrow,
restrictions: {
trial: true,
plan: License::PREMIUM_PLAN,
active_user_count: 1,
previous_user_count: 1
})
license = build(:license, data: gl_license.export)
expect do
post :create, params: { license: { data: license.data } } post :create, params: { license: { data: license.data } }
end.to change(License, :count).by(1)
expect(response).to redirect_to(admin_license_path) expect(response).to redirect_to(admin_license_path)
end end
end end
def build_license(type: nil, restrictions: {})
license_restrictions = {
trial: false,
plan: License::PREMIUM_PLAN,
active_user_count: 1,
previous_user_count: 1
}.merge(restrictions)
gl_license = build(:gitlab_license, type: type, restrictions: license_restrictions)
build(:license, data: gl_license.export)
end
end end
describe 'GET show' do describe 'GET show' do
......
...@@ -23591,7 +23591,7 @@ msgstr "" ...@@ -23591,7 +23591,7 @@ msgstr ""
msgid "Please enter a valid number" msgid "Please enter a valid number"
msgstr "" msgstr ""
msgid "Please enter or upload a license." msgid "Please enter or upload a valid license."
msgstr "" msgstr ""
msgid "Please fill in a descriptive name for your group." msgid "Please fill in a descriptive name for your group."
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment