2019-10-25 05:06:06 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-02 01:01:02 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe ProjectMemberPresenter do
|
2017-03-02 01:01:02 -05:00
|
|
|
let(:user) { double(:user) }
|
|
|
|
let(:project) { double(:project) }
|
|
|
|
let(:project_member) { double(:project_member, source: project) }
|
|
|
|
let(:presenter) { described_class.new(project_member, current_user: user) }
|
|
|
|
|
|
|
|
describe '#can_resend_invite?' do
|
|
|
|
context 'when project_member is invited' do
|
|
|
|
before do
|
|
|
|
expect(project_member).to receive(:invite?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and user can admin_project_member' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :admin_project_member, project).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(presenter.can_resend_invite?).to eq(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and user cannot admin_project_member' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :admin_project_member, project).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(presenter.can_resend_invite?).to eq(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project_member is not invited' do
|
|
|
|
before do
|
|
|
|
expect(project_member).to receive(:invite?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and user can admin_project_member' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :admin_project_member, project).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(presenter.can_resend_invite?).to eq(false) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and user cannot admin_project_member' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :admin_project_member, project).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(presenter.can_resend_invite?).to eq(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#can_update?' do
|
2022-08-03 20:08:55 -04:00
|
|
|
context 'when user is NOT attempting to update an Owner' do
|
2017-03-02 01:01:02 -05:00
|
|
|
before do
|
2022-08-03 20:08:55 -04:00
|
|
|
allow(project_member).to receive(:owner?).and_return(false)
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
|
2022-08-03 20:08:55 -04:00
|
|
|
context 'when user can update_project_member' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :update_project_member, presenter).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify { expect(presenter.can_update?).to eq(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user cannot update_project_member' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :update_project_member, presenter).and_return(false)
|
|
|
|
allow(presenter).to receive(:can?).with(user, :override_project_member, presenter).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify { expect(presenter.can_update?).to eq(false) }
|
|
|
|
end
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
|
2022-08-03 20:08:55 -04:00
|
|
|
context 'when user is attempting to update an Owner' do
|
2017-03-02 01:01:02 -05:00
|
|
|
before do
|
2022-08-03 20:08:55 -04:00
|
|
|
allow(project_member).to receive(:owner?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user can manage owners' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :manage_owners, project).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify { expect(presenter.can_update?).to eq(true) }
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
|
2022-08-03 20:08:55 -04:00
|
|
|
context 'when user cannot manage owners' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :manage_owners, project).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify { expect(presenter.can_update?).to eq(false) }
|
|
|
|
end
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#can_remove?' do
|
2022-08-03 20:08:55 -04:00
|
|
|
context 'when user is NOT attempting to remove an Owner' do
|
2017-03-02 01:01:02 -05:00
|
|
|
before do
|
2022-08-03 20:08:55 -04:00
|
|
|
allow(project_member).to receive(:owner?).and_return(false)
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
|
2022-08-03 20:08:55 -04:00
|
|
|
context 'when user can destroy_project_member' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :destroy_project_member, presenter).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify { expect(presenter.can_remove?).to eq(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user cannot destroy_project_member' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :destroy_project_member, presenter).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify { expect(presenter.can_remove?).to eq(false) }
|
|
|
|
end
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
|
2022-08-03 20:08:55 -04:00
|
|
|
context 'when user is attempting to remove an Owner' do
|
2017-03-02 01:01:02 -05:00
|
|
|
before do
|
2022-08-03 20:08:55 -04:00
|
|
|
allow(project_member).to receive(:owner?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user can manage owners' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :manage_owners, project).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify { expect(presenter.can_remove?).to eq(true) }
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
|
2022-08-03 20:08:55 -04:00
|
|
|
context 'when user cannot manage owners' do
|
|
|
|
before do
|
|
|
|
allow(presenter).to receive(:can?).with(user, :manage_owners, project).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify { expect(presenter.can_remove?).to eq(false) }
|
|
|
|
end
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#can_approve?' do
|
|
|
|
context 'when project_member has request an invite' do
|
|
|
|
before do
|
|
|
|
expect(project_member).to receive(:request?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and user can update_project_member' do
|
|
|
|
before do
|
2022-08-03 20:08:55 -04:00
|
|
|
allow(presenter).to receive(:can_update?).and_return(true)
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(presenter.can_approve?).to eq(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and user cannot update_project_member' do
|
|
|
|
before do
|
2022-08-03 20:08:55 -04:00
|
|
|
allow(presenter).to receive(:can_update?).and_return(false)
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(presenter.can_approve?).to eq(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project_member did not request an invite' do
|
|
|
|
before do
|
|
|
|
expect(project_member).to receive(:request?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and user can update_project_member' do
|
|
|
|
before do
|
2022-08-03 20:08:55 -04:00
|
|
|
allow(presenter).to receive(:can_update?).and_return(true)
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(presenter.can_approve?).to eq(false) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and user cannot update_project_member' do
|
|
|
|
before do
|
2022-08-03 20:08:55 -04:00
|
|
|
allow(presenter).to receive(:can_update?).and_return(false)
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(presenter.can_approve?).to eq(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-06 08:15:29 -05:00
|
|
|
|
2022-08-03 20:08:55 -04:00
|
|
|
describe 'valid level roles' do
|
2018-12-06 08:15:29 -05:00
|
|
|
before do
|
2022-08-03 20:08:55 -04:00
|
|
|
allow(Ability).to receive(:allowed?).and_call_original
|
|
|
|
allow(Ability).to receive(:allowed?).with(member_user, :manage_owners, entity).and_return(can_manage_owners)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user cannot manage owners' do
|
|
|
|
it_behaves_like '#valid_level_roles', :project do
|
|
|
|
let(:expected_roles) { { 'Developer' => 30, 'Maintainer' => 40, 'Reporter' => 20 } }
|
|
|
|
let(:can_manage_owners) { false }
|
|
|
|
|
|
|
|
before do
|
|
|
|
entity.group = group
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user can manage owners' do
|
|
|
|
it_behaves_like '#valid_level_roles', :project do
|
|
|
|
let(:expected_roles) { { 'Developer' => 30, 'Maintainer' => 40, 'Owner' => 50, 'Reporter' => 20 } }
|
|
|
|
let(:can_manage_owners) { true }
|
|
|
|
|
|
|
|
before do
|
|
|
|
entity.group = group
|
|
|
|
end
|
|
|
|
end
|
2018-12-06 08:15:29 -05:00
|
|
|
end
|
|
|
|
end
|
2017-03-02 01:01:02 -05:00
|
|
|
end
|