From b4d0a8ff834843cb177638ac8a38a8ab3bab361b Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Tue, 14 Sep 2021 10:10:14 +0200 Subject: [PATCH 1/3] Add context helper for project creation [SCI-6072] --- spec/controllers/assets_controller_spec.rb | 35 ++++------- spec/controllers/canvas_controller_spec.rb | 14 +---- .../experiments_controller_spec.rb | 8 +-- .../my_module_comments_controller_spec.rb | 23 ++------ .../my_module_repositories_controller_spec.rb | 20 +------ .../my_module_tags_controller_spec.rb | 14 +---- .../controllers/my_modules_controller_spec.rb | 14 +---- .../project_comments_controller_spec.rb | 21 +------ spec/support/project_generator_context.rb | 58 +++++++++++++++++++ 9 files changed, 84 insertions(+), 123 deletions(-) create mode 100644 spec/support/project_generator_context.rb diff --git a/spec/controllers/assets_controller_spec.rb b/spec/controllers/assets_controller_spec.rb index 11fef9a54..df363dcc3 100644 --- a/spec/controllers/assets_controller_spec.rb +++ b/spec/controllers/assets_controller_spec.rb @@ -3,28 +3,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 'project_generator', { + 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 +21,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,7 +28,8 @@ 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 + p result_asset_comment expect(Activities::CreateActivityService).to receive(:call) .with(hash_including(activity_type: :edit_image_on_step)) action @@ -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..e530c2ab6 100644 --- a/spec/controllers/canvas_controller_spec.rb +++ b/spec/controllers/canvas_controller_spec.rb @@ -3,20 +3,8 @@ require 'rails_helper' describe CanvasController do - login_user + include_context 'project_generator' - 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 diff --git a/spec/controllers/experiments_controller_spec.rb b/spec/controllers/experiments_controller_spec.rb index f07198da2..af643b0f8 100644 --- a/spec/controllers/experiments_controller_spec.rb +++ b/spec/controllers/experiments_controller_spec.rb @@ -3,13 +3,7 @@ require 'rails_helper' describe ExperimentsController, type: :controller do - login_user - - 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 } + include_context 'project_generator' before do create_user_assignment(experiment, owner_role, user) diff --git a/spec/controllers/my_module_comments_controller_spec.rb b/spec/controllers/my_module_comments_controller_spec.rb index 33153c1ed..5571567ff 100644 --- a/spec/controllers/my_module_comments_controller_spec.rb +++ b/spec/controllers/my_module_comments_controller_spec.rb @@ -3,21 +3,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 'project_generator' , { + role: :normal_user_role, + my_module_comment: true + } describe 'POST create' do let(:action) { post :create, params: params, format: :json } @@ -42,7 +31,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 +51,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..0ea6ee8e6 100644 --- a/spec/controllers/my_module_repositories_controller_spec.rb +++ b/spec/controllers/my_module_repositories_controller_spec.rb @@ -3,17 +3,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 'project_generator' , { + 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 +13,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..6f7ed5248 100644 --- a/spec/controllers/my_module_tags_controller_spec.rb +++ b/spec/controllers/my_module_tags_controller_spec.rb @@ -3,19 +3,7 @@ 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 'project_generator' describe 'POST create' do let(:tag) { create :tag, project: project } diff --git a/spec/controllers/my_modules_controller_spec.rb b/spec/controllers/my_modules_controller_spec.rb index 93f711393..e9d617874 100644 --- a/spec/controllers/my_modules_controller_spec.rb +++ b/spec/controllers/my_modules_controller_spec.rb @@ -3,24 +3,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 'project_generator' 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 } diff --git a/spec/controllers/project_comments_controller_spec.rb b/spec/controllers/project_comments_controller_spec.rb index 47d002649..5063376c6 100644 --- a/spec/controllers/project_comments_controller_spec.rb +++ b/spec/controllers/project_comments_controller_spec.rb @@ -3,24 +3,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 'project_generator', { + 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/project_generator_context.rb b/spec/support/project_generator_context.rb new file mode 100644 index 000000000..d0b689a89 --- /dev/null +++ b/spec/support/project_generator_context.rb @@ -0,0 +1,58 @@ +RSpec.shared_context 'project_generator' do |config| + login_user + + let(:user) { subject.current_user } + let(:role) { create (config[:role] || :owner_role) } + let!(: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 } + let(:protocol) { create :protocol, my_module: my_module, team: team, added_by: user } + + if config[:tag] + let(:tag) { create :tag, project: project} + end + + if config[:project_comment] + let(:project_comment) { create :project_comment, project: project, user: user } + end + + if config[:my_module_comment] + let(:my_module_comment) { create :task_comment, my_module: my_module, user: user } + end + + if config[:step] + let(:step) { create :step, protocol: protocol, user: user} + + if config[:step_comment] + let(:step_comment) { create :step_comment, step: step, user: user} + end + + if config[:step_asset] + let(:step_asset) { create :step_asset, step: step } + end + + if config[:step_table] + let(:step_table) { create :step_table, step: step } + end + + if config[:step_checklist] + let(:step_checklist) { create :step_checklist, step: step } + end + 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 )} + + if config[:result_comment] + let("#{result}_comment") { create :result_comment, result: public_send(result).result, user: user } + end + end + end + + before do + create_user_assignment(my_module, role, user) unless config[:skip_assignments] + end +end From a0fef97fbcd83d85dfe3177728879345bb3b5fac Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Tue, 14 Sep 2021 12:45:44 +0200 Subject: [PATCH 2/3] Update context helper [SCI-6072] --- spec/controllers/assets_controller_spec.rb | 6 +- spec/controllers/canvas_controller_spec.rb | 7 +- .../experiments_controller_spec.rb | 8 +-- .../my_module_comments_controller_spec.rb | 3 +- .../my_module_repositories_controller_spec.rb | 3 +- .../my_module_tags_controller_spec.rb | 6 +- .../controllers/my_modules_controller_spec.rb | 5 +- .../project_comments_controller_spec.rb | 3 +- spec/support/project_generator_context.rb | 58 --------------- .../reference_project_structure_context.rb | 71 +++++++++++++++++++ 10 files changed, 94 insertions(+), 76 deletions(-) delete mode 100644 spec/support/project_generator_context.rb create mode 100644 spec/support/reference_project_structure_context.rb diff --git a/spec/controllers/assets_controller_spec.rb b/spec/controllers/assets_controller_spec.rb index df363dcc3..453d56a37 100644 --- a/spec/controllers/assets_controller_spec.rb +++ b/spec/controllers/assets_controller_spec.rb @@ -3,7 +3,9 @@ require 'rails_helper' describe AssetsController, type: :controller do - include_context 'project_generator', { + login_user + + include_context 'reference_project_structure', { role: :owner_role, result_asset: true, step: true, @@ -29,11 +31,9 @@ describe AssetsController, type: :controller do end it 'calls create activity service (start edit image on step)' do params[:id] = step_asset.asset.id - p result_asset_comment 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 diff --git a/spec/controllers/canvas_controller_spec.rb b/spec/controllers/canvas_controller_spec.rb index e530c2ab6..317058647 100644 --- a/spec/controllers/canvas_controller_spec.rb +++ b/spec/controllers/canvas_controller_spec.rb @@ -3,7 +3,8 @@ require 'rails_helper' describe CanvasController do - include_context 'project_generator' + login_user + include_context 'reference_project_structure' let(:experiment2) { create :experiment, project: project } @@ -82,9 +83,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 af643b0f8..cadd73cf0 100644 --- a/spec/controllers/experiments_controller_spec.rb +++ b/spec/controllers/experiments_controller_spec.rb @@ -3,11 +3,9 @@ require 'rails_helper' describe ExperimentsController, type: :controller do - include_context 'project_generator' + login_user + include_context 'reference_project_structure' - before do - create_user_assignment(experiment, owner_role, user) - end describe 'POST create' do let(:action) { post :create, params: params, format: :json } @@ -72,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 5571567ff..ab0f3a038 100644 --- a/spec/controllers/my_module_comments_controller_spec.rb +++ b/spec/controllers/my_module_comments_controller_spec.rb @@ -3,7 +3,8 @@ require 'rails_helper' describe MyModuleCommentsController, type: :controller do - include_context 'project_generator' , { + login_user + include_context 'reference_project_structure' , { role: :normal_user_role, my_module_comment: true } diff --git a/spec/controllers/my_module_repositories_controller_spec.rb b/spec/controllers/my_module_repositories_controller_spec.rb index 0ea6ee8e6..77ca0b71b 100644 --- a/spec/controllers/my_module_repositories_controller_spec.rb +++ b/spec/controllers/my_module_repositories_controller_spec.rb @@ -3,7 +3,8 @@ require 'rails_helper' describe MyModuleRepositoriesController, type: :controller do - include_context 'project_generator' , { + login_user + include_context 'reference_project_structure' , { role: :normal_user_role } let!(:repository) { create :repository, created_by: user, team: team } diff --git a/spec/controllers/my_module_tags_controller_spec.rb b/spec/controllers/my_module_tags_controller_spec.rb index 6f7ed5248..3a2d5c1b6 100644 --- a/spec/controllers/my_module_tags_controller_spec.rb +++ b/spec/controllers/my_module_tags_controller_spec.rb @@ -3,10 +3,12 @@ require 'rails_helper' describe MyModuleTagsController, type: :controller do - include_context 'project_generator' + login_user + 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 e9d617874..6dcf8ba3d 100644 --- a/spec/controllers/my_modules_controller_spec.rb +++ b/spec/controllers/my_modules_controller_spec.rb @@ -3,7 +3,8 @@ require 'rails_helper' describe MyModulesController, type: :controller do - include_context 'project_generator' + login_user + include_context 'reference_project_structure' let!(:repository) { create :repository, created_by: user, team: team } let!(:repository_row) do @@ -180,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 5063376c6..ef074bac0 100644 --- a/spec/controllers/project_comments_controller_spec.rb +++ b/spec/controllers/project_comments_controller_spec.rb @@ -3,7 +3,8 @@ require 'rails_helper' describe ProjectCommentsController, type: :controller do - include_context 'project_generator', { + login_user + include_context 'reference_project_structure', { project_comment: true } describe 'POST create' do diff --git a/spec/support/project_generator_context.rb b/spec/support/project_generator_context.rb deleted file mode 100644 index d0b689a89..000000000 --- a/spec/support/project_generator_context.rb +++ /dev/null @@ -1,58 +0,0 @@ -RSpec.shared_context 'project_generator' do |config| - login_user - - let(:user) { subject.current_user } - let(:role) { create (config[:role] || :owner_role) } - let!(: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 } - let(:protocol) { create :protocol, my_module: my_module, team: team, added_by: user } - - if config[:tag] - let(:tag) { create :tag, project: project} - end - - if config[:project_comment] - let(:project_comment) { create :project_comment, project: project, user: user } - end - - if config[:my_module_comment] - let(:my_module_comment) { create :task_comment, my_module: my_module, user: user } - end - - if config[:step] - let(:step) { create :step, protocol: protocol, user: user} - - if config[:step_comment] - let(:step_comment) { create :step_comment, step: step, user: user} - end - - if config[:step_asset] - let(:step_asset) { create :step_asset, step: step } - end - - if config[:step_table] - let(:step_table) { create :step_table, step: step } - end - - if config[:step_checklist] - let(:step_checklist) { create :step_checklist, step: step } - end - 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 )} - - if config[:result_comment] - let("#{result}_comment") { create :result_comment, result: public_send(result).result, user: user } - end - end - end - - before do - create_user_assignment(my_module, role, user) unless config[:skip_assignments] - end -end diff --git a/spec/support/reference_project_structure_context.rb b/spec/support/reference_project_structure_context.rb new file mode 100644 index 000000000..b88123151 --- /dev/null +++ b/spec/support/reference_project_structure_context.rb @@ -0,0 +1,71 @@ +# 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 } + unless config[:skip_role] + let(:role) { create (config[:role] || :owner_role) } + end + 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 } + + 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 + create_user_assignment(my_module, role, user) unless config[:skip_assignments] + end +end From 97be2672605f61333309510d708adcebf142bf47 Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Tue, 14 Sep 2021 13:30:52 +0200 Subject: [PATCH 3/3] Add skip my_module flag [SCI-6072] --- spec/controllers/canvas_controller_spec.rb | 4 +++- spec/support/permission_helpers.rb | 13 ++++++++----- .../support/reference_project_structure_context.rb | 14 +++++++++----- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/spec/controllers/canvas_controller_spec.rb b/spec/controllers/canvas_controller_spec.rb index 317058647..57881e9b0 100644 --- a/spec/controllers/canvas_controller_spec.rb +++ b/spec/controllers/canvas_controller_spec.rb @@ -4,7 +4,9 @@ require 'rails_helper' describe CanvasController do login_user - include_context 'reference_project_structure' + include_context 'reference_project_structure', { + skip_my_module: true + } let(:experiment2) { create :experiment, project: project } 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 index b88123151..c575945ef 100644 --- a/spec/support/reference_project_structure_context.rb +++ b/spec/support/reference_project_structure_context.rb @@ -16,14 +16,12 @@ RSpec.shared_context 'reference_project_structure' do |config| config ||= {} let(:user) { subject.current_user } - unless config[:skip_role] - let(:role) { create (config[:role] || :owner_role) } - end + 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 } + 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] @@ -66,6 +64,12 @@ RSpec.shared_context 'reference_project_structure' do |config| end before do - create_user_assignment(my_module, role, user) unless config[:skip_assignments] + 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