Merge pull request #4147 from okriuchykhin/ok_SCI_6834

Update tests with new team user assignments [SCI-6843]
This commit is contained in:
Alex Kriuchykhin 2022-09-05 14:34:16 +02:00 committed by GitHub
commit 8a0656ce21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
128 changed files with 702 additions and 2088 deletions

View file

@ -42,14 +42,7 @@ module Api
private private
def load_connections def load_connections
@connections = Connection.joins( @connections = @experiment.connections
'LEFT JOIN my_modules AS inputs ON input_id = inputs.id'
).joins(
'LEFT JOIN my_modules AS outputs ON output_id = outputs.id'
).where(
'inputs.experiment_id = ? OR outputs.experiment_id = ?',
@experiment.id, @experiment.id
)
end end
def load_connection def load_connection

View file

@ -111,13 +111,6 @@ class ProjectsController < ApplicationController
@project.created_by = current_user @project.created_by = current_user
@project.last_modified_by = current_user @project.last_modified_by = current_user
if @project.save if @project.save
# Create user-project association
user_project = UserProject.new(
role: :owner,
user: current_user,
project: @project
)
user_project.save
log_activity(:create_project) log_activity(:create_project)
message = t('projects.create.success_flash', name: escape_input(@project.name)) message = t('projects.create.success_flash', name: escape_input(@project.name))

View file

@ -10,7 +10,7 @@ class ReportsController < ApplicationController
generate_docx new_template_values project_contents) generate_docx new_template_values project_contents)
before_action :load_wizard_vars, only: %i(new edit) before_action :load_wizard_vars, only: %i(new edit)
before_action :load_available_repositories, only: %i(index save_pdf_to_inventory_modal available_repositories) before_action :load_available_repositories, only: %i(index save_pdf_to_inventory_modal available_repositories)
before_action :check_read_permissions, except: %i(index datatable new create edit update generate_pdf before_action :check_read_permissions, except: %i(index datatable new create edit update destroy generate_pdf
generate_docx new_template_values project_contents) generate_docx new_template_values project_contents)
before_action :check_create_permissions, only: %i(new create) before_action :check_create_permissions, only: %i(new create)
before_action :check_manage_permissions, only: %i(edit update generate_pdf before_action :check_manage_permissions, only: %i(edit update generate_pdf

View file

@ -9,7 +9,9 @@ class RepositoryRowsController < ApplicationController
before_action :load_repository, except: %i(show print_modal print) before_action :load_repository, except: %i(show print_modal print)
before_action :load_repository_or_snapshot, only: %i(print_modal print) before_action :load_repository_or_snapshot, only: %i(print_modal print)
before_action :load_repository_row, only: %i(update assigned_task_list active_reminder_repository_cells) before_action :load_repository_row, only: %i(update assigned_task_list active_reminder_repository_cells)
before_action :check_read_permissions, except: %i(show create update delete_records copy_records reminder_repository_cells) before_action :check_read_permissions, except: %i(show create update delete_records
copy_records reminder_repository_cells
delete_records archive_records restore_records)
before_action :check_snapshotting_status, only: %i(create update delete_records copy_records) before_action :check_snapshotting_status, only: %i(create update delete_records copy_records)
before_action :check_create_permissions, only: :create before_action :check_create_permissions, only: :create
before_action :check_delete_permissions, only: %i(delete_records archive_records restore_records) before_action :check_delete_permissions, only: %i(delete_records archive_records restore_records)

View file

@ -87,6 +87,16 @@ class Experiment < ApplicationRecord
.where(project: Project.viewable_by_user(user, teams)) .where(project: Project.viewable_by_user(user, teams))
end end
def connections
Connection.joins(
'LEFT JOIN my_modules AS inputs ON input_id = inputs.id'
).joins(
'LEFT JOIN my_modules AS outputs ON output_id = outputs.id'
).where(
'inputs.experiment_id = ? OR outputs.experiment_id = ?', id, id
)
end
def archived_branch? def archived_branch?
archived? || project.archived? archived? || project.archived?
end end

View file

@ -58,7 +58,7 @@ class Project < ApplicationRecord
reject_if: :all_blank reject_if: :all_blank
scope :visible_to, (lambda do |user, team| scope :visible_to, (lambda do |user, team|
unless can_manage_team?(team) unless team.permission_granted?(user, TeamPermissions::MANAGE)
left_outer_joins(user_assignments: :user_role) left_outer_joins(user_assignments: :user_role)
.where(user_assignments: { user: user }) .where(user_assignments: { user: user })
.where('? = ANY(user_roles.permissions)', ProjectPermissions::READ) .where('? = ANY(user_roles.permissions)', ProjectPermissions::READ)

View file

@ -204,6 +204,14 @@ class Protocol < ApplicationRecord
user_id: user.id)) user_id: user.id))
end end
def created_by
in_module? ? my_module.created_by : added_by
end
def permission_parent
in_module? ? my_module : team
end
def linked_modules def linked_modules
MyModule.joins(:protocols).where('protocols.parent_id = ?', id) MyModule.joins(:protocols).where('protocols.parent_id = ?', id)
end end

View file

@ -87,6 +87,14 @@ class Report < ApplicationRecord
where(project: Project.viewable_by_user(user, teams)) where(project: Project.viewable_by_user(user, teams))
end end
def created_by
user
end
def permission_parent
team
end
def root_elements def root_elements
report_elements.active.where(parent: nil).order(:position) report_elements.active.where(parent: nil).order(:position)
end end

View file

@ -26,7 +26,7 @@ class Repository < RepositoryBase
class_name: 'RepositorySnapshot', class_name: 'RepositorySnapshot',
foreign_key: :parent_id, foreign_key: :parent_id,
inverse_of: :original_repository inverse_of: :original_repository
has_many :repository_ledger_records, as: :references, dependent: :nullify has_many :repository_ledger_records, as: :reference, dependent: :nullify
has_many :repository_table_filters, dependent: :destroy has_many :repository_table_filters, dependent: :destroy
before_save :sync_name_with_snapshots, if: :name_changed? before_save :sync_name_with_snapshots, if: :name_changed?
@ -101,6 +101,10 @@ class Repository < RepositoryBase
end end
end end
def permission_parent
team
end
def default_table_state def default_table_state
Constants::REPOSITORY_TABLE_DEFAULT_STATE Constants::REPOSITORY_TABLE_DEFAULT_STATE
end end

View file

@ -20,7 +20,7 @@ Canaid::Permissions.register_for(Repository) do
create_repository_columns) create_repository_columns)
.each do |perm| .each do |perm|
can perm do |_, repository| can perm do |_, repository|
repository.repository_snapshots.provisioning.none? repository.active? && repository.repository_snapshots.provisioning.none?
end end
end end
@ -38,7 +38,7 @@ Canaid::Permissions.register_for(Repository) do
# repository: destroy # repository: destroy
can :delete_repository do |user, repository| can :delete_repository do |user, repository|
repository.archived? && can_manage_repository?(user, repository) repository.archived? && repository.permission_granted?(user, RepositoryPermissions::MANAGE)
end end
# repository: share # repository: share

View file

@ -763,6 +763,7 @@ class TeamImporter
end end
protocol.id = nil protocol.id = nil
protocol.added_by_id = find_user(protocol.added_by_id) protocol.added_by_id = find_user(protocol.added_by_id)
protocol.added_by_id ||= my_module.present? ? my_module.created_by_id : team.created_by_id
protocol.team = team || my_module.experiment.project.team protocol.team = team || my_module.experiment.project.team
protocol.archived_by_id = find_user(protocol.archived_by_id) protocol.archived_by_id = find_user(protocol.archived_by_id)
protocol.restored_by_id = find_user(protocol.restored_by_id) protocol.restored_by_id = find_user(protocol.restored_by_id)

View file

@ -2606,6 +2606,7 @@ en:
date: date:
formats: formats:
full: "%{date_format}" full: "%{date_format}"
full_date: "%{date_format}"
time: time:
formats: formats:
full_js: "D.M.YYYY HH:mm" full_js: "D.M.YYYY HH:mm"

View file

@ -7,16 +7,16 @@ describe AccessPermissions::ExperimentsController, type: :controller do
let!(:user) { subject.current_user } let!(:user) { subject.current_user }
let!(:team) { create :team, created_by: user } let!(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let!(:experiment) { create :experiment, project: project, created_by: user } let!(:experiment) { create :experiment, project: project, created_by: user }
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) } let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
let!(:viewer_user_role) { create :viewer_role } let!(:viewer_user_role) { create :viewer_role }
let!(:technician_role) { create :technician_role } let!(:technician_role) { create :technician_role }
let!(:project) { create :project, team: team, created_by: user } let!(:project) { create :project, team: team, created_by: user }
let!(:viewer_user) { create :user, confirmed_at: Time.zone.now } let!(:viewer_user) { create :user, confirmed_at: Time.zone.now }
let!(:normal_user_team) { create :user_team, :normal_user, user: viewer_user, team: team }
before do before do
create_user_assignment(team, owner_role, user)
create_user_assignment(team, viewer_user_role, viewer_user)
create_user_assignment(experiment, owner_role, user) create_user_assignment(experiment, owner_role, user)
create_user_assignment(experiment, viewer_user_role, viewer_user) create_user_assignment(experiment, viewer_user_role, viewer_user)
end end

View file

@ -7,7 +7,6 @@ describe AccessPermissions::MyModulesController, type: :controller do
let!(:user) { subject.current_user } let!(:user) { subject.current_user }
let!(:team) { create :team, created_by: user } let!(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let!(:experiment) { create :experiment, project: project, created_by: user } let!(:experiment) { create :experiment, project: project, created_by: user }
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) } let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
let!(:viewer_user_role) { create :viewer_role } let!(:viewer_user_role) { create :viewer_role }
@ -15,11 +14,12 @@ describe AccessPermissions::MyModulesController, type: :controller do
let!(:project) { create :project, team: team, created_by: user } let!(:project) { create :project, team: team, created_by: user }
let!(:user_project) { create :user_project, user: user, project: project } let!(:user_project) { create :user_project, user: user, project: project }
let!(:viewer_user) { create :user, confirmed_at: Time.zone.now } let!(:viewer_user) { create :user, confirmed_at: Time.zone.now }
let!(:normal_user_team) { create :user_team, :normal_user, user: viewer_user, team: team }
let!(:viewer_user_project) { create :user_project, user: viewer_user, project: project } let!(:viewer_user_project) { create :user_project, user: viewer_user, project: project }
let!(:my_module) { create :my_module, experiment: experiment, created_by: experiment.created_by, created_by: user } let!(:my_module) { create :my_module, experiment: experiment, created_by: experiment.created_by, created_by: user }
before do before do
create_user_assignment(team, owner_role, user)
create_user_assignment(team, viewer_user_role, viewer_user)
create_user_assignment(my_module, owner_role, user) create_user_assignment(my_module, owner_role, user)
create_user_assignment(my_module, viewer_user_role, viewer_user) create_user_assignment(my_module, viewer_user_role, viewer_user)
end end

View file

@ -7,18 +7,16 @@ describe AccessPermissions::ProjectsController, type: :controller do
let!(:user) { subject.current_user } let!(:user) { subject.current_user }
let!(:team) { create :team, created_by: user } let!(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let!(:project) { create :project, team: team, created_by: user } let!(:project) { create :project, team: team, created_by: user }
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) } let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
let!(:normal_user_role) { create :normal_user_role } let!(:normal_user_role) { create :normal_user_role }
let!(:technician_role) { create :technician_role } let!(:technician_role) { create :technician_role }
let!(:user_project) { create :user_project, user: user, project: project } let!(:user_project) { create :user_project, user: user, project: project }
let!(:normal_user) { create :user, confirmed_at: Time.zone.now } let!(:normal_user) { create :user, confirmed_at: Time.zone.now }
let!(:normal_user_team) do
create :user_team, before do
:normal_user, create_user_assignment(team, owner_role, user)
user: normal_user, create_user_assignment(team, normal_user_role, normal_user)
team: team
end end
describe 'GET #new' do describe 'GET #new' do

View file

@ -6,14 +6,14 @@ describe AssetsController, type: :controller do
login_user login_user
include_context 'reference_project_structure', { include_context 'reference_project_structure', {
role: :owner, team_role: :owner,
result_asset: true, result_asset: true,
step: true, step: true,
step_asset: true, step_asset: true,
result_comment: true result_comment: true
} }
let(:protocol_in_repository) { create :protocol, :in_public_repository, team: team } let(:protocol_in_repository) { create :protocol, :in_public_repository, team: team, added_by: user }
let(:step_in_repository) { create :step, protocol: protocol_in_repository, user: user } let(:step_in_repository) { create :step, protocol: protocol_in_repository, user: user }
let!(:asset) { create :asset } let!(:asset) { create :asset }
@ -45,7 +45,6 @@ describe AssetsController, type: :controller do
it 'calls create activity service (start edit image on step in repository)' do it 'calls create activity service (start edit image on step in repository)' do
params[:id] = step_asset_in_repository.asset.id params[:id] = step_asset_in_repository.asset.id
user_team
expect(Activities::CreateActivityService).to receive(:call) expect(Activities::CreateActivityService).to receive(:call)
.with(hash_including(activity_type: :edit_image_on_step_in_repository)) .with(hash_including(activity_type: :edit_image_on_step_in_repository))
action action

View file

@ -83,13 +83,6 @@ describe CanvasController do
} }
end end
before do
8.times do |i|
create_user_assignment(public_send("task#{i+1}"), role, user)
end
create_user_assignment(experiment2, role, user)
end
context 'when have a lot changes on canvas' do context 'when have a lot changes on canvas' do
it 'everything goes right, redirected to canvas' do it 'everything goes right, redirected to canvas' do
action action

View file

@ -1,20 +0,0 @@
require 'rails_helper'
describe ClientApi::ActivitiesController, type: :controller, broken: true do
login_user
render_views
before do
project = create :project, created_by: User.first
UserProject.create(user: User.first, project: project, role: 2)
create :activity, user: User.first, project: project
end
describe '#index' do
it 'returns a valid object' do
get :index, format: :json
expect(response.status).to eq(200)
expect(response.body).to match_response_schema('activities')
end
end
end

View file

@ -1,10 +0,0 @@
require 'rails_helper'
describe ClientApi::ConfigurationsController, type: :controller, broken: true do
login_user
describe '#about_scinote' do
let(:subject) { get :about_scinote, format: :json }
it { is_expected.to be_success }
end
end

View file

@ -1,27 +0,0 @@
require 'rails_helper'
describe ClientApi::NotificationsController, type: :controller, broken: true do
login_user
let(:notification) { create :notification }
let(:user_notification) do
create :user_notification,
user: User.first,
notification: notification
end
describe '#recent_notifications' do
it 'returns a list of notifications' do
get :recent_notifications, format: :json
expect(response).to be_success
expect(response).to render_template('client_api/notifications/index')
end
end
describe '#unreaded_notifications_number' do
it 'returns a number of unreaded notifications' do
get :unread_notifications_count, format: :json
expect(response).to be_success
expect(response.body).to include('count')
end
end
end

View file

@ -1,73 +0,0 @@
require 'rails_helper'
describe ClientApi::PermissionsController, type: :controller, broken: true do
login_user
describe '#status' do
let!(:user) { User.first || create(:user) }
let!(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, user: user, team: team, role: 2 }
let(:params) do
{ requiredPermissions: ['can_read_team'],
resource: { type: 'Team', id: team.id } }
end
let(:subject) { post :status, format: :json, params: params }
it { is_expected.to be_success }
it 'returns an object with the permission' do
body = JSON.parse(subject.body)
expect(body).to eq('can_read_team' => true)
end
it 'raises an error if no required permissions passed' do
expect do
post :status,
format: :json,
params: { resource: { type: 'Team', id: team.id } }
end
.to raise_error(NoMethodError)
end
it 'raises an error if no required resource type invalid' do
expect do
post :status,
format: :json,
params: { requiredPermissions: ['can_read_team'],
resource: { type: 'Banana', id: team.id } }
end
.to raise_error(ArgumentError)
end
it 'raises an error if no required resource id is not present' do
expect do
post :status,
format: :json,
params: { requiredPermissions: ['can_read_team'],
resource: { type: 'Team' } }
end
.to raise_error(ArgumentError)
end
context 'raises an error if can\'t find permission invalid when resource' do
it 'is absent' do
expect do
post :status,
format: :json,
params: { requiredPermissions: ['can_throw_bananas'] }
end
.to raise_error(ArgumentError)
end
it 'is present' do
expect do
post :status,
format: :json,
params: { requiredPermissions: ['can_throw_bananas'],
resource: { type: 'Team', id: team.id } }
end
.to raise_error(ArgumentError)
end
end
end
end

View file

@ -1,134 +0,0 @@
require 'rails_helper'
describe ClientApi::Teams::TeamsController, type: :controller, broken: true do
login_user
before do
@user_one = User.first
@user_two = create :user, email: 'sec_user@asdf.com'
@team_one = create :team, created_by: @user_one
@team_two = create :team, name: 'Team two', created_by: @user_two
create :user_team, team: @team_one, user: @user_one, role: 2
end
describe 'GET #index' do
it 'should return HTTP success response' do
get :index, format: :json
expect(response).to be_success
expect(response).to have_http_status(:ok)
end
end
describe 'POST #create' do
before do
@team_one.update_attribute(:name, 'My Team')
@team_one.update_attribute(:description, 'Lorem ipsum ipsum')
end
it 'should return HTTP success response' do
post :create, params: { team: { name: 'My New Team' } }, as: :json
expect(response).to have_http_status(:ok)
end
it 'should return HTTP unprocessable_entity response if name too short' do
@team_one.update_attribute(
:name,
('a' * (Constants::NAME_MIN_LENGTH - 1)).to_s
)
post :create, params: { team: @team_one }, as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
it 'should return HTTP unprocessable_entity response if name too long' do
@team_one.update_attribute(
:name,
('a' * (Constants::NAME_MAX_LENGTH + 1)).to_s
)
post :create, params: { team: @team_one }, as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
it 'should return HTTP unprocessable_entity response if description too long' do
@team_one.update_attribute(
:description,
('a' * (Constants::TEXT_MAX_LENGTH + 1)).to_s
)
post :create, params: { team: @team_one }, as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
end
describe 'POST #change_team' do
it 'should return HTTP success response' do
create :user_team, team: @team_two, user: @user_one, role: 2
@user_one.update_attribute(:current_team_id, @team_one.id)
post :change_team, params: { team_id: @team_two.id }, as: :json
expect(response).to have_http_status(:ok)
end
it 'should return HTTP unprocessable_entity response if user not in team' do
@user_one.update_attribute(:current_team_id, @team_one.id)
post :change_team, params: { team_id: @team_two.id }, as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
it 'should return HTTP success response if same team as current' do
@user_one.update_attribute(:current_team_id, @team_one.id)
post :change_team, params: { team_id: @team_one.id }, as: :json
expect(response).to have_http_status(:ok)
end
end
describe 'GET #details' do
it 'should return HTTP success response' do
create :user_team, team: @team_two, user: @user_one, role: 2
@user_one.update_attribute(:current_team_id, @team_one.id)
get :details, params: { team_id: @team_two.id }, as: :json
expect(response).to have_http_status(:ok)
end
it 'should return HTTP unprocessable_entity response if user not in team' do
@user_one.update_attribute(:current_team_id, @team_one.id)
get :details, params: { team_id: @team_two.id }, as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
it 'should return HTTP unprocessable_entity response if team_id not valid' do
get :details, params: { team_id: 'banana' }, as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
end
describe 'POST #update' do
let(:user_team) do
create :user_team, team: @team_two, user: @user_one, role: 2
end
it 'should return HTTP success response' do
user_team
post :update,
params: { team_id: @team_two.id,
team: { description: 'My description' } },
as: :json
expect(response).to have_http_status(:ok)
end
it 'should return HTTP unprocessable_entity response iput not valid' do
user_team
post :update,
params: {
team_id: @team_two.id,
team: {
description: "super long: #{'a' * Constants::TEXT_MAX_LENGTH}"
}
},
as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
end
describe 'GET #current_team' do
let(:subject) { get :current_team, as: :json }
it { is_expected.to have_http_status(:ok) }
end
end

View file

@ -1,40 +0,0 @@
require 'rails_helper'
describe ClientApi::Users::InvitationsController, type: :controller, broken: true do
login_user
let(:user_one) { User.first }
let(:team_one) { create :team }
let(:emails_one) { Array.new(3) { Faker::Internet.email } }
describe '#invite_users' do
it 'returns HTTP success if users were invited' do
post :invite_users, params: { user_role: 'normal_user',
emails: emails_one },
format: :json
expect(response).to be_success
expect(response).to have_http_status(:ok)
expect(response).to render_template('client_api/users/invite_users')
end
it 'returns HTTP success if users were invited to team' do
create :user_team, team: team_one, user: user_one
post :invite_users, params: { team_id: team_one.id,
user_role: 'normal_user',
emails: emails_one },
format: :json
expect(response).to be_success
expect(response).to have_http_status(:ok)
expect(response).to render_template('client_api/users/invite_users')
end
it 'returns HTTP unprocessable_entity if users can\'t be invited to team' do
post :invite_users, params: { team_id: team_one.id,
user_role: 'normal_user',
emails: emails_one },
format: :json
expect(response).to_not be_success
expect(response).to have_http_status(:unprocessable_entity)
expect(response.media_type).to eq 'application/json'
end
end
end

View file

@ -1,82 +0,0 @@
require 'rails_helper'
describe ClientApi::Users::UserTeamsController, type: :controller, broken: true do
login_user
let(:user_one) { User.first }
let(:user_two) { create :user, email: Faker::Internet.email }
let(:team) { create :team }
let(:user_team) { create :user_team, team: team, user: user_one }
describe 'DELETE #leave_team' do
it 'should return HTTP success if user can leave the team' do
create :user_team, team: team, user: user_two
delete :leave_team,
params: { team: team.id, user_team: user_team.id },
format: :json
expect(response).to be_success
expect(response).to have_http_status(:ok)
end
it 'should return HTTP unprocessable_entity if user can\'t ' \
'leave the team' do
delete :leave_team,
params: { team: team.id, user_team: user_team.id },
format: :json
expect(response).to_not be_success
expect(response).to have_http_status(:unprocessable_entity)
end
it 'should return HTTP unprocessable_entity if no params given' do
delete :leave_team, format: :json
expect(response).to_not be_success
expect(response).to have_http_status(:unprocessable_entity)
end
end
describe 'POST #update_role' do
it 'should return HTTP success if user can leave the team' do
create :user_team, team: team, user: user_two, role: 2
post :update_role,
params: { team: team.id,
user_team: user_team.id,
role: 'normal_user' },
format: :json
expect(response).to be_success
expect(response).to have_http_status(:ok)
end
it 'should return HTTP unprocessable_entity if user can\'t ' \
'leave the team' do
post :update_role,
params: { team: team.id,
user_team: user_team.id,
role: 'normal_user' },
format: :json
expect(response).to_not be_success
expect(response).to have_http_status(:unprocessable_entity)
end
end
describe 'DELETE #remove_user' do
it 'should return HTTP success if user can be removed' do
user_team
user_team_two = create :user_team, team: team, user: user_two
post :remove_user,
params: { team: team.id, user_team: user_team_two.id },
format: :json
expect(response).to be_success
expect(response).to have_http_status(:ok)
end
it 'should return HTTP unprocessable_entity if user can\'t ' \
'be removed' do
post :remove_user,
params: { team: team.id,
user_team: user_team.id,
role: 'normal_user' },
format: :json
expect(response).to_not be_success
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

