scinote-web/spec/requests/api/service/experiments_controller_spec.rb
2021-10-28 12:28:46 +02:00

94 lines
2.3 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe "Api::Service::ExperimentsController", type: :request do
before :all do
@user = create(:user)
@team = create(:team, created_by: @user)
create(:user_team, user: @user, team: @team, role: 2)
@valid_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
create(:user_assignment,
assignable: @valid_project,
user: @user,
user_role: UserRole.find_by(name: I18n.t('user_roles.predefined.owner')),
assigned_by: @user)
@unaccessible_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@unaccessible_project.user_assignments.destroy_all
@experiment = create(:experiment, created_by: @user, last_modified_by: @user, project: @valid_project)
@valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) }
end
describe 'POST clone experiment, #clone' do
before :all do
@valid_headers['Content-Type'] = 'application/json'
end
let(:action) do
post(
api_service_team_clone_experiment_path(team_id: @valid_project.team.id),
params: request_body.to_json,
headers: @valid_headers
)
end
context 'when has valid params' do
let(:request_body) do
{
clone_experiment: {
experiment_id: @experiment.id,
to_project_id: @valid_project.id
}
}
end
it 'creates new experiment' do
expect { action }.to change { Experiment.count }.by(1)
end
it 'returns status 200' do
action
expect(response).to have_http_status 200
end
end
context 'when has missing param' do
let(:request_body) do
{
clone_experiment: {
experiment_id: @experiment.id
}
}
end
it 'renders 400' do
action
expect(response).to have_http_status(400)
end
end
context 'when has wrong project' do
let(:request_body) do
{
clone_experiment: {
experiment_id: @experiment.id,
to_project_id: @unaccessible_project.id
}
}
end
it 'renders 403' do
action
expect(response).to have_http_status(403)
end
end
end
end