Skip to content

Commit

Permalink
Raise error
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Orner committed Apr 23, 2023
1 parent 04539fc commit 424def4
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 6 deletions.
3 changes: 3 additions & 0 deletions app/controllers/admin/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def create
flash[:error] = "Failed to create Organization."
render :new
end
rescue => e
flash[:error] = e
render :new
end

def show
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def create
resource: Organization.find(user_params[:organization_id]))
flash[:notice] = "Created a new user!"
redirect_to admin_users_path
rescue
flash[:error] = "Failed to create user"
rescue => e
flash[:error] = "Failed to create user: #{e}"
render "admin/users/new"
end

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def invite_user
roles: [Role::ORG_USER],
resource: Organization.find(params[:org]))
redirect_to organization_path, notice: "User invited to organization!"
rescue => e
redirect_to organization_path, alert: e.message
end

def resend_user_invitation
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/partners/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def create
flash[:error] = user.errors.full_messages.join("")
redirect_to new_partners_user_path
end
rescue => e
flash[:error] = e.message
redirect_to new_partners_user_path
end

private
Expand Down
2 changes: 2 additions & 0 deletions app/services/partner_invite_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def call
roles: [Role::PARTNER],
resource: partner,
force: @force)
rescue => e
errors.add(:base, e.message)
end

attr_reader :partner
Expand Down
4 changes: 3 additions & 1 deletion app/services/user_invite_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def self.invite(email:, resource:, name: nil, roles: [], force: false)
user = User.find_by(email: email)

# return if user already has all the roles we're trying to add
return if !force && user && roles.all? { |role| user.has_role?(role, resource) }
if !force && user && roles.all? { |role| user.has_role?(role, resource) }
raise "User already has the requested role!"
end

if user
add_roles(user, resource: resource, roles: roles)
Expand Down
8 changes: 5 additions & 3 deletions spec/services/user_invite_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
User.create!(name: "Some Name", email: "email@email.com", password: "blahblah!")
end

it "should not reinvite the existing user" do
expect { described_class.invite(email: "email@email.com", resource: @organization) }
.not_to change { ActionMailer::Base.deliveries.count }
it "should raise an error when reinviting an existing user with the same role" do
expect {
expect { described_class.invite(email: "email@email.com", resource: @organization) }
.to raise_error("User already has the requested role!")
}.not_to change { ActionMailer::Base.deliveries.count }
expect(UserMailer).not_to have_received(:role_added)
end

Expand Down

0 comments on commit 424def4

Please sign in to comment.