From c504067e2bfe64a381d2ff069c8deb2fd534772e Mon Sep 17 00:00:00 2001 From: Murtaza Bagwala Date: Tue, 21 Sep 2021 10:20:12 +0530 Subject: [PATCH] fix: Add blank check for file param in Import API (#3057) --- .../api/v1/accounts/contacts_controller.rb | 3 +++ config/locales/en.yml | 3 +++ .../api/v1/accounts/contacts_controller_spec.rb | 14 ++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/app/controllers/api/v1/accounts/contacts_controller.rb b/app/controllers/api/v1/accounts/contacts_controller.rb index 444f0c918..82b92f090 100644 --- a/app/controllers/api/v1/accounts/contacts_controller.rb +++ b/app/controllers/api/v1/accounts/contacts_controller.rb @@ -30,10 +30,13 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController end def import + render json: { error: I18n.t('errors.contacts.import.failed') }, status: :unprocessable_entity and return if params[:import_file].blank? + ActiveRecord::Base.transaction do import = Current.account.data_imports.create!(data_type: 'contacts') import.import_file.attach(params[:import_file]) end + head :ok end diff --git a/config/locales/en.yml b/config/locales/en.yml index 061d3055e..8e3d5d19f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -41,6 +41,9 @@ en: invalid_email: You have entered an invalid email email_already_exists: "You have already signed up for an account with %{email}" failed: Signup failed + contacts: + import: + failed: File is blank reports: period: Reporting period %{since} to %{until} diff --git a/spec/controllers/api/v1/accounts/contacts_controller_spec.rb b/spec/controllers/api/v1/accounts/contacts_controller_spec.rb index 4b6bbf6db..af94fcd19 100644 --- a/spec/controllers/api/v1/accounts/contacts_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/contacts_controller_spec.rb @@ -105,6 +105,20 @@ RSpec.describe 'Contacts API', type: :request do expect(account.data_imports.first.import_file.attached?).to eq(true) end end + + context 'when file is empty' do + let(:admin) { create(:user, account: account, role: :administrator) } + + it 'returns Unprocessable Entity' do + post "/api/v1/accounts/#{account.id}/contacts/import", + headers: admin.create_new_auth_token + + json_response = JSON.parse(response.body) + + expect(response).to have_http_status(:unprocessable_entity) + expect(json_response['error']).to eq('File is blank') + end + end end describe 'GET /api/v1/accounts/{account.id}/contacts/active' do