Skip to content

Commit

Permalink
fix: Add blank check for file param in Import API (chatwoot#3057)
Browse files Browse the repository at this point in the history
  • Loading branch information
murtaza-bagwala authored Sep 21, 2021
1 parent b59e73b commit c504067
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/controllers/api/v1/accounts/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ def search
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

Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/api/v1/accounts/contacts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@
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
Expand Down

0 comments on commit c504067

Please sign in to comment.