Commit 4643d944 authored by Kamil Trzciński's avatar Kamil Trzciński

Fix after code review

parent 95ed2c38
...@@ -1959,7 +1959,7 @@ ActiveRecord::Schema.define(version: 20180926140319) do ...@@ -1959,7 +1959,7 @@ ActiveRecord::Schema.define(version: 20180926140319) do
t.string "token", null: false t.string "token", null: false
end end
add_index "operations_feature_flags_clients", ["token"], name: "index_operations_feature_flags_clients_on_token", unique: true, using: :btree add_index "operations_feature_flags_clients", ["project_id", "token"], name: "index_operations_feature_flags_clients_on_project_id_and_token", unique: true, using: :btree
create_table "packages_maven_metadata", id: :bigserial, force: :cascade do |t| create_table "packages_maven_metadata", id: :bigserial, force: :cascade do |t|
t.integer "package_id", limit: 8, null: false t.integer "package_id", limit: 8, null: false
......
# frozen_string_literal: true # frozen_string_literal: true
module FeatureFlagsHelper module FeatureFlagsHelper
include ::API::Helpers::RelatedResourcesHelpers
def unleash_api_url(project) def unleash_api_url(project)
"#{root_url(only_path: false)}api/v4/feature_flags/unleash/#{project.id}" expose_url(api_v4_feature_flags_unleash_path(project_id: project.id))
end end
def unleash_api_instanceid(project) def unleash_api_instance_id(project)
project.feature_flags_client_token project.feature_flags_client_token
end end
end end
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
= label_tag :instance_id, s_('FeatureFlags|Instance ID'), class: 'label-bold' = label_tag :instance_id, s_('FeatureFlags|Instance ID'), class: 'label-bold'
.input-group .input-group
= text_field_tag :instance_id, = text_field_tag :instance_id,
unleash_api_instanceid(@project), unleash_api_instance_id(@project),
readonly: true, readonly: true,
class: "form-control js-select-on-focus" class: "form-control js-select-on-focus"
%span.input-group-append %span.input-group-append
......
#error_explanation #error_explanation
.alert.alert-danger .alert.alert-danger
- @feature_flag.errors.full_messages.each do |msg| - @feature_flag.errors.full_messages.each do |message|
%p= msg %p= message
...@@ -23,7 +23,7 @@ class AddFeatureFlagsToProjects < ActiveRecord::Migration ...@@ -23,7 +23,7 @@ class AddFeatureFlagsToProjects < ActiveRecord::Migration
t.integer :project_id, null: false t.integer :project_id, null: false
t.string :token, null: false t.string :token, null: false
t.index :token, unique: true t.index [:project_id, :token], unique: true
t.foreign_key :projects, column: :project_id, on_delete: :cascade t.foreign_key :projects, column: :project_id, on_delete: :cascade
end end
......
...@@ -6,14 +6,19 @@ module API ...@@ -6,14 +6,19 @@ module API
resource :unleash, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do resource :unleash, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
params do params do
requires :project_id, type: String, desc: 'The ID of a project' requires :project_id, type: String, desc: 'The ID of a project'
optional :instanceid, type: String, desc: 'The Instance ID of Unleash Client' optional :instance_id, type: String, desc: 'The Instance ID of Unleash Client'
end end
route_param :project_id do route_param :project_id do
before do before do
authorize_by_unleash_instanceid! authorize_by_unleash_instance_id!
authorize_feature_flags_feature! authorize_feature_flags_feature!
end end
get do
# not supported yet
status :ok
end
get 'features' do get 'features' do
present project, with: ::EE::API::Entities::UnleashFeatures present project, with: ::EE::API::Entities::UnleashFeatures
end end
...@@ -36,13 +41,13 @@ module API ...@@ -36,13 +41,13 @@ module API
@project ||= find_project(params[:project_id]) @project ||= find_project(params[:project_id])
end end
def unleash_instanceid def unleash_instance_id
params[:instanceid] || env['HTTP_UNLEASH_INSTANCEID'] params[:instance_id] || env['HTTP_UNLEASH_INSTANCEID']
end end
def authorize_by_unleash_instanceid! def authorize_by_unleash_instance_id!
unauthorized! unless Operations::FeatureFlagsClient unauthorized! unless Operations::FeatureFlagsClient
.find_for_project_and_token(project, unleash_instanceid) .find_for_project_and_token(project, unleash_instance_id)
end end
def authorize_feature_flags_feature! def authorize_feature_flags_feature!
......
require 'spec_helper' require 'spec_helper'
describe Projects::FeatureFlagsController do describe Projects::FeatureFlagsController do
include Rails.application.routes.url_helpers include Gitlab::Routing
set(:user) { create(:user) } set(:user) { create(:user) }
set(:project) { create(:project) } set(:project) { create(:project) }
......
...@@ -9,8 +9,8 @@ describe FeatureFlagsHelper do ...@@ -9,8 +9,8 @@ describe FeatureFlagsHelper do
it { is_expected.to end_with("/api/v4/feature_flags/unleash/#{project.id}") } it { is_expected.to end_with("/api/v4/feature_flags/unleash/#{project.id}") }
end end
context '#unleash_api_instanceid' do context '#unleash_api_instance_id' do
subject { helper.unleash_api_instanceid(project) } subject { helper.unleash_api_instance_id(project) }
it { is_expected.not_to be_empty } it { is_expected.not_to be_empty }
end end
......
...@@ -12,9 +12,9 @@ describe API::Unleash do ...@@ -12,9 +12,9 @@ describe API::Unleash do
end end
shared_examples 'authenticated request' do shared_examples 'authenticated request' do
context 'when using instanceid' do context 'when using instance id' do
let(:client) { create(:operations_feature_flags_client, project: project) } let(:client) { create(:operations_feature_flags_client, project: project) }
let(:params) { { instanceid: client.token } } let(:params) { { instance_id: client.token } }
it 'responds with OK' do it 'responds with OK' do
subject subject
...@@ -44,8 +44,8 @@ describe API::Unleash do ...@@ -44,8 +44,8 @@ describe API::Unleash do
end end
end end
context 'when using bogus instanceid' do context 'when using bogus instance id' do
let(:params) { { instanceid: 'token' } } let(:params) { { instance_id: 'token' } }
it 'responds with unauthorized' do it 'responds with unauthorized' do
subject subject
...@@ -56,7 +56,7 @@ describe API::Unleash do ...@@ -56,7 +56,7 @@ describe API::Unleash do
context 'when using not existing project' do context 'when using not existing project' do
let(:project_id) { -5000 } let(:project_id) { -5000 }
let(:params) { { instanceid: 'token' } } let(:params) { { instance_id: 'token' } }
it 'responds with unauthorized' do it 'responds with unauthorized' do
subject subject
......
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