2021-05-02 17:22:59 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe ProjectMember, type: :model do
|
2021-09-30 17:32:11 +08:00
|
|
|
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
|
2021-05-02 17:22:59 +08:00
|
|
|
let!(:user) { create :user }
|
2021-09-30 17:32:11 +08:00
|
|
|
let!(:project) { create :project, created_by: user }
|
2021-05-02 17:22:59 +08:00
|
|
|
let(:normal_user_role) { create :normal_user_role }
|
|
|
|
|
|
|
|
let(:subject) { described_class.new(user, project, user) }
|
|
|
|
|
2021-05-23 00:36:30 +08:00
|
|
|
describe '#update' do
|
2021-05-02 17:22:59 +08:00
|
|
|
let!(:user_project) { create :user_project, user: user, project: project }
|
|
|
|
|
|
|
|
it 'updates only the user assignment role' do
|
|
|
|
subject.user_role_id = normal_user_role.id
|
|
|
|
subject.update
|
2022-03-09 21:12:16 +08:00
|
|
|
expect(subject.user_assignment.user_role).to eq normal_user_role
|
2021-05-02 17:22:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs a change_user_role_on_project activity' do
|
|
|
|
subject.user_role_id = normal_user_role.id
|
2021-05-30 01:25:46 +08:00
|
|
|
expect {
|
2021-05-02 17:22:59 +08:00
|
|
|
subject.update
|
|
|
|
}.to change(Activity, :count).by(1)
|
|
|
|
expect(Activity.last.type_of).to eq 'change_user_role_on_project'
|
|
|
|
end
|
2021-06-06 15:54:47 +08:00
|
|
|
|
|
|
|
it 'triggers the UserAssignments::PropagateAssignmentJob job' do
|
|
|
|
subject.user_role_id = normal_user_role.id
|
|
|
|
expect(UserAssignments::PropagateAssignmentJob).to receive(:perform_later).with(
|
2023-08-11 21:31:29 +08:00
|
|
|
project, user.id, normal_user_role, user.id
|
2021-06-06 15:54:47 +08:00
|
|
|
)
|
|
|
|
subject.update
|
|
|
|
end
|
2021-05-02 17:22:59 +08:00
|
|
|
end
|
|
|
|
|
2021-05-23 00:36:30 +08:00
|
|
|
describe '#destroy' do
|
2021-05-02 17:22:59 +08:00
|
|
|
let!(:user_two) { create :user }
|
|
|
|
let!(:user_project_two) { create :user_project, user: user_two, project: project }
|
2021-05-30 01:25:46 +08:00
|
|
|
let!(:user_assignment_two) do
|
2021-05-22 20:41:56 +08:00
|
|
|
create :user_assignment,
|
|
|
|
assignable: project,
|
|
|
|
user: user_two,
|
|
|
|
user_role: owner_role,
|
|
|
|
assigned_by: user
|
2021-05-30 01:25:46 +08:00
|
|
|
end
|
2021-05-02 17:22:59 +08:00
|
|
|
let!(:user_project) { create :user_project, user: user, project: project }
|
2021-09-30 17:32:11 +08:00
|
|
|
let!(:user_assignment) { project.user_assignments.first }
|
2021-05-02 17:22:59 +08:00
|
|
|
|
|
|
|
it 'removes the user_assignment and user_project' do
|
|
|
|
expect {
|
|
|
|
subject.destroy
|
|
|
|
}.to change(UserAssignment, :count).by(-1).and \
|
|
|
|
change(UserProject, :count).by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not remove the user_assignment and user_project if the user is last owner' do
|
|
|
|
user_assignment_two.update!(user_role: normal_user_role)
|
|
|
|
|
|
|
|
expect {
|
|
|
|
subject.destroy
|
|
|
|
}.to change(UserAssignment, :count).by(0).and \
|
|
|
|
change(UserProject, :count).by(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs a unassign_user_from_project activity' do
|
2021-05-30 01:25:46 +08:00
|
|
|
expect {
|
2021-05-02 17:22:59 +08:00
|
|
|
subject.destroy
|
|
|
|
}.to change(Activity, :count).by(1)
|
|
|
|
expect(Activity.last.type_of).to eq 'unassign_user_from_project'
|
|
|
|
end
|
2021-06-06 15:54:47 +08:00
|
|
|
|
|
|
|
it 'triggers the UserAssignments::PropagateAssignmentJob job' do
|
|
|
|
expect(UserAssignments::PropagateAssignmentJob).to receive(:perform_later).with(
|
2023-08-11 21:31:29 +08:00
|
|
|
project, user.id, owner_role, user.id, destroy: true
|
2021-06-06 15:54:47 +08:00
|
|
|
)
|
|
|
|
subject.destroy
|
|
|
|
end
|
2021-05-02 17:22:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
|
|
|
it 'validates presence or user, project, user_role_id when assign is true' do
|
|
|
|
subject = described_class.new(nil, nil)
|
|
|
|
subject.assign = true
|
|
|
|
subject.valid?
|
|
|
|
expect(subject.errors).to have_key(:project)
|
|
|
|
expect(subject.errors).to have_key(:user)
|
|
|
|
expect(subject.errors).to have_key(:user_role_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates user project assignment existence' do
|
|
|
|
subject.assign = true
|
2022-03-09 21:12:16 +08:00
|
|
|
subject.user_assignment.user_role_id = owner_role.id
|
2021-05-02 17:22:59 +08:00
|
|
|
subject.valid?
|
|
|
|
expect(subject.errors).to have_key(:user_role_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'user_role' do
|
|
|
|
it 'adds an error when user role does not exist' do
|
|
|
|
subject.assign = true
|
|
|
|
subject.user_role_id = 1234
|
|
|
|
subject.valid?
|
|
|
|
expect(subject.errors).to have_key(:user_role_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an error when role exists' do
|
2021-09-30 17:32:11 +08:00
|
|
|
project.user_assignments.destroy_all
|
2021-05-02 17:22:59 +08:00
|
|
|
subject.assign = true
|
|
|
|
subject.user_role_id = owner_role.id
|
|
|
|
subject.valid?
|
|
|
|
expect(subject.errors).not_to have_key(:user_role_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|