Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KG - Authorized User Email Fixes #1807

Merged
merged 3 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions app/mailers/user_mailer.rb
Expand Up @@ -21,16 +21,14 @@
class UserMailer < ActionMailer::Base
default :from => Setting.get_value("no_reply_from")

def authorized_user_changed(protocol, recipients, modified_roles, action)
def authorized_user_changed(protocol, recipient, modified_roles, action)
@protocol = protocol
@modified_roles = modified_roles
@action = action
@protocol_link = dashboard_protocol_url(@protocol)
@service_request = @protocol.service_requests.first

recipients.each do |recipient|
send_email(recipient, t('mailer.email_title.general', email_status: "Authorized Users Update", type: "Protocol", id: @protocol.id))
end
send_email(recipient, t('mailer.email_title.general', email_status: "Authorized Users Update", type: "Protocol", id: @protocol.id))
end

def notification_received(user, ssr)
Expand Down
43 changes: 43 additions & 0 deletions app/models/modified_role.rb
@@ -0,0 +1,43 @@
# Copyright © 2011-2019 MUSC Foundation for Research Development
# All rights reserved.

# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with the distribution.

# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# This is a wrapper structure to hold Project Role information
# to avoid persistence issues within delayed jobs

class ModifiedRole

attr_accessor *ProjectRole.column_names

def initialize(project_role_attrs)
project_role_attrs.each{ |attr, value| instance_variable_set("@#{attr}", value) }
end

def identity
Identity.find(@identity_id)
end

def display_rights
case @project_rights
when "none" then "Member Only"
when "view" then "View Rights"
when "approve" then "Authorize/Change Study Charges"
end
end
end
5 changes: 3 additions & 2 deletions app/models/protocol.rb
Expand Up @@ -402,9 +402,10 @@ def email_about_change_in_authorized_user(modified_roles, action)
# For example: if a SR has SSRs all with a status of 'draft', don't send emails

if Setting.get_value("send_authorized_user_emails") && self.service_requests.any?(&:previously_submitted?)
alert_users = Identity.where(id: (self.emailed_associated_users + modified_roles.reject{ |pr| pr.project_rights == 'none' }).map(&:identity_id))
alert_users = Identity.where(id: (self.emailed_associated_users + modified_roles.reject{ |pr| pr.project_rights == 'none' }).map(&:identity_id))
modified_roles = modified_roles.map{ |pr| ModifiedRole.new(pr.attributes) }

UserMailer.authorized_user_changed(self, alert_users, modified_roles, action).deliver
alert_users.each{ |u| UserMailer.delay.authorized_user_changed(self, u, modified_roles, action) }
end
end

Expand Down
Expand Up @@ -21,6 +21,14 @@
require 'rails_helper'

RSpec.describe Dashboard::AssociatedUsersController do
before :each do
Delayed::Worker.delay_jobs = false
end

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

describe 'DELETE destroy' do
context "when not authorized" do
before :each do
Expand Down Expand Up @@ -121,7 +129,7 @@

it 'should email authorized user' do
delete :destroy, params: { id: @protocol_role.id }, xhr: true
expect(UserMailer).to have_received(:authorized_user_changed)
expect(UserMailer).to have_received(:authorized_user_changed).twice
end

it 'should render appropriate template' do
Expand Down
10 changes: 9 additions & 1 deletion spec/lib/associated_user_creator_spec.rb
Expand Up @@ -21,6 +21,14 @@
require "rails_helper"

RSpec.describe AssociatedUserCreator do
before :each do
Delayed::Worker.delay_jobs = false
end

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

context "params[:project_role] describes a valid ProjectRole" do
before(:each) do
@identity = create(:identity)
Expand Down Expand Up @@ -60,7 +68,7 @@
end

AssociatedUserCreator.new(@project_role_attrs, @identity)
expect(UserMailer).to have_received(:authorized_user_changed)
expect(UserMailer).to have_received(:authorized_user_changed).twice
end
end

Expand Down
12 changes: 6 additions & 6 deletions spec/mailers/user_mailer_spec.rb
Expand Up @@ -37,7 +37,7 @@
@ssr = create(:sub_service_request_without_validations, service_request: @sr, protocol: @protocol, organization: program)
@li = create(:line_item, sub_service_request: @ssr, service_request: @sr, service: service)
@protocol_role = create(:project_role, protocol: @protocol, identity: modified_identity, project_rights: 'approve', role: 'consultant')
@mail = UserMailer.authorized_user_changed(@protocol, [identity], [@protocol_role], 'add')
@mail = UserMailer.authorized_user_changed(@protocol, identity, [@protocol_role], 'add')
end