View file

@ -1,255 +0,0 @@
require 'rails_helper'
describe ClientApi::Users::UsersController, type: :controller, broken: true do
login_user
before do
# user password is set in user factory defaults to 'asdf1243'
@user = User.first
end
describe '#sign_out_user' do
it 'returns unauthorized response' do
sign_out @user
get :sign_out_user, format: :json
expect(response).to have_http_status(:unauthorized)
end
it 'responds successfully if the user is signed out' do
get :sign_out_user, format: :json
expect(response).to have_http_status(:ok)
expect(subject.current_user).to eq(nil)
end
end
describe 'GET current_user_info' do
it 'responds successfully' do
get :current_user_info, format: :json
expect(response).to have_http_status(:ok)
end
end
describe 'POST update' do
let(:new_password) { 'secretPassword' }
let(:new_email) { 'banana@fruit.com' }
it 'responds successfully if all password params are set' do
post :update,
params: { user: { password: new_password,
password_confirmation: new_password,
current_password: 'asdf1243' } },
format: :json
expect(response).to have_http_status(:ok)
end
it 'responds unsuccessfully if no current_password is provided' do
post :update,
params: { user: { password: new_password,
password_confirmation: new_password } },
format: :json
expect(response).to have_http_status(:unprocessable_entity)
end
it 'responds unsuccessfully if no password_confirmation is provided' do
post :update,
params: { user: { password: new_password,
current_password: 'asdf1243' } },
format: :json
expect(response).to have_http_status(:unprocessable_entity)
end
it 'responds successfully if time_zone is updated' do
post :update, params: { user: { time_zone: 'Pacific/Fiji' } },
format: :json
expect(response).to have_http_status(:ok)
end
it 'responds successfully if email is updated' do
post :update, params: { user: { email: new_email,
current_password: 'asdf1243' } },
format: :json
expect(response).to have_http_status(:ok)
expect(@user.reload.email).to eq(new_email)
end
it 'responds unsuccessfully if email is updated without password' do
post :update, params: { user: { email: new_email } },
format: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(@user.reload.email).to_not eq(new_email)
end
it 'responds unsuccessfully if email is updated with invalid email' do
post :update, params: { user: { email: 'bananafruit.com',
current_password: 'asdf1243' } },
format: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(@user.reload.email).to_not eq(new_email)
end
it 'changes timezone' do
user = User.first
expect(user.time_zone).to eq('UTC')
post :update, params: { user: { time_zone: 'Pacific/Fiji' } },
format: :json
expect(user.reload.time_zone).to eq('Pacific/Fiji')
end
it 'responds successfully if initials are provided' do
post :update, params: { user: { initials: 'TD' } }, format: :json
expect(response).to have_http_status(:ok)
end
it 'updates user initials' do
user = User.first
expect(user.initials).not_to eq('TD')
post :update, params: { user: { initials: 'TD' } }, format: :json
expect(user.reload.initials).to eq('TD')
end
it 'responds successfully if system_message_email_notification provided' do
post :update,
params: { user: { system_message_email_notificationatus: 'false' } },
format: :json
expect(response).to have_http_status(:ok)
end
it 'changes system_message_email_notification from false => true' do
user = User.first
user.system_message_email_notification = false
user.save
post :update,
params: { user: { system_message_email_notification: true } },
format: :json
expect(user.reload.system_message_email_notification).to eql('true')
end
it 'changes system_message_email_notification from true => false' do
user = User.first
user.system_message_email_notification = true
user.save
post :update,
params: { user: { system_message_email_notification: false } },
format: :json
expect(user.reload.system_message_email_notification).to eql('false')
end
it 'responds successfully if recent_email_notification provided' do
post :update,
params: { user: { recent_email_notification: false } },
format: :json
expect(response).to have_http_status(:ok)
end
it 'changes recent_email_notification from false => true' do
user = User.first
user.recent_email_notification = false
user.save
post :update,
params: { user: { recent_email_notification: true } },
format: :json
expect(user.reload.recent_email_notification).to eql('true')
end
it 'changes notification from true => false' do
user = User.first
user.recent_email_notification = true
user.save
post :update,
params: { user: { recent_email_notification: false } },
format: :json
expect(user.reload.recent_email_notification).to eql('false')
end
it 'responds successfully if recent_notification provided' do
post :update, params: { user: { recent_notification: false } },
format: :json
expect(response).to have_http_status(:ok)
end
it 'changes recent_notification from false => true' do
user = User.first
user.recent_notification = false
user.save
post :update, params: { user: { recent_notification: true } },
format: :json
expect(user.reload.recent_notification).to eql('true')
end
it 'changes recent_notification from true => false' do
user = User.first
user.recent_notification = true
user.save
post :update, params: { user: { recent_notification: false } },
format: :json
expect(user.reload.recent_notification).to eq('false')
end
it 'responds successfully if assignments_email_notification provided' do
post :update,
params: { user: { assignments_email_notification: false } },
format: :json
expect(response).to have_http_status(:ok)
end
it 'changes assignments_email_notification from false => true' do
user = User.first
user.assignments_email_notification = false
user.save
post :update,
params: { user: { assignments_email_notification: true } },
format: :json
expect(user.reload.assignments_email_notification).to eq('true')
end
it 'changes assignments_email_notification from true => false' do
user = User.first
user.assignments_email_notification = true
user.save
post :update,
params: { user: { assignments_email_notification: false } },
format: :json
expect(user.reload.assignments_email_notification).to eq('false')
end
it 'responds successfully if assignments_notification provided' do
post :update,
params: { user: { assignments_notification: false } },
format: :json
expect(response).to have_http_status(:ok)
end
it 'changes assignments_notification from false => true' do
user = User.first
user.assignments_notification = false
user.save
post :update,
params: { user: { assignments_notification: true } },
format: :json
expect(user.reload.assignments_notification).to eq('true')
end
it 'changes assignments_notification from true => false' do
user = User.first
user.assignments_notification = true
user.save
post :update,
params: { user: { assignments_notification: false } },
format: :json
expect(user.reload.assignments_notification).to eq('false')
end
end
end

View file

