fix: Add blank check for file param in Import API (#3057)

This commit is contained in:
Murtaza Bagwala 2021-09-21 10:20:12 +05:30 committed by GitHub
parent b59e73b10b
commit c504067e2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View file

@ -30,10 +30,13 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
end end
def import 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 ActiveRecord::Base.transaction do
import = Current.account.data_imports.create!(data_type: 'contacts') import = Current.account.data_imports.create!(data_type: 'contacts')
import.import_file.attach(params[:import_file]) import.import_file.attach(params[:import_file])
end end
head :ok head :ok
end end

View file

@ -41,6 +41,9 @@ en:
invalid_email: You have entered an invalid email invalid_email: You have entered an invalid email
email_already_exists: "You have already signed up for an account with %{email}" email_already_exists: "You have already signed up for an account with %{email}"
failed: Signup failed failed: Signup failed
contacts:
import:
failed: File is blank
reports: reports:
period: Reporting period %{since} to %{until} period: Reporting period %{since} to %{until}

View file

@ -105,6 +105,20 @@ RSpec.describe 'Contacts API', type: :request do
expect(account.data_imports.first.import_file.attached?).to eq(true) expect(account.data_imports.first.import_file.attached?).to eq(true)
end end
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 end
describe 'GET /api/v1/accounts/{account.id}/contacts/active' do describe 'GET /api/v1/accounts/{account.id}/contacts/active' do