Commit 26cfd3ee authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'api_post_license' into 'master'

Allow adding a new license via the API

Fixes #313 

Add a new `post` license API endpoint. The endpoint is `/license` and the only/required attribute is `license`. The license should be sent in string format. 

I also renamed the `licenses.rb` api file because it deals with license templates and was pretty ambiguous. The endpoints didn't change, and are still equally ambiguous, unfortunately. We should really consider changing those in 9.0 to `/license_template` or something. 

cc/ @MrChrisW 

@rymai Will you review, please?

See merge request !400
parents ab14807e c9492eb5
...@@ -4,6 +4,7 @@ v 8.8.0 (unreleased) ...@@ -4,6 +4,7 @@ v 8.8.0 (unreleased)
- [Elastic] Database indexer prints its status - [Elastic] Database indexer prints its status
- [Elastic][Fix] Database indexer skips projects with invalid HEAD reference - [Elastic][Fix] Database indexer skips projects with invalid HEAD reference
- Fix skipping pages when restoring backups - Fix skipping pages when restoring backups
- Add EE license via API !400
- [Elastic] More efficient snippets search - [Elastic] More efficient snippets search
- [Elastic] Add rake task for removing all indexes - [Elastic] Add rake task for removing all indexes
- [Elastic] Add rake task for clearing indexing status - [Elastic] Add rake task for clearing indexing status
......
...@@ -56,12 +56,12 @@ module API ...@@ -56,12 +56,12 @@ module API
mount ::API::Settings mount ::API::Settings
mount ::API::Keys mount ::API::Keys
mount ::API::Tags mount ::API::Tags
mount ::API::LicenseInfo mount ::API::License
mount ::API::Triggers mount ::API::Triggers
mount ::API::Builds mount ::API::Builds
mount ::API::Variables mount ::API::Variables
mount ::API::Runners mount ::API::Runners
mount ::API::Licenses mount ::API::LicenseTemplates
mount ::API::Subscriptions mount ::API::Subscriptions
end end
end end
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, 400)
end
end
end
end
end
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
module API module API
# Licenses API # Licenses API
class Licenses < Grape::API class LicenseTemplates < Grape::API
PROJECT_TEMPLATE_REGEX = PROJECT_TEMPLATE_REGEX =
/[\<\{\[] /[\<\{\[]
(project|description| (project|description|
......
...@@ -24,4 +24,29 @@ describe API::API, api: true do ...@@ -24,4 +24,29 @@ describe API::API, api: true do
expect(response.status).to eq 403 expect(response.status).to eq 403
end end
end end
describe 'POST /license' do
it 'adds 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 'denies access if not admin' do
post api('/license', user), license: license
expect(response.status).to eq 403
end
it 'returns 400 if the license cannot be saved' do
post api('/license', admin), license: 'foo'
expect(response.status).to eq(400)
end
end
end end
require 'spec_helper' require 'spec_helper'
describe API::Licenses, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
describe 'Entity' do describe 'Entity' do
......
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