From fa73b5290c39e6fefc1fb8b2821b73fdb23e8542 Mon Sep 17 00:00:00 2001 From: smartdev58 <30475578+smartdev58@users.noreply.github.com> Date: Thu, 20 Oct 2022 06:16:36 +0200 Subject: [PATCH] chore: Allow feature flag parameters in platform API for account creation (#5589) Co-authored-by: Pranav Raj S --- .../platform/api/v1/accounts_controller.rb | 16 +++--- .../api/v1/accounts/create.json.jbuilder | 1 + .../api/v1/accounts/show.json.jbuilder | 1 + .../api/v1/accounts/update.json.jbuilder | 1 + .../api/v1/models/_account.json.jbuilder | 9 ++++ .../api/v1/accounts_controller_spec.rb | 50 +++++++++++++++++-- 6 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 app/views/platform/api/v1/accounts/create.json.jbuilder create mode 100644 app/views/platform/api/v1/accounts/show.json.jbuilder create mode 100644 app/views/platform/api/v1/accounts/update.json.jbuilder create mode 100644 app/views/platform/api/v1/models/_account.json.jbuilder diff --git a/app/controllers/platform/api/v1/accounts_controller.rb b/app/controllers/platform/api/v1/accounts_controller.rb index 9c8f60f38..78143410f 100644 --- a/app/controllers/platform/api/v1/accounts_controller.rb +++ b/app/controllers/platform/api/v1/accounts_controller.rb @@ -3,16 +3,12 @@ class Platform::Api::V1::AccountsController < PlatformController @resource = Account.new(account_params) @resource.save! @platform_app.platform_app_permissibles.find_or_create_by(permissible: @resource) - render json: @resource end - def show - render json: @resource - end + def show; end def update @resource.update!(account_params) - render json: @resource end def destroy @@ -27,6 +23,14 @@ class Platform::Api::V1::AccountsController < PlatformController end def account_params - params.permit(:name, :locale) + if permitted_params[:enabled_features] + return permitted_params.except(:enabled_features).merge(selected_feature_flags: permitted_params[:enabled_features].map(&:to_sym)) + end + + permitted_params + end + + def permitted_params + params.permit(:name, :locale, enabled_features: [], limits: {}) end end diff --git a/app/views/platform/api/v1/accounts/create.json.jbuilder b/app/views/platform/api/v1/accounts/create.json.jbuilder new file mode 100644 index 000000000..449293eb6 --- /dev/null +++ b/app/views/platform/api/v1/accounts/create.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'platform/api/v1/models/account', formats: [:json], resource: @resource diff --git a/app/views/platform/api/v1/accounts/show.json.jbuilder b/app/views/platform/api/v1/accounts/show.json.jbuilder new file mode 100644 index 000000000..449293eb6 --- /dev/null +++ b/app/views/platform/api/v1/accounts/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'platform/api/v1/models/account', formats: [:json], resource: @resource diff --git a/app/views/platform/api/v1/accounts/update.json.jbuilder b/app/views/platform/api/v1/accounts/update.json.jbuilder new file mode 100644 index 000000000..449293eb6 --- /dev/null +++ b/app/views/platform/api/v1/accounts/update.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'platform/api/v1/models/account', formats: [:json], resource: @resource diff --git a/app/views/platform/api/v1/models/_account.json.jbuilder b/app/views/platform/api/v1/models/_account.json.jbuilder new file mode 100644 index 000000000..9a22e6518 --- /dev/null +++ b/app/views/platform/api/v1/models/_account.json.jbuilder @@ -0,0 +1,9 @@ +json.id resource.id +json.name resource.name +json.locale resource.locale +json.domain resource.domain +json.support_email resource.support_email +json.enabled_features resource.enabled_features +json.custom_attributes resource.custom_attributes +json.limits resource.limits +json.status resource.status diff --git a/spec/controllers/platform/api/v1/accounts_controller_spec.rb b/spec/controllers/platform/api/v1/accounts_controller_spec.rb index addb3c885..b854e44d4 100644 --- a/spec/controllers/platform/api/v1/accounts_controller_spec.rb +++ b/spec/controllers/platform/api/v1/accounts_controller_spec.rb @@ -32,12 +32,49 @@ RSpec.describe 'Platform Accounts API', type: :request do end it 'creates an account with locale' do + InstallationConfig.where(name: 'ACCOUNT_LEVEL_FEATURE_DEFAULTS').first_or_create!(value: [{ 'name' => 'agent_management', + 'enabled' => true }]) post '/platform/api/v1/accounts', params: { name: 'Test Account', locale: 'es' }, headers: { api_access_token: platform_app.access_token.token }, as: :json + expect(response).to have_http_status(:success) + + json_response = JSON.parse(response.body) + expect(json_response['name']).to eq('Test Account') + expect(json_response['locale']).to eq('es') + expect(json_response['enabled_features']).to eq( + { + 'agent_management' => true + } + ) + end + + it 'creates an account with feature flags' do + InstallationConfig.where(name: 'ACCOUNT_LEVEL_FEATURE_DEFAULTS').first_or_create!(value: [{ 'name' => 'inbox_management', + 'enabled' => true }]) + + post '/platform/api/v1/accounts', params: { name: 'Test Account', enabled_features: %w[feature_ip_lookup feature_help_center] }, + headers: { api_access_token: platform_app.access_token.token }, as: :json + + json_response = JSON.parse(response.body) + expect(json_response['name']).to include('Test Account') + expect(json_response['enabled_features']).to eq( + { + 'inbox_management' => true, + 'ip_lookup' => true, + 'help_center' => true + } + ) + end + + it 'creates an account with limits settings' do + post '/platform/api/v1/accounts', params: { name: 'Test Account', limits: { agents: 5, inboxes: 10 } }, + headers: { api_access_token: platform_app.access_token.token }, as: :json + expect(response).to have_http_status(:success) expect(response.body).to include('Test Account') - expect(response.body).to include('es') + expect(response.body).to include('5') + expect(response.body).to include('10') end end end @@ -105,11 +142,18 @@ RSpec.describe 'Platform Accounts API', type: :request do it 'updates an account when its permissible object' do create(:platform_app_permissible, platform_app: platform_app, permissible: account) - patch "/platform/api/v1/accounts/#{account.id}", params: { name: 'Test Account' }, - headers: { api_access_token: platform_app.access_token.token }, as: :json + patch "/platform/api/v1/accounts/#{account.id}", params: { + name: 'Test Account', + enabled_features: %w[feature_ip_lookup feature_help_center], + limits: { agents: 5, inboxes: 10 } + }, headers: { api_access_token: platform_app.access_token.token }, as: :json expect(response).to have_http_status(:success) expect(account.reload.name).to eq('Test Account') + expect(account.reload.enabled_features['ip_lookup']).to be(true) + expect(account.reload.enabled_features['help_center']).to be(true) + expect(account.reload.limits['agents']).to eq(5) + expect(account.reload.limits['inboxes']).to eq(10) end end end