Add Create and Update endpoints for experiments [SCI-4999]

This commit is contained in:
Oleksii Kriuchykhin 2020-09-16 23:15:24 +02:00
parent 5b564ed7d4
commit 0147e1aee1
3 changed files with 190 additions and 1 deletions

View file

@ -8,6 +8,7 @@ module Api
before_action only: :show do
load_experiment(:id)
end
before_action :load_experiment_for_managing, only: %i(update)
def index
experiments = @project.experiments
@ -19,6 +20,47 @@ module Api
def show
render jsonapi: @experiment, serializer: ExperimentSerializer
end
def create
raise PermissionError.new(Experiment, :create) unless can_create_experiments?(@project)
experiment = @project.experiments.create!(experiment_params.merge!(created_by: current_user,
last_modified_by: current_user))
render jsonapi: experiment, serializer: ExperimentSerializer, status: :created
end
def update
@experiment.assign_attributes(experiment_params)
return render body: nil, status: :no_content unless @experiment.changed?
if @experiment.archived_changed?
if @experiment.archived?
@experiment.archived_by = current_user
@experiment.archived_on = DateTime.now
else
@experiment.restored_by = current_user
@experiment.restored_on = DateTime.now
end
end
@experiment.last_modified_by = current_user
@experiment.save!
render jsonapi: @experiment, serializer: ExperimentSerializer, status: :ok
end
private
def experiment_params
raise TypeError unless params.require(:data).require(:type) == 'experiments'
params.require(:data).require(:attributes).permit(:name, :description, :archived)
end
def load_experiment_for_managing
@experiment = @project.experiments.find(params.require(:id))
raise PermissionError.new(Experiment, :manage) unless can_manage_experiment?(@experiment)
end
end
end
end

View file

@ -704,7 +704,7 @@ Rails.application.routes.draw do
get 'activities', to: 'projects#activities'
resources :reports, only: %i(index show),
path: 'reports', as: :reports
resources :experiments, only: %i(index show) do
resources :experiments, only: %i(index show create update) do
resources :task_groups, only: %i(index show)
resources :connections, only: %i(index show)
resources :tasks, only: %i(index show create update) do

View file

@ -11,6 +11,8 @@ RSpec.describe "Api::V1::ExperimentsController", type: :request do
@valid_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first)
create(:user_project, :owner, user: @user, project: @valid_project)
@unaccessible_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second)
@ -126,4 +128,149 @@ RSpec.describe "Api::V1::ExperimentsController", type: :request do
expect(hash_body['errors'][0]).to include('status': 404)
end
end
describe 'POST experiment, #create' do
before :all do
@valid_headers['Content-Type'] = 'application/json'
end
let(:action) do
post(
api_v1_team_project_experiments_path(
team_id: @valid_project.team.id,
project_id: @valid_project
),
params: request_body.to_json,
headers: @valid_headers
)
end
context 'when has valid params' do
let(:request_body) do
{
data: {
type: 'experiments',
attributes: {
name: 'Experiment name',
description: 'Experiment description'
}
}
}
end
it 'creates new experiment' do
expect { action }.to change { Experiment.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: 'experiments',
attributes: hash_including(name: 'Experiment name', description: 'Experiment description')
)
)
)
end
end
context 'when has missing param' do
let(:request_body) do
{
data: {
type: 'experiments',
attributes: {
}
}
}
end
it 'renders 400' do
action
expect(response).to have_http_status(400)
end
end
end
describe 'PATCH experiment, #update' do
before :all do
@valid_headers['Content-Type'] = 'application/json'
@experiment = @valid_project.experiments.first
end
let(:action) do
patch(
api_v1_team_project_experiment_path(
team_id: @valid_project.team.id,
project_id: @valid_project.id,
id: @experiment.id
),
params: request_body.to_json,
headers: @valid_headers
)
end
context 'when has valid params' do
let(:request_body) do
{
data: {
type: 'experiments',
attributes: {
name: 'New experiment name',
description: 'New experiment description',
archived: true
}
}
}
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: 'experiments',
attributes: hash_including(
name: 'New experiment name', description: 'New experiment description', archived: true
)
)
)
)
end
end
context 'when has missing param' do
let(:request_body) do
{
data: {
type: 'experiments',
attributes: {
}
}
}
end
it 'renders 400' do
action
expect(response).to have_http_status(400)
end
end
end
end