2021-06-06 17:46:16 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
module UserAssignments
|
|
|
|
RSpec.describe GenerateUserAssignmentsJob, type: :job do
|
2021-09-30 17:32:11 +08:00
|
|
|
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
|
2021-06-06 17:46:16 +08:00
|
|
|
let!(:viewer_role) { create :viewer_role }
|
|
|
|
let!(:technician_role) { create :technician_role }
|
|
|
|
let!(:user_one) { create :user }
|
|
|
|
let!(:user_two) { create :user }
|
|
|
|
let!(:user_three) { create :user }
|
|
|
|
let!(:team) { create :team, created_by: user_one }
|
|
|
|
let!(:project) { create :project, team: team, created_by: user_one }
|
|
|
|
|
2021-09-30 17:32:11 +08:00
|
|
|
before(:each) do
|
2021-06-06 17:46:16 +08:00
|
|
|
available_roles = [owner_role, viewer_role, technician_role]
|
2022-03-09 21:12:16 +08:00
|
|
|
create :user_assignment, user: user_two, assignable: project, user_role: viewer_role, assigned_by: user_one
|
|
|
|
|
|
|
|
create :user_assignment, user: user_three, assignable: project, user_role: technician_role, assigned_by: user_one
|
2021-06-06 17:46:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'perform' do
|
|
|
|
context 'experiment' do
|
2022-03-09 21:12:16 +08:00
|
|
|
let!(:experiment) { create :experiment, project: project, created_by: project.created_by }
|
2021-06-06 17:46:16 +08:00
|
|
|
|
2021-09-30 17:32:11 +08:00
|
|
|
it 'user assignments should be created automatically upon experiment creation' do
|
|
|
|
# check that all users are assigned
|
|
|
|
experiment.reload
|
|
|
|
expect([user_one, user_two, user_three] - experiment.user_assignments.reload.map(&:user)).to eq([])
|
2021-06-06 17:46:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns the same role as the user had on project level' do
|
2023-08-11 21:31:29 +08:00
|
|
|
described_class.perform_now(experiment, user_one.id)
|
2021-06-06 17:46:16 +08:00
|
|
|
user_two_assignment = UserAssignment.find_by(user: user_two, assignable: experiment)
|
|
|
|
user_three_assignment = UserAssignment.find_by(user: user_three, assignable: experiment)
|
|
|
|
expect(user_two_assignment.user_role).to eq viewer_role
|
|
|
|
expect(user_three_assignment.user_role).to eq technician_role
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'my_module' do
|
2022-03-09 21:12:16 +08:00
|
|
|
let!(:experiment) { create :experiment, project: project, created_by: project.created_by }
|
|
|
|
let!(:my_module) { create :my_module, experiment: experiment, created_by: experiment.created_by }
|
2021-06-06 17:46:16 +08:00
|
|
|
|
2021-09-30 17:32:11 +08:00
|
|
|
it 'user assignments should be created automatically upon my_module creation' do
|
|
|
|
# check that all users are assigned
|
|
|
|
my_module.reload
|
|
|
|
expect([user_one, user_two, user_three] - my_module.user_assignments.reload.map(&:user)).to eq([])
|
2021-06-06 17:46:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns the same role as the user had on project level' do
|
2023-08-11 21:31:29 +08:00
|
|
|
described_class.perform_now(my_module, user_one.id)
|
2021-06-06 17:46:16 +08:00
|
|
|
user_two_assignment = UserAssignment.find_by(user: user_two, assignable: my_module)
|
|
|
|
user_three_assignment = UserAssignment.find_by(user: user_three, assignable: my_module)
|
|
|
|
expect(user_two_assignment.user_role).to eq viewer_role
|
|
|
|
expect(user_three_assignment.user_role).to eq technician_role
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|