From 18bf1a9e629b28fa5657f81f82da8060190a7c58 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Mon, 13 Jan 2020 14:25:23 +0530 Subject: [PATCH] Ignore case from input email (#424) --- .../api/v1/widget/messages_controller.rb | 12 ++++++++---- .../api/v1/widget/messages_controller_spec.rb | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/widget/messages_controller.rb b/app/controllers/api/v1/widget/messages_controller.rb index 903e4b26e..a6087bb8c 100644 --- a/app/controllers/api/v1/widget/messages_controller.rb +++ b/app/controllers/api/v1/widget/messages_controller.rb @@ -15,8 +15,8 @@ class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController end def update - @message.update!(input_submitted_email: permitted_params[:contact][:email]) - update_contact(permitted_params[:contact][:email]) + @message.update!(input_submitted_email: contact_email) + update_contact(contact_email) head :no_content rescue StandardError => e render json: { error: @contact.errors, message: e.message }.to_json, status: 500 @@ -88,14 +88,18 @@ class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController ::ContactMergeAction.new(account: @account, base_contact: contact_with_email, mergee_contact: @contact).perform else @contact.update!( - email: permitted_params[:contact][:email], + email: email, name: contact_name ) end end + def contact_email + permitted_params[:contact][:email].downcase + end + def contact_name - permitted_params[:contact][:email].split('@')[0] + contact_email.split('@')[0] end def permitted_params diff --git a/spec/controllers/api/v1/widget/messages_controller_spec.rb b/spec/controllers/api/v1/widget/messages_controller_spec.rb index 213335eeb..7246167bb 100644 --- a/spec/controllers/api/v1/widget/messages_controller_spec.rb +++ b/spec/controllers/api/v1/widget/messages_controller_spec.rb @@ -79,6 +79,21 @@ RSpec.describe '/api/v1/widget/messages', type: :request do message.reload expect { contact.reload }.to raise_error(ActiveRecord::RecordNotFound) end + + it 'ignores the casing of email, updates message in conversation and deletes the current contact' do + message = create(:message, account: account, inbox: web_widget.inbox, conversation: conversation) + email = Faker::Internet.email + create(:contact, account: account, email: email) + contact_params = { email: email.upcase } + put api_v1_widget_message_url(message.id), + params: { website_token: web_widget.website_token, contact: contact_params }, + headers: { 'X-Auth-Token' => token }, + as: :json + + expect(response).to have_http_status(:success) + message.reload + expect { contact.reload }.to raise_error(ActiveRecord::RecordNotFound) + end end end end