diff --git a/app/controllers/platform/api/v1/accounts_controller.rb b/app/controllers/platform/api/v1/accounts_controller.rb index e11cf9d4a..4521930a6 100644 --- a/app/controllers/platform/api/v1/accounts_controller.rb +++ b/app/controllers/platform/api/v1/accounts_controller.rb @@ -1,4 +1,11 @@ class Platform::Api::V1::AccountsController < PlatformController + def index + @resources = @platform_app.platform_app_permissibles + .where(permissible_type: 'Account') + .includes(:permissible) + .map(&:permissible) + end + def show; end def create diff --git a/app/views/platform/api/v1/accounts/index.json.jbuilder b/app/views/platform/api/v1/accounts/index.json.jbuilder new file mode 100644 index 000000000..2e8e9f73f --- /dev/null +++ b/app/views/platform/api/v1/accounts/index.json.jbuilder @@ -0,0 +1,3 @@ +json.array! @resources do |account| + json.partial! 'platform/api/v1/models/account', formats: [:json], resource: account +end diff --git a/config/routes.rb b/config/routes.rb index 7fb348084..10749062e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -425,7 +425,7 @@ Rails.application.routes.draw do resources :agent_bots, only: [:index, :create, :show, :update, :destroy] do delete :avatar, on: :member end - resources :accounts, only: [:create, :show, :update, :destroy] do + resources :accounts, only: [:index, :create, :show, :update, :destroy] do resources :account_users, only: [:index, :create] do collection do delete :destroy diff --git a/spec/controllers/platform/api/v1/accounts_controller_spec.rb b/spec/controllers/platform/api/v1/accounts_controller_spec.rb index c7b5b275b..63f53d0d6 100644 --- a/spec/controllers/platform/api/v1/accounts_controller_spec.rb +++ b/spec/controllers/platform/api/v1/accounts_controller_spec.rb @@ -78,6 +78,42 @@ RSpec.describe 'Platform Accounts API', type: :request do end end + describe 'GET /platform/api/v1/accounts' do + context 'when it is an unauthenticated platform app' do + it 'returns unauthorized' do + get '/platform/api/v1/accounts' + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an invalid platform app token' do + it 'returns unauthorized' do + get '/platform/api/v1/accounts', headers: { api_access_token: 'invalid' }, as: :json + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated platform app' do + let(:platform_app) { create(:platform_app) } + let!(:account1) { create(:account, name: 'Account A') } + let!(:account2) { create(:account, name: 'Account B') } + + before do + create(:platform_app_permissible, platform_app: platform_app, permissible: account1) + create(:platform_app_permissible, platform_app: platform_app, permissible: account2) + end + + it 'returns all permissible accounts' do + get '/platform/api/v1/accounts', headers: { api_access_token: platform_app.access_token.token }, as: :json + + expect(response).to have_http_status(:success) + json_response = response.parsed_body + expect(json_response.size).to eq(2) + expect(json_response.map { |acc| acc['name'] }).to include('Account A', 'Account B') + end + end + end + describe 'GET /platform/api/v1/accounts/{account_id}' do context 'when it is an unauthenticated platform app' do it 'returns unauthorized' do