PageRenderTime 33ms CodeModel.GetById 7ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/finders/access_requests_finder_spec.rb

https://gitlab.com/tnir/gitlab-ce
Ruby | 96 lines | 77 code | 18 blank | 1 comment | 0 complexity | d6770c3603cff27ade42b569e11c0a7e MD5 | raw file
 1# frozen_string_literal: true
 2
 3require 'spec_helper'
 4
 5describe AccessRequestsFinder do
 6  let(:user) { create(:user) }
 7  let(:access_requester) { create(:user) }
 8
 9  let(:project) do
10    create(:project, :public) do |project|
11      project.request_access(access_requester)
12    end
13  end
14
15  let(:group) do
16    create(:group, :public) do |group|
17      group.request_access(access_requester)
18    end
19  end
20
21  shared_examples 'a finder returning access requesters' do |method_name|
22    it 'returns access requesters' do
23      access_requesters = described_class.new(source).public_send(method_name, user)
24
25      expect(access_requesters.size).to eq(1)
26      expect(access_requesters.first).to be_a "#{source.class}Member".constantize
27      expect(access_requesters.first.user).to eq(access_requester)
28    end
29  end
30
31  shared_examples 'a finder returning no results' do |method_name|
32    it 'raises Gitlab::Access::AccessDeniedError' do
33      expect(described_class.new(source).public_send(method_name, user)).to be_empty
34    end
35  end
36
37  shared_examples 'a finder raising Gitlab::Access::AccessDeniedError' do |method_name|
38    it 'raises Gitlab::Access::AccessDeniedError' do
39      expect { described_class.new(source).public_send(method_name, user) }.to raise_error(Gitlab::Access::AccessDeniedError)
40    end
41  end
42
43  describe '#execute' do
44    context 'when current user cannot see project access requests' do
45      it_behaves_like 'a finder returning no results', :execute do
46        let(:source) { project }
47      end
48
49      it_behaves_like 'a finder returning no results', :execute do
50        let(:source) { group }
51      end
52    end
53
54    context 'when current user can see access requests' do
55      before do
56        project.add_maintainer(user)
57        group.add_owner(user)
58      end
59
60      it_behaves_like 'a finder returning access requesters', :execute do
61        let(:source) { project }
62      end
63
64      it_behaves_like 'a finder returning access requesters', :execute do
65        let(:source) { group }
66      end
67    end
68  end
69
70  describe '#execute!' do
71    context 'when current user cannot see access requests' do
72      it_behaves_like 'a finder raising Gitlab::Access::AccessDeniedError', :execute! do
73        let(:source) { project }
74      end
75
76      it_behaves_like 'a finder raising Gitlab::Access::AccessDeniedError', :execute! do
77        let(:source) { group }
78      end
79    end
80
81    context 'when current user can see access requests' do
82      before do
83        project.add_maintainer(user)
84        group.add_owner(user)
85      end
86
87      it_behaves_like 'a finder returning access requesters', :execute! do
88        let(:source) { project }
89      end
90
91      it_behaves_like 'a finder returning access requesters', :execute! do
92        let(:source) { group }
93      end
94    end
95  end
96end