Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Validate email addresses (#14)
Browse files Browse the repository at this point in the history
* see what an invalid email looks like

* 400 for invalid emails
  • Loading branch information
jshawl authored Jan 4, 2024
1 parent 9856b51 commit 19e42ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/controllers/api/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ class ApiController < ApplicationController
def notify
return head 400 if params[:to]&.match(/psnator/)

@notification = Notification.from_params(params)
begin
@notification = Notification.from_params(params)
rescue => e
return render json: {error: e.message}, status: 400
end
render json: @notification
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/controllers/api/api_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
}
expect(response.code).to eq('400')
end
it 'handles invalid email addresses' do
post :notify, params:
{
to: 'not a valid email address',
subject: 'hello',
body: 'goodbye'
}
expect(response.code).to eq('400')
expect(response.body).to match('email(.*)invalid')
end
it 'supports multiple recipients' do
stub_request(:post, 'https://api.sendgrid.com/v3/mail/send')
.with(
Expand Down

0 comments on commit 19e42ce

Please sign in to comment.