Skip to content

Commit

Permalink
notify the admin from the controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibautGery committed May 31, 2016
1 parent b73f5ca commit cbe9bf8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,25 @@ def create

respond_to do |format|
if(@api_user.save)
send_email = (params[:options] && params[:options][:send_welcome_email].to_s == "true")
send_welcome_email = (params[:options] && params[:options][:send_welcome_email].to_s == "true")
send_notify_email = (params[:options] && params[:options][:send_notify_email].to_s == "true")

# For the admin tool, it's easier to have this attribute on the user
# model, rather than options, so check there for whether we should send
# e-mail. Also note that for backwards compatibility, we only check for
# the presence of this attribute, and not it's actual value.
if(!send_email && params[:user] && params[:user][:send_welcome_email])
send_email = true
if(!send_welcome_email && params[:user] && params[:user][:send_welcome_email])
send_welcome_email = true
end

if(send_email)
if(send_welcome_email)
ApiUserMailer.delay(:queue => "mailers").signup_email(@api_user, params[:options] || {})
end

if(send_notify_email)
ApiUserMailer.delay(:queue => "mailers").notify_api_admin(@api_user)
end

format.json { render("show", :status => :created, :location => api_v1_user_url(@api_user)) }
else
format.json { render(:json => errors_response(@api_user), :status => :unprocessable_entity) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,64 @@
end
end
end

describe "notify e-mail" do
before(:each) do
Delayed::Worker.delay_jobs = false
ActionMailer::Base.deliveries.clear
ApiUmbrellaConfig[:web][:contact_form_email] = "[email protected]"
end

after(:each) do
Delayed::Worker.delay_jobs = true
end

it "sends a notify e-mail to be sent when requested" do
admin_token_auth(@admin)
expect do
p = params
p[:options] = { :send_notify_email => true }
post :create, p
end.to change { ActionMailer::Base.deliveries.count }.by(1)
end

it "does not send notify e-mails when explicitly disabled" do
admin_token_auth(@admin)
expect do
p = params
p[:options] = { :send_notify_email => false }
post :create, p
end.to change { ActionMailer::Base.deliveries.count }.by(0)
end

it "does not send a notify e-mail when the option is an unknown value" do
admin_token_auth(@admin)
expect do
p = params
p[:options] = { :send_notify_email => 1 }
post :create, p
end.to change { ActionMailer::Base.deliveries.count }.by(0)
end

it "does not send notify e-mails by default" do
admin_token_auth(@admin)
expect do
post :create, params
end.to change { ActionMailer::Base.deliveries.count }.by(0)
end

it "queues a welcome e-mail to when delayed job is enabled" do
Delayed::Worker.delay_jobs = true
admin_token_auth(@admin)
expect do
expect do
p = params
p[:options] = { :send_notify_email => true }
post :create, p
end.to change { Delayed::Job.count }.by(1)
end.to change { ActionMailer::Base.deliveries.count }.by(0)
end
end
end

describe "PUT update" do
Expand Down
13 changes: 8 additions & 5 deletions src/api-umbrella/web-app/spec/mailers/api_user_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@
ApiUmbrellaConfig[:web][:contact_form_email] = "[email protected]"
end

let(:api_user) { FactoryGirl.create(:api_user,
:first_name => "aaa",
:last_name => "bbb",
:use_description => "I WANNA DO EVERYTHING",
:email => "[email protected]") }
let(:api_user) do
FactoryGirl.create(
:api_user,
:first_name => "aaa",
:last_name => "bbb",
:use_description => "I WANNA DO EVERYTHING",
:email => "[email protected]")
end

subject { ApiUserMailer.notify_api_admin(api_user).deliver }

Expand Down

0 comments on commit cbe9bf8

Please sign in to comment.