scinote-web/spec/models/project_member_spec.rb

118 lines
3.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'rails_helper'
describe ProjectMember, type: :model do
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
let!(:user) { create :user }
let!(:project) { create :project, created_by: user }
let(:normal_user_role) { create :normal_user_role }
let(:subject) { described_class.new(user, project, user) }
describe '#update' do
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
expect(subject.user_assignment.user_role).to eq normal_user_role
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 {
subject.update
}.to change(Activity, :count).by(1)
expect(Activity.last.type_of).to eq 'change_user_role_on_project'
end
it 'triggers the UserAssignments::PropagateAssignmentJob job' do
subject.user_role_id = normal_user_role.id
expect(UserAssignments::PropagateAssignmentJob).to receive(:perform_later).with(
project, user, normal_user_role, user
)
subject.update
end
end
describe '#destroy' do
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
let!(:user_project) { create :user_project, user: user, project: project }
let!(:user_assignment) { project.user_assignments.first }
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 {
subject.destroy
}.to change(Activity, :count).by(1)
expect(Activity.last.type_of).to eq 'unassign_user_from_project'
end
it 'triggers the UserAssignments::PropagateAssignmentJob job' do
expect(UserAssignments::PropagateAssignmentJob).to receive(:perform_later).with(
project, user, owner_role, user, destroy: true
)
subject.destroy
end
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
subject.user_assignment.user_role_id = owner_role.id
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
project.user_assignments.destroy_all
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