diff --git a/CHANGELOG-EE b/CHANGELOG-EE index b2c6ca7bdf66a88d4ed62e75673983f81bb4e990..3914b193da0a3aca7ce892ae9a62b563a313c0ec 100644 --- a/CHANGELOG-EE +++ b/CHANGELOG-EE @@ -4,6 +4,7 @@ v 8.8.0 (unreleased) - [Elastic] Database indexer prints its status - [Elastic][Fix] Database indexer skips projects with invalid HEAD reference - Fix skipping pages when restoring backups + - Add EE license via API !400 - [Elastic] More efficient snippets search - [Elastic] Add rake task for removing all indexes - [Elastic] Add rake task for clearing indexing status diff --git a/lib/api/api.rb b/lib/api/api.rb index 5c5ff52d7db3b1ac956714200f521732e45acc25..d9e6bd67e13a859cf72063b8bb433ae1bf1b3023 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -56,12 +56,12 @@ module API mount ::API::Settings mount ::API::Keys mount ::API::Tags - mount ::API::LicenseInfo + mount ::API::License mount ::API::Triggers mount ::API::Builds mount ::API::Variables mount ::API::Runners - mount ::API::Licenses + mount ::API::LicenseTemplates mount ::API::Subscriptions end end diff --git a/lib/api/license.rb b/lib/api/license.rb new file mode 100644 index 0000000000000000000000000000000000000000..ea42ba2a07e8349ac96c80658e23b520524fc517 --- /dev/null +++ b/lib/api/license.rb @@ -0,0 +1,35 @@ +module API + class License < Grape::API + before { authenticated_as_admin! } + + resource :license do + + # Get information on the currently active license + # + # Example request: + # GET /license + get do + license = ::License.current + + present license, with: Entities::License + end + + # Add a new license + # + # Parameters: + # license (required) - The license text + # + # Example request: + # POST /license + post do + required_attributes! [:license] + license = ::License.new(data: params[:license]) + if license.save + present license, with: Entities::License + else + render_api_error!(license.errors.full_messages.first, 409) + end + end + end + end +end diff --git a/lib/api/license_info.rb b/lib/api/license_info.rb deleted file mode 100644 index c2c173af4292d8e2aacfb5458c5a51031c8bdc45..0000000000000000000000000000000000000000 --- a/lib/api/license_info.rb +++ /dev/null @@ -1,18 +0,0 @@ -module API - class LicenseInfo < Grape::API - before { authenticated_as_admin! } - - resource :license do - - # Get information on the currently active license - # - # Example request: - # GET /license - get do - @license = License.current - - present @license, with: Entities::License - end - end - end -end diff --git a/lib/api/licenses.rb b/lib/api/license_templates.rb similarity index 97% rename from lib/api/licenses.rb rename to lib/api/license_templates.rb index 187d2c047036bb6c73eda93790d785fa6223dbf6..62fa9fb6caf0ce43411e357c2a21f189edf7107c 100644 --- a/lib/api/licenses.rb +++ b/lib/api/license_templates.rb @@ -1,6 +1,6 @@ module API # Licenses API - class Licenses < Grape::API + class LicenseTemplates < Grape::API PROJECT_TEMPLATE_REGEX = /[\<\{\[] (project|description| diff --git a/spec/requests/api/license_spec.rb b/spec/requests/api/license_spec.rb index 6ff56cd708f493d703344ba2bdcadb92eebc5e23..d7c7dea550c29a6b7bf6d6f99c66da7461100119 100644 --- a/spec/requests/api/license_spec.rb +++ b/spec/requests/api/license_spec.rb @@ -24,4 +24,21 @@ describe API::API, api: true do expect(response.status).to eq 403 end end + + describe 'POST /license' do + it 'should add a new license if admin is logged in' do + post api('/license', admin), license: gl_license.export + expect(response.status).to eq 201 + expect(json_response['user_limit']).to eq 0 + expect(Date.parse(json_response['starts_at'])).to eq Date.today - 1.month + expect(Date.parse(json_response['expires_at'])).to eq Date.today + 11.months + expect(json_response['active_users']).to eq 1 + expect(json_response['licensee']).to_not be_empty + end + + it 'should deny access if not admin' do + post api('/license', user), license: license + expect(response.status).to eq 403 + end + end end diff --git a/spec/requests/api/licenses_spec.rb b/spec/requests/api/license_templates_spec.rb similarity index 99% rename from spec/requests/api/licenses_spec.rb rename to spec/requests/api/license_templates_spec.rb index c17dcb222a9d36b5bdb8d0319c945d46f53fc424..64c800f257853a0f6103c614e742c96fcdf393d1 100644 --- a/spec/requests/api/licenses_spec.rb +++ b/spec/requests/api/license_templates_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe API::Licenses, api: true do +describe API::API, api: true do include ApiHelpers describe 'Entity' do