From cbe9bf8d22b27dacd91b353df071270bcb2f8d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibaut=20G=C3=A9ry?= Date: Tue, 31 May 2016 17:12:42 +0200 Subject: [PATCH] notify the admin from the controller --- .../controllers/api/v1/users_controller.rb | 13 +++-- .../api/v1/users_controller_spec.rb | 58 +++++++++++++++++++ .../spec/mailers/api_user_mailer_spec.rb | 13 +++-- 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/src/api-umbrella/web-app/app/controllers/api/v1/users_controller.rb b/src/api-umbrella/web-app/app/controllers/api/v1/users_controller.rb index e96351506..ed76447f0 100644 --- a/src/api-umbrella/web-app/app/controllers/api/v1/users_controller.rb +++ b/src/api-umbrella/web-app/app/controllers/api/v1/users_controller.rb @@ -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) } diff --git a/src/api-umbrella/web-app/spec/controllers/api/v1/users_controller_spec.rb b/src/api-umbrella/web-app/spec/controllers/api/v1/users_controller_spec.rb index 86e102a63..1fa7fe2f3 100644 --- a/src/api-umbrella/web-app/spec/controllers/api/v1/users_controller_spec.rb +++ b/src/api-umbrella/web-app/spec/controllers/api/v1/users_controller_spec.rb @@ -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] = "aa@bb.com" + 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 diff --git a/src/api-umbrella/web-app/spec/mailers/api_user_mailer_spec.rb b/src/api-umbrella/web-app/spec/mailers/api_user_mailer_spec.rb index 436b19fc8..fdf94863b 100644 --- a/src/api-umbrella/web-app/spec/mailers/api_user_mailer_spec.rb +++ b/src/api-umbrella/web-app/spec/mailers/api_user_mailer_spec.rb @@ -93,11 +93,14 @@ ApiUmbrellaConfig[:web][:contact_form_email] = "aaa@bbb.com" end - let(:api_user) { FactoryGirl.create(:api_user, - :first_name => "aaa", - :last_name => "bbb", - :use_description => "I WANNA DO EVERYTHING", - :email => "foo@example.com") } + let(:api_user) do + FactoryGirl.create( + :api_user, + :first_name => "aaa", + :last_name => "bbb", + :use_description => "I WANNA DO EVERYTHING", + :email => "foo@example.com") + end subject { ApiUserMailer.notify_api_admin(api_user).deliver }