2018-10-15 22:27:22 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2019-09-16 15:31:30 +08:00
|
|
|
RSpec.describe 'Api::V1::TasksController', type: :request do
|
2018-10-15 22:27:22 +08:00
|
|
|
before :all do
|
2020-09-08 20:57:26 +08:00
|
|
|
MyModuleStatusFlow.ensure_default
|
|
|
|
|
2018-10-15 22:27:22 +08:00
|
|
|
@user = create(:user)
|
2022-06-07 00:21:57 +08:00
|
|
|
@another_user = create(:user)
|
|
|
|
@team1 = create(:team, created_by: @user)
|
|
|
|
@team2 = create(:team, created_by: @another_user)
|
2018-10-15 22:27:22 +08:00
|
|
|
@valid_project = create(:project, name: Faker::Name.unique.name,
|
2022-06-07 00:21:57 +08:00
|
|
|
created_by: @user, team: @team1)
|
2018-10-15 22:27:22 +08:00
|
|
|
|
|
|
|
@unaccessible_project = create(:project, name: Faker::Name.unique.name,
|
2022-06-07 00:21:57 +08:00
|
|
|
created_by: @another_user, team: @team2)
|
2018-10-15 22:27:22 +08:00
|
|
|
|
|
|
|
@valid_experiment = create(:experiment, created_by: @user,
|
2022-06-07 00:21:57 +08:00
|
|
|
last_modified_by: @another_user, project: @valid_project)
|
|
|
|
@unaccessible_experiment = create(:experiment, created_by: @another_user,
|
|
|
|
last_modified_by: @another_user, project: @unaccessible_project)
|
2021-06-07 16:34:58 +08:00
|
|
|
create_list(:my_module, 3, :with_due_date, created_by: @user,
|
2018-10-15 22:27:22 +08:00
|
|
|
last_modified_by: @user, experiment: @valid_experiment)
|
2022-06-07 00:21:57 +08:00
|
|
|
create_list(:my_module, 3, :with_due_date, created_by: @another_user,
|
|
|
|
last_modified_by: @another_user, experiment: @unaccessible_experiment)
|
2018-10-15 22:27:22 +08:00
|
|
|
|
|
|
|
@valid_headers =
|
|
|
|
{ 'Authorization': 'Bearer ' + generate_token(@user.id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET tasks, #index' do
|
|
|
|
it 'Response with correct tasks' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_project_experiment_tasks_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team1.id,
|
2018-10-15 22:27:22 +08:00
|
|
|
project_id: @valid_project,
|
|
|
|
experiment_id: @valid_experiment
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
|
|
|
expect(hash_body[:data]).to match(
|
2021-06-07 16:34:58 +08:00
|
|
|
JSON.parse(
|
|
|
|
ActiveModelSerializers::SerializableResource
|
|
|
|
.new(@valid_experiment.my_modules, each_serializer: Api::V1::TaskSerializer)
|
|
|
|
.to_json
|
|
|
|
)['data']
|
2018-10-15 22:27:22 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, experiment from another project' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_project_experiment_tasks_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team1.id,
|
2018-10-15 22:27:22 +08:00
|
|
|
project_id: @valid_project,
|
|
|
|
experiment_id: @unaccessible_experiment
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-11-07 23:39:00 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 404)
|
2018-10-15 22:27:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, user in not member of the team' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_project_experiment_tasks_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team2.id,
|
2018-10-15 22:27:22 +08:00
|
|
|
project_id: @unaccessible_project,
|
|
|
|
experiment_id: @unaccessible_experiment
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(403)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-11-07 23:39:00 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 403)
|
2018-10-15 22:27:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, non existing experiment' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_project_experiment_tasks_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team1.id,
|
2018-10-15 22:27:22 +08:00
|
|
|
project_id: @valid_project,
|
|
|
|
experiment_id: -1
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-11-07 23:39:00 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 404)
|
2018-10-15 22:27:22 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET task, #show' do
|
|
|
|
it 'When valid request, user can read task' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_project_experiment_task_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team1.id,
|
2018-10-15 22:27:22 +08:00
|
|
|
project_id: @valid_project,
|
|
|
|
experiment_id: @valid_experiment,
|
|
|
|
id: @valid_experiment.my_modules.first.id
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
|
|
|
expect(hash_body[:data]).to match(
|
2021-06-07 16:34:58 +08:00
|
|
|
JSON.parse(
|
|
|
|
ActiveModelSerializers::SerializableResource
|
|
|
|
.new(@valid_experiment.my_modules.first, serializer: Api::V1::TaskSerializer)
|
|
|
|
.to_json
|
|
|
|
)['data']
|
2018-10-15 22:27:22 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, user in not member of the team' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_project_experiment_task_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team2.id,
|
2018-10-15 22:27:22 +08:00
|
|
|
project_id: @valid_project,
|
|
|
|
experiment_id: @valid_experiment,
|
|
|
|
id: @valid_experiment.my_modules.first.id
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(403)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-11-07 23:39:00 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 403)
|
2018-10-15 22:27:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, non existing task' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_project_experiment_task_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team1.id,
|
2018-10-15 22:27:22 +08:00
|
|
|
project_id: @valid_project,
|
|
|
|
experiment_id: @valid_experiment,
|
|
|
|
id: -1
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-11-07 23:39:00 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 404)
|
2018-10-15 22:27:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'When invalid request, task from unaccessible experiment' do
|
|
|
|
hash_body = nil
|
|
|
|
get api_v1_team_project_experiment_task_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team1.id,
|
2018-10-15 22:27:22 +08:00
|
|
|
project_id: @valid_project,
|
|
|
|
experiment_id: @valid_experiment,
|
|
|
|
id: @unaccessible_experiment.my_modules.first.id
|
|
|
|
), headers: @valid_headers
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect { hash_body = json }.not_to raise_exception
|
2018-11-07 23:39:00 +08:00
|
|
|
expect(hash_body['errors'][0]).to include('status': 404)
|
2018-10-15 22:27:22 +08:00
|
|
|
end
|
|
|
|
end
|
2019-09-16 15:31:30 +08:00
|
|
|
|
|
|
|
describe 'POST tasks, #create' do
|
|
|
|
before :all do
|
2021-05-30 00:40:26 +08:00
|
|
|
create :user_project, user: @user, project: @valid_project
|
2019-09-16 15:31:30 +08:00
|
|
|
@valid_headers['Content-Type'] = 'application/json'
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
type: 'tasks',
|
|
|
|
attributes: {
|
|
|
|
name: 'task name',
|
|
|
|
x: 1,
|
|
|
|
y: 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has valid params' do
|
|
|
|
let(:action) do
|
|
|
|
post(api_v1_team_project_experiment_tasks_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team1.id,
|
2019-09-16 15:31:30 +08:00
|
|
|
project_id: @valid_project.id,
|
|
|
|
experiment_id: @valid_experiment.id
|
|
|
|
),
|
|
|
|
params: request_body.to_json,
|
|
|
|
headers: @valid_headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates new my module' do
|
|
|
|
expect { action }.to change { MyModule.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns status 201' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status 201
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns well formated response' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(json).to match(
|
|
|
|
hash_including(
|
|
|
|
data: hash_including(
|
|
|
|
type: 'tasks',
|
|
|
|
attributes: hash_including(name: 'task name'),
|
|
|
|
relationships: hash_including(outputs: { data: [] }, inputs: { data: [] })
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has not valid params' do
|
|
|
|
it 'renders 404 when project not found' do
|
|
|
|
post(api_v1_team_project_experiment_tasks_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team1.id,
|
2019-09-16 15:31:30 +08:00
|
|
|
project_id: -1,
|
|
|
|
experiment_id: @valid_experiment.id
|
|
|
|
),
|
|
|
|
params: request_body.to_json,
|
|
|
|
headers: @valid_headers)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 403 when user is not member of the team' do
|
|
|
|
post(api_v1_team_project_experiment_tasks_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team2.id,
|
2019-09-16 15:31:30 +08:00
|
|
|
project_id: @valid_project.id,
|
|
|
|
experiment_id: @valid_experiment.id
|
|
|
|
),
|
|
|
|
params: request_body.to_json,
|
|
|
|
headers: @valid_headers)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 403 for use with view permissions' do
|
2022-06-07 00:21:57 +08:00
|
|
|
user_assignment = UserAssignment.where(user: @user, assignable: @valid_experiment).first
|
2021-05-30 00:40:26 +08:00
|
|
|
user_assignment.update!(user_role: create(:viewer_role))
|
2019-09-16 15:31:30 +08:00
|
|
|
|
|
|
|
post(api_v1_team_project_experiment_tasks_path(
|
2022-06-07 00:21:57 +08:00
|
|
|
team_id: @team1.id,
|
2019-09-16 15:31:30 +08:00
|
|
|
project_id: @valid_project.id,
|
|
|
|
experiment_id: @valid_experiment.id
|
|
|
|
),
|
|
|
|
params: request_body.to_json,
|
|
|
|
headers: @valid_headers)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-07-02 23:03:29 +08:00
|
|
|
|
|
|
|
describe 'PATCH task, #update' do
|
|
|
|
before :all do
|
|
|
|
@valid_headers['Content-Type'] = 'application/json'
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:task) { @valid_experiment.my_modules.take }
|
|
|
|
|
|
|
|
let(:action) do
|
|
|
|
patch(
|
|
|
|
api_v1_team_project_experiment_task_path(
|
|
|
|
team_id: @valid_project.team.id,
|
|
|
|
project_id: @valid_project.id,
|
|
|
|
experiment_id: @valid_experiment.id,
|
|
|
|
id: task.id
|
|
|
|
),
|
|
|
|
params: request_body.to_json,
|
|
|
|
headers: @valid_headers
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has valid params' do
|
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
type: 'tasks',
|
|
|
|
attributes: {
|
|
|
|
name: 'New task name',
|
|
|
|
description: 'New description about task'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns status 200' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status 200
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns well formated response' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(json).to match(
|
|
|
|
hash_including(
|
|
|
|
data: hash_including(
|
|
|
|
type: 'tasks',
|
|
|
|
attributes: hash_including(name: 'New task name', description: 'New description about task')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-15 17:19:25 +08:00
|
|
|
context 'direct task completion disabled, when has valid params' do
|
2020-07-02 23:03:29 +08:00
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
type: 'tasks',
|
|
|
|
attributes: {
|
|
|
|
state: 'completed'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-09-15 17:19:25 +08:00
|
|
|
it 'returns status 204, no changes to task' do
|
2020-07-02 23:03:29 +08:00
|
|
|
action
|
|
|
|
|
2020-09-15 17:19:25 +08:00
|
|
|
expect(response).to have_http_status 204
|
2020-07-02 23:03:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has missing param' do
|
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
type: 'tasks',
|
|
|
|
attributes: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders 400' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-15 22:27:22 +08:00
|
|
|
end
|