@ -70,10 +70,6 @@ describe ExperimentsController, type: :controller do
} }
end end
before do
create_user_assignment(archived_experiment, role, user)
end
it 'calls create activity for unarchiving experiment' do it 'calls create activity for unarchiving experiment' do
expect(Activities::CreateActivityService) expect(Activities::CreateActivityService)
.to(receive(:call) .to(receive(:call)

View file

@ -7,7 +7,6 @@ describe ExternalProtocolsController, type: :controller do
let(:user) { subject.current_user } let(:user) { subject.current_user }
let(:team) { create :team, created_by: user } let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
describe 'GET index' do describe 'GET index' do
let(:params) do let(:params) do

View file

@ -5,18 +5,15 @@ require 'rails_helper'
describe Users::InvitationsController, type: :controller do describe Users::InvitationsController, type: :controller do
login_user login_user
let(:user) { subject.current_user } include_context 'reference_project_structure'
let!(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
describe 'POST invite_users' do describe 'POST invite_users' do
let(:invited_user) { create :user } let(:invited_user) { create :user }
let(:team) { create :team }
let(:params) do let(:params) do
{ {
team_ids: [team.id], team_ids: [team.id],
emails: [invited_user.email], emails: [invited_user.email],
role: 'guest' role_id: viewer_role.id
} }
end end
let(:action) { post :invite_users, params: params, format: :json } let(:action) { post :invite_users, params: params, format: :json }
@ -35,9 +32,9 @@ describe Users::InvitationsController, type: :controller do
.to(change { Activity.count }) .to(change { Activity.count })
end end
it 'adds user_team record in DB' do it 'adds user team assignment record in DB' do
expect { action } expect { action }
.to(change { UserTeam.count }) .to(change { UserAssignment.count })
end end
end end
end end

View file

@ -5,7 +5,7 @@ require 'rails_helper'
describe MyModuleCommentsController, type: :controller do describe MyModuleCommentsController, type: :controller do
login_user login_user
include_context 'reference_project_structure' , { include_context 'reference_project_structure' , {
role: :normal_user, team_role: :normal_user,
my_module_comment: true my_module_comment: true
} }

View file

@ -4,9 +4,7 @@ require 'rails_helper'
describe MyModuleRepositoriesController, type: :controller do describe MyModuleRepositoriesController, type: :controller do
login_user login_user
include_context 'reference_project_structure' , { include_context 'reference_project_structure'
role: :normal_user
}
let!(:repository) { create :repository, created_by: user, team: team } let!(:repository) { create :repository, created_by: user, team: team }
let!(:repository_row) do let!(:repository_row) do
create :repository_row, created_by: user, repository: repository create :repository_row, created_by: user, repository: repository

View file

@ -173,18 +173,11 @@ describe MyModulesController, type: :controller do
my_modules_ids: [task1.id, task2.id, task3.id] my_modules_ids: [task1.id, task2.id, task3.id]
} }
end end
let(:experiment) { create :experiment }
let(:task1) { create :my_module, :archived, experiment: experiment, created_by: experiment.created_by } let(:task1) { create :my_module, :archived, experiment: experiment, created_by: experiment.created_by }
let(:task2) { create :my_module, :archived, experiment: experiment, created_by: experiment.created_by } let(:task2) { create :my_module, :archived, experiment: experiment, created_by: experiment.created_by }
let(:task3) { create :my_module, :archived, experiment: experiment, created_by: experiment.created_by } let(:task3) { create :my_module, :archived, experiment: experiment, created_by: experiment.created_by }
let(:user) { controller.current_user } let(:user) { controller.current_user }
before do
3.times do |i|
create_user_assignment(public_send("task#{i+1}"), role, user)
end
end
context 'when tasks are restored' do context 'when tasks are restored' do
it 'tasks are active' do it 'tasks are active' do
action action

View file

@ -31,12 +31,6 @@ describe ProjectFoldersController, type: :controller do
create :project, name: 'test project C', team: team, project_folder: project_folder_3, created_by: user create :project, name: 'test project C', team: team, project_folder: project_folder_3, created_by: user
end end
before do
3.times do |i|
create_user_assignment(public_send("project_#{i+1}"), role, user)
end
end
context 'in JSON format' do context 'in JSON format' do
let(:action) { post :move_to, params: params, format: :json } let(:action) { post :move_to, params: params, format: :json }
let(:params) do let(:params) do

View file

@ -26,9 +26,9 @@ describe ProtocolsController, type: :controller do
end end
describe 'GET export' do describe 'GET export' do
let(:protocol) { create :protocol, :in_public_repository, team: team } let(:protocol) { create :protocol, :in_public_repository, team: team, added_by: user }
let(:second_protocol) do let(:second_protocol) do
create :protocol, :in_public_repository, team: team create :protocol, :in_public_repository, team: team, added_by: user
end end
let(:params) { { protocol_ids: [protocol.id, second_protocol.id] } } let(:params) { { protocol_ids: [protocol.id, second_protocol.id] } }
let(:action) { get :export, params: params } let(:action) { get :export, params: params }
@ -48,7 +48,7 @@ describe ProtocolsController, type: :controller do
end end
describe 'GET export from MyModule' do describe 'GET export from MyModule' do
let(:protocol) { create :protocol, :in_public_repository, team: team } let(:protocol) { create :protocol, :in_public_repository, team: team, added_by: user }
let(:params) { { protocol_ids: [protocol.id], my_module_id: my_module.id } } let(:params) { { protocol_ids: [protocol.id], my_module_id: my_module.id } }
let(:action) { get :export, params: params } let(:action) { get :export, params: params }

View file

@ -7,11 +7,10 @@ describe RepositoriesController, type: :controller do
let!(:user) { controller.current_user } let!(:user) { controller.current_user }
let!(:team) { create :team, created_by: user } let!(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let(:action) { post :create, params: params, format: :json } let(:action) { post :create, params: params, format: :json }
describe 'index' do describe 'index' do
let(:repository) { create :repository, team: team } let(:repository) { create :repository, team: team, created_by: user }
let(:action) { get :index, format: :json } let(:action) { get :index, format: :json }
it 'correct JSON format' do it 'correct JSON format' do
@ -42,11 +41,12 @@ describe RepositoriesController, type: :controller do
end end
describe 'DELETE destroy' do describe 'DELETE destroy' do
let(:repository) { create :repository, team: team } let(:repository) { create :repository, team: team, created_by: user }
let(:params) { { id: repository.id, team_id: team.id } } let(:params) { { id: repository.id, team_id: team.id } }
let(:action) { delete :destroy, params: params } let(:action) { delete :destroy, params: params }
it 'calls create activity for deleting inventory' do it 'calls create activity for deleting inventory' do
repository.archive!(user)
expect(Activities::CreateActivityService) expect(Activities::CreateActivityService)
.to(receive(:call) .to(receive(:call)
.with(hash_including(activity_type: :delete_inventory))) .with(hash_including(activity_type: :delete_inventory)))
@ -55,13 +55,14 @@ describe RepositoriesController, type: :controller do
end end
it 'adds activity in DB' do it 'adds activity in DB' do
repository.archive!(user)
expect { action } expect { action }
.to(change { Activity.count }) .to(change { Activity.count })
end end
end end
describe 'PUT update' do describe 'PUT update' do
let(:repository) { create :repository, team: team } let(:repository) { create :repository, team: team, created_by: user }
let(:params) do let(:params) do
{ id: repository.id, team_id: team.id, repository: { name: 'new_name' } } { id: repository.id, team_id: team.id, repository: { name: 'new_name' } }
end end
@ -82,7 +83,7 @@ describe RepositoriesController, type: :controller do
end end
describe 'POST export_repository' do describe 'POST export_repository' do
let(:repository) { create :repository, team: team } let(:repository) { create :repository, team: team, created_by: user }
let(:repository_row) { create :repository_row, repository: repository } let(:repository_row) { create :repository_row, repository: repository }
let(:repository_column) do let(:repository_column) do
create :repository_column, repository: repository create :repository_column, repository: repository
@ -116,7 +117,7 @@ describe RepositoriesController, type: :controller do
end end
describe 'POST import_records' do describe 'POST import_records' do
let(:repository) { create :repository, team: team } let(:repository) { create :repository, team: team, created_by: user }
let(:mappings) do let(:mappings) do
{ '0': '-1', '1': '', '2': '', '3': '', '4': '', '5': '' } { '0': '-1', '1': '', '2': '', '3': '', '4': '', '5': '' }
end end

View file

@ -7,9 +7,11 @@ RSpec.describe RepositoryColumns::AssetColumnsController, type: :controller do
let(:user) { subject.current_user } let(:user) { subject.current_user }
let(:team) { create :team, created_by: user } let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let(:repository) { create :repository, created_by: user, team: team } let(:repository) { create :repository, created_by: user, team: team }
let(:repository_column) { create(:repository_column, :asset_type, repository: repository) } let(:repository_column) { create(:repository_column, :asset_type, repository: repository) }
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
let!(:viewer_role) { create :viewer_role }
let!(:team_assignment) { create_user_assignment(team, owner_role, user) }
describe 'POST repository_asset_columns, #create' do describe 'POST repository_asset_columns, #create' do
let(:action) { post :create, params: params } let(:action) { post :create, params: params }
@ -57,8 +59,7 @@ RSpec.describe RepositoryColumns::AssetColumnsController, type: :controller do
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do
@ -134,8 +135,7 @@ RSpec.describe RepositoryColumns::AssetColumnsController, type: :controller do
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do

View file

@ -7,9 +7,11 @@ RSpec.describe RepositoryColumns::ChecklistColumnsController, type: :controller
let(:user) { subject.current_user } let(:user) { subject.current_user }
let(:team) { create :team, created_by: user } let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let(:repository) { create :repository, created_by: user, team: team } let(:repository) { create :repository, created_by: user, team: team }
let(:repository_column) { create(:repository_column, :status_type, repository: repository) } let(:repository_column) { create(:repository_column, :status_type, repository: repository) }
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
let!(:viewer_role) { create :viewer_role }
let!(:team_assignment) { create_user_assignment(team, owner_role, user) }
describe 'POST repository_checklist_columns, #create' do describe 'POST repository_checklist_columns, #create' do
let(:action) { post :create, params: params } let(:action) { post :create, params: params }
@ -57,8 +59,7 @@ RSpec.describe RepositoryColumns::ChecklistColumnsController, type: :controller
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do
@ -134,8 +135,7 @@ RSpec.describe RepositoryColumns::ChecklistColumnsController, type: :controller
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do

View file

@ -7,9 +7,11 @@ RSpec.describe RepositoryColumns::DateTimeColumnsController, type: :controller d
let(:user) { subject.current_user } let(:user) { subject.current_user }
let(:team) { create :team, created_by: user } let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let(:repository) { create :repository, created_by: user, team: team } let(:repository) { create :repository, created_by: user, team: team }
let(:repository_column) { create(:repository_column, :date_time_type, repository: repository) } let(:repository_column) { create(:repository_column, :date_time_type, repository: repository) }
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
let!(:viewer_role) { create :viewer_role }
let!(:team_assignment) { create_user_assignment(team, owner_role, user) }
describe 'POST repository_date_time_columns, #create' do describe 'POST repository_date_time_columns, #create' do
let(:action) { post :create, params: params } let(:action) { post :create, params: params }
@ -58,8 +60,7 @@ RSpec.describe RepositoryColumns::DateTimeColumnsController, type: :controller d
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do
@ -135,8 +136,7 @@ RSpec.describe RepositoryColumns::DateTimeColumnsController, type: :controller d
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do

View file

@ -7,9 +7,11 @@ RSpec.describe RepositoryColumns::ListColumnsController, type: :controller do
let(:user) { subject.current_user } let(:user) { subject.current_user }
let(:team) { create :team, created_by: user } let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let(:repository) { create :repository, created_by: user, team: team } let(:repository) { create :repository, created_by: user, team: team }
let(:repository_column) { create(:repository_column, :status_type, repository: repository) } let(:repository_column) { create(:repository_column, :status_type, repository: repository) }
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
let!(:viewer_role) { create :viewer_role }
let!(:team_assignment) { create_user_assignment(team, owner_role, user) }
describe 'POST repository_list_columns, #create' do describe 'POST repository_list_columns, #create' do
let(:action) { post :create, params: params } let(:action) { post :create, params: params }
@ -57,8 +59,7 @@ RSpec.describe RepositoryColumns::ListColumnsController, type: :controller do
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do
@ -134,8 +135,7 @@ RSpec.describe RepositoryColumns::ListColumnsController, type: :controller do
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do

View file

@ -7,9 +7,11 @@ RSpec.describe RepositoryColumns::StatusColumnsController, type: :controller do
let(:user) { subject.current_user } let(:user) { subject.current_user }
let(:team) { create :team, created_by: user } let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let(:repository) { create :repository, created_by: user, team: team } let(:repository) { create :repository, created_by: user, team: team }
let(:repository_column) { create(:repository_column, :status_type, repository: repository) } let(:repository_column) { create(:repository_column, :status_type, repository: repository) }
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
let!(:viewer_role) { create :viewer_role }
let!(:team_assignment) { create_user_assignment(team, owner_role, user) }
describe 'POST repository_status_columns, #create' do describe 'POST repository_status_columns, #create' do
let(:action) { post :create, params: params } let(:action) { post :create, params: params }
@ -57,8 +59,7 @@ RSpec.describe RepositoryColumns::StatusColumnsController, type: :controller do
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do
@ -134,8 +135,7 @@ RSpec.describe RepositoryColumns::StatusColumnsController, type: :controller do
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do

View file

@ -7,9 +7,11 @@ RSpec.describe RepositoryColumns::TextColumnsController, type: :controller do
let(:user) { subject.current_user } let(:user) { subject.current_user }
let(:team) { create :team, created_by: user } let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let(:repository) { create :repository, created_by: user, team: team } let(:repository) { create :repository, created_by: user, team: team }
let(:repository_column) { create(:repository_column, :text_type, repository: repository) } let(:repository_column) { create(:repository_column, :text_type, repository: repository) }
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
let!(:viewer_role) { create :viewer_role }
let!(:team_assignment) { create_user_assignment(team, owner_role, user) }
describe 'POST repository_text_columns, #create' do describe 'POST repository_text_columns, #create' do
let(:action) { post :create, params: params } let(:action) { post :create, params: params }
@ -57,8 +59,7 @@ RSpec.describe RepositoryColumns::TextColumnsController, type: :controller do
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do
@ -134,8 +135,7 @@ RSpec.describe RepositoryColumns::TextColumnsController, type: :controller do
context 'when user does not have permissions' do context 'when user does not have permissions' do
before do before do
user_team.role = :guest repository.user_assignments.update(user_role: viewer_role)
user_team.save
end end
it 'respons with status 403' do it 'respons with status 403' do

View file

@ -7,7 +7,6 @@ describe RepositoryColumnsController, type: :controller do
let(:user) { subject.current_user } let(:user) { subject.current_user }
let(:team) { create :team, created_by: user } let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
let(:repository) { create :repository, created_by: user, team: team } let(:repository) { create :repository, created_by: user, team: team }
let(:repository_column) do let(:repository_column) do
create :repository_column, created_by: user, repository: repository create :repository_column, created_by: user, repository: repository

View file

@ -7,7 +7,7 @@ describe RepositoryRowsController, type: :controller do
render_views render_views
let!(:user) { controller.current_user } let!(:user) { controller.current_user }
let!(:team) { create :team, created_by: user } let!(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, team: team, user: user } let!(:viewer_role) { create :viewer_role }
let!(:repository) { create :repository, team: team, created_by: user } let!(:repository) { create :repository, team: team, created_by: user }
let!(:repository_state) do let!(:repository_state) do
RepositoryTableState.create( RepositoryTableState.create(
@ -23,8 +23,7 @@ describe RepositoryRowsController, type: :controller do
end end
let!(:user_two) { create :user, email: 'new@user.com' } let!(:user_two) { create :user, email: 'new@user.com' }
let!(:team_two) { create :team, created_by: user } let!(:team_two) { create :team, created_by: user_two }
let!(:user_team_two) { create :user_team, team: team_two, user: user_two }
let!(:repository_two) do let!(:repository_two) do
create :repository, team: team_two, created_by: user_two create :repository, team: team_two, created_by: user_two
end end
@ -236,21 +235,16 @@ describe RepositoryRowsController, type: :controller do
end end
context 'when does not have permission' do context 'when does not have permission' do
let!(:user_team) { create :user_team, :guest, team: second_team, user: user }
let(:second_team) { create :team, created_by: second_user }
let(:second_user) { create :user }
let(:repository) { create :repository, team: second_team, created_by: second_user }
context 'when guest' do context 'when guest' do
it 'renders 403' do it 'renders 403' do
repository.user_assignments.update(user_role: viewer_role)
action action
expect(response).to have_http_status(:forbidden) expect(response).to have_http_status(:forbidden)
end end
end end
context 'when does not see repository' do context 'when does not see repository' do
let(:repository) { create :repository, team: (create :team), created_by: second_user } let(:repository) { create :repository, team: team_two, created_by: user_two }
it 'renders 404' do it 'renders 404' do
action action
@ -305,21 +299,16 @@ describe RepositoryRowsController, type: :controller do
end end
context 'when does not have permission' do context 'when does not have permission' do
let!(:user_team) { create :user_team, :guest, team: second_team, user: user }
let(:second_team) { create :team, created_by: second_user }
let(:second_user) { create :user }
let(:repository) { create :repository, team: second_team, created_by: second_user }
context 'when guest' do context 'when guest' do
it 'renders 403' do it 'renders 403' do
repository.user_assignments.update(user_role: viewer_role)
action action
expect(response).to have_http_status(:forbidden) expect(response).to have_http_status(:forbidden)
end end
end end
context 'when does not see repository' do context 'when does not see repository' do
let(:repository) { create :repository, team: (create :team), created_by: second_user } let(:repository) { create :repository, team: team_two, created_by: user_two }
it 'renders 404' do it 'renders 404' do
action action

View file

@ -12,7 +12,6 @@ describe RepositoryStockValuesController, type: :controller do
login_user login_user
let!(:user) { controller.current_user } let!(:user) { controller.current_user }
let!(:team) { create :team, created_by: user } let!(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, team: team, user: user }
let!(:repository) { create :repository, team: team, created_by: user } let!(:repository) { create :repository, team: team, created_by: user }
let(:repository_column) do let(:repository_column) do
create :repository_column, :stock_type, created_by: user, repository: repository create :repository_column, :stock_type, created_by: user, repository: repository
@ -26,7 +25,7 @@ describe RepositoryStockValuesController, type: :controller do
let!(:repository_stock_unit_item) {create :repository_stock_unit_item, created_by: user, let!(:repository_stock_unit_item) {create :repository_stock_unit_item, created_by: user,
last_modified_by: user, last_modified_by: user,
repository_column: repository_column} repository_column: repository_column}
describe 'create' do describe 'create' do
let(:params) do { let(:params) do {
@ -35,7 +34,7 @@ describe RepositoryStockValuesController, type: :controller do
unit_item_id: repository_stock_unit_item.id, unit_item_id: repository_stock_unit_item.id,
comment: 'test', comment: 'test',
low_stock_threshold: '' low_stock_threshold: ''
}, },
operator: 'set', operator: 'set',
change_amount: 0, change_amount: 0,
repository_id: repository.id, repository_id: repository.id,

View file

@ -5,15 +5,13 @@ require 'rails_helper'
describe TeamRepositoriesController, type: :controller do describe TeamRepositoriesController, type: :controller do
login_user login_user
let(:user) { controller.current_user } include_context 'reference_project_structure'
let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team } let(:repository) { create :repository, team: team, created_by: user }
let(:repository) { create :repository, team: team } let(:target_team) { create :team, created_by: user}
let(:target_team) { create :team }
describe 'DELETE destroy' do describe 'DELETE destroy' do
let(:second_team) { create :team } let(:second_team) { create :team, created_by: user }
let!(:second_user_team) { create :user_team, user: user, team: second_team }
let(:team_repository) { create :team_repository, :read, team: second_team, repository: repository } let(:team_repository) { create :team_repository, :read, team: second_team, repository: repository }
context 'when resource can be deleted' do context 'when resource can be deleted' do
@ -49,12 +47,10 @@ describe TeamRepositoriesController, type: :controller do
end end
context 'when user do not have access to inventory' do context 'when user do not have access to inventory' do
let(:new_repository) { create :repository }
it 'renders 404' do it 'renders 404' do
delete :destroy, params: { repository_id: new_repository.id, team_id: team.id, id: team_repository.id } repository.user_assignments.update(user_role: viewer_role)
delete :destroy, params: { repository_id: repository.id, team_id: team.id, id: team_repository.id }
expect(response).to have_http_status(404) expect(response).to have_http_status(403)
end end
end end
end end

View file

@ -7,7 +7,6 @@ describe TeamsController, type: :controller do
let(:user) { subject.current_user } let(:user) { subject.current_user }
let(:team) { create :team, created_by: user } let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, user: user, team: team }
describe 'POST export_projects' do describe 'POST export_projects' do
let!(:first_project) { create :project, team: team, created_by: user } let!(:first_project) { create :project, team: team, created_by: user }

View file

@ -17,7 +17,7 @@ describe WopiController, type: :controller do
end end
let(:protocol_in_repository) { create :protocol, :in_public_repository, team: team } let(:protocol_in_repository) { create :protocol, :in_public_repository, team: team, added_by: user }
let(:step_in_repository) { create :step, protocol: protocol_in_repository, user: user } let(:step_in_repository) { create :step, protocol: protocol_in_repository, user: user }
let!(:asset) { create :asset } let!(:asset) { create :asset }
@ -81,7 +81,6 @@ describe WopiController, type: :controller do
describe 'Step asset in repository' do describe 'Step asset in repository' do
before do before do
step_asset_in_repository step_asset_in_repository
user_team
end end
it 'calls create activity for finish wopi editing' do it 'calls create activity for finish wopi editing' do

View file

@ -4,7 +4,7 @@ FactoryBot.define do
factory :repository do factory :repository do
sequence(:name) { |n| "My repository-#{n}" } sequence(:name) { |n| "My repository-#{n}" }
created_by { create :user } created_by { create :user }
team team { association :team, created_by: created_by }
trait :write_shared do trait :write_shared do
permission_level { :shared_write } permission_level { :shared_write }
end end

View file

@ -4,7 +4,7 @@ FactoryBot.define do
factory :repository_column do factory :repository_column do
sequence(:name) { |n| "My column-#{n}" } sequence(:name) { |n| "My column-#{n}" }
created_by { create :user } created_by { create :user }
repository repository { association :repository, created_by: created_by }
data_type { :RepositoryTextValue } data_type { :RepositoryTextValue }
trait :text_type do trait :text_type do

View file

@ -273,14 +273,4 @@ describe Experiment, type: :model do
end end
end end
end end
describe 'after_create_commit' do
it 'triggers the UserAssignments::GenerateUserAssignmentsJob job' do
new_experiment = build:experiment, project: experiment.project
expect(UserAssignments::GenerateUserAssignmentsJob).to receive(:perform_later).with(
new_experiment, new_experiment.created_by
)
new_experiment.save!
end
end
end end

View file

@ -106,15 +106,4 @@ describe MyModule, type: :model do
it { is_expected.to validate_presence_of :experiment } it { is_expected.to validate_presence_of :experiment }
end end
end end
describe 'after_create_commit' do
it 'triggers the UserAssignments::GenerateUserAssignmentsJob job' do
experiment = create :experiment
new_my_module = build :my_module, experiment: experiment
expect(UserAssignments::GenerateUserAssignmentsJob).to receive(:perform_later).with(
new_my_module, new_my_module.created_by
)
new_my_module.save!
end
end
end end

View file

@ -58,8 +58,9 @@ describe Protocol, type: :model do
end end
describe '.archive(user)' do describe '.archive(user)' do
let(:protocol) { create :protocol, :in_public_repository, added_by: user }
let(:user) { create :user } let(:user) { create :user }
let(:team) { create :team, created_by: user }
let(:protocol) { create :protocol, :in_public_repository, team: team, added_by: user }
it 'calls create activity for archiving protocol' do it 'calls create activity for archiving protocol' do
expect(Activities::CreateActivityService) expect(Activities::CreateActivityService)
@ -76,8 +77,9 @@ describe Protocol, type: :model do
end end
describe '.restore(user)' do describe '.restore(user)' do
let(:protocol) { create :protocol, :in_public_repository, added_by: user }
let(:user) { create :user } let(:user) { create :user }
let(:team) { create :team, created_by: user }
let(:protocol) { create :protocol, :in_public_repository, team: team, added_by: user }
it 'calls create activity for restoring protocol' do it 'calls create activity for restoring protocol' do
expect(Activities::CreateActivityService) expect(Activities::CreateActivityService)
@ -94,8 +96,9 @@ describe Protocol, type: :model do
end end
describe '.publish(user)' do describe '.publish(user)' do
let(:protocol) { create :protocol, :in_public_repository, added_by: user }
let(:user) { create :user } let(:user) { create :user }
let(:team) { create :team, created_by: user }
let(:protocol) { create :protocol, :in_public_repository, team: team, added_by: user }
it 'calls create activity for restoring protocol' do it 'calls create activity for restoring protocol' do
expect(Activities::CreateActivityService) expect(Activities::CreateActivityService)
@ -112,8 +115,9 @@ describe Protocol, type: :model do
end end
describe '.make_private(user)' do describe '.make_private(user)' do
let(:protocol) { create :protocol, :in_public_repository, added_by: user }
let(:user) { create :user } let(:user) { create :user }
let(:team) { create :team, created_by: user }
let(:protocol) { create :protocol, :in_public_repository, team: team, added_by: user }
it 'calls create activity for restoring protocol' do it 'calls create activity for restoring protocol' do
expect(Activities::CreateActivityService) expect(Activities::CreateActivityService)
@ -130,8 +134,9 @@ describe Protocol, type: :model do
end end
describe '.deep_clone_repository' do describe '.deep_clone_repository' do
let(:protocol) { create :protocol, :in_public_repository, added_by: user }
let(:user) { create :user } let(:user) { create :user }
let(:team) { create :team, created_by: user }
let(:protocol) { create :protocol, :in_public_repository, team: team, added_by: user }
it 'calls create activity for protocol copy to repository' do it 'calls create activity for protocol copy to repository' do
expect(Activities::CreateActivityService) expect(Activities::CreateActivityService)

View file

@ -47,10 +47,12 @@ describe Repository, type: :model do
end end
describe 'Scopes' do describe 'Scopes' do
let(:user) { create :user }
describe '#active and #archived' do describe '#active and #archived' do
before do before do
create :repository create :repository, created_by: user
create :repository, :archived create :repository, :archived, created_by: user
end end
it 'returns only active rows' do it 'returns only active rows' do
@ -68,28 +70,30 @@ describe Repository, type: :model do
end end
describe '.copy' do describe '.copy' do
let(:created_by) { create :user } let(:user) { create :user }
let(:repository) { create :repository } let(:repository) { create :repository, created_by: user }
it 'calls create activity for copying inventory' do it 'calls create activity for copying inventory' do
expect(Activities::CreateActivityService) expect(Activities::CreateActivityService)
.to(receive(:call).with(hash_including(activity_type: :copy_inventory))) .to(receive(:call).with(hash_including(activity_type: :copy_inventory)))
repository.copy(created_by, 'name for copied repo') repository.copy(user, 'name for copied repo')
end end
it 'adds activity in DB' do it 'adds activity in DB' do
expect { repository.copy(created_by, 'name for copied repo') } expect { repository.copy(user, 'name for copied repo') }
.to(change { Activity.count }) .to(change { Activity.count })
end end
end end
describe '.within_global_limits?' do describe '.within_global_limits?' do
let(:user) { create :user }
context 'when have an archived repository' do context 'when have an archived repository' do
before do before do
Rails.configuration.x.global_repositories_limit = 2 Rails.configuration.x.global_repositories_limit = 2
create :repository create :repository, created_by: user
create :repository, :archived create :repository, :archived, created_by: user
end end
after do after do
@ -103,18 +107,20 @@ describe Repository, type: :model do
end end
describe '.within_team_limits?' do describe '.within_team_limits?' do
let(:user) { create :user }
context 'when have an archived repository' do context 'when have an archived repository' do
before do before do
Rails.configuration.x.team_repositories_limit = 2 Rails.configuration.x.team_repositories_limit = 2
create :repository, team: team create :repository, team: team, created_by: user
create :repository, :archived, team: team create :repository, :archived, team: team, created_by: user
end end
after do after do
Rails.configuration.x.team_repositories_limit = 0 Rails.configuration.x.team_repositories_limit = 0
end end
let(:team) { create :team } let(:team) { create :team, created_by: user }
it 'includes archived repositories in condition and returns false' do it 'includes archived repositories in condition and returns false' do
expect(described_class.within_team_limits?(team)).to be_falsey expect(described_class.within_team_limits?(team)).to be_falsey

View file

@ -3,9 +3,9 @@
require 'rails_helper' require 'rails_helper'
describe RepositoryStockValue, type: :model do describe RepositoryStockValue, type: :model do
let(:repository_stock_value) { build :repository_stock_value }
let(:repository) { build :repository }
let(:user) { build :user } let(:user) { build :user }
let(:repository) { build :repository }
let(:repository_stock_value) { build :repository_stock_value }
it 'is valid' do it 'is valid' do
@ -36,21 +36,27 @@ describe RepositoryStockValue, type: :model do
describe 'Saving stock value' do describe 'Saving stock value' do
let(:repository_stock_value1) { build :repository_stock_value }
it 'Save stock value' do it 'Save stock value' do
expect { repository_stock_value.save }.to change(RepositoryStockValue, :count).by(1) expect { repository_stock_value.save }.to change(RepositoryStockValue, :count).by(1)
end end
it 'Updating stock value' do it 'Updating stock value' do
repository_stock_unit_item =
create :repository_stock_unit_item, repository_column: repository_stock_value.repository_cell.repository_column
repository_stock_value.repository_cell.repository_column.reload
repository_stock_value.save repository_stock_value.save
expect { repository_stock_value.update_data!({amount: 10, low_stock_threshold:''}, user) } expect { repository_stock_value.update_data!(
.to change(RepositoryStockValue, :count).by(0) { amount: 10, low_stock_threshold: '', unit_item_id: repository_stock_unit_item.id }, user
) }
.to change(RepositoryStockValue, :count).by(0)
end end
it 'Updating stock value with ledger' do it 'Updating stock value with ledger' do
repository_stock_value.save repository_stock_unit_item =
expect { repository_stock_value.update_stock_with_ledger!(10, repository, "") } create :repository_stock_unit_item, repository_column: repository_stock_value.repository_cell.repository_column
.to (change(RepositoryLedgerRecord, :count).by(1)) repository_stock_unit_item.save
repository_stock_value.repository_stock_unit_item = repository_stock_unit_item
expect { repository_stock_value.save }.to (change(RepositoryLedgerRecord, :count).by(1))
end end
end end
end end

View file

@ -3,10 +3,13 @@
require 'rails_helper' require 'rails_helper'
describe TeamRepository, type: :model do describe TeamRepository, type: :model do
let(:team_repository) { build :team_repository, :read } let(:user) { create :user }
let(:team) { create :team, created_by: user }
let(:another_team) { create :team, created_by: user }
let(:repository) { create :repository, team: team, created_by: user }
let(:team_repository) { build :team_repository, :read, team: another_team, repository: repository }
it 'is valid' do it 'is valid' do
team_repository.team = create :team
expect(team_repository).to be_valid expect(team_repository).to be_valid
end end
@ -21,7 +24,7 @@ describe TeamRepository, type: :model do
it { expect(team_repository).to validate_uniqueness_of(:repository).scoped_to(:team_id) } it { expect(team_repository).to validate_uniqueness_of(:repository).scoped_to(:team_id) }
it 'invalid when repo team is same as sharring team' do it 'invalid when repo team is same as sharring team' do
repo = create :repository repo = create :repository, team: team, created_by: user
invalid_team_repository = build :team_repository, :read, repository: repo, team: repo.team invalid_team_repository = build :team_repository, :read, repository: repo, team: repo.team
expect(invalid_team_repository).to be_invalid expect(invalid_team_repository).to be_invalid

View file

@ -32,7 +32,7 @@ describe Api::V1::ProjectsController, type: :controller do
login_api_user login_api_user
include_context 'reference_project_structure', { include_context 'reference_project_structure', {
team_role: :guest team_role: :viewer
} }
it_behaves_like "a controller action with permissions checking", :get, :show do it_behaves_like "a controller action with permissions checking", :get, :show do
@ -48,7 +48,7 @@ describe Api::V1::ProjectsController, type: :controller do
it_behaves_like "a controller action with permissions checking", :post, :create do it_behaves_like "a controller action with permissions checking", :post, :create do
let(:testable) { project } let(:testable) { project }
let(:permissions) { [] } let(:permissions) { [TeamPermissions::PROJECTS_CREATE] }
let(:action_params) { let(:action_params) {
{ {
team_id: team.id, team_id: team.id,

View file

@ -96,7 +96,7 @@ describe Api::V1::StepsController, type: :controller do
it_behaves_like "a controller action with permissions checking", :put, :update do it_behaves_like "a controller action with permissions checking", :put, :update do
let(:testable) { my_module } let(:testable) { my_module }
let(:permissions) { [MyModulePermissions::PROTOCOL_MANAGE] } let(:permissions) { [MyModulePermissions::STEPS_MANAGE] }
let(:action_params) { let(:action_params) {
{ {
team_id: team.id, team_id: team.id,

View file

@ -18,6 +18,13 @@ describe Api::V1::TaskInventoryItemsController, type: :controller do
experiment_id: 1, experiment_id: 1,
task_id: 1, task_id: 1,
id: 1 id: 1
},
update: {
team_id: 1,
project_id: 1,
experiment_id: 1,
task_id: 1,
id: 1
} }
}, [], }, [],
:unauthorized :unauthorized

View file

@ -13,17 +13,17 @@ describe MyModuleRepositoriesController, type: :controller do
repositories_list_html: { my_module_id: 1 }, repositories_list_html: { my_module_id: 1 },
full_view_table: { my_module_id: 1, id: 1 }, full_view_table: { my_module_id: 1, id: 1 },
repositories_dropdown_list: { my_module_id: 1 }, repositories_dropdown_list: { my_module_id: 1 },
export_repository: { my_module_id: 1, id: 1 } export_repository: { my_module_id: 1, id: 1 },
consume_modal: { my_module_id: 1, id: 1 },
update_consumption: { my_module_id: 1, id: 1 }
}, [] }, []
login_user login_user
describe 'permissions checking' do describe 'permissions checking' do
include_context 'reference_project_structure', { include_context 'reference_project_structure'
team_role: :normal_user
}
let(:repository) { create :repository, team: team } let(:repository) { create :repository, team: team, created_by: user }
let (:repository_row) { create :repository_row, repository: repository, created_by: user, last_modified_by: user } let (:repository_row) { create :repository_row, repository: repository, created_by: user, last_modified_by: user }

View file

@ -24,7 +24,7 @@ describe MyModuleRepositorySnapshotsController, type: :controller do
team_role: :normal_user team_role: :normal_user
} }
let(:repository) { create :repository, team: team } let(:repository) { create :repository, team: team, created_by: user }
let (:repository_row) { create :repository_row, repository: repository, created_by: user, last_modified_by: user } let (:repository_row) { create :repository_row, repository: repository, created_by: user, last_modified_by: user }
let (:repository_snapshot) { create :repository_snapshot, original_repository: repository, my_module: my_module } let (:repository_snapshot) { create :repository_snapshot, original_repository: repository, my_module: my_module }

View file

@ -19,7 +19,8 @@ describe MyModulesController, type: :controller do
results: { id: 1 }, results: { id: 1 },
archive: { id: 1 }, archive: { id: 1 },
restore_group: { id: 1 }, restore_group: { id: 1 },
update_state: { id: 1 } update_state: { id: 1 },
canvas_dropdown_menu: { id: 1 }
}, [] }, []
login_user login_user
@ -124,5 +125,11 @@ describe MyModulesController, type: :controller do
end end
end end
end end
it_behaves_like "a controller action with permissions checking", :get, :canvas_dropdown_menu do
let(:testable) { my_module }
let(:permissions) { [MyModulePermissions::READ] }
let(:action_params) { { id: my_module.id } }
end
end end
end end

View file

@ -1,120 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'RepositoryPermissions' do
include Canaid::Helpers::PermissionsHelper
let(:user) { create :user, current_team_id: team.id }
let(:repository) { build :repository, team: team }
let(:team) { create :team }
let(:write_shared_repository) { create :repository, :write_shared }
let(:read_shared_repository) { create :repository, :read_shared }
describe 'create_repository_rows, manage_repository_rows, create_repository_columns' do
context 'when team\'s repository' do
it 'should be true for admin' do
create :user_team, :admin, user: user, team: team
expect(can_create_repository_rows?(user, repository)).to be_truthy
end
it 'should be true for normal_user' do
create :user_team, :normal_user, user: user, team: team
expect(can_create_repository_rows?(user, repository)).to be_truthy
end
it 'should be false for guest' do
create :user_team, :guest, user: user, team: team
expect(can_create_repository_rows?(user, repository)).to be_falsey
end
end
context 'when shared repository' do
let(:new_team) { create :team }
let(:new_repository) { create :repository, team: new_team }
it 'should be true when have sharred repo with write' do
create :user_team, :normal_user, user: user, team: team
create :team_repository, :write, team: team, repository: new_repository
expect(can_create_repository_rows?(user, new_repository)).to be_truthy
end
it 'should be false when have sharred repo with read' do
create :user_team, :normal_user, user: user, team: team
create :team_repository, :read, team: team, repository: new_repository
expect(can_create_repository_rows?(user, new_repository)).to be_falsey
end
it 'should be false when do not have sharred repo' do
create :user_team, :normal_user, user: user, team: team
create :team_repository, :read, team: team
expect(can_create_repository_rows?(user, new_repository)).to be_falsey
end
it 'should be false when have sharred repo with write but user is guest' do
create :user_team, :guest, user: user, team: team
create :team_repository, :write, team: team, repository: new_repository
expect(can_create_repository_rows?(user, new_repository)).to be_falsey
end
end
context 'when shared with all organization' do
it 'should be true when repo has write permission' do
create :user_team, :normal_user, user: user, team: team
allow_any_instance_of(User).to receive(:current_team).and_return(team)
expect(can_create_repository_rows?(user, write_shared_repository)).to be_truthy
end
it 'should be false when repo has read permission' do
expect(can_create_repository_rows?(user, read_shared_repository)).to be_falsey
end
end
end
describe 'read_repository' do
context 'when team\'s repository' do
it 'should be true' do
create :user_team, :normal_user, user: user, team: team
expect(can_read_repository?(user, repository)).to be_truthy
end
end
context 'when shared repository' do
let(:new_team) { create :team }
let(:new_repository) { create :repository, team: new_team }
it 'should be true when have sharred repo with read' do
create :user_team, :normal_user, user: user, team: team
create :team_repository, :read, team: team, repository: new_repository
expect(can_read_repository?(user, new_repository)).to be_truthy
end
it 'should be false when do not have sharred repo' do
create :user_team, :normal_user, user: user, team: team
create :team_repository, :read, team: team
expect(can_read_repository?(user, new_repository)).to be_falsey
end
end
context 'when shared with all organization' do
it 'should be true when repo has write permission' do
expect(can_read_repository?(user, write_shared_repository)).to be_truthy
end
it 'should be true when repo has read permission' do
expect(can_read_repository?(user, read_shared_repository)).to be_truthy
end
end
end
end

View file