it 'should display correct subject' do
Expand Down Expand Up @@ -72,7 +72,7 @@
@ssr = create(:sub_service_request_without_validations, service_request: @sr, protocol: @protocol, organization: program)
@li = create(:line_item, sub_service_request: @ssr, service_request: @sr, service: service)
@protocol_role = create(:project_role, protocol: @protocol, identity: modified_identity, project_rights: 'approve', role: 'consultant')
@mail = UserMailer.authorized_user_changed(@protocol, [identity], [@protocol_role], 'add')
@mail = UserMailer.authorized_user_changed(@protocol, identity, [@protocol_role], 'add')
end

it 'should show epic column' do
Expand All @@ -87,7 +87,7 @@
@ssr = create(:sub_service_request_without_validations, service_request: @sr, protocol: @protocol, organization: program)
@li = create(:line_item, sub_service_request: @ssr, service_request: @sr, service: service)
@protocol_role = create(:project_role, protocol: @protocol, identity: modified_identity, project_rights: 'approve', role: 'consultant')
@mail = UserMailer.authorized_user_changed(@protocol, [identity], [@protocol_role], 'add')
@mail = UserMailer.authorized_user_changed(@protocol, identity, [@protocol_role], 'add')
end

it 'should not show epic col' do
Expand All @@ -104,7 +104,7 @@
@ssr = create(:sub_service_request_without_validations, service_request: @sr, protocol: @protocol, organization: program)
@li = create(:line_item, sub_service_request: @ssr, service_request: @sr, service: service)
@protocol_role = create(:project_role, protocol: @protocol, identity: modified_identity, project_rights: 'approve', role: 'consultant')
@mail = UserMailer.authorized_user_changed(@protocol, [identity], [@protocol_role], 'destroy')
@mail = UserMailer.authorized_user_changed(@protocol, identity, [@protocol_role], 'destroy')
end

it "should display the 'deleted' message" do
Expand Down Expand Up @@ -135,7 +135,7 @@
@ssr = create(:sub_service_request_without_validations, service_request: @sr, protocol: @protocol, organization: program)
@li = create(:line_item, sub_service_request: @ssr, service_request: @sr, service: service)
@protocol_role = create(:project_role, protocol: @protocol, identity: modified_identity, project_rights: 'approve', role: 'consultant')
@mail = UserMailer.authorized_user_changed(@protocol, [identity], [@protocol_role], 'destroy')
@mail = UserMailer.authorized_user_changed(@protocol, identity, [@protocol_role], 'destroy')
end

it 'should show epic column' do
Expand All @@ -150,7 +150,7 @@
@ssr = create(:sub_service_request_without_validations, service_request: @sr, protocol: @protocol, organization: program)
@li = create(:line_item, sub_service_request: @ssr, service_request: @sr, service: service)
@protocol_role = create(:project_role, protocol: @protocol, identity: modified_identity, project_rights: 'approve', role: 'consultant')
@mail = UserMailer.authorized_user_changed(@protocol, [identity], [@protocol_role], 'destroy')
@mail = UserMailer.authorized_user_changed(@protocol, identity, [@protocol_role], 'destroy')
end

it 'should not show epic col' do
Expand Down
12 changes: 10 additions & 2 deletions spec/models/protocol/email_about_change_in_authorized_user_spec.rb
Expand Up @@ -20,6 +20,14 @@
require 'rails_helper'

RSpec.describe Protocol, type: :model do
before :each do
Delayed::Worker.delay_jobs = false
end

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

describe "#email_about_change_in_authorized_user" do
context "send_authorized_user_emails is true and sr was previously submitted" do
it "should send authorized user email" do
Expand All @@ -30,14 +38,14 @@
create(:project_role_with_identity, protocol: @protocol1)
modified_role = create(:project_role_with_identity, protocol: @protocol1)

allow(UserMailer).to receive(:authorized_user_changed) do
allow(UserMailer).to receive(:authorized_user_changed).twice do
mailer = double("mailer")
expect(mailer).to receive(:deliver)
mailer
end

@protocol1.email_about_change_in_authorized_user([modified_role], 'destroy')
expect(UserMailer).to have_received(:authorized_user_changed)
expect(UserMailer).to have_received(:authorized_user_changed).twice
end
end

Expand Down