Merge remote-tracking branch 'upstream/core-api' into core-api

This commit is contained in:
Luka Murn 2018-11-07 13:52:50 +01:00
commit 70bb539061
3 changed files with 154 additions and 13 deletions

View file

@ -7,7 +7,6 @@ module Api
before_action :load_project
before_action :load_experiment
before_action :load_task
before_action :load_protocol, only: :show
def index
protocols = @task.protocols
@ -18,10 +17,6 @@ module Api
each_serializer: ProtocolSerializer
end
def show
render jsonapi: @protocol, serializer: ProtocolSerializer
end
private
def load_team
@ -46,12 +41,6 @@ module Api
def load_task
@task = @experiment.my_modules.find(params.require(:task_id))
end
def load_protocol
@protocol = @task.protocols.find(
params.require(:id)
)
end
end
end
end

View file

@ -581,7 +581,7 @@ Rails.application.routes.draw do
resources :task_inventory_items, only: %i(index show),
path: 'items',
as: :items
resources :task_users, only: %i(index show),
resources :task_users, only: %i(index),
path: 'users',
as: :users
resources :task_tags, only: %i(index show),

View file

@ -11,6 +11,8 @@ RSpec.describe "Api::V1::ResultsController", type: :request do
@valid_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first)
create(:user_project, user: @user, project: @valid_project, role: 0)
@unaccessible_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second)
@ -38,8 +40,22 @@ RSpec.describe "Api::V1::ResultsController", type: :request do
create(:result, user: @user, last_modified_by: @user,
my_module: @unaccessible_task))
@valid_text_hash_body =
{ data:
{ type: 'results',
attributes: {
name: Faker::Name.unique.name
} },
included: [
{ type: 'result_texts',
attributes: {
text: Faker::Lorem.sentence(25)
} }
] }
@valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) }
{ 'Authorization': 'Bearer ' + generate_token(@user.id),
'Content-Type': 'application/json' }
end
describe 'GET results, #index' do
@ -99,6 +115,142 @@ RSpec.describe "Api::V1::ResultsController", type: :request do
end
end
describe 'POST result, #create' do
before :all do
@valid_tinymce_hash_body = {
data:
{ type: 'results',
attributes: {
name: Faker::Name.unique.name
} },
included: [
{ type: 'result_texts',
attributes: {
text: 'Result text 1 [~tiny_mce_id:a1]'
} },
{ type: 'tiny_mce_assets',
attributes: {
file_data: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAA'\
'AACCAIAAAD91JpzAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAE0lE'\
'QVQIHWP8//8/AwMDExADAQAkBgMBOOSShwAAAABJRU5ErkJggg==',
file_token: 'a1',
file_name: 'test.png'
} }
]
}
end
it 'Response with correct text result' do
hash_body = nil
post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id,
project_id: @valid_project,
experiment_id: @valid_experiment,
task_id: @valid_task
), params: @valid_text_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status 201
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(Result.last,
serializer: Api::V1::ResultSerializer)
.as_json[:data]
)
expect(hash_body[:included]).to match(
ActiveModelSerializers::SerializableResource
.new(Result.last, serializer: Api::V1::ResultSerializer,
include: :text)
.as_json[:included]
)
end
it 'Response with correct text result and TinyMCE images' do
hash_body = nil
post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id,
project_id: @valid_project,
experiment_id: @valid_experiment,
task_id: @valid_task
), params: @valid_tinymce_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status 201
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(Result.last, serializer: Api::V1::ResultSerializer)
.as_json[:data]
)
expect(hash_body[:included]).to match(
ActiveModelSerializers::SerializableResource
.new(Result.last, serializer: Api::V1::ResultSerializer,
include: :text)
.as_json[:included]
)
end
it 'When invalid request, mismatching file token' do
invalid_hash_body = @valid_tinymce_hash_body
invalid_hash_body[:included][1][:attributes][:file_token] = 'a2'
post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id,
project_id: @valid_project,
experiment_id: @valid_experiment,
task_id: @valid_task
), params: invalid_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status 400
end
it 'When invalid request, missing file reference in text' do
invalid_hash_body = @valid_tinymce_hash_body
invalid_hash_body[:included][0][:attributes][:text] = 'Result text 1'
post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id,
project_id: @valid_project,
experiment_id: @valid_experiment,
task_id: @valid_task
), params: invalid_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status 400
end
it 'When invalid request, non existing task' do
hash_body = nil
post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id,
project_id: @valid_project,
experiment_id: @valid_experiment,
task_id: -1
), params: @valid_text_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception
expect(hash_body).to match({})
end
it 'When invalid request, user in not member of the team' do
hash_body = nil
post api_v1_team_project_experiment_task_results_path(
team_id: @teams.second.id,
project_id: @unaccessible_project,
experiment_id: @unaccessible_experiment,
task_id: @unaccessible_task
), params: @valid_text_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception
expect(hash_body).to match({})
end
it 'When invalid request, task from another experiment' do
hash_body = nil
post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id,
project_id: @valid_project,
experiment_id: @valid_experiment,
task_id: @unaccessible_task
), params: @valid_text_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception
expect(hash_body).to match({})
end
end
describe 'GET result, #show' do
it 'When valid request, user can read result' do
hash_body = nil