@ -1,52 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'ResultPermissions' do
include Canaid::Helpers::PermissionsHelper
let(:user) { create :user, current_team_id: team.id }
let(:team) { create :team }
let(:result) { create :result, user: user, my_module: my_module }
let(:my_module) { create :my_module, experiment: experiment, created_by: experiment.created_by }
let(:experiment) { create :experiment, user: user }
before do
create_user_assignment(my_module, UserRole.find_by(name: I18n.t('user_roles.predefined.owner')), user)
end
describe 'can_read_result?' do
it 'should be true for active result' do
expect(can_read_result?(user, result)).to be_truthy
end
it 'should be true for archived result' do
result.archive!(user)
expect(can_read_result?(user, result)).to be_truthy
end
it 'should be true for archived experiment' do
experiment.update(archived_on: Time.zone.now, archived_by: user)
expect(can_read_result?(user, result)).to be_truthy
end
end
describe 'can_manage_result?' do
it 'should be true for active result' do
expect(can_manage_result?(user, result)).to be_truthy
end
it 'should be false for archived result' do
result.archive!(user)
expect(can_manage_result?(user, result)).to be_falsey
end
it 'should be false for archived experiment' do
experiment.update(archived_on: Time.zone.now, archived_by: user, archived: true)
expect(can_manage_result?(user, result)).to be_falsey
end
end
end

View file

@ -6,8 +6,6 @@ RSpec.describe "Api::Service::ExperimentsController", type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@team = create(:team, created_by: @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) @valid_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@unaccessible_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team) @unaccessible_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@unaccessible_project.user_assignments.destroy_all @unaccessible_project.user_assignments.destroy_all

View file

@ -12,8 +12,6 @@ RSpec.describe 'Api::V1::AssetsController', type: :request do
@protocol = create(:protocol, my_module: @task) @protocol = create(:protocol, my_module: @task)
@step = create(:step, protocol: @protocol) @step = create(:step, protocol: @protocol)
create(:user_team, user: @user, team: @team)
create_user_assignment(@task, UserRole.find_by(name: I18n.t('user_roles.predefined.owner')), @user) create_user_assignment(@task, UserRole.find_by(name: I18n.t('user_roles.predefined.owner')), @user)
@valid_headers = @valid_headers =

View file

