2020-06-11 02:08:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-26 05:08:59 -04:00
|
|
|
RSpec.describe AlertManagement::Alerts::UpdateService do
|
2020-06-11 02:08:22 -04:00
|
|
|
let_it_be(:user_with_permissions) { create(:user) }
|
2020-06-23 11:08:41 -04:00
|
|
|
let_it_be(:other_user_with_permissions) { create(:user) }
|
2020-06-11 02:08:22 -04:00
|
|
|
let_it_be(:user_without_permissions) { create(:user) }
|
2020-07-08 23:09:01 -04:00
|
|
|
let_it_be(:alert, reload: true) { create(:alert_management_alert, :triggered) }
|
2020-06-11 02:08:22 -04:00
|
|
|
let_it_be(:project) { alert.project }
|
|
|
|
|
|
|
|
let(:current_user) { user_with_permissions }
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
let(:service) { described_class.new(alert, current_user, params) }
|
|
|
|
|
|
|
|
before_all do
|
|
|
|
project.add_developer(user_with_permissions)
|
2020-06-23 11:08:41 -04:00
|
|
|
project.add_developer(other_user_with_permissions)
|
2020-06-11 02:08:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
2020-06-23 11:08:41 -04:00
|
|
|
shared_examples 'does not add a todo' do
|
|
|
|
specify { expect { response }.not_to change(Todo, :count) }
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'does not add a system note' do
|
|
|
|
specify { expect { response }.not_to change(Note, :count) }
|
|
|
|
end
|
|
|
|
|
2020-07-08 23:09:01 -04:00
|
|
|
shared_examples 'adds a system note' do
|
|
|
|
specify { expect { response }.to change { alert.reload.notes.count }.by(1) }
|
|
|
|
end
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
shared_examples 'error response' do |message|
|
|
|
|
it_behaves_like 'does not add a todo'
|
|
|
|
it_behaves_like 'does not add a system note'
|
|
|
|
|
|
|
|
it 'has an informative message' do
|
|
|
|
expect(response).to be_error
|
|
|
|
expect(response.message).to eq(message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-11 02:08:22 -04:00
|
|
|
subject(:response) { service.execute }
|
|
|
|
|
2020-06-16 17:08:20 -04:00
|
|
|
context 'when the current_user is nil' do
|
|
|
|
let(:current_user) { nil }
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
it_behaves_like 'error response', 'You have no permissions'
|
2020-06-16 17:08:20 -04:00
|
|
|
end
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
context 'when current_user does not have permission to update alerts' do
|
2020-06-11 02:08:22 -04:00
|
|
|
let(:current_user) { user_without_permissions }
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
it_behaves_like 'error response', 'You have no permissions'
|
2020-06-11 02:08:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no parameters are included' do
|
2020-06-23 11:08:41 -04:00
|
|
|
it_behaves_like 'error response', 'Please provide attributes to update'
|
2020-06-11 02:08:22 -04:00
|
|
|
end
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
context 'when an error occurs during update' do
|
2020-06-11 02:08:22 -04:00
|
|
|
let(:params) { { title: nil } }
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
it_behaves_like 'error response', "Title can't be blank"
|
2020-06-11 02:08:22 -04:00
|
|
|
end
|
|
|
|
|
2020-06-11 11:08:36 -04:00
|
|
|
context 'when a model attribute is included without assignees' do
|
2020-06-11 02:08:22 -04:00
|
|
|
let(:params) { { title: 'This is an updated alert.' } }
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
it_behaves_like 'does not add a todo'
|
|
|
|
it_behaves_like 'does not add a system note'
|
|
|
|
|
2020-06-11 02:08:22 -04:00
|
|
|
it 'updates the attribute' do
|
|
|
|
original_title = alert.title
|
|
|
|
|
|
|
|
expect { response }.to change { alert.title }.from(original_title).to(params[:title])
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when assignees are included' do
|
2020-06-23 11:08:41 -04:00
|
|
|
shared_examples 'adds a todo' do
|
|
|
|
let(:assignee) { expected_assignees.first }
|
2020-06-11 02:08:22 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
specify do
|
|
|
|
expect { response }.to change { assignee.reload.todos.count }.by(1)
|
|
|
|
expect(assignee.todos.last.author).to eq(current_user)
|
|
|
|
end
|
2020-06-11 02:08:22 -04:00
|
|
|
end
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
shared_examples 'successful assignment' do
|
|
|
|
it_behaves_like 'adds a system note'
|
|
|
|
it_behaves_like 'adds a todo'
|
2020-06-12 08:08:56 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
after do
|
|
|
|
alert.assignees = []
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { response }.to change { alert.reload.assignees }.from([]).to(expected_assignees)
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
2020-06-12 08:08:56 -04:00
|
|
|
end
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
let(:expected_assignees) { params[:assignees] }
|
2020-06-16 17:08:20 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
context 'when the assignee is the current user' do
|
|
|
|
let(:params) { { assignees: [current_user] } }
|
2020-06-16 17:08:20 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
it_behaves_like 'successful assignment'
|
|
|
|
end
|
2020-06-16 17:08:20 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
context 'when the assignee has read permissions' do
|
|
|
|
let(:params) { { assignees: [other_user_with_permissions] } }
|
2020-06-16 17:08:20 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
it_behaves_like 'successful assignment'
|
|
|
|
end
|
2020-06-16 17:08:20 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
context 'when the assignee does not have read permissions' do
|
|
|
|
let(:params) { { assignees: [user_without_permissions] } }
|
2020-06-16 17:08:20 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
it_behaves_like 'error response', 'Assignee has no permissions'
|
|
|
|
end
|
2020-06-16 17:08:20 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
context 'when user is already assigned' do
|
|
|
|
let(:params) { { assignees: [user_with_permissions] } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
alert.assignees << user_with_permissions
|
2020-06-16 17:08:20 -04:00
|
|
|
end
|
2020-06-23 11:08:41 -04:00
|
|
|
|
|
|
|
it_behaves_like 'does not add a system note'
|
|
|
|
# TODO: We should not add another todo in this scenario
|
|
|
|
it_behaves_like 'adds a todo'
|
2020-06-16 17:08:20 -04:00
|
|
|
end
|
|
|
|
|
2020-06-11 02:08:22 -04:00
|
|
|
context 'with multiple users included' do
|
|
|
|
let(:params) { { assignees: [user_with_permissions, user_without_permissions] } }
|
2020-06-23 11:08:41 -04:00
|
|
|
let(:expected_assignees) { [user_with_permissions] }
|
2020-06-11 02:08:22 -04:00
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
it_behaves_like 'successful assignment'
|
2020-06-11 02:08:22 -04:00
|
|
|
end
|
|
|
|
end
|
2020-07-08 23:09:01 -04:00
|
|
|
|
|
|
|
context 'when a status is included' do
|
|
|
|
let(:params) { { status: new_status } }
|
|
|
|
let(:new_status) { AlertManagement::Alert::STATUSES[:acknowledged] }
|
|
|
|
|
|
|
|
it 'successfully changes the status' do
|
|
|
|
expect { response }.to change { alert.acknowledged? }.to(true)
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.payload[:alert]).to eq(alert)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'adds a system note'
|
|
|
|
|
|
|
|
context 'with unknown status' do
|
|
|
|
let(:new_status) { -1 }
|
|
|
|
|
|
|
|
it_behaves_like 'error response', 'Invalid status'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with resolving status' do
|
|
|
|
let(:new_status) { AlertManagement::Alert::STATUSES[:resolved] }
|
|
|
|
|
|
|
|
it 'changes the status' do
|
|
|
|
expect { response }.to change { alert.resolved? }.to(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "resolves the current user's related todos" do
|
|
|
|
todo = create(:todo, :pending, target: alert, user: current_user, project: alert.project)
|
|
|
|
|
|
|
|
expect { response }.to change { todo.reload.state }.from('pending').to('done')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-11 02:08:22 -04:00
|
|
|
end
|
|
|
|
end
|