BigW Consortium Gitlab

projects_finder_spec.rb 4.68 KB
Newer Older
1 2
require 'spec_helper'

3
describe ProjectsFinder do
4 5
  describe '#execute' do
    let(:user) { create(:user) }
Douwe Maan committed
6
    let(:group) { create(:group, :public) }
7

8
    let!(:private_project) do
9
      create(:project, :private, name: 'A', path: 'A')
10 11 12
    end

    let!(:internal_project) do
13
      create(:project, :internal, group: group, name: 'B', path: 'B')
14 15 16
    end

    let!(:public_project) do
17
      create(:project, :public, group: group, name: 'C', path: 'C')
18
    end
19

20
    let!(:shared_project) do
21
      create(:project, :private, name: 'D', path: 'D')
22 23
    end

24 25 26 27 28 29
    let(:params) { {} }
    let(:current_user) { user }
    let(:project_ids_relation) { nil }
    let(:finder) { described_class.new(params: params, current_user: current_user, project_ids_relation: project_ids_relation) }

    subject { finder.execute }
30

Ahmad Sherif committed
31
    describe 'without a user' do
32
      let(:current_user) { nil }
33

Ahmad Sherif committed
34
      it { is_expected.to eq([public_project]) }
35 36
    end

Ahmad Sherif committed
37 38
    describe 'with a user' do
      describe 'without private projects' do
39
        it { is_expected.to match_array([public_project, internal_project]) }
40
      end
41

Ahmad Sherif committed
42 43
      describe 'with private projects' do
        before do
44
          private_project.add_master(user)
45 46
        end

47
        it { is_expected.to match_array([public_project, internal_project, private_project]) }
48 49
      end
    end
50 51 52 53 54 55

    describe 'with project_ids_relation' do
      let(:project_ids_relation) { Project.where(id: internal_project.id) }

      it { is_expected.to eq([internal_project]) }
    end
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 90 91 92

    describe 'filter by visibility_level' do
      before do
        private_project.add_master(user)
      end

      context 'private' do
        let(:params) { { visibility_level: Gitlab::VisibilityLevel::PRIVATE } }

        it { is_expected.to eq([private_project]) }
      end

      context 'internal' do
        let(:params) { { visibility_level: Gitlab::VisibilityLevel::INTERNAL } }

        it { is_expected.to eq([internal_project]) }
      end

      context 'public' do
        let(:params) { { visibility_level: Gitlab::VisibilityLevel::PUBLIC } }

        it { is_expected.to eq([public_project]) }
      end
    end

    describe 'filter by tags' do
      before do
        public_project.tag_list.add('foo')
        public_project.save!
      end

      let(:params) { { tag: 'foo' } }

      it { is_expected.to eq([public_project]) }
    end

    describe 'filter by personal' do
93
      let!(:personal_project) { create(:project, namespace: user.namespace) }
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
      let(:params) { { personal: true } }

      it { is_expected.to eq([personal_project]) }
    end

    describe 'filter by search' do
      let(:params) { { search: 'C' } }

      it { is_expected.to eq([public_project]) }
    end

    describe 'filter by name for backward compatibility' do
      let(:params) { { name: 'C' } }

      it { is_expected.to eq([public_project]) }
    end

    describe 'filter by archived' do
112
      let!(:archived_project) { create(:project, :public, :archived, name: 'E', path: 'E') }
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 139

      context 'non_archived=true' do
        let(:params) { { non_archived: true } }

        it { is_expected.to match_array([public_project, internal_project]) }
      end

      context 'non_archived=false' do
        let(:params) { { non_archived: false } }

        it { is_expected.to match_array([public_project, internal_project, archived_project]) }
      end

      describe 'filter by archived for backward compatibility' do
        let(:params) { { archived: false } }

        it { is_expected.to match_array([public_project, internal_project]) }
      end
    end

    describe 'filter by trending' do
      let!(:trending_project) { create(:trending_project, project: public_project)  }
      let(:params) { { trending: true } }

      it { is_expected.to eq([public_project]) }
    end

140 141
    describe 'filter by owned' do
      let(:params) { { owned: true } }
142
      let!(:owned_project) { create(:project, :private, namespace: current_user.namespace) }
143 144 145 146

      it { is_expected.to eq([owned_project]) }
    end

147 148 149 150 151 152 153 154 155
    describe 'filter by non_public' do
      let(:params) { { non_public: true } }
      before do
        private_project.add_developer(current_user)
      end

      it { is_expected.to eq([private_project]) }
    end

156
    describe 'filter by starred' do
157 158 159 160 161 162
      let(:params) { { starred: true } }
      before do
        current_user.toggle_star(public_project)
      end

      it { is_expected.to eq([public_project]) }
163 164 165 166 167 168

      it 'returns only projects the user has access to' do
        current_user.toggle_star(private_project)

        is_expected.to eq([public_project])
      end
169 170 171 172 173 174 175
    end

    describe 'sorting' do
      let(:params) { { sort: 'name_asc' } }

      it { is_expected.to eq([internal_project, public_project]) }
    end
176 177
  end
end