@ -12,7 +12,6 @@ RSpec.describe 'Api::V1::ChecklistsController', type: :request do
@protocol = create(:protocol, my_module: @task) @protocol = create(:protocol, my_module: @task)
@step = create(:step, protocol: @protocol) @step = create(:step, protocol: @protocol)
@checklist = create(:checklist, step: @step) @checklist = create(:checklist, step: @step)
create(:user_team, user: @user, team: @team)
@valid_headers = { @valid_headers = {
'Authorization': 'Bearer ' + generate_token(@user.id), 'Authorization': 'Bearer ' + generate_token(@user.id),

View file

@ -11,7 +11,6 @@ RSpec.describe 'Api::V1::ChecklistsController', type: :request do
@task = @experiment.my_modules.first @task = @experiment.my_modules.first
@protocol = create(:protocol, my_module: @task) @protocol = create(:protocol, my_module: @task)
@step = create(:step, protocol: @protocol) @step = create(:step, protocol: @protocol)
create(:user_team, user: @user, team: @team)
@valid_headers = { @valid_headers = {
'Authorization': 'Bearer ' + generate_token(@user.id), 'Authorization': 'Bearer ' + generate_token(@user.id),
'Content-Type': 'application/json' 'Content-Type': 'application/json'

View file

@ -7,23 +7,24 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
MyModuleStatusFlow.ensure_default MyModuleStatusFlow.ensure_default
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @another_user = create(:user)
create(:user_team, user: @user, team: @teams.first, role: 2) @team1 = create(:team, created_by: @user)
@team2 = create(:team, created_by: @another_user)
@valid_project = create(:project, name: Faker::Name.unique.name, @valid_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
@unaccessible_project = create(:project, name: Faker::Name.unique.name, @unaccessible_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @another_user, team: @team2)
@valid_experiment = create(:experiment, created_by: @user, @valid_experiment = create(:experiment, created_by: @user,
last_modified_by: @user, project: @valid_project) last_modified_by: @user, project: @valid_project)
@unaccessible_experiment = create(:experiment, created_by: @user, @unaccessible_experiment = create(:experiment, created_by: @another_user,
last_modified_by: @user, project: @unaccessible_project) last_modified_by: @another_user, project: @unaccessible_project)
create_list(:my_module, 3, :with_due_date, created_by: @user, create_list(:my_module, 3, :with_due_date, created_by: @user,
last_modified_by: @user, experiment: @valid_experiment) last_modified_by: @user, experiment: @valid_experiment)
create_list(:my_module, 3, :with_due_date, created_by: @user, create_list(:my_module, 3, :with_due_date, created_by: @another_user,
last_modified_by: @user, experiment: @unaccessible_experiment) last_modified_by: @another_user, experiment: @unaccessible_experiment)
MyModule.where(experiment: @valid_experiment).each_slice(2) do |input_my_module, output_my_module| MyModule.where(experiment: @valid_experiment).each_slice(2) do |input_my_module, output_my_module|
Connection.create( Connection.create(
@ -40,7 +41,7 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
it 'Response with correct connections' do it 'Response with correct connections' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_connections_path( get api_v1_team_project_experiment_connections_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment experiment_id: @valid_experiment
), headers: @valid_headers ), headers: @valid_headers
@ -57,7 +58,7 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
it 'When invalid request, experiment from another project' do it 'When invalid request, experiment from another project' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_connections_path( get api_v1_team_project_experiment_connections_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @unaccessible_experiment experiment_id: @unaccessible_experiment
), headers: @valid_headers ), headers: @valid_headers
@ -69,7 +70,7 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_connections_path( get api_v1_team_project_experiment_connections_path(
team_id: @teams.second.id, team_id: @team2.id,
project_id: @unaccessible_project, project_id: @unaccessible_project,
experiment_id: @unaccessible_experiment experiment_id: @unaccessible_experiment
), headers: @valid_headers ), headers: @valid_headers
@ -81,7 +82,7 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
it 'When invalid request, non existing experiment' do it 'When invalid request, non existing experiment' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_connections_path( get api_v1_team_project_experiment_connections_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: -1 experiment_id: -1
), headers: @valid_headers ), headers: @valid_headers
@ -95,7 +96,7 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
it 'When valid request, user can read connection' do it 'When valid request, user can read connection' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_connection_path( get api_v1_team_project_experiment_connection_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
id: @valid_experiment.connections.first.id id: @valid_experiment.connections.first.id
@ -113,7 +114,7 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_connection_path( get api_v1_team_project_experiment_connection_path(
team_id: @teams.second.id, team_id: @team2.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
id: @valid_experiment.my_modules.first.id id: @valid_experiment.my_modules.first.id
@ -126,7 +127,7 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
it 'When invalid request, non existing connection' do it 'When invalid request, non existing connection' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_connection_path( get api_v1_team_project_experiment_connection_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
id: -1 id: -1
@ -139,7 +140,7 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
it 'When invalid request, connection from unaccessible experiment' do it 'When invalid request, connection from unaccessible experiment' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_connection_path( get api_v1_team_project_experiment_connection_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
id: @unaccessible_experiment.my_modules.first.id id: @unaccessible_experiment.my_modules.first.id
@ -171,7 +172,7 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
context 'when has valid params' do context 'when has valid params' do
let(:action) do let(:action) do
post(api_v1_team_project_experiment_connections_path( post(api_v1_team_project_experiment_connections_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project.id, project_id: @valid_project.id,
experiment_id: @valid_experiment.id experiment_id: @valid_experiment.id
), ),
@ -202,7 +203,7 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
context 'when has not valid params' do context 'when has not valid params' do
it 'renders 404 when project not found' do it 'renders 404 when project not found' do
post(api_v1_team_project_experiment_connections_path( post(api_v1_team_project_experiment_connections_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: -1, project_id: -1,
experiment_id: @valid_experiment.id experiment_id: @valid_experiment.id
), ),
@ -214,7 +215,7 @@ RSpec.describe 'Api::V1::ConnectionsController', type: :request do
it 'renders 403 when user is not member of the team' do it 'renders 403 when user is not member of the team' do
post(api_v1_team_project_experiment_connections_path( post(api_v1_team_project_experiment_connections_path(
team_id: @teams.second.id, team_id: @team2.id,
project_id: @valid_project.id, project_id: @valid_project.id,
experiment_id: @valid_experiment.id experiment_id: @valid_experiment.id
), ),

View file

@ -9,8 +9,7 @@ RSpec.describe "Api::V1::ExperimentUserAssignmentsController", type: :request do
@team = create(:team, created_by: @user) @team = create(:team, created_by: @user)
@normal_user_role = create :normal_user_role @normal_user_role = create :normal_user_role
@owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) @owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
create(:user_team, user: @user, team: @team, role: :normal_user) create_user_assignment(@team, @normal_user_role, @another_user)
create(:user_team, user: @another_user, team: @team, role: :normal_user)
@own_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team) @own_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@own_experiment = create :experiment, @own_experiment = create :experiment,
name: Faker::Name.unique.name, name: Faker::Name.unique.name,

View file

@ -5,14 +5,15 @@ require 'rails_helper'
RSpec.describe "Api::V1::ExperimentsController", type: :request do RSpec.describe "Api::V1::ExperimentsController", type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @another_user = create(:user)
create(:user_team, user: @user, team: @teams.first, role: 2) @team1 = create(:team, created_by: @user)
@team2 = create(:team, created_by: @another_user)
@valid_project = create(:project, name: Faker::Name.unique.name, @valid_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
@unaccessible_project = create(:project, name: Faker::Name.unique.name, @unaccessible_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @user, team: @team2)
create_list(:experiment, 3, created_by: @user, last_modified_by: @user, create_list(:experiment, 3, created_by: @user, last_modified_by: @user,
project: @valid_project) project: @valid_project)
@ -26,7 +27,7 @@ RSpec.describe "Api::V1::ExperimentsController", type: :request do
describe 'GET experiments, #index' do describe 'GET experiments, #index' do
it 'Response with correct experiments' do it 'Response with correct experiments' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiments_path(team_id: @teams.first.id, get api_v1_team_project_experiments_path(team_id: @team1.id,
project_id: @valid_project), headers: @valid_headers project_id: @valid_project), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
@ -41,7 +42,7 @@ RSpec.describe "Api::V1::ExperimentsController", type: :request do
it 'When invalid request, project from another team' do it 'When invalid request, project from another team' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiments_path(team_id: @teams.second.id, get api_v1_team_project_experiments_path(team_id: @team2.id,
project_id: @valid_project), headers: @valid_headers project_id: @valid_project), headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -50,7 +51,7 @@ RSpec.describe "Api::V1::ExperimentsController", type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiments_path(team_id: @teams.second.id, get api_v1_team_project_experiments_path(team_id: @team2.id,
project_id: @unaccessible_project), headers: @valid_headers project_id: @unaccessible_project), headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -59,7 +60,7 @@ RSpec.describe "Api::V1::ExperimentsController", type: :request do
it 'When invalid request, non existing project' do it 'When invalid request, non existing project' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiments_path(team_id: @teams.first.id, get api_v1_team_project_experiments_path(team_id: @team1.id,
project_id: -1), headers: @valid_headers project_id: -1), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -70,7 +71,7 @@ RSpec.describe "Api::V1::ExperimentsController", type: :request do
describe 'GET experiment, #show' do describe 'GET experiment, #show' do
it 'When valid request, user can read experiment' do it 'When valid request, user can read experiment' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_path(team_id: @teams.first.id, get api_v1_team_project_experiment_path(team_id: @team1.id,
project_id: @valid_project, id: @valid_project.experiments.first.id), project_id: @valid_project, id: @valid_project.experiments.first.id),
headers: @valid_headers headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -87,7 +88,7 @@ RSpec.describe "Api::V1::ExperimentsController", type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_path( get api_v1_team_project_experiment_path(
team_id: @teams.second.id, team_id: @team2.id,
project_id: @unaccessible_project, project_id: @unaccessible_project,
id: @unaccessible_project.experiments.first.id id: @unaccessible_project.experiments.first.id
), headers: @valid_headers ), headers: @valid_headers
@ -98,7 +99,7 @@ RSpec.describe "Api::V1::ExperimentsController", type: :request do
it 'When invalid request, non existing experiment' do it 'When invalid request, non existing experiment' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_path(team_id: @teams.first.id, get api_v1_team_project_experiment_path(team_id: @team1.id,
project_id: @valid_project, id: -1), project_id: @valid_project, id: -1),
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
@ -109,7 +110,7 @@ RSpec.describe "Api::V1::ExperimentsController", type: :request do
it 'When invalid request, experiment from another project' do it 'When invalid request, experiment from another project' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_path( get api_v1_team_project_experiment_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
id: @unaccessible_project.experiments.first.id id: @unaccessible_project.experiments.first.id
), headers: @valid_headers ), headers: @valid_headers
@ -121,7 +122,7 @@ RSpec.describe "Api::V1::ExperimentsController", type: :request do
it 'When invalid request, experiment from unaccessible project' do it 'When invalid request, experiment from unaccessible project' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_path( get api_v1_team_project_experiment_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @unaccessible_project, project_id: @unaccessible_project,
id: @unaccessible_project.experiments.first.id id: @unaccessible_project.experiments.first.id
), headers: @valid_headers ), headers: @valid_headers

View file

@ -5,20 +5,21 @@ require 'rails_helper'
RSpec.describe 'Api::V1::InventoriesController', type: :request do RSpec.describe 'Api::V1::InventoriesController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @another_user = create(:user)
create(:user_team, user: @user, team: @teams.first, role: 2) @team1 = create(:team, created_by: @user)
@team2 = create(:team, created_by: @another_user)
# valid_inventories # valid_inventories
create(:repository, name: Faker::Name.unique.name, create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
create(:repository, name: Faker::Name.unique.name, create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
# unaccessable_inventories # unaccessable_inventories
create(:repository, name: Faker::Name.unique.name, create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @another_user, team: @team2)
create(:repository, name: Faker::Name.unique.name, create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @another_user, team: @team2)
@valid_headers = @valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) } { 'Authorization': 'Bearer ' + generate_token(@user.id) }
@ -27,13 +28,13 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
describe 'GET inventories, #index' do describe 'GET inventories, #index' do
it 'Response with correct inventories' do it 'Response with correct inventories' do
hash_body = nil hash_body = nil
get api_v1_team_inventories_path(team_id: @teams.first.id), get api_v1_team_inventories_path(team_id: @team1.id),
headers: @valid_headers headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
JSON.parse( JSON.parse(
ActiveModelSerializers::SerializableResource ActiveModelSerializers::SerializableResource
.new(@teams.first.repositories, each_serializer: Api::V1::InventorySerializer) .new(@team1.repositories, each_serializer: Api::V1::InventorySerializer)
.to_json .to_json
)['data'] )['data']
) )
@ -41,7 +42,7 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_inventories_path(team_id: @teams.second.id), get api_v1_team_inventories_path(team_id: @team2.id),
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -50,13 +51,13 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
context 'when have some archived inventories' do context 'when have some archived inventories' do
before do before do
create(:repository, :archived, name: Faker::Name.unique.name, created_by: @user, team: @teams.first) create(:repository, :archived, name: Faker::Name.unique.name, created_by: @user, team: @team1)
end end
it 'will ignore them' do it 'will ignore them' do
hash_body = nil hash_body = nil
get api_v1_team_inventories_path(team_id: @teams.first.id), headers: @valid_headers get api_v1_team_inventories_path(team_id: @team1.id), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body['data'].count).to be_eql 2 expect(hash_body['data'].count).to be_eql 2
@ -67,15 +68,15 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
describe 'GET inventory, #show' do describe 'GET inventory, #show' do
it 'When valid request, user is member of the team' do it 'When valid request, user is member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_path(team_id: @teams.first.id, get api_v1_team_inventory_path(team_id: @team1.id,
id: @teams.first.repositories.first.id), id: @team1.repositories.first.id),
headers: @valid_headers headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
JSON.parse( JSON.parse(
ActiveModelSerializers::SerializableResource ActiveModelSerializers::SerializableResource
.new(@teams.first.repositories.first, serializer: Api::V1::InventorySerializer) .new(@team1.repositories.first, serializer: Api::V1::InventorySerializer)
.to_json .to_json
)['data'] )['data']
) )
@ -83,8 +84,8 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_path(team_id: @teams.second.id, get api_v1_team_inventory_path(team_id: @team2.id,
id: @teams.second.repositories.first.id), id: @team2.repositories.first.id),
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -93,7 +94,7 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
it 'When invalid request, non existing inventory' do it 'When invalid request, non existing inventory' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_path(team_id: @teams.first.id, id: -1), get api_v1_team_inventory_path(team_id: @team1.id, id: -1),
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -102,8 +103,8 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_path(team_id: @teams.first.id, get api_v1_team_inventory_path(team_id: @team1.id,
id: @teams.second.repositories.first.id), id: @team2.repositories.first.id),
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -124,7 +125,7 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
it 'Response with correct inventory' do it 'Response with correct inventory' do
hash_body = nil hash_body = nil
post api_v1_team_inventories_path( post api_v1_team_inventories_path(
team_id: @teams.first.id team_id: @team1.id
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
expect(response).to have_http_status 201 expect(response).to have_http_status 201
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -139,7 +140,7 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
post api_v1_team_inventories_path( post api_v1_team_inventories_path(
team_id: @teams.second.id team_id: @team2.id
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
expect(response).to have_http_status 403 expect(response).to have_http_status 403
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -161,7 +162,7 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
invalid_request_body[:data][:type] = 'repository_rows' invalid_request_body[:data][:type] = 'repository_rows'
hash_body = nil hash_body = nil
post api_v1_team_inventories_path( post api_v1_team_inventories_path(
team_id: @teams.first.id team_id: @team1.id
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception expect { hash_body = json }.to_not raise_exception
@ -171,7 +172,7 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
it 'When invalid request, missing data param' do it 'When invalid request, missing data param' do
hash_body = nil hash_body = nil
post api_v1_team_inventories_path( post api_v1_team_inventories_path(
team_id: @teams.first.id team_id: @team1.id
), params: {}, headers: @valid_headers ), params: {}, headers: @valid_headers
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception expect { hash_body = json }.to_not raise_exception
@ -183,7 +184,7 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
invalid_request_body[:data].delete(:attributes) invalid_request_body[:data].delete(:attributes)
hash_body = nil hash_body = nil
post api_v1_team_inventories_path( post api_v1_team_inventories_path(
team_id: @teams.first.id team_id: @team1.id
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception expect { hash_body = json }.to_not raise_exception
@ -195,7 +196,7 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
invalid_request_body[:data].delete(:type) invalid_request_body[:data].delete(:type)
hash_body = nil hash_body = nil
post api_v1_team_inventories_path( post api_v1_team_inventories_path(
team_id: @teams.first.id team_id: @team1.id
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception expect { hash_body = json }.to_not raise_exception
@ -219,7 +220,7 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
Faker::Name.unique.name Faker::Name.unique.name
patch api_v1_team_inventory_path( patch api_v1_team_inventory_path(
id: updated_inventory[:data][:id], id: updated_inventory[:data][:id],
team_id: @teams.first.id team_id: @team1.id
), params: updated_inventory.to_json, ), params: updated_inventory.to_json,
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status 200 expect(response).to have_http_status 200
@ -233,8 +234,8 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
updated_inventory[:data][:attributes][:name] = updated_inventory[:data][:attributes][:name] =
Faker::Name.unique.name Faker::Name.unique.name
patch api_v1_team_inventory_path( patch api_v1_team_inventory_path(
id: @teams.second.repositories.first.id, id: @team2.repositories.first.id,
team_id: @teams.first.id team_id: @team1.id
), params: updated_inventory.to_json, ), params: updated_inventory.to_json,
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status 404 expect(response).to have_http_status 404
@ -249,7 +250,7 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
Faker::Name.unique.name Faker::Name.unique.name
patch api_v1_team_inventory_path( patch api_v1_team_inventory_path(
id: -1, id: -1,
team_id: @teams.first.id team_id: @team1.id
), params: updated_inventory.to_json, ), params: updated_inventory.to_json,
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status 404 expect(response).to have_http_status 404
@ -263,8 +264,8 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
updated_inventory[:data][:attributes][:name] = updated_inventory[:data][:attributes][:name] =
Faker::Name.unique.name Faker::Name.unique.name
patch api_v1_team_inventory_path( patch api_v1_team_inventory_path(
id: @teams.second.repositories.first.id, id: @team2.repositories.first.id,
team_id: @teams.second.id team_id: @team2.id
), params: updated_inventory.to_json, ), params: updated_inventory.to_json,
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status 403 expect(response).to have_http_status 403
@ -275,10 +276,11 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
describe 'DELETE inventories, #destroy' do describe 'DELETE inventories, #destroy' do
it 'Destroys inventory' do it 'Destroys inventory' do
deleted_id = @teams.first.repositories.last.id deleted_id = @team1.repositories.last.id
@team1.repositories.last.archive!(@user)
delete api_v1_team_inventory_path( delete api_v1_team_inventory_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id team_id: @team1.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(Repository.where(id: deleted_id)).to_not exist expect(Repository.where(id: deleted_id)).to_not exist
@ -289,16 +291,16 @@ RSpec.describe 'Api::V1::InventoriesController', type: :request do
it 'Invalid request, non existing inventory' do it 'Invalid request, non existing inventory' do
delete api_v1_team_inventory_path( delete api_v1_team_inventory_path(
id: -1, id: -1,
team_id: @teams.first.id team_id: @team1.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
deleted_id = @teams.first.repositories.last.id deleted_id = @team1.repositories.last.id
delete api_v1_team_inventory_path( delete api_v1_team_inventory_path(
id: deleted_id, id: deleted_id,
team_id: @teams.second.id team_id: @team2.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect(Repository.where(id: deleted_id)).to exist expect(Repository.where(id: deleted_id)).to exist

View file

@ -5,15 +5,15 @@ require 'rails_helper'
RSpec.describe 'Api::V1::InventoryCellsController', type: :request do RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@another_user = create(:user)
@team = create(:team, created_by: @user) @team = create(:team, created_by: @user)
@wrong_team = create(:team, created_by: @user) @wrong_team = create(:team, created_by: @another_user)
create(:user_team, user: @user, team: @team, role: 2)
# valid_inventory # valid_inventory
@valid_inventory = create(:repository, name: Faker::Name.unique.name, created_by: @user, team: @team) @valid_inventory = create(:repository, name: Faker::Name.unique.name, created_by: @user, team: @team)
# unaccessable_inventory # unaccessable_inventory
@wrong_inventory = create(:repository, name: Faker::Name.unique.name, created_by: @user, team: @wrong_team) @wrong_inventory = create(:repository, name: Faker::Name.unique.name, created_by: @another_user, team: @wrong_team)
create(:repository_row, repository: @wrong_inventory) create(:repository_row, repository: @wrong_inventory)
@ -42,11 +42,11 @@ RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
@date_time_range_column = create(:repository_column, @date_time_range_column = create(:repository_column,
repository: @valid_inventory, data_type: :RepositoryDateTimeRangeValue) repository: @valid_inventory, data_type: :RepositoryDateTimeRangeValue)
@number_column = create(:repository_column, repository: @valid_inventory, data_type: :RepositoryNumberValue) @number_column = create(:repository_column, repository: @valid_inventory, data_type: :RepositoryNumberValue)
@stock_column = create(:repository_column, name: Faker::Name.unique.name, @stock_column = create(:repository_column, name: Faker::Name.unique.name,
data_type: :RepositoryStockValue, repository: @valid_inventory) data_type: :RepositoryStockValue, repository: @valid_inventory)
@repository_stock_unit_item = create( :repository_stock_unit_item, created_by: @user, @repository_stock_unit_item = create( :repository_stock_unit_item, created_by: @user,
last_modified_by: @user, last_modified_by: @user,
repository_column: @stock_column) repository_column: @stock_column)
@valid_item = create(:repository_row, repository: @valid_inventory) @valid_item = create(:repository_row, repository: @valid_inventory)
@ -1170,7 +1170,7 @@ RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
describe 'DELETE inventory_cells, #destroy' do describe 'DELETE inventory_cells, #destroy' do
it 'Destroys inventory cell' do it 'Destroys inventory cell' do
deleted_id = @valid_item.repository_cells.where(repository_column: @number_column).first.id deleted_id = @valid_item.repository_cells.where(repository_column: @number_column).first.id
delete api_v1_team_inventory_item_cell_path( delete api_v1_team_inventory_item_cell_path(
id: deleted_id, id: deleted_id,
team_id: @team.id, team_id: @team.id,
@ -1182,15 +1182,15 @@ RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
end end
it 'Destroys stock inventory cell' do it 'Destroys stock inventory cell' do
deleted_id = @valid_item.repository_cells.where(repository_column: @stock_column).first.id deleted_id = @valid_item.repository_cells.where(repository_column: @stock_column).first.id
delete api_v1_team_inventory_item_cell_path( delete api_v1_team_inventory_item_cell_path(
id: deleted_id, id: deleted_id,
team_id: @team.id, team_id: @team.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
item_id: @valid_item.id item_id: @valid_item.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(400) expect(response).to have_http_status(200)
expect(RepositoryCell.where(id: deleted_id)).to exist expect(RepositoryCell.where(id: deleted_id)).to_not exist
end end
it 'Invalid request, non existing inventory item' do it 'Invalid request, non existing inventory item' do

View file

@ -5,14 +5,15 @@ require 'rails_helper'
RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @another_user = create(:user)
create(:user_team, user: @user, team: @teams.first, role: 2) @team1 = create(:team, created_by: @user)
@team2 = create(:team, created_by: @another_user)
@valid_inventory = create(:repository, name: Faker::Name.unique.name, @valid_inventory = create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
@wrong_inventory = create(:repository, name: Faker::Name.unique.name, @wrong_inventory = create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @another_user, team: @team2)
create(:repository_column, name: Faker::Name.unique.name, create(:repository_column, name: Faker::Name.unique.name,
repository: @wrong_inventory, data_type: :RepositoryTextValue) repository: @wrong_inventory, data_type: :RepositoryTextValue)
@ -35,8 +36,8 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
it 'Response with correct inventory list items, default per page' do it 'Response with correct inventory list items, default per page' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_checklist_items_path( get api_v1_team_inventory_column_checklist_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id, inventory_id: @team1.repositories.first.id,
column_id: @checklist_column.id column_id: @checklist_column.id
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -65,7 +66,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
it 'When invalid request, non existing inventory' do it 'When invalid request, non existing inventory' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_checklist_items_path( get api_v1_team_inventory_column_checklist_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 123, inventory_id: 123,
column_id: 999 column_id: 999
), headers: @valid_headers ), headers: @valid_headers
@ -77,7 +78,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_checklist_items_path( get api_v1_team_inventory_column_checklist_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @wrong_inventory.id, inventory_id: @wrong_inventory.id,
column_id: @checklist_column.id column_id: @checklist_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -89,7 +90,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
it 'When invalid request, items from text column' do it 'When invalid request, items from text column' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_checklist_items_path( get api_v1_team_inventory_column_checklist_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @text_column.id column_id: @text_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -104,8 +105,8 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_checklist_item_path( get api_v1_team_inventory_column_checklist_item_path(
id: @checklist_column.repository_checklist_items.first.id, id: @checklist_column.repository_checklist_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id, inventory_id: @team1.repositories.first.id,
column_id: @checklist_column.id column_id: @checklist_column.id
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -123,8 +124,8 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_checklist_item_path( get api_v1_team_inventory_column_checklist_item_path(
id: 999, id: 999,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id, inventory_id: @team1.repositories.first.id,
column_id: @checklist_column.id column_id: @checklist_column.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
@ -136,7 +137,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_checklist_items_path( get api_v1_team_inventory_column_checklist_items_path(
id: @wrong_checklist_column.repository_checklist_items.first.id, id: @wrong_checklist_column.repository_checklist_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @wrong_inventory.id, inventory_id: @wrong_inventory.id,
column_id: @checklist_column.id column_id: @checklist_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -160,7 +161,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
it 'Response with correct inventory list item' do it 'Response with correct inventory list item' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_checklist_items_path( post api_v1_team_inventory_column_checklist_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column column_id: @checklist_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -178,7 +179,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_checklist_items_path( post api_v1_team_inventory_column_checklist_items_path(
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column column_id: @checklist_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -190,7 +191,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
it 'When invalid request, non existing inventory' do it 'When invalid request, non existing inventory' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_checklist_items_path( post api_v1_team_inventory_column_checklist_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 123, inventory_id: 123,
column_id: @checklist_column column_id: @checklist_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -202,7 +203,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_checklist_items_path( post api_v1_team_inventory_column_checklist_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @wrong_inventory.id, inventory_id: @wrong_inventory.id,
column_id: @checklist_column column_id: @checklist_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -216,7 +217,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
invalid_request_body[:data][:type] = 'repository_rows' invalid_request_body[:data][:type] = 'repository_rows'
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_checklist_items_path( post api_v1_team_inventory_column_checklist_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column column_id: @checklist_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -230,7 +231,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
invalid_request_body[:data].delete(:type) invalid_request_body[:data].delete(:type)
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_checklist_items_path( post api_v1_team_inventory_column_checklist_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column column_id: @checklist_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -244,7 +245,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
invalid_request_body = @request_body.deep_dup invalid_request_body = @request_body.deep_dup
invalid_request_body[:data][:attributes].delete(:data) invalid_request_body[:data][:attributes].delete(:data)
post api_v1_team_inventory_column_checklist_items_path( post api_v1_team_inventory_column_checklist_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column column_id: @checklist_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -271,7 +272,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
item_id = @checklist_column.repository_checklist_items.first.id item_id = @checklist_column.repository_checklist_items.first.id
put api_v1_team_inventory_column_checklist_item_path( put api_v1_team_inventory_column_checklist_item_path(
id: item_id, id: item_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column column_id: @checklist_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -294,7 +295,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
hash_body = nil hash_body = nil
put api_v1_team_inventory_column_checklist_item_path( put api_v1_team_inventory_column_checklist_item_path(
id: @checklist_column.repository_checklist_items.first.id, id: @checklist_column.repository_checklist_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column column_id: @checklist_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -309,7 +310,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
invalid_request_body[:data][:attributes].delete(:data) invalid_request_body[:data][:attributes].delete(:data)
put api_v1_team_inventory_column_checklist_item_path( put api_v1_team_inventory_column_checklist_item_path(
id: @checklist_column.repository_checklist_items.first.id, id: @checklist_column.repository_checklist_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column column_id: @checklist_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -324,7 +325,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
invalid_request_body[:id] = 999 invalid_request_body[:id] = 999
put api_v1_team_inventory_column_checklist_item_path( put api_v1_team_inventory_column_checklist_item_path(
id: 999, id: 999,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column column_id: @checklist_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -339,7 +340,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
deleted_id = @checklist_column.repository_checklist_items.last.id deleted_id = @checklist_column.repository_checklist_items.last.id
delete api_v1_team_inventory_column_checklist_item_path( delete api_v1_team_inventory_column_checklist_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column.id column_id: @checklist_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -350,7 +351,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
it 'Invalid request, non existing inventory list item' do it 'Invalid request, non existing inventory list item' do
delete api_v1_team_inventory_column_checklist_item_path( delete api_v1_team_inventory_column_checklist_item_path(
id: 1001, id: 1001,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column.id column_id: @checklist_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -361,7 +362,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
deleted_id = @checklist_column.repository_checklist_items.last.id deleted_id = @checklist_column.repository_checklist_items.last.id
delete api_v1_team_inventory_column_checklist_item_path( delete api_v1_team_inventory_column_checklist_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 9999, inventory_id: 9999,
column_id: @checklist_column.id column_id: @checklist_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -373,7 +374,7 @@ RSpec.describe 'Api::V1::InventoryChecklistItemsController', type: :request do
deleted_id = @checklist_column.repository_checklist_items.last.id deleted_id = @checklist_column.repository_checklist_items.last.id
delete api_v1_team_inventory_column_checklist_item_path( delete api_v1_team_inventory_column_checklist_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @checklist_column.id column_id: @checklist_column.id
), headers: @valid_headers ), headers: @valid_headers

View file

@ -9,30 +9,31 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
) )
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @another_user = create(:user)
create(:user_team, user: @user, team: @teams.first, role: 2) @team1 = create(:team, created_by: @user)
@team2 = create(:team, created_by: @another_user)
# valid_inventory # valid_inventory
@valid_inventory = create(:repository, name: Faker::Name.unique.name, @valid_inventory = create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
# valid_stock inventory # valid_stock inventory
@valid_stock_inventory = create(:repository, name: Faker::Name.unique.name, @valid_stock_inventory = create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
# unaccessable_inventory # unaccessable_inventory
create(:repository, name: Faker::Name.unique.name, create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @another_user, team: @team2)
stock_column = create(:repository_column, name: Faker::Name.unique.name, @stock_column = create(:repository_column, name: Faker::Name.unique.name,
data_type: :RepositoryStockValue, repository: @valid_stock_inventory) data_type: :RepositoryStockValue, repository: @valid_stock_inventory)
create(:repository_column, name: Faker::Name.unique.name, create(:repository_column, name: Faker::Name.unique.name,
repository: @valid_inventory, data_type: :RepositoryTextValue) repository: @valid_inventory, data_type: :RepositoryTextValue)
list_column = create(:repository_column, name: Faker::Name.unique.name, @list_column = create(:repository_column, name: Faker::Name.unique.name,
repository: @valid_inventory, data_type: :RepositoryListValue) repository: @valid_inventory, data_type: :RepositoryListValue)
create(:repository_list_item, repository_column: list_column, data: Faker::Name.unique.name) create(:repository_list_item, repository_column: @list_column, data: Faker::Name.unique.name)
status_column = create(:repository_column, name: Faker::Name.unique.name, @status_column = create(:repository_column, name: Faker::Name.unique.name,
repository: @valid_inventory, data_type: :RepositoryStatusValue) repository: @valid_inventory, data_type: :RepositoryStatusValue)
create(:repository_status_item, repository_column: status_column, status: Faker::Name.unique.name, icon: 'icon') create(:repository_status_item, repository_column: @status_column, status: Faker::Name.unique.name, icon: 'icon')
create(:repository_column, name: Faker::Name.unique.name, create(:repository_column, name: Faker::Name.unique.name,
repository: @valid_inventory, data_type: :RepositoryAssetValue) repository: @valid_inventory, data_type: :RepositoryAssetValue)
@ -44,8 +45,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'Response with correct inventory items, default per page' do it 'Response with correct inventory items, default per page' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_columns_path( get api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
@ -62,8 +63,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_columns_path( get api_v1_team_inventory_columns_path(
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @teams.second.repositories.first.id inventory_id: @team2.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -73,7 +74,7 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'When invalid request, non existing inventory' do it 'When invalid request, non existing inventory' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_columns_path( get api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 123 inventory_id: 123
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
@ -84,8 +85,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_columns_path( get api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.second.repositories.first.id inventory_id: @team2.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -99,7 +100,7 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_path( get api_v1_team_inventory_column_path(
id: text_column.id, id: text_column.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id inventory_id: @valid_inventory.id
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -114,18 +115,18 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
end end
it 'Valid list column response' do it 'Valid list column response' do
list_column = @valid_inventory.repository_columns.second
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_path( get api_v1_team_inventory_column_path(
id: list_column.id, id: @list_column.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id inventory_id: @valid_inventory.id,
include: 'inventory_list_items'
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
JSON.parse( JSON.parse(
ActiveModelSerializers::SerializableResource ActiveModelSerializers::SerializableResource
.new(list_column, serializer: Api::V1::InventoryColumnSerializer) .new(@list_column, serializer: Api::V1::InventoryColumnSerializer)
.to_json .to_json
)['data'] )['data']
) )
@ -142,18 +143,18 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
end end
it 'Valid status column response' do it 'Valid status column response' do
status_column = @valid_inventory.repository_columns.status_type.first
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_path( get api_v1_team_inventory_column_path(
id: status_column.id, id: @status_column.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id inventory_id: @valid_inventory.id,
include: 'inventory_status_items'
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
JSON.parse( JSON.parse(
ActiveModelSerializers::SerializableResource ActiveModelSerializers::SerializableResource
.new(status_column, serializer: Api::V1::InventoryColumnSerializer) .new(@status_column, serializer: Api::V1::InventoryColumnSerializer)
.to_json .to_json
)['data'] )['data']
) )
@ -174,7 +175,7 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_path( get api_v1_team_inventory_column_path(
id: file_column.id, id: file_column.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id inventory_id: @valid_inventory.id
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -191,29 +192,29 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'Invalid request, non existing inventory column' do it 'Invalid request, non existing inventory column' do
get api_v1_team_inventory_column_path( get api_v1_team_inventory_column_path(
id: 1001, id: 1001,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
it 'When invalid request, incorrect repository' do it 'When invalid request, incorrect repository' do
id = @teams.first.repositories.first.repository_columns.last.id id = @team1.repositories.first.repository_columns.last.id
get api_v1_team_inventory_column_path( get api_v1_team_inventory_column_path(
id: id, id: id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.second.repositories.first.id inventory_id: @team2.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect(RepositoryColumn.where(id: id)).to exist expect(RepositoryColumn.where(id: id)).to exist
end end
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
id = @teams.first.repositories.first.repository_columns.last.id id = @team1.repositories.first.repository_columns.last.id
get api_v1_team_inventory_column_path( get api_v1_team_inventory_column_path(
id: id, id: id,
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect(RepositoryColumn.where(id: id)).to exist expect(RepositoryColumn.where(id: id)).to exist
@ -244,8 +245,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'Response with correct inventory column' do it 'Response with correct inventory column' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_columns_path( post api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
expect(response).to have_http_status 201 expect(response).to have_http_status 201
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -261,8 +262,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_columns_path( post api_v1_team_inventory_columns_path(
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
expect(response).to have_http_status 403 expect(response).to have_http_status 403
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -272,7 +273,7 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'When invalid request, non existing inventory' do it 'When invalid request, non existing inventory' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_columns_path( post api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 123 inventory_id: 123
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
expect(response).to have_http_status 404 expect(response).to have_http_status 404
@ -283,8 +284,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_columns_path( post api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.second.repositories.first.id inventory_id: @team2.repositories.first.id
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -296,8 +297,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
invalid_request_body[:data][:type] = 'repository_rows' invalid_request_body[:data][:type] = 'repository_rows'
hash_body = nil hash_body = nil
post api_v1_team_inventory_columns_path( post api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception expect { hash_body = json }.to_not raise_exception
@ -307,8 +308,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'When invalid request, missing data param' do it 'When invalid request, missing data param' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_columns_path( post api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: {}, headers: @valid_headers ), params: {}, headers: @valid_headers
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception expect { hash_body = json }.to_not raise_exception
@ -320,8 +321,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
invalid_request_body[:data].delete(:attributes) invalid_request_body[:data].delete(:attributes)
hash_body = nil hash_body = nil
post api_v1_team_inventory_columns_path( post api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception expect { hash_body = json }.to_not raise_exception
@ -333,8 +334,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
invalid_request_body[:data].delete(:type) invalid_request_body[:data].delete(:type)
hash_body = nil hash_body = nil
post api_v1_team_inventory_columns_path( post api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception expect { hash_body = json }.to_not raise_exception
@ -347,8 +348,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
invalid_request_body = @request_body.deep_dup invalid_request_body = @request_body.deep_dup
invalid_request_body[:data][:attributes].delete(attr) invalid_request_body[:data][:attributes].delete(attr)
post api_v1_team_inventory_columns_path( post api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect { hash_body = json }.to_not raise_exception expect { hash_body = json }.to_not raise_exception
@ -359,8 +360,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'Response with correct stock inventory column' do it 'Response with correct stock inventory column' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_columns_path( post api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: request_body_stock.to_json, headers: @valid_headers ), params: request_body_stock.to_json, headers: @valid_headers
expect(response).to have_http_status 201 expect(response).to have_http_status 201
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -376,8 +377,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'Raised error with already exsisting stock column in column' do it 'Raised error with already exsisting stock column in column' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_columns_path( post api_v1_team_inventory_columns_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.second.id inventory_id: @team1.repositories.second.id
), params: request_body_stock.to_json, headers: @valid_headers ), params: request_body_stock.to_json, headers: @valid_headers
expect(response).to have_http_status 400 expect(response).to have_http_status 400
end end
@ -385,11 +386,11 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
describe 'DELETE inventory_columns, #destroy' do describe 'DELETE inventory_columns, #destroy' do
it 'Destroys inventory column' do it 'Destroys inventory column' do
deleted_id = @teams.first.repositories.first.repository_columns.last.id deleted_id = @team1.repositories.first.repository_columns.last.id
delete api_v1_team_inventory_column_path( delete api_v1_team_inventory_column_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(RepositoryColumn.where(id: deleted_id)).to_not exist expect(RepositoryColumn.where(id: deleted_id)).to_not exist
@ -399,40 +400,40 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
it 'Invalid request, non existing inventory column' do it 'Invalid request, non existing inventory column' do
delete api_v1_team_inventory_column_path( delete api_v1_team_inventory_column_path(
id: 1001, id: 1001,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
it 'When invalid request, incorrect repository' do it 'When invalid request, incorrect repository' do
deleted_id = @teams.first.repositories.first.repository_columns.last.id deleted_id = @team1.repositories.first.repository_columns.last.id
delete api_v1_team_inventory_column_path( delete api_v1_team_inventory_column_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.second.repositories.first.id inventory_id: @team2.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect(RepositoryColumn.where(id: deleted_id)).to exist expect(RepositoryColumn.where(id: deleted_id)).to exist
end end
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
deleted_id = @teams.first.repositories.first.repository_columns.last.id deleted_id = @team1.repositories.first.repository_columns.last.id
delete api_v1_team_inventory_column_path( delete api_v1_team_inventory_column_path(
id: deleted_id, id: deleted_id,
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect(RepositoryColumn.where(id: deleted_id)).to exist expect(RepositoryColumn.where(id: deleted_id)).to exist
end end
it 'Destroy Stock inventory column' do it 'Destroy Stock inventory column' do
deleted_id = @teams.first.repositories.second.repository_columns.last.id deleted_id = @team1.repositories.second.repository_columns.last.id
delete api_v1_team_inventory_column_path( delete api_v1_team_inventory_column_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.second.id inventory_id: @team1.repositories.second.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(RepositoryColumn.where(id: deleted_id)).to_not exist expect(RepositoryColumn.where(id: deleted_id)).to_not exist
@ -451,7 +452,7 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
let!(:request_body_stock_update) { let!(:request_body_stock_update) {
ActiveModelSerializers::SerializableResource.new( ActiveModelSerializers::SerializableResource.new(
@teams.first.repositories.second.repository_columns.last, @team1.repositories.second.repository_columns.last,
serializer: Api::V1::InventoryColumnSerializer serializer: Api::V1::InventoryColumnSerializer
) )
@ -466,8 +467,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
updated_inventory_column[:data][:attributes].delete(:data_type) updated_inventory_column[:data][:attributes].delete(:data_type)
patch api_v1_team_inventory_column_path( patch api_v1_team_inventory_column_path(
id: RepositoryColumn.last.id, id: RepositoryColumn.last.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: updated_inventory_column.to_json, ), params: updated_inventory_column.to_json,
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status 200 expect(response).to have_http_status 200
@ -482,9 +483,9 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
returned_inventory_column = updated_inventory_column.deep_dup returned_inventory_column = updated_inventory_column.deep_dup
updated_inventory_column[:data][:attributes].delete(:data_type) updated_inventory_column[:data][:attributes].delete(:data_type)
patch api_v1_team_inventory_column_path( patch api_v1_team_inventory_column_path(
id: @teams.first.repositories.second.repository_columns.last.id, id: @team1.repositories.second.repository_columns.last.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.second.id inventory_id: @team1.repositories.second.id
), params: updated_inventory_column.to_json, ), params: updated_inventory_column.to_json,
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status 200 expect(response).to have_http_status 200
@ -499,8 +500,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
Faker::Name.unique.name Faker::Name.unique.name
patch api_v1_team_inventory_column_path( patch api_v1_team_inventory_column_path(
id: RepositoryColumn.last.id, id: RepositoryColumn.last.id,
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: updated_inventory_column.to_json, ), params: updated_inventory_column.to_json,
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status 403 expect(response).to have_http_status 403
@ -515,8 +516,8 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
Faker::Name.unique.name Faker::Name.unique.name
patch api_v1_team_inventory_column_path( patch api_v1_team_inventory_column_path(
id: RepositoryColumn.last.id, id: RepositoryColumn.last.id,
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @teams.second.repositories.first.id inventory_id: @team2.repositories.first.id
), params: updated_inventory_column.to_json, ), params: updated_inventory_column.to_json,
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status 403 expect(response).to have_http_status 403
@ -531,7 +532,7 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do
Faker::Name.unique.name Faker::Name.unique.name
patch api_v1_team_inventory_column_path( patch api_v1_team_inventory_column_path(
id: RepositoryColumn.last.id, id: RepositoryColumn.last.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 123 inventory_id: 123
), params: updated_inventory_column.to_json, ), params: updated_inventory_column.to_json,
headers: @valid_headers headers: @valid_headers

View file

@ -5,16 +5,17 @@ require 'rails_helper'
RSpec.describe 'Api::V1::InventoryItemsController', type: :request do RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @another_user = create(:user)
create(:user_team, user: @user, team: @teams.first, role: 2) @team1 = create(:team, created_by: @user)
@team2 = create(:team, created_by: @another_user)
# valid_inventory # valid_inventory
@valid_inventory = create(:repository, name: Faker::Name.unique.name, @valid_inventory = create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
# unaccessable_inventory # unaccessable_inventory
create(:repository, name: Faker::Name.unique.name, create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @another_user, team: @team2)
text_column = create(:repository_column, name: Faker::Name.unique.name, text_column = create(:repository_column, name: Faker::Name.unique.name,
repository: @valid_inventory, data_type: :RepositoryTextValue) repository: @valid_inventory, data_type: :RepositoryTextValue)
@ -63,8 +64,8 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
it 'Response with correct inventory items, default per page' do it 'Response with correct inventory items, default per page' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_items_path( get api_v1_team_inventory_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
@ -82,8 +83,8 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
it 'Response with correct inventory items, included cells' do it 'Response with correct inventory items, included cells' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_items_path( get api_v1_team_inventory_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id, inventory_id: @team1.repositories.first.id,
include: 'inventory_cells' include: 'inventory_cells'
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -110,8 +111,8 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
it 'Response with correct inventory items, 100 per page' do it 'Response with correct inventory items, 100 per page' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_items_path( get api_v1_team_inventory_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: { page: { size: 100 } }, headers: @valid_headers ), params: { page: { size: 100 } }, headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
@ -129,8 +130,8 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_items_path( get api_v1_team_inventory_items_path(
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @teams.second.repositories.first.id inventory_id: @team2.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -140,7 +141,7 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
it 'When invalid request, non existing inventory' do it 'When invalid request, non existing inventory' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_items_path( get api_v1_team_inventory_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 123 inventory_id: 123
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
@ -151,8 +152,8 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_items_path( get api_v1_team_inventory_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.second.repositories.first.id inventory_id: @team2.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -162,15 +163,15 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
context 'when have some archived rows' do context 'when have some archived rows' do
before do before do
create(:repository_row, :archived, create(:repository_row, :archived,
name: Faker::Name.unique.name, created_by: @user, repository: @teams.first.repositories.first) name: Faker::Name.unique.name, created_by: @user, repository: @team1.repositories.first)
end end
it 'will ignore them' do it 'will ignore them' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_items_path( get api_v1_team_inventory_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), params: { page: { size: 200 } }, headers: @valid_headers ), params: { page: { size: 200 } }, headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -181,13 +182,13 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
describe 'DELETE inventory_items, #destroy' do describe 'DELETE inventory_items, #destroy' do
it 'Destroys inventory item' do it 'Destroys inventory item' do
row = @teams.first.repositories.first.repository_rows.last row = @team1.repositories.first.repository_rows.last
row.archive!(@user) row.archive!(@user)
deleted_id = row.id deleted_id = row.id
delete api_v1_team_inventory_item_path( delete api_v1_team_inventory_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(RepositoryRow.where(id: deleted_id)).to_not exist expect(RepositoryRow.where(id: deleted_id)).to_not exist
@ -198,29 +199,29 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
deleted_id = RepositoryRow.last.id + 1 deleted_id = RepositoryRow.last.id + 1
delete api_v1_team_inventory_item_path( delete api_v1_team_inventory_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
it 'When invalid request, incorrect repository' do it 'When invalid request, incorrect repository' do
deleted_id = @teams.first.repositories.first.repository_rows.last.id deleted_id = @team1.repositories.first.repository_rows.last.id
delete api_v1_team_inventory_item_path( delete api_v1_team_inventory_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.second.repositories.first.id inventory_id: @team2.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect(RepositoryRow.where(id: deleted_id)).to exist expect(RepositoryRow.where(id: deleted_id)).to exist
end end
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
deleted_id = @teams.first.repositories.first.repository_rows.last.id deleted_id = @team1.repositories.first.repository_rows.last.id
delete api_v1_team_inventory_item_path( delete api_v1_team_inventory_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @teams.first.repositories.first.id inventory_id: @team1.repositories.first.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect(RepositoryRow.where(id: deleted_id)).to exist expect(RepositoryRow.where(id: deleted_id)).to exist
@ -231,7 +232,7 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
it 'Response with correct inventory item' do it 'Response with correct inventory item' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_items_path( post api_v1_team_inventory_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id inventory_id: @valid_inventory.id
), params: @valid_hash_body.to_json, headers: @valid_headers ), params: @valid_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status 201 expect(response).to have_http_status 201
@ -255,7 +256,7 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
it 'When invalid request, non existing inventory' do it 'When invalid request, non existing inventory' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_items_path( post api_v1_team_inventory_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: -1 inventory_id: -1
), params: @valid_hash_body.to_json, headers: @valid_headers ), params: @valid_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
@ -266,7 +267,7 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_items_path( post api_v1_team_inventory_items_path(
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @valid_inventory.id inventory_id: @valid_inventory.id
), params: @valid_hash_body.to_json, headers: @valid_headers ), params: @valid_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
@ -277,8 +278,8 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_items_path( post api_v1_team_inventory_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.second.repositories.first.id inventory_id: @team2.repositories.first.id
), params: @valid_hash_body.to_json, headers: @valid_headers ), params: @valid_hash_body.to_json, headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -302,7 +303,7 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do
updated_inventory_item['attributes']['name'] = Faker::Name.unique.name updated_inventory_item['attributes']['name'] = Faker::Name.unique.name
patch api_v1_team_inventory_item_path( patch api_v1_team_inventory_item_path(
id: RepositoryRow.last.id, id: RepositoryRow.last.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id inventory_id: @valid_inventory.id
), params: { data: updated_inventory_item }.to_json, ), params: { data: updated_inventory_item }.to_json,
headers: @valid_headers headers: @valid_headers

View file

@ -5,14 +5,15 @@ require 'rails_helper'
RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @another_user = create(:user)
create(:user_team, user: @user, team: @teams.first, role: 2) @team1 = create(:team, created_by: @user)
@team2 = create(:team, created_by: @another_user)
@valid_inventory = create(:repository, name: Faker::Name.unique.name, @valid_inventory = create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
@wrong_inventory = create(:repository, name: Faker::Name.unique.name, @wrong_inventory = create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @another_user, team: @team2)
create(:repository_column, name: Faker::Name.unique.name, create(:repository_column, name: Faker::Name.unique.name,
repository: @wrong_inventory, data_type: :RepositoryTextValue) repository: @wrong_inventory, data_type: :RepositoryTextValue)
@ -35,8 +36,8 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
it 'Response with correct inventory list items, default per page' do it 'Response with correct inventory list items, default per page' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_list_items_path( get api_v1_team_inventory_column_list_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id, inventory_id: @team1.repositories.first.id,
column_id: @list_column.id column_id: @list_column.id
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -64,7 +65,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
it 'When invalid request, non existing inventory' do it 'When invalid request, non existing inventory' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_list_items_path( get api_v1_team_inventory_column_list_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 123, inventory_id: 123,
column_id: 999 column_id: 999
), headers: @valid_headers ), headers: @valid_headers
@ -76,7 +77,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_list_items_path( get api_v1_team_inventory_column_list_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @wrong_inventory.id, inventory_id: @wrong_inventory.id,
column_id: @list_column.id column_id: @list_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -88,7 +89,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
it 'When invalid request, items from text column' do it 'When invalid request, items from text column' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_list_items_path( get api_v1_team_inventory_column_list_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @text_column.id column_id: @text_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -103,8 +104,8 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_list_item_path( get api_v1_team_inventory_column_list_item_path(
id: @list_column.repository_list_items.first.id, id: @list_column.repository_list_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id, inventory_id: @team1.repositories.first.id,
column_id: @list_column.id column_id: @list_column.id
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -121,8 +122,8 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_list_item_path( get api_v1_team_inventory_column_list_item_path(
id: 999, id: 999,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id, inventory_id: @team1.repositories.first.id,
column_id: @list_column.id column_id: @list_column.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
@ -134,7 +135,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_list_items_path( get api_v1_team_inventory_column_list_items_path(
id: @wrong_list_column.repository_list_items.first.id, id: @wrong_list_column.repository_list_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @wrong_inventory.id, inventory_id: @wrong_inventory.id,
column_id: @list_column.id column_id: @list_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -158,7 +159,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
it 'Response with correct inventory list item' do it 'Response with correct inventory list item' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_list_items_path( post api_v1_team_inventory_column_list_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column column_id: @list_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -176,7 +177,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_list_items_path( post api_v1_team_inventory_column_list_items_path(
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column column_id: @list_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -188,7 +189,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
it 'When invalid request, non existing inventory' do it 'When invalid request, non existing inventory' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_list_items_path( post api_v1_team_inventory_column_list_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 123, inventory_id: 123,
column_id: @list_column column_id: @list_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -200,7 +201,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_list_items_path( post api_v1_team_inventory_column_list_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @wrong_inventory.id, inventory_id: @wrong_inventory.id,
column_id: @list_column column_id: @list_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -214,7 +215,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
invalid_request_body[:data][:type] = 'repository_rows' invalid_request_body[:data][:type] = 'repository_rows'
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_list_items_path( post api_v1_team_inventory_column_list_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column column_id: @list_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -228,7 +229,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
invalid_request_body[:data].delete(:type) invalid_request_body[:data].delete(:type)
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_list_items_path( post api_v1_team_inventory_column_list_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column column_id: @list_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -242,7 +243,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
invalid_request_body = @request_body.deep_dup invalid_request_body = @request_body.deep_dup
invalid_request_body[:data][:attributes].delete(:data) invalid_request_body[:data][:attributes].delete(:data)
post api_v1_team_inventory_column_list_items_path( post api_v1_team_inventory_column_list_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column column_id: @list_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -269,7 +270,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
item_id = @list_column.repository_list_items.first.id item_id = @list_column.repository_list_items.first.id
put api_v1_team_inventory_column_list_item_path( put api_v1_team_inventory_column_list_item_path(
id: item_id, id: item_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column column_id: @list_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -291,7 +292,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
hash_body = nil hash_body = nil
put api_v1_team_inventory_column_list_item_path( put api_v1_team_inventory_column_list_item_path(
id: @list_column.repository_list_items.first.id, id: @list_column.repository_list_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column column_id: @list_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -306,7 +307,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
invalid_request_body[:data][:attributes].delete(:data) invalid_request_body[:data][:attributes].delete(:data)
put api_v1_team_inventory_column_list_item_path( put api_v1_team_inventory_column_list_item_path(
id: @list_column.repository_list_items.first.id, id: @list_column.repository_list_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column column_id: @list_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -321,7 +322,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
invalid_request_body[:id] = 999 invalid_request_body[:id] = 999
put api_v1_team_inventory_column_list_item_path( put api_v1_team_inventory_column_list_item_path(
id: 999, id: 999,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column column_id: @list_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -336,7 +337,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
deleted_id = @list_column.repository_list_items.last.id deleted_id = @list_column.repository_list_items.last.id
delete api_v1_team_inventory_column_list_item_path( delete api_v1_team_inventory_column_list_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column.id column_id: @list_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -347,7 +348,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
it 'Invalid request, non existing inventory list item' do it 'Invalid request, non existing inventory list item' do
delete api_v1_team_inventory_column_list_item_path( delete api_v1_team_inventory_column_list_item_path(
id: 1001, id: 1001,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column.id column_id: @list_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -358,7 +359,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
deleted_id = @list_column.repository_list_items.last.id deleted_id = @list_column.repository_list_items.last.id
delete api_v1_team_inventory_column_list_item_path( delete api_v1_team_inventory_column_list_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 9999, inventory_id: 9999,
column_id: @list_column.id column_id: @list_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -370,7 +371,7 @@ RSpec.describe 'Api::V1::InventoryListItemsController', type: :request do
deleted_id = @list_column.repository_list_items.last.id deleted_id = @list_column.repository_list_items.last.id
delete api_v1_team_inventory_column_list_item_path( delete api_v1_team_inventory_column_list_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @list_column.id column_id: @list_column.id
), headers: @valid_headers ), headers: @valid_headers

View file

@ -5,11 +5,11 @@ require 'rails_helper'
RSpec.describe 'Api::V1::InventoryStatusItemsController', type: :request do RSpec.describe 'Api::V1::InventoryStatusItemsController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@another_user = create(:user)
@team1 = create(:team, created_by: @user) @team1 = create(:team, created_by: @user)
@team2 = create(:team, created_by: @user) @team2 = create(:team, created_by: @another_user)
@user_team = create(:user_team, :admin, user: @user, team: @team1)
@inventory = create(:repository, name: Faker::Name.unique.name, created_by: @user, team: @team1) @inventory = create(:repository, name: Faker::Name.unique.name, created_by: @user, team: @team1)
@wrong_inventory = create(:repository, name: Faker::Name.unique.name, created_by: @user, team: @team2) @wrong_inventory = create(:repository, name: Faker::Name.unique.name, created_by: @another_user, team: @team2)
@status_column = create(:repository_column, name: Faker::Name.unique.name, repository: @inventory, @status_column = create(:repository_column, name: Faker::Name.unique.name, repository: @inventory,
data_type: :RepositoryStatusValue) data_type: :RepositoryStatusValue)
create_list(:repository_status_item, 10, repository_column: @status_column) create_list(:repository_status_item, 10, repository_column: @status_column)
@ -18,6 +18,7 @@ RSpec.describe 'Api::V1::InventoryStatusItemsController', type: :request do
@wrong_column = create(:repository_column, name: Faker::Name.unique.name, repository: @wrong_inventory, @wrong_column = create(:repository_column, name: Faker::Name.unique.name, repository: @wrong_inventory,
data_type: :RepositoryStatusValue) data_type: :RepositoryStatusValue)
@wrong_status_item = create(:repository_status_item, repository_column: @wrong_column) @wrong_status_item = create(:repository_status_item, repository_column: @wrong_column)
@viewer_role = create(:viewer_role)
@valid_headers = @valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) } { 'Authorization': 'Bearer ' + generate_token(@user.id) }
end end
@ -231,7 +232,7 @@ RSpec.describe 'Api::V1::InventoryStatusItemsController', type: :request do
context 'when user does not has manage permissions' do context 'when user does not has manage permissions' do
it 'renders 403' do it 'renders 403' do
@user_team.reload.update!(role: :guest) @inventory.user_assignments.reload.update(user_role: @viewer_role)
action action
@ -239,7 +240,7 @@ RSpec.describe 'Api::V1::InventoryStatusItemsController', type: :request do
end end
it 'does not creats an item' do it 'does not creats an item' do
@user_team.reload.update!(role: :guest) @inventory.user_assignments.reload.update(user_role: @viewer_role)
expect { action }.not_to(change { RepositoryStatusItem.count }) expect { action }.not_to(change { RepositoryStatusItem.count })
end end
@ -367,7 +368,7 @@ RSpec.describe 'Api::V1::InventoryStatusItemsController', type: :request do
context 'when user does not has manage permissions' do context 'when user does not has manage permissions' do
it 'renders 403' do it 'renders 403' do
@user_team.reload.update!(role: :guest) @inventory.user_assignments.reload.update(user_role: @viewer_role)
action action
@ -444,7 +445,7 @@ RSpec.describe 'Api::V1::InventoryStatusItemsController', type: :request do
context 'when user does not has manage permissions' do context 'when user does not has manage permissions' do
it 'renders 403' do it 'renders 403' do
@user_team.reload.update!(role: :guest) @inventory.user_assignments.reload.update(user_role: @viewer_role)
action action
@ -452,7 +453,7 @@ RSpec.describe 'Api::V1::InventoryStatusItemsController', type: :request do
end end
it 'does not delets any item' do it 'does not delets any item' do
@user_team.reload.update!(role: :guest) @inventory.user_assignments.reload.update(user_role: @viewer_role)
expect { action }.not_to(change { RepositoryStatusItem.count }) expect { action }.not_to(change { RepositoryStatusItem.count })
end end

View file

@ -5,14 +5,13 @@ require 'rails_helper'
RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @another_user = create(:user)
create(:user_team, user: @user, team: @teams.first, role: 2) @team1 = create(:team, created_by: @user)
@team2 = create(:team, created_by: @another_user)
@valid_inventory = create(:repository, name: Faker::Name.unique.name, @valid_inventory = create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
@wrong_inventory = create(:repository, name: Faker::Name.unique.name, @wrong_inventory = create(:repository, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @another_user, team: @team2)
create(:repository_column, name: Faker::Name.unique.name, create(:repository_column, name: Faker::Name.unique.name,
repository: @wrong_inventory, data_type: :RepositoryTextValue) repository: @wrong_inventory, data_type: :RepositoryTextValue)
@ -25,7 +24,9 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
repository: @wrong_inventory, repository: @wrong_inventory,
data_type: :RepositoryStockValue) data_type: :RepositoryStockValue)
create_list(:repository_stock_unit_item, 10, repository_column: @stock_column) create_list(:repository_stock_unit_item, 10, repository_column: @stock_column)
@stock_column.repository_stock_unit_items.reload
create(:repository_stock_unit_item, repository_column: @wrong_stock_unit_column) create(:repository_stock_unit_item, repository_column: @wrong_stock_unit_column)
@wrong_stock_unit_column.repository_stock_unit_items.reload
@valid_headers = { 'Authorization': 'Bearer ' + generate_token(@user.id) } @valid_headers = { 'Authorization': 'Bearer ' + generate_token(@user.id) }
end end
@ -34,15 +35,15 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
it 'Response with correct inventory stock unit items, default per page' do it 'Response with correct inventory stock unit items, default per page' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_stock_unit_items_path( get api_v1_team_inventory_column_stock_unit_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id, inventory_id: @team1.repositories.first.id,
column_id: @stock_column.id column_id: @stock_column.id
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
JSON.parse( JSON.parse(
ActiveModelSerializers::SerializableResource ActiveModelSerializers::SerializableResource
.new(@stock_column.repository_stock_unit_items.limit(10), .new(@stock_column.repository_stock_unit_items.limit(10),
each_serializer: Api::V1::InventoryStockUnitItemSerializer) each_serializer: Api::V1::InventoryStockUnitItemSerializer)
.to_json .to_json
)['data'] )['data']
@ -64,7 +65,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
it 'When invalid request, non existing inventory' do it 'When invalid request, non existing inventory' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_stock_unit_items_path( get api_v1_team_inventory_column_stock_unit_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 123, inventory_id: 123,
column_id: 999 column_id: 999
), headers: @valid_headers ), headers: @valid_headers
@ -76,7 +77,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_stock_unit_items_path( get api_v1_team_inventory_column_stock_unit_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @wrong_inventory.id, inventory_id: @wrong_inventory.id,
column_id: @stock_column.id column_id: @stock_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -88,7 +89,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
it 'When invalid request, items from text column' do it 'When invalid request, items from text column' do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_stock_unit_items_path( get api_v1_team_inventory_column_stock_unit_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @text_column.id column_id: @text_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -103,8 +104,8 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_stock_unit_item_path( get api_v1_team_inventory_column_stock_unit_item_path(
id: @stock_column.repository_stock_unit_items.first.id, id: @stock_column.repository_stock_unit_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id, inventory_id: @team1.repositories.first.id,
column_id: @stock_column.id column_id: @stock_column.id
), headers: @valid_headers ), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -121,8 +122,8 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_stock_unit_item_path( get api_v1_team_inventory_column_stock_unit_item_path(
id: 999, id: 999,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @teams.first.repositories.first.id, inventory_id: @team1.repositories.first.id,
column_id: @stock_column.id column_id: @stock_column.id
), headers: @valid_headers ), headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
@ -134,7 +135,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
hash_body = nil hash_body = nil
get api_v1_team_inventory_column_stock_unit_items_path( get api_v1_team_inventory_column_stock_unit_items_path(
id: @wrong_stock_unit_column.repository_stock_unit_items.first.id, id: @wrong_stock_unit_column.repository_stock_unit_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @wrong_inventory.id, inventory_id: @wrong_inventory.id,
column_id: @stock_column.id column_id: @stock_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -158,7 +159,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
it 'Response with correct inventory stock unit item' do it 'Response with correct inventory stock unit item' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_stock_unit_items_path( post api_v1_team_inventory_column_stock_unit_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column column_id: @stock_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -176,7 +177,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_stock_unit_items_path( post api_v1_team_inventory_column_stock_unit_items_path(
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column column_id: @stock_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -188,7 +189,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
it 'When invalid request, non existing inventory' do it 'When invalid request, non existing inventory' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_stock_unit_items_path( post api_v1_team_inventory_column_stock_unit_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 123, inventory_id: 123,
column_id: @stock_column column_id: @stock_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -200,7 +201,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
it 'When invalid request, repository from another team' do it 'When invalid request, repository from another team' do
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_stock_unit_items_path( post api_v1_team_inventory_column_stock_unit_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @wrong_inventory.id, inventory_id: @wrong_inventory.id,
column_id: @stock_column column_id: @stock_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -214,7 +215,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
invalid_request_body[:data][:type] = 'repository_rows' invalid_request_body[:data][:type] = 'repository_rows'
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_stock_unit_items_path( post api_v1_team_inventory_column_stock_unit_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column column_id: @stock_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -228,7 +229,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
invalid_request_body[:data].delete(:type) invalid_request_body[:data].delete(:type)
hash_body = nil hash_body = nil
post api_v1_team_inventory_column_stock_unit_items_path( post api_v1_team_inventory_column_stock_unit_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column column_id: @stock_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -242,7 +243,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
invalid_request_body = @request_body.deep_dup invalid_request_body = @request_body.deep_dup
invalid_request_body[:data][:attributes].delete(:data) invalid_request_body[:data][:attributes].delete(:data)
post api_v1_team_inventory_column_stock_unit_items_path( post api_v1_team_inventory_column_stock_unit_items_path(
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column column_id: @stock_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -255,9 +256,10 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
describe 'PUT inventory_stock_unit_item, #update' do describe 'PUT inventory_stock_unit_item, #update' do
before :all do before :all do
@valid_headers['Content-Type'] = 'application/vnd.api+json' @valid_headers['Content-Type'] = 'application/vnd.api+json'
@repository_stock_unit_item = @stock_column.repository_stock_unit_items.first
@request_body = { @request_body = {
data: { data: {
id: @stock_column.repository_stock_unit_items.first.id, id: @repository_stock_unit_item.id,
type: 'inventory_stock_unit_items', type: 'inventory_stock_unit_items',
attributes: { data: 'Updated' } attributes: { data: 'Updated' }
} }
@ -266,10 +268,9 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
it 'Response with correct inventory stock unit item' do it 'Response with correct inventory stock unit item' do
hash_body = nil hash_body = nil
item_id = @stock_column.repository_stock_unit_items.first.id
put api_v1_team_inventory_column_stock_unit_item_path( put api_v1_team_inventory_column_stock_unit_item_path(
id: item_id, id: @repository_stock_unit_item.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column column_id: @stock_column
), params: @request_body.to_json, headers: @valid_headers ), params: @request_body.to_json, headers: @valid_headers
@ -278,11 +279,11 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
JSON.parse( JSON.parse(
ActiveModelSerializers::SerializableResource ActiveModelSerializers::SerializableResource
.new(@stock_column.repository_stock_unit_items.find(item_id), serializer: Api::V1::InventoryStockUnitItemSerializer) .new(@repository_stock_unit_item.reload, serializer: Api::V1::InventoryStockUnitItemSerializer)
.to_json .to_json
)['data'] )['data']
) )
expect(@stock_column.repository_stock_unit_items.find(item_id).data).to match('Updated') expect(@repository_stock_unit_item.data).to match('Updated')
end end
it 'When invalid request, incorrect type' do it 'When invalid request, incorrect type' do
@ -291,7 +292,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
hash_body = nil hash_body = nil
put api_v1_team_inventory_column_stock_unit_item_path( put api_v1_team_inventory_column_stock_unit_item_path(
id: @stock_column.repository_stock_unit_items.first.id, id: @stock_column.repository_stock_unit_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column column_id: @stock_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -306,7 +307,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
invalid_request_body[:data][:attributes].delete(:data) invalid_request_body[:data][:attributes].delete(:data)
put api_v1_team_inventory_column_stock_unit_item_path( put api_v1_team_inventory_column_stock_unit_item_path(
id: @stock_column.repository_stock_unit_items.first.id, id: @stock_column.repository_stock_unit_items.first.id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column column_id: @stock_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -321,7 +322,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
invalid_request_body[:id] = 999 invalid_request_body[:id] = 999
put api_v1_team_inventory_column_stock_unit_item_path( put api_v1_team_inventory_column_stock_unit_item_path(
id: 999, id: 999,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column column_id: @stock_column
), params: invalid_request_body.to_json, headers: @valid_headers ), params: invalid_request_body.to_json, headers: @valid_headers
@ -336,7 +337,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
deleted_id = @stock_column.repository_stock_unit_items.last.id deleted_id = @stock_column.repository_stock_unit_items.last.id
delete api_v1_team_inventory_column_stock_unit_item_path( delete api_v1_team_inventory_column_stock_unit_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column.id column_id: @stock_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -347,7 +348,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
it 'Invalid request, non existing inventory stock unit item' do it 'Invalid request, non existing inventory stock unit item' do
delete api_v1_team_inventory_column_stock_unit_item_path( delete api_v1_team_inventory_column_stock_unit_item_path(
id: 1001, id: 1001,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column.id column_id: @stock_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -358,7 +359,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
deleted_id = @stock_column.repository_stock_unit_items.last.id deleted_id = @stock_column.repository_stock_unit_items.last.id
delete api_v1_team_inventory_column_stock_unit_item_path( delete api_v1_team_inventory_column_stock_unit_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.first.id, team_id: @team1.id,
inventory_id: 9999, inventory_id: 9999,
column_id: @stock_column.id column_id: @stock_column.id
), headers: @valid_headers ), headers: @valid_headers
@ -370,7 +371,7 @@ RSpec.describe 'Api::V1::InventoryStockUnitItemsController', type: :request do
deleted_id = @stock_column.repository_stock_unit_items.last.id deleted_id = @stock_column.repository_stock_unit_items.last.id
delete api_v1_team_inventory_column_stock_unit_item_path( delete api_v1_team_inventory_column_stock_unit_item_path(
id: deleted_id, id: deleted_id,
team_id: @teams.second.id, team_id: @team2.id,
inventory_id: @valid_inventory.id, inventory_id: @valid_inventory.id,
column_id: @stock_column.id column_id: @stock_column.id
), headers: @valid_headers ), headers: @valid_headers

View file

@ -6,7 +6,6 @@ RSpec.describe 'Api::V1::ProjectFoldersController', type: :request do
let(:user) { create :user } let(:user) { create :user }
let(:valid_headers) { { 'Authorization': 'Bearer ' + generate_token(user.id) } } let(:valid_headers) { { 'Authorization': 'Bearer ' + generate_token(user.id) } }
let(:team) { create :team, created_by: user } let(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, team: team, user: user }
let(:project_folder) do let(:project_folder) do
create :project_folder, team: team create :project_folder, team: team
end end

View file

@ -7,12 +7,11 @@ RSpec.describe "Api::V1::ProjectUserAssignmentsController", type: :request do
@user = create(:user) @user = create(:user)
@another_user = create(:user) @another_user = create(:user)
@team = create(:team, created_by: @user) @team = create(:team, created_by: @user)
create(:user_team, user: @user, team: @team, role: :normal_user) @normal_user_role = create :normal_user_role
create(:user_team, user: @another_user, team: @team, role: :normal_user) create_user_assignment(@team, @normal_user_role, @another_user)
@own_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team) @own_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@invalid_project = @invalid_project =
create(:project, name: Faker::Name.unique.name, created_by: @another_user, team: @team, visibility: :hidden) create(:project, name: Faker::Name.unique.name, created_by: @another_user, team: @team, visibility: :hidden)
@normal_user_role = create :normal_user_role
@valid_headers = { 'Authorization': 'Bearer ' + generate_token(@user.id) } @valid_headers = { 'Authorization': 'Bearer ' + generate_token(@user.id) }
end end
@ -173,7 +172,6 @@ RSpec.describe "Api::V1::ProjectUserAssignmentsController", type: :request do
describe 'PATCH #update' do describe 'PATCH #update' do
before :all do before :all do
@valid_headers['Content-Type'] = 'application/json' @valid_headers['Content-Type'] = 'application/json'
create(:user_project, user: @another_user, project: @own_project)
@technician_user_role = create :technician_role @technician_user_role = create :technician_role
end end
@ -182,7 +180,7 @@ RSpec.describe "Api::V1::ProjectUserAssignmentsController", type: :request do
api_v1_team_project_user_path( api_v1_team_project_user_path(
team_id: @own_project.team.id, team_id: @own_project.team.id,
project_id: @own_project.id, project_id: @own_project.id,
id: UserAssignment.first.id id: @own_project.user_assignments.first.id
), ),
params: request_body.to_json, params: request_body.to_json,
headers: @valid_headers headers: @valid_headers

View file

@ -5,19 +5,20 @@ require 'rails_helper'
RSpec.describe 'Api::V1::ProjectsController', type: :request do RSpec.describe 'Api::V1::ProjectsController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @another_user = create(:user)
create(:user_team, user: @user, team: @teams.first, role: 2) @team1 = create(:team, created_by: @user)
owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) @team2 = create(:team, created_by: @another_user)
# valid_projects # valid_projects
2.times do 2.times do
project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @teams.first) project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team1)
end end
# unaccessable_projects # unaccessable_projects
create(:project, name: Faker::Name.unique.name, create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @another_user, team: @team2)
create(:project, name: Faker::Name.unique.name, create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @another_user, team: @team2)
@valid_headers = @valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) } { 'Authorization': 'Bearer ' + generate_token(@user.id) }
@ -26,13 +27,13 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
describe 'GET projects, #index' do describe 'GET projects, #index' do
it 'Response with correct projects' do it 'Response with correct projects' do
hash_body = nil hash_body = nil
get api_v1_team_projects_path(team_id: @teams.first.id), get api_v1_team_projects_path(team_id: @team1.id),
headers: @valid_headers headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
JSON.parse( JSON.parse(
ActiveModelSerializers::SerializableResource ActiveModelSerializers::SerializableResource
.new(@teams.first.projects, each_serializer: Api::V1::ProjectSerializer) .new(@team1.projects, each_serializer: Api::V1::ProjectSerializer)
.to_json .to_json
)['data'] )['data']
) )
@ -40,7 +41,7 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_projects_path(team_id: @teams.second.id), get api_v1_team_projects_path(team_id: @team2.id),
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -59,14 +60,14 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
describe 'GET project, #show' do describe 'GET project, #show' do
it 'When valid request, user can read project' do it 'When valid request, user can read project' do
hash_body = nil hash_body = nil
get api_v1_team_project_path(team_id: @teams.first.id, get api_v1_team_project_path(team_id: @team1.id,
id: @teams.first.projects.first.id), id: @team1.projects.first.id),
headers: @valid_headers headers: @valid_headers
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match( expect(hash_body[:data]).to match(
JSON.parse( JSON.parse(
ActiveModelSerializers::SerializableResource ActiveModelSerializers::SerializableResource
.new(@teams.first.projects.first, serializer: Api::V1::ProjectSerializer) .new(@team1.projects.first, serializer: Api::V1::ProjectSerializer)
.to_json .to_json
)['data'] )['data']
) )
@ -74,8 +75,8 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_project_path(team_id: @teams.second.id, get api_v1_team_project_path(team_id: @team2.id,
id: @teams.second.projects.first.id), id: @team2.projects.first.id),
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -84,7 +85,7 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
it 'When invalid request, non existing project' do it 'When invalid request, non existing project' do
hash_body = nil hash_body = nil
get api_v1_team_project_path(team_id: @teams.first.id, id: -1), get api_v1_team_project_path(team_id: @team1.id, id: -1),
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -93,8 +94,8 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
it 'When invalid request, project from another team' do it 'When invalid request, project from another team' do
hash_body = nil hash_body = nil
get api_v1_team_project_path(team_id: @teams.first.id, get api_v1_team_project_path(team_id: @team1.id,
id: @teams.second.projects.first.id), id: @team2.projects.first.id),
headers: @valid_headers headers: @valid_headers
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception
@ -108,7 +109,7 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
end end
let(:action) do let(:action) do
post(api_v1_team_projects_path(team_id: @teams.first.id), params: request_body.to_json, headers: @valid_headers) post(api_v1_team_projects_path(team_id: @team1.id), params: request_body.to_json, headers: @valid_headers)
end end
context 'when has valid params' do context 'when has valid params' do
@ -160,7 +161,7 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
} }
} }
end end
let(:project_folder) { create :project_folder, team: @teams.first } let(:project_folder) { create :project_folder, team: @team1 }
it 'renders 201' do it 'renders 201' do
action action
@ -170,7 +171,7 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
end end
context 'when folder from a different team' do context 'when folder from a different team' do
let(:project_folder) { create :project_folder, team: @teams.last } let(:project_folder) { create :project_folder, team: @team2 }
it do it do
action action
@ -262,7 +263,7 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
} }
} }
end end
let(:project_folder) { create :project_folder, team: @teams.first } let(:project_folder) { create :project_folder, team: @team1 }
it 'renders 201' do it 'renders 201' do
action action

View file

@ -5,14 +5,15 @@ require 'rails_helper'
RSpec.describe 'Api::V1::ResultsController', type: :request do RSpec.describe 'Api::V1::ResultsController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @another_user = create(:user)
create(:user_team, user: @user, team: @teams.first, role: 2) @team1 = create(:team, created_by: @user)
@team2 = create(:team, created_by: @another_user)
@valid_project = create(:project, name: Faker::Name.unique.name, @valid_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
@unaccessible_project = create(:project, name: Faker::Name.unique.name, @unaccessible_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @user, team: @team2)
@valid_experiment = create(:experiment, created_by: @user, @valid_experiment = create(:experiment, created_by: @user,
last_modified_by: @user, project: @valid_project) last_modified_by: @user, project: @valid_project)
@ -60,7 +61,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'Response with correct results' do it 'Response with correct results' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_results_path( get api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task task_id: @valid_task
@ -78,7 +79,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'When invalid request, task from another experiment' do it 'When invalid request, task from another experiment' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_results_path( get api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @unaccessible_task task_id: @unaccessible_task
@ -91,7 +92,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_results_path( get api_v1_team_project_experiment_task_results_path(
team_id: @teams.second.id, team_id: @team2.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task task_id: @valid_task
@ -104,7 +105,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'When invalid request, non existing task' do it 'When invalid request, non existing task' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_results_path( get api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: -1 task_id: -1
@ -145,7 +146,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'Response with correct text result' do it 'Response with correct text result' do
hash_body = nil hash_body = nil
post api_v1_team_project_experiment_task_results_path( post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task task_id: @valid_task
@ -171,7 +172,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'Response with correct text result and TinyMCE images' do it 'Response with correct text result and TinyMCE images' do
hash_body = nil hash_body = nil
post api_v1_team_project_experiment_task_results_path( post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task task_id: @valid_task
@ -199,7 +200,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
hash_body = nil hash_body = nil
@valid_tinymce_hash_body[:included][0][:attributes][:text] = 'Result text 1 [~tiny_mce_id:a1]' @valid_tinymce_hash_body[:included][0][:attributes][:text] = 'Result text 1 [~tiny_mce_id:a1]'
post api_v1_team_project_experiment_task_results_path( post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task task_id: @valid_task
@ -213,7 +214,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
invalid_hash_body = @valid_tinymce_hash_body invalid_hash_body = @valid_tinymce_hash_body
invalid_hash_body[:included][1][:attributes][:file_token] = 'a2' invalid_hash_body[:included][1][:attributes][:file_token] = 'a2'
post api_v1_team_project_experiment_task_results_path( post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task task_id: @valid_task
@ -225,7 +226,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
invalid_hash_body = @valid_tinymce_hash_body invalid_hash_body = @valid_tinymce_hash_body
invalid_hash_body[:included][0][:attributes][:text] = 'Result text 1' invalid_hash_body[:included][0][:attributes][:text] = 'Result text 1'
post api_v1_team_project_experiment_task_results_path( post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task task_id: @valid_task
@ -236,7 +237,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'When invalid request, non existing task' do it 'When invalid request, non existing task' do
hash_body = nil hash_body = nil
post api_v1_team_project_experiment_task_results_path( post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: -1 task_id: -1
@ -249,7 +250,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
post api_v1_team_project_experiment_task_results_path( post api_v1_team_project_experiment_task_results_path(
team_id: @teams.second.id, team_id: @team2.id,
project_id: @unaccessible_project, project_id: @unaccessible_project,
experiment_id: @unaccessible_experiment, experiment_id: @unaccessible_experiment,
task_id: @unaccessible_task task_id: @unaccessible_task
@ -262,7 +263,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'When invalid request, task from another experiment' do it 'When invalid request, task from another experiment' do
hash_body = nil hash_body = nil
post api_v1_team_project_experiment_task_results_path( post api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @unaccessible_task task_id: @unaccessible_task
@ -287,7 +288,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
let(:action) do let(:action) do
post(api_v1_team_project_experiment_task_results_path( post(api_v1_team_project_experiment_task_results_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task task_id: @valid_task
@ -340,7 +341,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'When valid request, user can read result' do it 'When valid request, user can read result' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_result_path( get api_v1_team_project_experiment_task_result_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task, task_id: @valid_task,
@ -359,7 +360,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_result_path( get api_v1_team_project_experiment_task_result_path(
team_id: @teams.second.id, team_id: @team2.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task, task_id: @valid_task,
@ -373,7 +374,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'When invalid request, non existing result' do it 'When invalid request, non existing result' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_result_path( get api_v1_team_project_experiment_task_result_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task, task_id: @valid_task,
@ -387,7 +388,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'When invalid request, result from unaccessible task' do it 'When invalid request, result from unaccessible task' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_result_path( get api_v1_team_project_experiment_task_result_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @unaccessible_task, task_id: @unaccessible_task,
@ -421,7 +422,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
end end
let(:action) do let(:action) do
put(api_v1_team_project_experiment_task_result_path( put(api_v1_team_project_experiment_task_result_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task, task_id: @valid_task,
@ -446,7 +447,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
context 'when has new image for update' do context 'when has new image for update' do
let(:action) do let(:action) do
put(api_v1_team_project_experiment_task_result_path( put(api_v1_team_project_experiment_task_result_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task, task_id: @valid_task,
@ -508,7 +509,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
it 'returns 204' do it 'returns 204' do
put(api_v1_team_project_experiment_task_result_path( put(api_v1_team_project_experiment_task_result_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
task_id: @valid_task, task_id: @valid_task,
@ -526,7 +527,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
# let(:result_text) { @valid_task.results.first } # let(:result_text) { @valid_task.results.first }
# let(:action) do # let(:action) do
# put(api_v1_team_project_experiment_task_result_path( # put(api_v1_team_project_experiment_task_result_path(
# team_id: @teams.first.id, # team_id: @team1.id,
# project_id: @valid_project, # project_id: @valid_project,
# experiment_id: @valid_experiment, # experiment_id: @valid_experiment,
# task_id: @valid_task, # task_id: @valid_task,

View file

@ -9,7 +9,6 @@ RSpec.describe 'Api::V1::StepsController', type: :request do
@project = create(:project, team: @team, created_by: @user) @project = create(:project, team: @team, created_by: @user)
@experiment = create(:experiment, :with_tasks, project: @project, created_by: @user) @experiment = create(:experiment, :with_tasks, project: @project, created_by: @user)
@task = @experiment.my_modules.first @task = @experiment.my_modules.first
create(:user_team, user: @user, team: @team)
@valid_headers = @valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) } { 'Authorization': 'Bearer ' + generate_token(@user.id) }

View file

@ -11,7 +11,6 @@ RSpec.describe 'Api::V1::TablesController', type: :request do
@task = @experiment.my_modules.first @task = @experiment.my_modules.first
@protocol = create(:protocol, my_module: @task) @protocol = create(:protocol, my_module: @task)
@step = create(:step, protocol: @protocol) @step = create(:step, protocol: @protocol)
create(:user_team, user: @user, team: @team)
@valid_headers = { @valid_headers = {
'Authorization': 'Bearer ' + generate_token(@user.id), 'Authorization': 'Bearer ' + generate_token(@user.id),

View file

@ -11,13 +11,9 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
@user = create(:user) @user = create(:user)
@team = create(:team, created_by: @user) @team = create(:team, created_by: @user)
create(:user_team, user: @user, team: @team, role: 1) @project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
@project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @team)
@experiment = create(:experiment, created_by: @user, @experiment = create(:experiment, created_by: @user, last_modified_by: @user, project: @project)
last_modified_by: @user, project: @project)
@my_module = create( @my_module = create(
:my_module, :my_module,
@ -27,14 +23,17 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
experiment: @experiment experiment: @experiment
) )
@repository = create(:repository) @repository = create(:repository, created_by: @user, team: @team)
create(:user_team, user: @user, team: @repository.team, role: 1) @repository_stock_column = create(:repository_column, :stock_type, repository: @repository)
#@repository_stock_column = create(:repository_column, :stock_type, repository: @repository)
@repository_row = create(:repository_row, name: 'Test row', repository: @repository) @repository_row = create(:repository_row, name: 'Test row', repository: @repository)
@repository_stock_cell = create( @repository_stock_unit_item = create(:repository_stock_unit_item, created_by: @user,
:repository_cell, last_modified_by: @user,
:stock_value, repository_column: @repository_stock_column)
repository_row: @repository_row @repository_stock_value = create(
:repository_stock_value,
amount: 100,
repository_stock_unit_item: @repository_stock_unit_item,
repository_cell_attributes: { repository_row: @repository_row, repository_column: @repository_stock_column }
) )
@my_module_repository_row = create( @my_module_repository_row = create(

View file

@ -9,8 +9,6 @@ RSpec.describe "Api::V1::TaskUserAssignmentsController", type: :request do
@team = create(:team, created_by: @user) @team = create(:team, created_by: @user)
@normal_user_role = create :normal_user_role @normal_user_role = create :normal_user_role
@owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) @owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
create(:user_team, user: @user, team: @team, role: :normal_user)
create(:user_team, user: @another_user, team: @team, role: :normal_user)
@own_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team) @own_project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@own_experiment = create :experiment, @own_experiment = create :experiment,
name: Faker::Name.unique.name, name: Faker::Name.unique.name,

View file

@ -7,23 +7,23 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
MyModuleStatusFlow.ensure_default MyModuleStatusFlow.ensure_default
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @another_user = create(:user)
create(:user_team, user: @user, team: @teams.first, role: 2) @team1 = create(:team, created_by: @user)
@owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) @team2 = create(:team, created_by: @another_user)
@valid_project = create(:project, name: Faker::Name.unique.name, @valid_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.first) created_by: @user, team: @team1)
@unaccessible_project = create(:project, name: Faker::Name.unique.name, @unaccessible_project = create(:project, name: Faker::Name.unique.name,
created_by: @user, team: @teams.second) created_by: @another_user, team: @team2)
@valid_experiment = create(:experiment, created_by: @user, @valid_experiment = create(:experiment, created_by: @user,
last_modified_by: @user, project: @valid_project) last_modified_by: @another_user, project: @valid_project)
@unaccessible_experiment = create(:experiment, created_by: @user, @unaccessible_experiment = create(:experiment, created_by: @another_user,
last_modified_by: @user, project: @unaccessible_project) last_modified_by: @another_user, project: @unaccessible_project)
create_list(:my_module, 3, :with_due_date, created_by: @user, create_list(:my_module, 3, :with_due_date, created_by: @user,
last_modified_by: @user, experiment: @valid_experiment) last_modified_by: @user, experiment: @valid_experiment)
create_list(:my_module, 3, :with_due_date, created_by: @user, create_list(:my_module, 3, :with_due_date, created_by: @another_user,
last_modified_by: @user, experiment: @unaccessible_experiment) last_modified_by: @another_user, experiment: @unaccessible_experiment)
@valid_headers = @valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) } { 'Authorization': 'Bearer ' + generate_token(@user.id) }
@ -33,7 +33,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
it 'Response with correct tasks' do it 'Response with correct tasks' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_tasks_path( get api_v1_team_project_experiment_tasks_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment experiment_id: @valid_experiment
), headers: @valid_headers ), headers: @valid_headers
@ -50,7 +50,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
it 'When invalid request, experiment from another project' do it 'When invalid request, experiment from another project' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_tasks_path( get api_v1_team_project_experiment_tasks_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @unaccessible_experiment experiment_id: @unaccessible_experiment
), headers: @valid_headers ), headers: @valid_headers
@ -62,7 +62,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_tasks_path( get api_v1_team_project_experiment_tasks_path(
team_id: @teams.second.id, team_id: @team2.id,
project_id: @unaccessible_project, project_id: @unaccessible_project,
experiment_id: @unaccessible_experiment experiment_id: @unaccessible_experiment
), headers: @valid_headers ), headers: @valid_headers
@ -74,7 +74,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
it 'When invalid request, non existing experiment' do it 'When invalid request, non existing experiment' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_tasks_path( get api_v1_team_project_experiment_tasks_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: -1 experiment_id: -1
), headers: @valid_headers ), headers: @valid_headers
@ -88,7 +88,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
it 'When valid request, user can read task' do it 'When valid request, user can read task' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_path( get api_v1_team_project_experiment_task_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
id: @valid_experiment.my_modules.first.id id: @valid_experiment.my_modules.first.id
@ -106,7 +106,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_path( get api_v1_team_project_experiment_task_path(
team_id: @teams.second.id, team_id: @team2.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
id: @valid_experiment.my_modules.first.id id: @valid_experiment.my_modules.first.id
@ -119,7 +119,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
it 'When invalid request, non existing task' do it 'When invalid request, non existing task' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_path( get api_v1_team_project_experiment_task_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
id: -1 id: -1
@ -132,7 +132,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
it 'When invalid request, task from unaccessible experiment' do it 'When invalid request, task from unaccessible experiment' do
hash_body = nil hash_body = nil
get api_v1_team_project_experiment_task_path( get api_v1_team_project_experiment_task_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project, project_id: @valid_project,
experiment_id: @valid_experiment, experiment_id: @valid_experiment,
id: @unaccessible_experiment.my_modules.first.id id: @unaccessible_experiment.my_modules.first.id
@ -165,7 +165,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
context 'when has valid params' do context 'when has valid params' do
let(:action) do let(:action) do
post(api_v1_team_project_experiment_tasks_path( post(api_v1_team_project_experiment_tasks_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project.id, project_id: @valid_project.id,
experiment_id: @valid_experiment.id experiment_id: @valid_experiment.id
), ),
@ -201,7 +201,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
context 'when has not valid params' do context 'when has not valid params' do
it 'renders 404 when project not found' do it 'renders 404 when project not found' do
post(api_v1_team_project_experiment_tasks_path( post(api_v1_team_project_experiment_tasks_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: -1, project_id: -1,
experiment_id: @valid_experiment.id experiment_id: @valid_experiment.id
), ),
@ -213,7 +213,7 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
it 'renders 403 when user is not member of the team' do it 'renders 403 when user is not member of the team' do
post(api_v1_team_project_experiment_tasks_path( post(api_v1_team_project_experiment_tasks_path(
team_id: @teams.second.id, team_id: @team2.id,
project_id: @valid_project.id, project_id: @valid_project.id,
experiment_id: @valid_experiment.id experiment_id: @valid_experiment.id
), ),
@ -224,12 +224,11 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
end end
it 'renders 403 for use with view permissions' do it 'renders 403 for use with view permissions' do
user_assignment = UserAssignment.where(user: @user, assignable: @valid_experiment) user_assignment = UserAssignment.where(user: @user, assignable: @valid_experiment).first
.first
user_assignment.update!(user_role: create(:viewer_role)) user_assignment.update!(user_role: create(:viewer_role))
post(api_v1_team_project_experiment_tasks_path( post(api_v1_team_project_experiment_tasks_path(
team_id: @teams.first.id, team_id: @team1.id,
project_id: @valid_project.id, project_id: @valid_project.id,
experiment_id: @valid_experiment.id experiment_id: @valid_experiment.id
), ),

View file

@ -6,8 +6,7 @@ RSpec.describe 'Api::V1::TeamsController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 3, created_by: @user) @teams = create_list(:team, 3, created_by: @user)
create(:user_team, user: @user, team: @teams.second, role: 2) @owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
create(:user_team, user: @user, team: @teams.third, role: 2)
@valid_headers = @valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) } { 'Authorization': 'Bearer ' + generate_token(@user.id) }
end end
@ -43,6 +42,7 @@ RSpec.describe 'Api::V1::TeamsController', type: :request do
it 'When invalid request, user in not member of the team' do it 'When invalid request, user in not member of the team' do
hash_body = nil hash_body = nil
@teams.first.user_assignments.delete_all
get api_v1_team_path(id: @teams.first.id), headers: @valid_headers get api_v1_team_path(id: @teams.first.id), headers: @valid_headers
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception expect { hash_body = json }.not_to raise_exception

View file

@ -10,10 +10,11 @@ RSpec.describe 'Api::V1::UsersController', type: :request do
@team1 = create(:team, created_by: @user1) @team1 = create(:team, created_by: @user1)
@team2 = create(:team, created_by: @user2) @team2 = create(:team, created_by: @user2)
@team3 = create(:team, created_by: @user3) @team3 = create(:team, created_by: @user3)
create(:user_team, user: @user1, team: @team1, role: 2) @owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
create(:user_team, user: @user2, team: @team1, role: 2) create_user_assignment(@team1, @owner_role, @user1)
create(:user_team, user: @user2, team: @team2, role: 2) create_user_assignment(@team1, @owner_role, @user2)
create(:user_team, user: @user3, team: @team3, role: 2) create_user_assignment(@team2, @owner_role, @user2)
create_user_assignment(@team3, @owner_role, @user3)
@valid_headers = @valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user1.id) } { 'Authorization': 'Bearer ' + generate_token(@user1.id) }
end end

View file

@ -6,7 +6,8 @@ RSpec.describe 'Api::V1::WrokflowStatusesController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @teams = create_list(:team, 2, created_by: @user)
create(:user_team, user: @user, team: @teams.first, role: 2) @owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
create_user_assignment(@teams.first, @owner_role, @user)
@valid_headers = @valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) } { 'Authorization': 'Bearer ' + generate_token(@user.id) }
MyModuleStatusFlow.ensure_default MyModuleStatusFlow.ensure_default

View file

@ -6,7 +6,8 @@ RSpec.describe 'Api::V1::WrokflowsController', type: :request do
before :all do before :all do
@user = create(:user) @user = create(:user)
@teams = create_list(:team, 2, created_by: @user) @teams = create_list(:team, 2, created_by: @user)
create(:user_team, user: @user, team: @teams.first, role: 2) @owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
create_user_assignment(@teams.first, @owner_role, @user)
@valid_headers = @valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) } { 'Authorization': 'Bearer ' + generate_token(@user.id) }
MyModuleStatusFlow.ensure_default MyModuleStatusFlow.ensure_default

View file

@ -5,8 +5,8 @@ require 'rails_helper'
describe Activities::ActivityFilterMatchingService do describe Activities::ActivityFilterMatchingService do
let(:user) { create :user } let(:user) { create :user }
let(:user_2) { create :user } let(:user_2) { create :user }
let(:team) { create :team, :with_members } let(:team) { create :team, created_by: user }
let(:team_2) { create :team } let(:team_2) { create :team, created_by: user_2 }
let(:project) do let(:project) do
create :project, team: team, user_projects: [] create :project, team: team, user_projects: []
end end

View file

@ -4,7 +4,7 @@ require 'rails_helper'
describe Activities::CreateActivityService do describe Activities::CreateActivityService do
let(:user) { create :user } let(:user) { create :user }
let(:team) { create :team, :with_members } let(:team) { create :team, created_by: user }
let(:project) do let(:project) do
create :project, team: team, user_projects: [] create :project, team: team, user_projects: []
end end

View file

@ -1,140 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
describe ClientApi::InvitationsService do
let(:team_one) { create :team }
let(:user_one) { create :user, email: Faker::Internet.email }
let(:emails_one) { Array.new(3) { Faker::Internet.email } }
it 'raises an ClientApi::CustomInvitationsError if ' \
'role is not assigned' do
expect do
ClientApi::InvitationsService.new(user: user_one,
team: team_one,
emails: emails_one)
end.to raise_error(ClientApi::CustomInvitationsError)
end
it 'raises an ClientApi::CustomInvitationsError if ' \
'emails are not assigned' do
expect do
ClientApi::InvitationsService.new(user: user_one,
team: team_one,
role: 'normal_user')
end.to raise_error(ClientApi::CustomInvitationsError)
end
it 'raises an ClientApi::CustomInvitationsError if ' \
'emails are not present' do
expect do
ClientApi::InvitationsService.new(user: user_one,
team: team_one,
role: 'normal_user',
emails: [])
end.to raise_error(ClientApi::CustomInvitationsError)
end
it 'raises an ClientApi::CustomInvitationsError if ' \
'role is not included in UserTeam.roles' do
expect do
ClientApi::InvitationsService.new(user: user_one,
team: team_one,
role: 'abnormal_user',
emails: emails_one)
end.to raise_error(ClientApi::CustomInvitationsError)
end
describe '#invitation' do
it 'returns too many emails response if invite users limit is exceeded' do
emails_exceeded = Array.new(Constants::INVITE_USERS_LIMIT + 1) do
Faker::Internet.email
end
invitation_service = ClientApi::InvitationsService.new(
user: user_one,
team: team_one,
role: 'normal_user',
emails: emails_exceeded
)
result = invitation_service.invitation
expect(result.last[:status]).to eq :too_many_emails
expect(result.count).to eq Constants::INVITE_USERS_LIMIT + 1
end
context 'when user is new' do
it 'returns invalid response if invited user is not valid' do
invitation_service = ClientApi::InvitationsService.new(
user: user_one,
role: 'normal_user',
emails: ['banana.man']
)
result_status = invitation_service.invitation.last[:status]
expect(result_status).to eq :user_invalid
end
it 'invites new user' do
invitation_service = ClientApi::InvitationsService.new(
user: user_one,
role: 'normal_user',
emails: ['new@banana.net']
)
result = invitation_service.invitation
expect(result.last[:status]).to eq :user_created
# test functions result
expect(result.last[:user].email).to eq 'new@banana.net'
expect(result.last[:user].invited_by).to eq user_one
# test in database
expect(User.last.email).to eq 'new@banana.net'
expect(User.last.invited_by).to eq user_one
end
it 'creates user-team relation and notification if team present' do
invitation_service = ClientApi::InvitationsService.new(
user: user_one,
team: team_one,
role: 'normal_user',
emails: ['new@banana.net']
)
result_status = invitation_service.invitation.last[:status]
expect(result_status).to eq :user_created_invited_to_team
expect(User.find_by_email('new@banana.net').teams).to include team_one
expect(Notification.last.users.last[:email]).to eq 'new@banana.net'
end
end
context 'when user already exists' do
let(:user_two) { create :user, email: Faker::Internet.email }
let(:service_one) do
ClientApi::InvitationsService.new(user: user_one,
team: team_one,
role: 'normal_user',
emails: [user_two.email])
end
it 'returns information, that user already exists' do
invitation_service = ClientApi::InvitationsService.new(
user: user_one,
role: 'normal_user',
emails: [user_two.email]
)
result_status = invitation_service.invitation.last[:status]
expect(result_status).to eq :user_exists_unconfirmed
end
it 'returns user exists in team response if team present ' \
'and user already part of the team' do
create :user_team, team: team_one, user: user_two
result_status = service_one.invitation.last[:status]
expect(result_status).to eq :user_exists_and_in_team_unconfirmed
end
it 'creates user-team relation and notification if team present ' \
'and user not part of the team' do
result_status = service_one.invitation.last[:status]
expect(result_status).to eq :user_exists_invited_to_team_unconfirmed
expect(User.find_by_email(user_two.email).teams).to include team_one
expect(Notification.last.users.last[:email]).to eq user_two.email
end
end
end
end

View file

@ -1,77 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
include ClientApi::Teams
describe ClientApi::Teams::CreateService do
let(:user) { create :user, email: 'user@asdf.com' }
let(:team) do
build :team, name: 'My Team', description: 'My Description'
end
it 'should raise a StandardError if current_user is not assigned' do
expect { CreateService.new }.to raise_error(StandardError)
end
it 'should create a new team' do
service = CreateService.new(
current_user: user,
params: { name: team.name, description: team.description }
)
result = service.execute
expect(result[:status]).to eq :success
team_n = Team.order(created_at: :desc).first
expect(team_n.name).to eq team.name
expect(team_n.description).to eq team.description
expect(team_n.created_by).to eq user
expect(team_n.users.count).to eq 1
expect(team_n.users.take).to eq user
end
it 'should return error response if params = {}' do
service = CreateService.new(current_user: user, params: {})
result = service.execute
expect(result[:status]).to eq :error
end
it 'should return error response if params are missing :name attribute' do
service = CreateService.new(
current_user: user,
params: { description: team.description }
)
result = service.execute
expect(result[:status]).to eq :error
end
it 'should return error response if name too short' do
team.name = ('a' * (Constants::NAME_MIN_LENGTH - 1)).to_s
service = CreateService.new(
current_user: user,
params: { name: team.name, description: team.description }
)
result = service.execute
expect(result[:status]).to eq :error
end
it 'should return error response if name too long' do
team.name = ('a' * (Constants::NAME_MAX_LENGTH + 1)).to_s
service = CreateService.new(
current_user: user,
params: { name: team.name, description: team.description }
)
result = service.execute
expect(result[:status]).to eq :error
end
it 'should return error response if description too long' do
team.description = ('a' * (Constants::TEXT_MAX_LENGTH + 1)).to_s
service = CreateService.new(
current_user: user,
params: { name: team.name, description: team.description }
)
result = service.execute
expect(result[:status]).to eq :error
end
end

View file

@ -1,112 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
describe ClientApi::TeamsService do
let(:team_one) { create :team }
let(:user_one) { create :user }
it 'should raise an ClientApi::CustomTeamError if user is not assigned' do
expect do
ClientApi::TeamsService.new(team_id: team_one.id)
end.to raise_error(ClientApi::CustomTeamError)
end
it 'should raise an ClientApi::CustomTeamError if team is not assigned' do
expect do
ClientApi::TeamsService.new(current_user: user_one)
end.to raise_error(ClientApi::CustomTeamError)
end
it 'should raise an ClientApi::CustomTeamError if team is not user team' do
expect do
ClientApi::TeamsService.new(current_user: user_one, team_id: team_one.id)
end.to raise_error(ClientApi::CustomTeamError)
end
describe '#change_current_team!' do
let(:team_two) { create :team, name: 'team two' }
let(:user_two) do
create :user, current_team_id: team_one.id, email: 'user_two@test.com'
end
it 'should change user current team' do
create :user_team, user: user_two, team: team_two
teams_service = ClientApi::TeamsService.new(current_user: user_two,
team_id: team_two.id)
teams_service.change_current_team!
expect(user_two.current_team_id).to eq team_two.id
end
end
describe '#team_page_details_data' do
let(:team_service) do
ClientApi::TeamsService.new(current_user: user_one, team_id: team_one.id)
end
it 'should return team page data' do
user_team = create :user_team, user: user_one, team: team_one
data = team_service.team_page_details_data
expect(data.fetch(:team).name).to eq team_one.name
expect(data.fetch(:team_users).first).to eq user_team
end
end
describe '#teams_data' do
let(:team_service) do
ClientApi::TeamsService.new(current_user: user_one, team_id: team_one.id)
end
it 'should return an array of valid teams' do
create :user_team, user: user_one, team: team_one
expect(team_service.teams_data).to(
match_response_schema('datatables_teams')
)
end
end
describe '#update_team!' do
let(:team_two) { create :team, name: 'Banana', created_by: user_one }
it 'should raise an error if input invalid' do
create :user_team, user: user_one, team: team_one
team_service = ClientApi::TeamsService.new(
current_user: user_one,
team_id: team_one.id,
params: {
description: "super long: #{'a' * Constants::TEXT_MAX_LENGTH}"
}
)
expect do
team_service.update_team!
end.to raise_error(ClientApi::CustomTeamError)
end
it 'should update the team description if the input is valid' do
create :user_team, user: user_one, team: team_two
desc = 'Banana Team description'
team_service = ClientApi::TeamsService.new(
current_user: user_one,
team_id: team_two.id,
params: {
description: desc
}
)
team_service.update_team!
# load values from db
team_two.reload
expect(team_two.description).to eq desc
end
end
describe '#single_team_details_data' do
let(:team_service) do
ClientApi::TeamsService.new(current_user: user_one, team_id: team_one.id)
end
it 'should return a team object' do
create :user_team, user: user_one, team: team_one
expect(team_service.single_team_details_data.fetch(:team)).to eq team_one
end
end
end

View file

@ -1,138 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
describe ClientApi::UserTeamService do
let(:team_one) { create :team }
let(:user_one) { create :user, email: Faker::Internet.email }
let(:user_two) { create :user, email: Faker::Internet.email }
let(:user_team) { create :user_team, :admin, user: user_one, team: team_one }
it 'should raise ClientApi::CustomUserTeamError if user is not assigned' do
expect do
ClientApi::UserTeamService.new(
team_id: team_one.id,
user_team_id: user_team.id
)
end.to raise_error(ClientApi::CustomUserTeamError)
end
it 'should raise ClientApi::CustomUserTeamError if team is not assigned' do
expect do
ClientApi::UserTeamService.new(user: user_one, user_team_id: user_team.id)
end.to raise_error(ClientApi::CustomUserTeamError)
end
it 'should raise ClientApi::CustomUserTeamError if ' \
'user_team is not assigned' do
expect do
ClientApi::UserTeamService.new(user: user_one, team_id: team_one.id)
end.to raise_error(ClientApi::CustomUserTeamError)
end
describe '#destroy_user_team_and_assign_new_team_owner!' do
it 'should raise ClientApi::CustomUserTeamError if user ' \
'can\'t leave the team' do
ut_service = ClientApi::UserTeamService.new(
team_id: team_one.id,
user_team_id: user_team.id,
user: user_one
)
expect do
ut_service.destroy_user_team_and_assign_new_team_owner!
end.to raise_error(ClientApi::CustomUserTeamError)
end
it 'should destroy the user_team relation' do
create :user_team, :admin, team: team_one, user: user_one
new_user_team = create :user_team, team: team_one, user: user_two
ut_service = ClientApi::UserTeamService.new(
team_id: team_one.id,
user_team_id: new_user_team.id,
user: user_one
)
ut_service.destroy_user_team_and_assign_new_team_owner!
expect(team_one.users).to_not include user_two
end
it 'should assign a new owner to the team' do
user_team_one = create :user_team, team: team_one, user: user_one
create :user_team, :admin, team: team_one, user: user_two
ut_service = ClientApi::UserTeamService.new(
team_id: team_one.id,
user_team_id: user_team_one.id,
user: user_one
)
ut_service.destroy_user_team_and_assign_new_team_owner!
expect(team_one.users).to include user_two
end
end
describe '#update_role!' do
it 'should raise ClientApi::CustomUserTeamError if no role is set' do
ut_service = ClientApi::UserTeamService.new(
user: user_one,
team_id: team_one.id,
user_team_id: user_team.id
)
expect do
ut_service.update_role!
end.to raise_error(ClientApi::CustomUserTeamError)
end
it 'should update user role' do
create :user_team, team: team_one, user: user_two
user_team = create :user_team, team: team_one, user: user_one
ut_service = ClientApi::UserTeamService.new(
user: user_one,
team_id: team_one.id,
user_team_id: user_team.id,
role: 1
)
ut_service.update_role!
user_team.reload
expect(user_team.role).to eq 'normal_user'
end
it 'should raise ClientApi::CustomUserTeamError if is the last ' \
'admin on the team' do
user_team = create :user_team, :admin, team: team_one, user: user_one
ut_service = ClientApi::UserTeamService.new(
user: user_one,
team_id: team_one.id,
user_team_id: user_team.id,
role: 1
)
expect do
ut_service.update_role!
end.to raise_error(ClientApi::CustomUserTeamError)
end
end
describe '#team_users_data' do
it 'should return a hash of team members' do
user_team = create :user_team, team: team_one, user: user_one
ut_service = ClientApi::UserTeamService.new(
user: user_one,
team_id: team_one.id,
user_team_id: user_team.id,
role: 1
)
expect(ut_service.team_users_data.fetch(:team_users)).to include user_team
end
end
describe '#teams_data' do
it 'should return a list of teams where user is a member' do
user_team = create :user_team, team: team_one, user: user_one
ut_service = ClientApi::UserTeamService.new(
user: user_one,
team_id: team_one.id,
user_team_id: user_team.id,
role: 1
)
team_id = ut_service.teams_data[:teams].first.id
expect(team_id).to eq team_one.id
end
end
end

View file

@ -1,81 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
include ClientApi::Users
describe ClientApi::Users::UpdateService do
let(:user) do
create :user,
full_name: 'User One',
initials: 'UO',
email: 'user@happy.com',
password: 'asdf1234',
password_confirmation: 'asdf1234'
end
it 'should update user email if the password is correct' do
email = 'new_user@happy.com'
params = { email: email, current_password: 'asdf1234' }
service = UpdateService.new(current_user: user,
params: params)
result = service.execute
expect(result[:status]).to eq :success
expect(user.email).to eq(email)
end
it 'should raise CustomUserError error if the password is not correct' do
email = 'new_user@happy.com'
params = { email: email, current_password: 'banana' }
service = UpdateService.new(current_user: user,
params: params)
result = service.execute
expect(result[:status]).to eq :error
end
it 'should update initials and full name without password confirmation' do
full_name = 'Happy User'
initials = 'HU'
service = UpdateService.new(
current_user: user,
params: { full_name: full_name, initials: initials }
)
result = service.execute
expect(result[:status]).to eq :success
expect(user.full_name).to eq(full_name)
expect(user.initials).to eq(initials)
end
it 'should raise an error if current password not present' do
service = UpdateService.new(
current_user: user,
params: { password: 'hello1234', password_confirmation: 'hello1234' }
)
result = service.execute
expect(result[:status]).to eq :error
end
it 'should raise an error if password_confirmation don\'t match' do
service = UpdateService.new(
current_user: user,
params: { password: 'hello1234',
password_confirmation: 'hello1234567890',
current_password: 'asdf1234' }
)
result = service.execute
expect(result[:status]).to eq :error
end
it 'should update the password' do
new_password = 'hello1234'
service = UpdateService.new(
current_user: user,
params: { password: new_password,
password_confirmation: new_password,
current_password: 'asdf1234' }
)
result = service.execute
expect(result[:status]).to eq :success
expect(user.valid_password?(new_password)).to be(true)
end
end

View file

@ -3,19 +3,19 @@
require 'rails_helper' require 'rails_helper'
describe Experiments::CopyExperimentAsTemplateService do describe Experiments::CopyExperimentAsTemplateService do
let(:team) { create :team, :with_members } let(:user) { create :user }
let(:user_project) { create :user_project, :normal_user, user: user } let(:team) { create :team, created_by: user }
let(:project) do let(:project) do
create :project, team: team create :project, team: team, created_by: user
end end
let(:new_project) do let(:new_project) do
create :project, team: team, user_projects: [user_project] create :project, team: team, created_by: user
end end
let(:experiment) do let(:experiment) do
create :experiment, :with_tasks, name: 'MyExp', project: project create :experiment, :with_tasks, name: 'MyExp', project: project
end end
let(:user) { create :user }
let(:service_call) do let(:service_call) do
Experiments::CopyExperimentAsTemplateService.call(experiment: experiment, project: new_project, user: user) Experiments::CopyExperimentAsTemplateService.call(experiment: experiment, project: new_project, user: user)
end end

View file

@ -3,8 +3,8 @@
require 'rails_helper' require 'rails_helper'
describe Experiments::MoveToProjectService do describe Experiments::MoveToProjectService do
let(:team) { create :team, :with_members }
let(:user) { create :user } let(:user) { create :user }
let(:team) { create :team, created_by: user }
let(:project) do let(:project) do
create :project, team: team, created_by: user create :project, team: team, created_by: user
end end
@ -91,7 +91,7 @@ describe Experiments::MoveToProjectService do
end end
it 'returns error if teams is not the same' do it 'returns error if teams is not the same' do
t = create :team, :with_members t = create :team
project.update(team: t) project.update(team: t)
expect(service_call.errors).to have_key(:target_project_not_valid) expect(service_call.errors).to have_key(:target_project_not_valid)

View file

@ -18,12 +18,12 @@ describe TeamImporter do
# when importing to avoid any defaults # when importing to avoid any defaults
create :user create :user
@user = create :user @user = create :user
@team = create :team @team = create :team, created_by: @user
create :project, name: 'Temp project', visibility: 1, create :project, name: 'Temp project', visibility: 1,
team: @team, archived: false, created_at: time team: @team, archived: false, created_at: time , created_by: @user
@project = create :project, name: 'Project', visibility: 1, team: @team, @project = create :project, name: 'Project', visibility: 1, team: @team,
archived: false, created_at: time archived: false, created_at: time , created_by: @user
# Reassign if multiple tests are run # Reassign if multiple tests are run
PROJECT_ID = @project.id PROJECT_ID = @project.id
@ -181,9 +181,6 @@ describe TeamImporter do
expect(db_protocol.updated_at).to eq( expect(db_protocol.updated_at).to eq(
json_protocol['updated_at'].to_time json_protocol['updated_at'].to_time
) )
expect(db_protocol.added_by_id).to eq(
json_protocol.dig('added_by_id')
)
expect(db_protocol.archived_by_id).to be_nil expect(db_protocol.archived_by_id).to be_nil
expect(db_protocol.archived_on).to be_nil expect(db_protocol.archived_on).to be_nil
expect(db_protocol.restored_by_id).to be_nil expect(db_protocol.restored_by_id).to be_nil

Some files were not shown because too many files have changed in this diff Show more