2019-09-19 22:12:15 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Api::V1::AssetsController', type: :request do
|
|
|
|
before :all do
|
|
|
|
@user = create(:user)
|
|
|
|
@team = create(:team, created_by: @user)
|
|
|
|
@project = create(:project, team: @team)
|
|
|
|
@experiment = create(:experiment, :with_tasks, project: @project)
|
|
|
|
@task = @experiment.my_modules.first
|
|
|
|
@protocol = create(:protocol, my_module: @task)
|
|
|
|
@step = create(:step, protocol: @protocol)
|
|
|
|
|
|
|
|
create(:user_team, user: @user, team: @team)
|
|
|
|
create(:user_project, :normal_user, user: @user, project: @project)
|
|
|
|
|
|
|
|
@valid_headers =
|
|
|
|
{ 'Authorization': 'Bearer ' + generate_token(@user.id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:asset) { create :asset, step: @step }
|
|
|
|
|
|
|
|
describe 'GET steps, #index' do
|
|
|
|
context 'when has valid params' do
|
|
|
|
it 'renders 200' do
|
|
|
|
create(:step_asset, step: @step, asset: asset)
|
|
|
|
|
|
|
|
get(api_v1_team_project_experiment_task_protocol_step_assets_path(
|
2020-07-24 20:56:42 +08:00
|
|
|
team_id: @team.id,
|
|
|
|
project_id: @project.id,
|
|
|
|
experiment_id: @experiment.id,
|
|
|
|
task_id: @task.id,
|
|
|
|
protocol_id: @protocol.id,
|
|
|
|
step_id: @step.id
|
|
|
|
), headers: @valid_headers)
|
2019-09-19 22:12:15 +08:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when protocol is not found' do
|
|
|
|
it 'renders 404' do
|
|
|
|
get(api_v1_team_project_experiment_task_protocol_step_assets_path(
|
2020-07-24 20:56:42 +08:00
|
|
|
team_id: @team.id,
|
|
|
|
project_id: @project.id,
|
|
|
|
experiment_id: @experiment.id,
|
|
|
|
task_id: @task.id,
|
|
|
|
protocol_id: -1,
|
|
|
|
step_id: @step.id
|
|
|
|
), headers: @valid_headers)
|
2019-09-19 22:12:15 +08:00
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET step, #show' do
|
|
|
|
context 'when has valid params' do
|
|
|
|
it 'renders 200' do
|
|
|
|
create(:step_asset, step: @step, asset: asset)
|
|
|
|
|
|
|
|
get(api_v1_team_project_experiment_task_protocol_step_asset_path(
|
2020-07-24 20:56:42 +08:00
|
|
|
team_id: @team.id,
|
|
|
|
project_id: @project.id,
|
|
|
|
experiment_id: @experiment.id,
|
|
|
|
task_id: @task.id,
|
|
|
|
protocol_id: @protocol.id,
|
|
|
|
step_id: @step.id,
|
|
|
|
id: asset.id
|
|
|
|
), headers: @valid_headers)
|
2019-09-19 22:12:15 +08:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when experiment is not found' do
|
|
|
|
it 'renders 404' do
|
|
|
|
get(api_v1_team_project_experiment_task_protocol_step_asset_path(
|
2020-07-24 20:56:42 +08:00
|
|
|
team_id: @team.id,
|
|
|
|
project_id: @project.id,
|
|
|
|
experiment_id: -1,
|
|
|
|
task_id: @task.id,
|
|
|
|
protocol_id: @protocol.id,
|
|
|
|
step_id: @step.id,
|
|
|
|
id: asset.id
|
|
|
|
), headers: @valid_headers)
|
2019-09-19 22:12:15 +08:00
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
2019-10-01 18:50:54 +08:00
|
|
|
|
|
|
|
context 'when asset is not found' do
|
|
|
|
it 'renders 404' do
|
|
|
|
get(api_v1_team_project_experiment_task_protocol_step_asset_path(
|
2020-07-24 20:56:42 +08:00
|
|
|
team_id: @team.id,
|
|
|
|
project_id: @project.id,
|
|
|
|
experiment_id: @experiment.id,
|
|
|
|
task_id: @task.id,
|
|
|
|
protocol_id: @protocol.id,
|
|
|
|
step_id: @step.id,
|
|
|
|
id: -1
|
|
|
|
), headers: @valid_headers)
|
2019-10-01 18:50:54 +08:00
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
2019-09-19 22:12:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST step, #create' do
|
|
|
|
before :all do
|
|
|
|
@valid_headers['Content-Type'] = 'application/json'
|
|
|
|
end
|
|
|
|
|
|
|
|
before :each do
|
2019-10-01 18:50:54 +08:00
|
|
|
allow_any_instance_of(Asset).to receive(:post_process_file)
|
2019-09-19 22:12:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:action) do
|
|
|
|
post(api_v1_team_project_experiment_task_protocol_step_assets_path(
|
2020-07-24 20:56:42 +08:00
|
|
|
team_id: @team.id,
|
|
|
|
project_id: @project.id,
|
|
|
|
experiment_id: @experiment.id,
|
|
|
|
task_id: @task.id,
|
|
|
|
protocol_id: @protocol.id,
|
|
|
|
step_id: @step.id
|
|
|
|
),
|
2019-09-24 18:41:45 +08:00
|
|
|
params: request_body,
|
2019-09-19 22:12:15 +08:00
|
|
|
headers: @valid_headers)
|
|
|
|
end
|
2020-07-24 20:56:42 +08:00
|
|
|
let(:request_body) do
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
type: 'attachments',
|
|
|
|
attributes: attributes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2019-09-19 22:12:15 +08:00
|
|
|
|
|
|
|
context 'when has valid params' do
|
2020-07-24 20:56:42 +08:00
|
|
|
context 'when multipart form' do
|
|
|
|
let(:file) { fixture_file_upload('files/test.jpg', 'image/jpg') }
|
|
|
|
let(:attributes) { { file: file } }
|
2019-09-19 22:12:15 +08:00
|
|
|
|
2020-07-24 20:56:42 +08:00
|
|
|
it 'creates new asset' do
|
|
|
|
expect { action }.to change { Asset.count }.by(1)
|
|
|
|
end
|
2019-09-19 22:12:15 +08:00
|
|
|
|
2020-07-24 20:56:42 +08:00
|
|
|
it 'returns status 201' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status 201
|
|
|
|
end
|
2019-09-19 22:12:15 +08:00
|
|
|
|
2020-07-24 20:56:42 +08:00
|
|
|
it 'calls post_process_file function for text extraction' do
|
|
|
|
expect_any_instance_of(Asset).to receive(:post_process_file)
|
|
|
|
|
|
|
|
action
|
|
|
|
end
|
2019-09-19 22:12:15 +08:00
|
|
|
end
|
2019-10-01 18:50:54 +08:00
|
|
|
|
2020-07-24 20:56:42 +08:00
|
|
|
context 'when base64 with json' do
|
|
|
|
let(:filedata_base64) do
|
|
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAIAA'\
|
|
|
|
'AACCAIAAAD91JpzAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAE0lE'\
|
|
|
|
'QVQIHWP8//8/AwMDExADAQAkBgMBOOSShwAAAABJRU5ErkJggg=='
|
|
|
|
end
|
2020-07-25 17:41:26 +08:00
|
|
|
let(:attributes) { { file_data: filedata_base64, file_name: 'file.png', file_type: 'image/png' } }
|
2020-07-24 20:56:42 +08:00
|
|
|
let(:request_body) { super().to_json }
|
|
|
|
|
|
|
|
it 'creates new asset' do
|
|
|
|
expect { action }.to change { Asset.count }.by(1)
|
|
|
|
end
|
2019-10-01 18:50:54 +08:00
|
|
|
|
2020-07-24 20:56:42 +08:00
|
|
|
it 'returns status 201' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status 201
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls post_process_file function for text extraction' do
|
|
|
|
expect_any_instance_of(Asset).to receive(:post_process_file)
|
|
|
|
|
|
|
|
action
|
|
|
|
end
|
2019-10-01 18:50:54 +08:00
|
|
|
end
|
2019-09-19 22:12:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has missing param' do
|
2020-07-25 17:41:26 +08:00
|
|
|
let(:request_body) { { data: { type: 'attachments', attributes: {} } } }
|
2019-09-19 22:12:15 +08:00
|
|
|
|
|
|
|
it 'renders 400' do
|
|
|
|
action
|
|
|
|
|
|
|
|
expect(response).to have_http_status(400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|