access_requests_finder_spec.rb 2.69 KB
Newer Older
Rémy Coutable's avatar
Rémy Coutable committed
1 2 3 4 5
require 'spec_helper'

describe AccessRequestsFinder, services: true do
  let(:user) { create(:user) }
  let(:access_requester) { create(:user) }
6 7
  let(:project) { create(:project, :public) }
  let(:group) { create(:group, :public) }
Rémy Coutable's avatar
Rémy Coutable committed
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

  before do
    project.request_access(access_requester)
    group.request_access(access_requester)
  end

  shared_examples 'a finder returning access requesters' do |method_name|
    it 'returns access requesters' do
      access_requesters = described_class.new(source).public_send(method_name, user)

      expect(access_requesters.size).to eq(1)
      expect(access_requesters.first).to be_a "#{source.class.to_s}Member".constantize
      expect(access_requesters.first.user).to eq(access_requester)
    end
  end

  shared_examples 'a finder returning no results' do |method_name|
    it 'raises Gitlab::Access::AccessDeniedError' do
      expect(described_class.new(source).public_send(method_name, user)).to be_empty
    end
  end

  shared_examples 'a finder raising Gitlab::Access::AccessDeniedError' do |method_name|
    it 'raises Gitlab::Access::AccessDeniedError' do
      expect { described_class.new(source).public_send(method_name, user) }.to raise_error(Gitlab::Access::AccessDeniedError)
    end
  end

  describe '#execute' do
    context 'when current user cannot see project access requests' do
      it_behaves_like 'a finder returning no results', :execute do
        let(:source) { project }
      end

      it_behaves_like 'a finder returning no results', :execute do
        let(:source) { group }
      end
    end

    context 'when current user can see access requests' do
      before do
        project.team << [user, :master]
        group.add_owner(user)
      end

      it_behaves_like 'a finder returning access requesters', :execute do
        let(:source) { project }
      end

      it_behaves_like 'a finder returning access requesters', :execute do
        let(:source) { group }
      end
    end
  end

  describe '#execute!' do
    context 'when current user cannot see access requests' do
      it_behaves_like 'a finder raising Gitlab::Access::AccessDeniedError', :execute! do
        let(:source) { project }
      end

      it_behaves_like 'a finder raising Gitlab::Access::AccessDeniedError', :execute! do
        let(:source) { group }
      end
    end

    context 'when current user can see access requests' do
      before do
        project.team << [user, :master]
        group.add_owner(user)
      end

      it_behaves_like 'a finder returning access requesters', :execute! do
        let(:source) { project }
      end

      it_behaves_like 'a finder returning access requesters', :execute! do
        let(:source) { group }
      end
    end
  end
end