Add API endpoint for Task creation

This commit is contained in:
Urban Rotnik 2019-09-16 09:31:30 +02:00
parent 7802d5ee8b
commit 24033bec7a
7 changed files with 122 additions and 8 deletions

View file

@ -23,6 +23,16 @@ module Api
render jsonapi: @task, serializer: TaskSerializer
end
def create
raise PermissionError.new(MyModule, :create) unless can_manage_experiment?(@experiment)
my_module = @experiment.my_modules.create!(my_module_params)
render jsonapi: my_module,
serializer: TaskSerializer,
status: :created
end
def activities
activities = ActivitiesService.my_module_activities(@task)
.page(params.dig(:page, :number))
@ -34,6 +44,14 @@ module Api
private
def my_module_params
raise TypeError unless params.require(:data).require(:type) == 'tasks'
attr_list = %i(name x y)
params.require(:data).require(:attributes).require(attr_list)
params.require(:data).require(:attributes).permit(attr_list + [:description])
end
# Made the method below because its more elegant than changing parameters
# in routes file, and here. It exists because when we call input or output
# for a task, the "id" that used to be task id is now an id for the output

View file

@ -17,7 +17,6 @@ class MyModule < ApplicationRecord
validates :experiment, presence: true
validates :my_module_group, presence: true,
if: proc { |mm| !mm.my_module_group_id.nil? }
validates_uniqueness_of :x, scope: %i(y experiment_id), message: :not_unique
belongs_to :created_by,
@ -427,7 +426,7 @@ class MyModule < ApplicationRecord
description: self.description,
x: self.x,
y: self.y)
clone.save
clone.save(validate: false)
# Remove the automatically generated protocol,
# & clone the protocol instead

View file

@ -620,7 +620,7 @@ Rails.application.routes.draw do
namespace :api, defaults: { format: 'json' } do
get 'health', to: 'api#health'
get 'status', to: 'api#status'
if Api.configuration.core_api_v1_enabled
if Api.configuration.core_api_v1_enabled || Rails.env.development?
namespace :v1 do
resources :teams, only: %i(index show) do
resources :inventories,
@ -655,7 +655,7 @@ Rails.application.routes.draw do
resources :experiments, only: %i(index show) do
resources :task_groups, only: %i(index show)
resources :connections, only: %i(index show)
resources :tasks, only: %i(index show) do
resources :tasks, only: %i(index show create) do
resources :task_inventory_items, only: %i(index show),
path: 'items',
as: :items

View file

@ -4,7 +4,7 @@ FactoryBot.define do
factory :my_module do
sequence(:name) { |n| "Task-#{n}" }
x { Faker::Number.between(from: 1, to: 100) }
y { Faker::Number.between(from: 1, to: 100) }
sequence(:y) { |n| n }
workflow_order { MyModule.where(experiment_id: experiment.id).count + 1 }
experiment
my_module_group { create :my_module_group, experiment: experiment }

View file

@ -120,7 +120,7 @@ describe Experiment, type: :model do
{ id: 'n' + i.to_s,
name: t.name + '_new',
x: 50,
y: 50 }
y: 50 + i }
end
end
let(:to_clone) do

View file

@ -2,7 +2,7 @@
require 'rails_helper'
RSpec.describe "Api::V1::TasksController", type: :request do
RSpec.describe 'Api::V1::TasksController', type: :request do
before :all do
@user = create(:user)
@teams = create_list(:team, 2, created_by: @user)
@ -139,4 +139,101 @@ RSpec.describe "Api::V1::TasksController", type: :request do
expect(hash_body['errors'][0]).to include('status': 404)
end
end
describe 'POST tasks, #create' do
before :all do
create :user_project, :normal_user, user: @user, project: @valid_project
@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(
team_id: @teams.first.id,
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(
team_id: @teams.first.id,
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(
team_id: @teams.second.id,
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
up = UserProject.where(user: @user, project: @valid_project).first
up.update_attribute(:role, :viewer)
post(api_v1_team_project_experiment_tasks_path(
team_id: @teams.first.id,
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
end

View file

@ -53,7 +53,7 @@
"updated_at": "2019-01-21T13:32:46.278Z",
"workflow_order": -1,
"x": 64,
"y": 46
"y": 22
},
"my_module_repository_rows": [],
"my_module_tags": [],