From 54a809ea542f6ce07089b723b879f4a5f2331b7b Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Tue, 28 Mar 2023 15:23:41 +0530 Subject: [PATCH] fix: Case insensitive email match (#6760) Fixes: https://linear.app/chatwoot/issue/CW-1354/email-id-case-sensitive Co-authored-by: Sojan --- app/models/user.rb | 4 ++++ spec/controllers/platform/api/v1/users_controller_spec.rb | 5 +++++ spec/models/user_spec.rb | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 278526557..c50ab208e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -106,6 +106,10 @@ class User < ApplicationRecord scope :order_by_full_name, -> { order('lower(name) ASC') } + before_validation do + self.email = email.try(:downcase) + end + def send_devise_notification(notification, *args) devise_mailer.with(account: Current.account).send(notification, self, *args).deliver_later end diff --git a/spec/controllers/platform/api/v1/users_controller_spec.rb b/spec/controllers/platform/api/v1/users_controller_spec.rb index ca3cd3bf9..edd91f36b 100644 --- a/spec/controllers/platform/api/v1/users_controller_spec.rb +++ b/spec/controllers/platform/api/v1/users_controller_spec.rb @@ -115,6 +115,11 @@ RSpec.describe 'Platform Users API', type: :request do ) ) expect(platform_app.platform_app_permissibles.first.permissible_id).to eq data['id'] + + post '/platform/api/v1/users/', params: { name: 'test', email: 'TesT@test.com', password: 'Password1!' }, + headers: { api_access_token: platform_app.access_token.token }, as: :json + data = JSON.parse(response.body) + expect(data['message']).to eq('Email has already been taken') end it 'fetch existing user and creates permissible for the user' do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 4882dc08f..7bae381ac 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -89,4 +89,11 @@ RSpec.describe User do expect(user.will_save_change_to_email?).to be true end end + + context 'when the supplied email is uppercase' do + it 'downcases the email on save' do + new_user = create(:user, email: 'Test123@test.com') + expect(new_user.email).to eq('test123@test.com') + end + end end