From 7a05e817efb2db9ecee49e773574e6c73d678724 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Fri, 8 Mar 2019 17:01:59 +0100 Subject: [PATCH] Add new Protocol steps activities --- app/controllers/steps_controller.rb | 20 ++++- spec/controllers/steps_controller_spec.rb | 95 +++++++++++++++++++++++ 2 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 spec/controllers/steps_controller_spec.rb diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index 18be89850..236bc9e7e 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -81,8 +81,7 @@ class StepsController < ApplicationController ) ) else - # TODO: Activity for team if step - # created in protocol management?? + log_activity(:add_step_to_protocol_repository) end # Update protocol timestamp @@ -201,8 +200,7 @@ class StepsController < ApplicationController ) ) else - # TODO: Activity for team if step - # updated in protocol management?? + log_activity(:edit_step_in_protocol_repository) end # Update protocol timestamp @@ -236,6 +234,8 @@ class StepsController < ApplicationController team = @protocol.team previous_size = @step.space_taken + log_activity(:delete_step_in_protocol_repository) + # Destroy the step @step.destroy(current_user) @@ -651,4 +651,16 @@ class StepsController < ApplicationController ] ) end + + def log_activity(type_of) + Activities::CreateActivityService + .call(activity_type: type_of, + owner: current_user, + subject: @protocol, + team: current_team, + message_items: { + protocol: @protocol.id, + step: @step.id + }) + end end diff --git a/spec/controllers/steps_controller_spec.rb b/spec/controllers/steps_controller_spec.rb new file mode 100644 index 000000000..2e54157fd --- /dev/null +++ b/spec/controllers/steps_controller_spec.rb @@ -0,0 +1,95 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe StepsController, type: :controller do + login_user + + let(:user) { subject.current_user } + let(:team) { create :team, created_by: user } + let!(:user_team) { create :user_team, :admin, user: user, team: team } + let(:protocol) do + create :protocol, :in_public_repository, team: team, added_by: user + end + let(:step) { create :step, protocol: protocol } + + describe 'POST create' do + context 'when in protocol repository' do + let(:params) do + { + protocol_id: protocol.id, + step: { + name: 'test', + description: 'description' + } + } + end + let(:action) { post :create, params: params, format: :json } + + it 'calls create activity for creating step in protocol repository' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: + :add_step_to_protocol_repository))) + + action + end + + it 'adds activity in DB' do + expect { action } + .to(change { Activity.count }) + end + end + end + + describe 'PUT update' do + context 'when in protocol repository' do + let(:params) do + { + id: step.id, + protocol_id: protocol.id, + step: { + name: 'updated name', + description: 'updated description' + } + } + end + let(:action) { put :update, params: params, format: :json } + + it 'calls create activity for editing step in protocol repository' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: + :edit_step_in_protocol_repository))) + + action + end + + it 'adds activity in DB' do + expect { action } + .to(change { Activity.count }) + end + end + end + + describe 'DELETE destroy' do + context 'when in protocol repository' do + let(:params) { { id: step.id } } + let(:action) { delete :destroy, params: params, format: :json } + + it 'calls create activity for deleting step in protocol repository' do + expect(Activities::CreateActivityService) + .to(receive(:call) + .with(hash_including(activity_type: + :delete_step_in_protocol_repository))) + + action + end + + it 'adds activity in DB' do + expect { action } + .to(change { Activity.count }) + end + end + end +end