Skip to content

Commit

Permalink
Merge pull request #1159 from sparc-request/wth-epic-origin-bug
Browse files Browse the repository at this point in the history
wth - (SPARCRequest & SPARCDashboard) Epic User Update New Feature
  • Loading branch information
Stuart-Johnson committed Oct 20, 2017
2 parents 1667f2a + fdaf9cc commit 8183230
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/controllers/associated_users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def edit
end

def create
creator = AssociatedUserCreator.new(project_role_params)
creator = AssociatedUserCreator.new(project_role_params, current_user)

if creator.successful?
flash.now[:success] = t(:authorized_users)[:created]
Expand All @@ -81,7 +81,7 @@ def create
end

def update
updater = AssociatedUserUpdater.new(id: params[:id], project_role: project_role_params)
updater = AssociatedUserUpdater.new(id: params[:id], project_role: project_role_params, current_identity: current_user)
protocol_role = updater.protocol_role
@return_to_dashboard = protocol_role.identity_id == current_user.id && ['none', 'view'].include?(protocol_role.project_rights)

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/dashboard/associated_users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def new
end

def create
creator = AssociatedUserCreator.new(project_role_params)
creator = AssociatedUserCreator.new(project_role_params, @user)

if creator.successful?
if @current_user_created = params[:project_role][:identity_id].to_i == @user.id
Expand All @@ -85,7 +85,7 @@ def create
end

def update
updater = AssociatedUserUpdater.new(id: params[:id], project_role: project_role_params)
updater = AssociatedUserUpdater.new(id: params[:id], project_role: project_role_params, current_identity: @user)

if updater.successful?
# We care about this because the new rights will determine what is rendered
Expand Down
4 changes: 2 additions & 2 deletions app/lib/associated_user_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
class AssociatedUserCreator
attr_reader :protocol_role

def initialize(params)
def initialize(params, current_identity)
protocol = Protocol.find(params[:protocol_id])
@protocol_role = protocol.project_roles.build(params)
eqm = EpicQueueManager.new(protocol, @protocol_role)
eqm = EpicQueueManager.new(protocol, current_identity, @protocol_role)

if @protocol_role.unique_to_protocol? && @protocol_role.fully_valid?
@successful = true
Expand Down
2 changes: 1 addition & 1 deletion app/lib/associated_user_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AssociatedUserUpdater
def initialize(params)
@protocol_role = ProjectRole.find(params[:id])
protocol = @protocol_role.protocol
eqm = EpicQueueManager.new(protocol, @protocol_role)
eqm = EpicQueueManager.new(protocol, params[:current_identity], @protocol_role)

epic_rights = @protocol_role.epic_rights.to_a # use to_a to eval ActiveRecord::Relation
@protocol_role.assign_attributes(params[:project_role])
Expand Down
5 changes: 3 additions & 2 deletions app/lib/epic_queue_manager.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
class EpicQueueManager

def initialize(protocol, protocol_role)
def initialize(protocol, identity, protocol_role)
@protocol = protocol
@protocol_role = protocol_role
@identity = identity
end

def create_epic_queue
if Setting.find_by_key("use_epic").value && withheld_from_epic?(@protocol) && @protocol_role.epic_access
unless withheld_epic_queue?(@protocol)
EpicQueue.create(
protocol_id: @protocol.id,
identity_id: @protocol_role.identity_id,
identity_id: @identity.id,
user_change: true
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@

post :create, params: {
protocol_id: protocol.id,
project_role: @new_project_roles_attrs
project_role: @new_project_roles_attrs
}, xhr: true
end

it "should use AssociatedUserCreator to create ProjectRole" do
expect(AssociatedUserCreator).to have_received(:new).
with controller_params(@new_project_roles_attrs)
with controller_params(@new_project_roles_attrs), identity
end

it "should not set @errors" do
Expand Down Expand Up @@ -96,7 +96,7 @@

it "should use AssociatedUserCreator to (attempt) to create ProjectRole" do
expect(AssociatedUserCreator).to have_received(:new).
with controller_params(@new_project_roles_attrs)
with controller_params(@new_project_roles_attrs), identity
end

it "should set @errors from built ProjectRole's errors" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

it 'should update @protocol_role using params[:project_role] using ProtocolUpdater' do
expect(AssociatedUserUpdater).to have_received(:new).
with id: @project_role.id.to_s, project_role: controller_params({identity_id: '1'})
with id: @project_role.id.to_s, project_role: controller_params({identity_id: '1'}), current_identity: identity
end

it 'should not set @errors' do
Expand Down
20 changes: 10 additions & 10 deletions spec/lib/associated_user_creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
end

it "#successful? should return true" do
creator = AssociatedUserCreator.new(@project_role_attrs)
creator = AssociatedUserCreator.new(@project_role_attrs, @identity)
expect(creator.successful?).to eq(true)
end

it "should create a new ProjectRole for Protocol from params[:project_role][:protocol_id]" do
expect { AssociatedUserCreator.new(@project_role_attrs) }.to change { ProjectRole.count }.by(1)
expect { AssociatedUserCreator.new(@project_role_attrs, @identity) }.to change { ProjectRole.count }.by(1)
end

it "should return new ProjectRole with #protocol_role" do
creator = AssociatedUserCreator.new(@project_role_attrs)
creator = AssociatedUserCreator.new(@project_role_attrs, @identity)
expect(creator.protocol_role).to eq(ProjectRole.last)
end

Expand All @@ -58,7 +58,7 @@
mailer
end

AssociatedUserCreator.new(@project_role_attrs)
AssociatedUserCreator.new(@project_role_attrs, @identity)
expect(UserMailer).to have_received(:authorized_user_changed).twice
end
end
Expand All @@ -69,7 +69,7 @@
it "should not send authorized user changed emails" do
@ssr.update_attribute(:status, 'complete')
allow(UserMailer).to receive(:authorized_user_changed)
AssociatedUserCreator.new(@project_role_attrs)
AssociatedUserCreator.new(@project_role_attrs, @identity)

expect(UserMailer).not_to have_received(:authorized_user_changed)
end
Expand All @@ -86,7 +86,7 @@
mailer
end

AssociatedUserCreator.new(@project_role_attrs)
AssociatedUserCreator.new(@project_role_attrs, @identity)

expect(Notifier).to have_received(:notify_for_epic_user_approval)
end
Expand All @@ -106,19 +106,19 @@
end

it "#successful? should return false" do
creator = AssociatedUserCreator.new(@project_role_attrs)
creator = AssociatedUserCreator.new(@project_role_attrs, @identity)
expect(creator.successful?).to eq(false)
end

it "should not create a new ProjectRole" do
expect { AssociatedUserCreator.new(@project_role_attrs) }.not_to change { ProjectRole.count }
expect { AssociatedUserCreator.new(@project_role_attrs, @identity) }.not_to change { ProjectRole.count }
end

context "send_authorized_user_emails is true" do
it "should not send authorized user changed emails" do
allow(UserMailer).to receive(:authorized_user_changed)

AssociatedUserCreator.new(@project_role_attrs)
AssociatedUserCreator.new(@project_role_attrs, @identity)

expect(UserMailer).not_to have_received(:authorized_user_changed)
end
Expand All @@ -130,7 +130,7 @@
it "should not notify for epic user approval" do
allow(Notifier).to receive(:notify_for_epic_user_approval)

AssociatedUserCreator.new(@project_role_attrs)
AssociatedUserCreator.new(@project_role_attrs, @identity)

expect(Notifier).not_to have_received(:notify_for_epic_user_approval)
end
Expand Down

0 comments on commit 8183230

Please sign in to comment.