2019-03-12 00:17:59 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe MyModulesController, type: :controller do
|
2021-09-14 18:45:44 +08:00
|
|
|
login_user
|
|
|
|
include_context 'reference_project_structure'
|
2021-09-09 21:41:42 +08:00
|
|
|
|
2019-03-27 18:24:44 +08:00
|
|
|
let!(:repository) { create :repository, created_by: user, team: team }
|
|
|
|
let!(:repository_row) do
|
|
|
|
create :repository_row, created_by: user, repository: repository
|
|
|
|
end
|
2021-09-09 21:41:42 +08:00
|
|
|
|
2019-03-12 00:17:59 +08:00
|
|
|
describe 'PUT update' do
|
|
|
|
let(:action) { put :update, params: params, format: :json }
|
2019-03-27 18:24:44 +08:00
|
|
|
|
|
|
|
context 'when changing task description' do
|
|
|
|
let(:params) do
|
|
|
|
{ id: my_module.id, my_module: { description: 'description changed' } }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity for changing task description' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type:
|
|
|
|
:change_module_description)))
|
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
2019-03-12 00:17:59 +08:00
|
|
|
|
|
|
|
context 'when setting due_date' do
|
|
|
|
let(:params) do
|
2020-03-17 01:30:17 +08:00
|
|
|
{ id: my_module.id, my_module: { due_date: '03/21/2019 23:59' } }
|
2019-03-12 00:17:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity for setting due date' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type:
|
|
|
|
:set_task_due_date)))
|
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when deleting due_date' do
|
|
|
|
let(:params) { { id: my_module.id, my_module: { due_date: '' } } }
|
|
|
|
let(:my_module) do
|
2021-09-30 17:32:11 +08:00
|
|
|
create :my_module, :with_due_date, experiment: experiment, created_by: experiment.created_by
|
2019-03-12 00:17:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity for removing due date' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type:
|
|
|
|
:remove_task_due_date)))
|
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when updating due_date' do
|
|
|
|
let(:params) do
|
2020-03-17 01:30:17 +08:00
|
|
|
{ id: my_module.id, my_module: { due_date: '02/21/2019 23:59' } }
|
2019-03-12 00:17:59 +08:00
|
|
|
end
|
|
|
|
let(:my_module) do
|
2021-09-30 17:32:11 +08:00
|
|
|
create :my_module, :with_due_date, experiment: experiment, created_by: experiment.created_by
|
2019-03-12 00:17:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity for changing due date' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call)
|
|
|
|
.with(hash_including(activity_type:
|
|
|
|
:change_task_due_date)))
|
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }
|
|
|
|
.to(change { Activity.count })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-27 18:24:44 +08:00
|
|
|
|
2020-07-15 00:48:18 +08:00
|
|
|
describe 'PUT update_state' do
|
|
|
|
let(:action) { put :update_state, params: params, format: :json }
|
|
|
|
let(:my_module_id) { my_module.id }
|
|
|
|
let(:status_id) { 'some-state-id' }
|
|
|
|
let(:params) do
|
2020-07-23 21:53:46 +08:00
|
|
|
{
|
|
|
|
id: my_module_id,
|
|
|
|
my_module: { status_id: status_id }
|
|
|
|
}
|
2020-07-15 00:48:18 +08:00
|
|
|
end
|
2019-03-27 18:24:44 +08:00
|
|
|
|
2020-08-27 19:26:17 +08:00
|
|
|
before(:all) do
|
|
|
|
MyModuleStatusFlow.ensure_default
|
|
|
|
end
|
2020-07-15 00:48:18 +08:00
|
|
|
|
2020-08-27 19:26:17 +08:00
|
|
|
context 'when states updated' do
|
|
|
|
let(:status_id) { my_module.my_module_status.next_status.id }
|
2019-03-27 18:24:44 +08:00
|
|
|
|
2020-07-15 00:48:18 +08:00
|
|
|
it 'changes status' do
|
2019-03-27 18:24:44 +08:00
|
|
|
action
|
2020-07-15 00:48:18 +08:00
|
|
|
|
2020-08-27 19:26:17 +08:00
|
|
|
expect(my_module.reload.my_module_status.id).to be_eql(status_id)
|
2019-03-27 18:24:44 +08:00
|
|
|
end
|
2020-08-04 22:40:06 +08:00
|
|
|
|
|
|
|
it 'creates activity' do
|
|
|
|
expect { action }.to(change { Activity.all.count }.by(1))
|
|
|
|
end
|
2019-03-27 18:24:44 +08:00
|
|
|
end
|
|
|
|
|
2020-09-01 20:49:58 +08:00
|
|
|
context 'when status not exist' do
|
2020-07-15 00:48:18 +08:00
|
|
|
let(:status_id) { -1 }
|
|
|
|
|
2020-09-01 20:49:58 +08:00
|
|
|
it 'renders 422' do
|
|
|
|
my_module.my_module_status_flow
|
2020-07-15 00:48:18 +08:00
|
|
|
action
|
|
|
|
|
2020-09-01 20:49:58 +08:00
|
|
|
expect(response).to have_http_status 422
|
2020-07-15 00:48:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-01 20:49:58 +08:00
|
|
|
context 'when status not correct' do
|
|
|
|
let(:status_id) { my_module.my_module_status.next_status.next_status.id }
|
2020-07-15 00:48:18 +08:00
|
|
|
|
2020-09-01 20:49:58 +08:00
|
|
|
it 'renders 422' do
|
2019-03-27 18:24:44 +08:00
|
|
|
action
|
2020-07-15 00:48:18 +08:00
|
|
|
|
2020-09-01 20:49:58 +08:00
|
|
|
expect(response).to have_http_status 422
|
2019-03-27 18:24:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-15 00:48:18 +08:00
|
|
|
context 'when user does not have permissions' do
|
|
|
|
it 'renders 403' do
|
2021-09-09 21:41:42 +08:00
|
|
|
UserAssignment.where(user: user, assignable: my_module).destroy_all
|
2020-07-15 00:48:18 +08:00
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status 403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when my_module not found' do
|
|
|
|
let(:my_module_id) { -1 }
|
|
|
|
|
|
|
|
it 'renders 404' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status 404
|
|
|
|
end
|
2019-03-27 18:24:44 +08:00
|
|
|
end
|
|
|
|
end
|
2021-01-04 22:11:24 +08:00
|
|
|
|
|
|
|
describe 'POST restore_tasks' do
|
|
|
|
let(:action) { post :restore_group, params: params }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
id: experiment.id,
|
|
|
|
my_modules_ids: [task1.id, task2.id, task3.id]
|
|
|
|
}
|
|
|
|
end
|
2021-09-30 17:32:11 +08:00
|
|
|
let(:task1) { create :my_module, :archived, experiment: experiment, created_by: experiment.created_by }
|
|
|
|
let(:task2) { create :my_module, :archived, experiment: experiment, created_by: experiment.created_by }
|
|
|
|
let(:task3) { create :my_module, :archived, experiment: experiment, created_by: experiment.created_by }
|
2021-01-04 22:11:24 +08:00
|
|
|
let(:user) { controller.current_user }
|
2021-09-09 21:41:42 +08:00
|
|
|
|
2021-01-04 22:11:24 +08:00
|
|
|
context 'when tasks are restored' do
|
|
|
|
it 'tasks are active' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(task1.reload.active?).to be_truthy
|
|
|
|
expect(task2.reload.active?).to be_truthy
|
|
|
|
expect(task3.reload.active?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls create activity service 3 times' do
|
|
|
|
expect(Activities::CreateActivityService)
|
|
|
|
.to(receive(:call).with(hash_including(activity_type: :restore_module))).exactly(3).times
|
|
|
|
|
|
|
|
action
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds activity in DB' do
|
|
|
|
expect { action }.to(change { Activity.count }.by(3))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 302' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(302)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when tasks are not restored' do
|
|
|
|
context 'when one task is invalid' do
|
|
|
|
before do
|
|
|
|
task3.name = Faker::Lorem.characters(number: 300)
|
|
|
|
task3.save(validate: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 302' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(302)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'only 2 activities added in DB' do
|
|
|
|
expect { action }.to(change { Activity.count }.by(2))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'one task is still archived' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(task1.reload.active?).to be_truthy
|
|
|
|
expect(task2.reload.active?).to be_truthy
|
|
|
|
expect(task3.reload.active?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have permissions for one task' do
|
|
|
|
before do
|
|
|
|
task3.restore!(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 302' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(302)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'only 2 activities added in DB' do
|
|
|
|
expect { action }.to(change { Activity.count }.by(2))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'tasks are restored' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(task1.reload.active?).to be_truthy
|
|
|
|
expect(task2.reload.active?).to be_truthy
|
|
|
|
expect(task3.reload.active?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-12 00:17:59 +08:00
|
|
|
end
|