project_member_presenter_spec.rb 4.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
require 'spec_helper'

describe ProjectMemberPresenter do
  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
    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

      it { 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)
67
        allow(presenter).to receive(:can?).with(user, :override_project_member, presenter).and_return(false)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
      end

      it { expect(presenter.can_update?).to eq(false) }
    end
  end

  describe '#can_remove?' do
    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

      it { 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

      it { expect(presenter.can_remove?).to eq(false) }
    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
          allow(presenter).to receive(:can?).with(user, :update_project_member, presenter).and_return(true)
        end

        it { expect(presenter.can_approve?).to eq(true) }
      end

      context 'and user cannot update_project_member' do
        before do
          allow(presenter).to receive(:can?).with(user, :update_project_member, presenter).and_return(false)
109
          allow(presenter).to receive(:can?).with(user, :override_project_member, presenter).and_return(false)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        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
          allow(presenter).to receive(:can?).with(user, :update_project_member, presenter).and_return(true)
        end

        it { expect(presenter.can_approve?).to eq(false) }
      end

      context 'and user cannot update_project_member' do
        before do
          allow(presenter).to receive(:can?).with(user, :update_project_member, presenter).and_return(false)
        end

        it { expect(presenter.can_approve?).to eq(false) }
      end
    end
  end
end