chore: Fix contact model silently discarding invalid attributes (#4994)
fixes: #4775
This commit is contained in:
@@ -115,5 +115,23 @@ describe ::ContactIdentifyAction do
|
||||
expect { contact.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when discard_invalid_attrs is set to false' do
|
||||
it 'will not update the name of the existing contact' do
|
||||
params = { email: 'blah blah blah', name: 'new name' }
|
||||
expect do
|
||||
described_class.new(contact: contact, params: params, retain_original_contact_name: true).perform
|
||||
end.to raise_error(ActiveRecord::RecordInvalid)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when discard_invalid_attrs is set to true' do
|
||||
it 'will not update the name of the existing contact' do
|
||||
params = { phone_number: 'blahblah blah', name: 'new name' }
|
||||
described_class.new(contact: contact, params: params, discard_invalid_attrs: true).perform
|
||||
expect(contact.reload.name).to eq 'new name'
|
||||
expect(contact.phone_number).to eq nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -33,7 +33,7 @@ RSpec.describe '/api/v1/widget/contacts', type: :request do
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expected_params = { contact: contact, params: params }
|
||||
expected_params = { contact: contact, params: params, discard_invalid_attrs: true }
|
||||
expect(ContactIdentifyAction).to have_received(:new).with(expected_params)
|
||||
expect(identify_action).to have_received(:perform)
|
||||
end
|
||||
|
||||
@@ -44,41 +44,29 @@ RSpec.describe Contact do
|
||||
end
|
||||
end
|
||||
|
||||
# rubocop:disable Rails/SkipsModelValidations
|
||||
context 'when phone number format' do
|
||||
it 'will not throw error for existing invalid phone number' do
|
||||
it 'will throw error for existing invalid phone number' do
|
||||
contact = create(:contact)
|
||||
contact.update_column(:phone_number, '344234')
|
||||
contact.reload
|
||||
expect(contact.update!(name: 'test')).to eq true
|
||||
expect(contact.phone_number).to eq '344234'
|
||||
expect { contact.update!(phone_number: '123456789') }.to raise_error(ActiveRecord::RecordInvalid)
|
||||
end
|
||||
|
||||
it 'updates phone number when adding valid phone number' do
|
||||
contact = create(:contact)
|
||||
contact.update_column(:phone_number, '344234')
|
||||
contact.reload
|
||||
expect(contact.update!(phone_number: '+12312312321')).to eq true
|
||||
expect(contact.phone_number).to eq '+12312312321'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when email format' do
|
||||
it 'will not throw error for existing invalid email' do
|
||||
it 'will throw error for existing invalid email' do
|
||||
contact = create(:contact)
|
||||
contact.update_column(:email, 'ssfdasdf <test@test')
|
||||
contact.reload
|
||||
expect(contact.update!(name: 'test')).to eq true
|
||||
expect(contact.email).to eq 'ssfdasdf <test@test'
|
||||
expect { contact.update!(email: '<2324234234') }.to raise_error(ActiveRecord::RecordInvalid)
|
||||
end
|
||||
|
||||
it 'updates email when adding valid email' do
|
||||
contact = create(:contact)
|
||||
contact.update_column(:email, 'ssfdasdf <test@test')
|
||||
contact.reload
|
||||
expect(contact.update!(email: 'test@test.com')).to eq true
|
||||
expect(contact.email).to eq 'test@test.com'
|
||||
end
|
||||
end
|
||||
# rubocop:enable Rails/SkipsModelValidations
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user