diff --git a/spec/controllers/assets_controller_spec.rb b/spec/controllers/assets_controller_spec.rb index 11fef9a54..453d56a37 100644 --- a/spec/controllers/assets_controller_spec.rb +++ b/spec/controllers/assets_controller_spec.rb @@ -5,26 +5,13 @@ require 'rails_helper' describe AssetsController, type: :controller do login_user - let(:user) { subject.current_user } - let!(:team) { create :team, created_by: user } - let(:user_team) { create :user_team, :admin, user: user, team: team } - let!(:user_project) { create :user_project, user: user } - let(:project) do - create :project, team: team, user_projects: [user_project] - end - let(:owner_role) { create :owner_role } - let(:experiment) { create :experiment, project: project } - let(:my_module) { create :my_module, name: 'test task', experiment: experiment } - let(:protocol) do - create :protocol, my_module: my_module, team: team, added_by: user - end - let(:step) { create :step, protocol: protocol, user: user } - let(:step_asset_task) { create :step_asset, step: step } - - let(:result) do - create :result, name: 'test result', my_module: my_module, user: user - end - let(:result_asset) { create :result_asset, result: result } + include_context 'reference_project_structure', { + role: :owner_role, + result_asset: true, + step: true, + step_asset: true, + result_comment: true + } let(:protocol_in_repository) { create :protocol, :in_public_repository, team: team } let(:step_in_repository) { create :step, protocol: protocol_in_repository, user: user } @@ -36,7 +23,6 @@ describe AssetsController, type: :controller do describe 'POST start_edit' do before do - create_user_assignment(my_module, owner_role, user) allow(controller).to receive(:check_edit_permission).and_return(true) end let(:action) { post :create_start_edit_image_activity, params: params, format: :json } @@ -44,11 +30,10 @@ describe AssetsController, type: :controller do { id: nil } end it 'calls create activity service (start edit image on step)' do - params[:id] = step_asset_task.asset.id + params[:id] = step_asset.asset.id expect(Activities::CreateActivityService).to receive(:call) .with(hash_including(activity_type: :edit_image_on_step)) action - p response.code end it 'calls create activity service (start edit image on result)' do @@ -67,7 +52,7 @@ describe AssetsController, type: :controller do end it 'adds activity in DB' do - params[:id] = step_asset_task.asset.id + params[:id] = step_asset.asset.id expect { action } .to(change { Activity.count }) end diff --git a/spec/controllers/canvas_controller_spec.rb b/spec/controllers/canvas_controller_spec.rb index cb57e6273..57881e9b0 100644 --- a/spec/controllers/canvas_controller_spec.rb +++ b/spec/controllers/canvas_controller_spec.rb @@ -4,19 +4,10 @@ require 'rails_helper' describe CanvasController do login_user + include_context 'reference_project_structure', { + skip_my_module: true + } - let(:user) { subject.current_user } - let(:team) { create :team, created_by: user } - let!(:user_team) { create :user_team, :admin, user: user, team: team } - let!(:user_project) do - create :user_project, user: user, project: project - end - let(:project) do - create :project, team: team - end - let(:owner_role) { create :owner_role } - - let(:experiment) { create :experiment, project: project } let(:experiment2) { create :experiment, project: project } # Idea of this "end to end" test is to put a lot "work" on method `@experiment.udpate_canvas` and controller actipn @@ -94,9 +85,9 @@ describe CanvasController do before do 8.times do |i| - create_user_assignment(public_send("task#{i+1}"), owner_role, user) + create_user_assignment(public_send("task#{i+1}"), role, user) end - create_user_assignment(experiment2, owner_role, user) + create_user_assignment(experiment2, role, user) end context 'when have a lot changes on canvas' do diff --git a/spec/controllers/experiments_controller_spec.rb b/spec/controllers/experiments_controller_spec.rb index f07198da2..cadd73cf0 100644 --- a/spec/controllers/experiments_controller_spec.rb +++ b/spec/controllers/experiments_controller_spec.rb @@ -4,16 +4,8 @@ require 'rails_helper' describe ExperimentsController, type: :controller do login_user + include_context 'reference_project_structure' - let!(:user) { controller.current_user } - let!(:team) { create :team, created_by: user, users: [user] } - let!(:project) { create :project, team: team } - let(:owner_role) { create :owner_role } - let(:experiment) { create :experiment, project: project } - - before do - create_user_assignment(experiment, owner_role, user) - end describe 'POST create' do let(:action) { post :create, params: params, format: :json } @@ -78,7 +70,7 @@ describe ExperimentsController, type: :controller do end before do - create_user_assignment(archived_experiment, owner_role, user) + create_user_assignment(archived_experiment, role, user) end it 'calls create activity for unarchiving experiment' do diff --git a/spec/controllers/my_module_comments_controller_spec.rb b/spec/controllers/my_module_comments_controller_spec.rb index 33153c1ed..ab0f3a038 100644 --- a/spec/controllers/my_module_comments_controller_spec.rb +++ b/spec/controllers/my_module_comments_controller_spec.rb @@ -4,20 +4,10 @@ require 'rails_helper' describe MyModuleCommentsController, type: :controller do login_user - - let(:user) { subject.current_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(:normal_user_role) { create :normal_user_role } - - let(:experiment) { create :experiment, project: project } - let(:my_module) { create :my_module, experiment: experiment } - let(:task_comment) { create :task_comment, user: user, my_module: my_module } - - before do - create_user_assignment(my_module, normal_user_role, user) - end + include_context 'reference_project_structure' , { + role: :normal_user_role, + my_module_comment: true + } describe 'POST create' do let(:action) { post :create, params: params, format: :json } @@ -42,7 +32,7 @@ describe MyModuleCommentsController, type: :controller do let(:action) { put :update, params: params, format: :json } let(:params) do { my_module_id: my_module.id, - id: task_comment.id, + id: my_module_comment.id, comment: { message: 'comment updated' } } end @@ -62,7 +52,7 @@ describe MyModuleCommentsController, type: :controller do describe 'DELETE destroy' do let(:action) { delete :destroy, params: params, format: :json } let(:params) do - { my_module_id: my_module.id, id: task_comment.id } + { my_module_id: my_module.id, id: my_module_comment.id } end it 'calls create activity for deleting comment on task' do diff --git a/spec/controllers/my_module_repositories_controller_spec.rb b/spec/controllers/my_module_repositories_controller_spec.rb index 1a9b47ae0..77ca0b71b 100644 --- a/spec/controllers/my_module_repositories_controller_spec.rb +++ b/spec/controllers/my_module_repositories_controller_spec.rb @@ -4,16 +4,9 @@ require 'rails_helper' describe MyModuleRepositoriesController, type: :controller do login_user - - let(:user) { subject.current_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!(:user_project) do - create :user_project, user: user, project: project - end - let(:normal_user_role) { create :normal_user_role } - + include_context 'reference_project_structure' , { + role: :normal_user_role + } let!(:repository) { create :repository, created_by: user, team: team } let!(:repository_row) do create :repository_row, created_by: user, repository: repository @@ -21,12 +14,6 @@ describe MyModuleRepositoriesController, type: :controller do let!(:repository_row_2) do create :repository_row, created_by: user, repository: repository end - let(:experiment) { create :experiment, project: project } - let(:my_module) { create :my_module, experiment: experiment } - - before do - create_user_assignment(my_module, normal_user_role, user) - end describe 'PUT update (assign repository records)' do let(:params) do diff --git a/spec/controllers/my_module_tags_controller_spec.rb b/spec/controllers/my_module_tags_controller_spec.rb index 63427bf74..3a2d5c1b6 100644 --- a/spec/controllers/my_module_tags_controller_spec.rb +++ b/spec/controllers/my_module_tags_controller_spec.rb @@ -4,21 +4,11 @@ require 'rails_helper' describe MyModuleTagsController, type: :controller do login_user - - let(:user) { subject.current_user } - let(:team) { create :team, created_by: user } - let!(:user_team) { create :user_team, :admin, user: user, team: team } - let(:project) { create :project, created_by: user } - - let(:experiment) { create :experiment, project: project } - let(:my_module) { create :my_module, experiment: experiment } - - before do - create_user_assignment(my_module, create(:owner_role), user) - end + include_context 'reference_project_structure', { + tag: true + } describe 'POST create' do - let(:tag) { create :tag, project: project } let(:action) { post :create, params: params, format: :json } let(:params) do { my_module_id: my_module.id, my_module_tag: { tag_id: tag.id } } diff --git a/spec/controllers/my_modules_controller_spec.rb b/spec/controllers/my_modules_controller_spec.rb index 93f711393..6dcf8ba3d 100644 --- a/spec/controllers/my_modules_controller_spec.rb +++ b/spec/controllers/my_modules_controller_spec.rb @@ -4,23 +4,12 @@ require 'rails_helper' describe MyModulesController, type: :controller do login_user - - let(:user) { subject.current_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(:owner_role) { create :owner_role } + include_context 'reference_project_structure' let!(:repository) { create :repository, created_by: user, team: team } let!(:repository_row) do create :repository_row, created_by: user, repository: repository end - let(:experiment) { create :experiment, project: project } - let(:my_module) { create :my_module, experiment: experiment } - - before do - create_user_assignment(my_module, owner_role, user) - end describe 'PUT update' do let(:action) { put :update, params: params, format: :json } @@ -192,7 +181,7 @@ describe MyModulesController, type: :controller do before do 3.times do |i| - create_user_assignment(public_send("task#{i+1}"), owner_role, user) + create_user_assignment(public_send("task#{i+1}"), role, user) end end diff --git a/spec/controllers/project_comments_controller_spec.rb b/spec/controllers/project_comments_controller_spec.rb index 47d002649..ef074bac0 100644 --- a/spec/controllers/project_comments_controller_spec.rb +++ b/spec/controllers/project_comments_controller_spec.rb @@ -4,23 +4,9 @@ require 'rails_helper' describe ProjectCommentsController, type: :controller do login_user - - let(:user) { subject.current_user } - let(:team) { create :team, created_by: user } - let(:user_team) { create :user_team, team: team, user: user } - let(:project) do - create :project, team: team - end - let(:owner_role) { create :owner_role } - let(:project_comment) do - create :project_comment, project: project, user: user - end - - before do - create_user_assignment(project, owner_role, user) - end - - + include_context 'reference_project_structure', { + project_comment: true + } describe 'POST create' do context 'in JSON format' do let(:action) { post :create, params: params, format: :json } diff --git a/spec/support/permission_helpers.rb b/spec/support/permission_helpers.rb index 3b42070c4..2aec98489 100644 --- a/spec/support/permission_helpers.rb +++ b/spec/support/permission_helpers.rb @@ -1,10 +1,13 @@ module PermissionHelpers def create_user_assignment(object, role, user, assigned_by = nil) - create :user_assignment, - assignable: object, - user: user, - user_role: role, - assigned_by: assigned_by || user + + unless UserAssignment.where(assignable: object, user: user, user_role: role).any? + create :user_assignment, + assignable: object, + user: user, + user_role: role, + assigned_by: assigned_by || user + end case object when MyModule diff --git a/spec/support/reference_project_structure_context.rb b/spec/support/reference_project_structure_context.rb new file mode 100644 index 000000000..c575945ef --- /dev/null +++ b/spec/support/reference_project_structure_context.rb @@ -0,0 +1,75 @@ +# Example +# +# include_context 'reference_project_structure', { +# role: :owner_role, +# result_asset: true, +# step: true, +# team: @team, +# step_asset: true, +# result_comment: true, +# project_comments: 4, +# tags: 2, +# skip_assignments: true +# } + +RSpec.shared_context 'reference_project_structure' do |config| + + config ||= {} + let(:user) { subject.current_user } + let(:role) { create (config[:role] || :owner_role) } unless config[:skip_role] + let!(:team) { config[:team] || (create :team, created_by: user) } + let!(:user_team) { create :user_team, :admin, user: user, team: team } + let(:project) { create :project, team: team } + let(:experiment) { create :experiment, project: project } + let(:my_module) { create :my_module, experiment: experiment } unless config[:skip_my_module] + + let(:tag) { create :tag, project: project} if config[:tag] + let(:tags) { create_list :tag, config[:tags], project: project} if config[:tags] + + let(:project_comment) { create :project_comment, project: project, user: user } if config[:project_comment] + let(:project_comments) { create_list :project_comment, config[:project_comments], project: project, user: user } if config[:project_comments] + + let(:my_module_comment) { create :task_comment, my_module: my_module, user: user } if config[:my_module_comment] + let(:my_module_comments) { create_list :task_comment, config[:my_module_comments], my_module: my_module, user: user } if config[:my_module_comments] + + if config[:step] + let(:step) { create :step, protocol: my_module.protocol, user: user} + let(:step_comment) { create :step_comment, step: step, user: user} if config[:step_comment] + let(:step_comments) { create_list :step_comment, config[:step_comments], step: step, user: user} if config[:step_comments] + + [:step_asset, :step_table, :step_checklist].each do |step_component| + let(step_component) { create step_component, step: step } if config[step_component] + end + [:step_assets, :step_tables, :step_checklists].each do |step_components| + let(step_components) { create_list step_components, config[step_components], step: step } if config[step_components] + end + end + + if config[:steps] + let(:steps) { create_list :step, config[:steps], protocol: my_module.protocol, user: user} + end + + [:result_asset, :result_text, :result_table].each do |result| + if config[result] + let(result) { create result, result: (create :result, my_module: my_module, user: user )} + let("#{result}_comment") { create :result_comment, result: public_send(result).result, user: user } if config[:result_comment] + let("#{result}_comments") { create_list :result_comment, config[:result_comments], result: public_send(result).result, user: user } if config[:result_comments] + end + end + + [:result_assets, :result_texts, :result_tables].each do |result| + if config[result] + let(result) { create_list result, config[result], result: (create :result, my_module: my_module, user: user )} + end + end + + before do + unless config[:skip_assignments] + if config[:skip_my_module] + create_user_assignment(experiment, role, user) + else + create_user_assignment(my_module, role, user) + end + end + end +end