Browse Source

Show validation error if user email already exists instead of crashing

pull/373/head
baarkerlounger 3 years ago
parent
commit
8f451444b1
  1. 13
      app/controllers/users_controller.rb
  2. 4
      config/locales/en.yml
  3. 38
      spec/requests/users_controller_spec.rb

13
app/controllers/users_controller.rb

@ -27,16 +27,21 @@ class UsersController < ApplicationController
def create def create
@resource = User.new @resource = User.new
if user_params["email"].empty? if user_params["email"].empty?
@resource.errors.add :email, "Enter an email address" @resource.errors.add :email, I18n.t("validations.email.blank")
elsif !email_valid?(user_params["email"]) elsif !email_valid?(user_params["email"])
@resource.errors.add :email, "Enter an email address in the correct format, like name@example.com" @resource.errors.add :email, I18n.t("validations.email.invalid")
end end
if @resource.errors.present? if @resource.errors.present?
render :new, status: :unprocessable_entity render :new, status: :unprocessable_entity
else else
@user = User.create!(user_params.merge(org_params).merge(password_params)) user = User.create(user_params.merge(org_params).merge(password_params))
@user.send_reset_password_instructions if user.persisted?
user.send_reset_password_instructions
redirect_to users_organisation_path(current_user.organisation) redirect_to users_organisation_path(current_user.organisation)
else
@resource.errors.add :email, I18n.t("validations.email.taken")
render :new, status: :unprocessable_entity
end
end end
end end

4
config/locales/en.yml

@ -43,6 +43,10 @@ en:
invalid_date: "Please enter a valid date" invalid_date: "Please enter a valid date"
outside_collection_window: "Date must be within the current collection windows" outside_collection_window: "Date must be within the current collection windows"
postcode: "Enter a postcode in the correct format, for example AA1 1AA" postcode: "Enter a postcode in the correct format, for example AA1 1AA"
email:
taken: "Email already exists"
invalid: "Enter an email address in the correct format, like name@example.com"
blank: "Enter an email address"
property: property:
mrcdate: mrcdate:

38
spec/requests/users_controller_spec.rb

@ -254,6 +254,44 @@ RSpec.describe UsersController, type: :request do
end end
end end
describe "#create" do
let(:params) do
{
"user": {
name: "new user",
email: "new_user@example.com",
role: "data_coordinator",
},
}
end
let(:request) { post "/users/", headers: headers, params: params }
before do
sign_in user
end
it "invites a new user" do
expect { request }.to change(User, :count).by(1)
end
it "redirects back to organisation users page" do
request
expect(response).to redirect_to("/organisations/#{user.organisation.id}/users")
end
context "when the email is already taken" do
before do
FactoryBot.create(:user, email: "new_user@example.com")
end
it "shows an error" do
request
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("validations.email.taken"))
end
end
end
describe "title link" do describe "title link" do
before do before do
sign_in user sign_in user

Loading…
Cancel
Save