Commit 75c459e6 authored by Kamil Trzciński's avatar Kamil Trzciński

Add Unleash API specs

parent a1c10223
...@@ -12,5 +12,12 @@ module Operations ...@@ -12,5 +12,12 @@ module Operations
add_authentication_token_field :token add_authentication_token_field :token
before_validation :ensure_token! before_validation :ensure_token!
def self.find_for_project_and_token(project, token)
return unless project
return unless token
find_by(token: token, project: project)
end
end end
end end
...@@ -10,7 +10,7 @@ module API ...@@ -10,7 +10,7 @@ module API
end end
route_param :project_id do route_param :project_id do
before do before do
authenticate_by_unleash_access_token! authenticate_by_unleash_instanceid!
end end
get 'features' do get 'features' do
...@@ -36,19 +36,12 @@ module API ...@@ -36,19 +36,12 @@ module API
end end
def unleash_instanceid def unleash_instanceid
params[:instanceid] || env[:HTTP_UNLEASH_INSTANCEID] params[:instanceid] || env['HTTP_UNLEASH_INSTANCEID']
end end
def unleash_access_token def authenticate_by_unleash_instanceid!
return unless unleash_instanceid unauthorized! unless Operations::FeatureFlagsClient
return unless project .find_for_project_and_token(project, unleash_instanceid)
@unleash_access_token ||= Operations::FeatureFlagsAccessToken.find_by(
token: unleash_instanceid, project: project)
end
def authenticate_by_unleash_access_token!
unauthorized! unless unleash_access_token
end end
end end
end end
......
...@@ -436,13 +436,17 @@ module EE ...@@ -436,13 +436,17 @@ module EE
class UnleashFeatures < Grape::Entity class UnleashFeatures < Grape::Entity
expose :version expose :version
expose :operations_feature_flags, as: :features, with: UnleashFeature expose :features, with: UnleashFeature
private private
def version def version
1 1
end end
def features
object.operations_feature_flags.ordered
end
end end
end end
end end
......
...@@ -56,6 +56,8 @@ project: ...@@ -56,6 +56,8 @@ project:
- vulnerability_feedback - vulnerability_feedback
- vulnerability_identifiers - vulnerability_identifiers
- vulnerability_scanners - vulnerability_scanners
- operations_feature_flags
- operations_feature_flags_instance
- prometheus_alerts - prometheus_alerts
- prometheus_alert_events - prometheus_alert_events
- software_license_policies - software_license_policies
......
...@@ -1768,10 +1768,10 @@ describe Project do ...@@ -1768,10 +1768,10 @@ describe Project do
end end
end end
describe '#feature_flag_access_token' do describe '#feature_flags_client_token' do
let(:project) { create(:project) } let(:project) { create(:project) }
subject { project.feature_flag_access_token } subject { project.feature_flags_client_token }
context 'when there is no access token' do context 'when there is no access token' do
it "creates a new one" do it "creates a new one" do
......
require 'spec_helper'
describe API::Unleash do
set(:project) { create(:project) }
let(:project_id) { project.id }
let(:params) { }
let(:headers) { }
shared_examples 'authenticated request' do
context 'when using instanceid' do
let(:client) { create(:operations_feature_flags_client, project: project) }
let(:params) { { instanceid: client.token } }
it 'responds with OK' do
subject
expect(response).to have_gitlab_http_status(200)
end
end
context 'when using header' do
let(:client) { create(:operations_feature_flags_client, project: project) }
let(:headers) { { "UNLEASH-INSTANCEID" => client.token }}
it 'responds with OK' do
subject
expect(response).to have_gitlab_http_status(200)
end
end
context 'when using bogus instanceid' do
let(:params) { { instanceid: 'token' } }
it 'responds with unauthorized' do
subject
expect(response).to have_gitlab_http_status(401)
end
end
context 'when using not existing project' do
let(:project_id) { -5000 }
let(:params) { { instanceid: 'token' } }
it 'responds with unauthorized' do
subject
expect(response).to have_gitlab_http_status(401)
end
end
end
describe 'GET /feature_flags/unleash/:project_id/features' do
subject { get api("/feature_flags/unleash/#{project_id}/features"), params, headers }
it_behaves_like 'authenticated request'
context 'with a list of feature flag' do
let(:client) { create(:operations_feature_flags_client, project: project) }
let(:headers) { { "UNLEASH-INSTANCEID" => client.token }}
let!(:enable_feature_flag) { create(:operations_feature_flag, project: project, name: 'feature1', active: true) }
let!(:disabled_feature_flag) { create(:operations_feature_flag, project: project, name: 'feature2', active: false) }
it 'responds with a list' do
subject
expect(response).to have_gitlab_http_status(200)
expect(json_response['version']).to eq(1)
expect(json_response['features']).not_to be_empty
expect(json_response['features'].first['name']).to eq('feature1')
end
it 'matches json schema' do
subject
expect(response).to have_gitlab_http_status(:ok)
# TODO: match schema
end
end
end
describe 'POST /feature_flags/unleash/:project_id/client/register' do
subject { post api("/feature_flags/unleash/#{project_id}/client/register"), params, headers }
it_behaves_like 'authenticated request'
end
describe 'POST /feature_flags/unleash/:project_id/client/metrics' do
subject { post api("/feature_flags/unleash/#{project_id}/client/metrics"), params, headers }
it_behaves_like 'authenticated request'
end
end
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