summaryrefslogtreecommitdiff
path: root/spec/controllers/import/available_namespaces_controller_spec.rb
blob: 26ea1d9218997a61a603388ff0b8cacd88498b7a (plain)
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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Import::AvailableNamespacesController do
  let_it_be(:user) { create(:user) }

  before do
    sign_in(user)
  end

  describe "GET index" do
    context "when having group with role never allowed to create projects" do
      using RSpec::Parameterized::TableSyntax

      where(
        role: [:guest, :reporter],
        default_project_creation_access: [::Gitlab::Access::MAINTAINER_PROJECT_ACCESS, ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS],
        group_project_creation_level: [nil, ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS, ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS])

      with_them do
        before do
          stub_application_setting(default_project_creation: default_project_creation_access)
        end

        it "does not include group with access level #{params[:role]} in list" do
          group = create(:group, project_creation_level: group_project_creation_level)
          group.add_member(user, role)
          get :index

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).not_to include({
            'id' => group.id,
            'full_path' => group.full_path
          })
        end
      end
    end

    context "when having group with role always allowed to create projects" do
      using RSpec::Parameterized::TableSyntax

      where(
        role: [:maintainer, :owner],
        default_project_creation_access: [::Gitlab::Access::MAINTAINER_PROJECT_ACCESS, ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS],
        group_project_creation_level: [nil, ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS, ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS])

      with_them do
        before do
          stub_application_setting(default_project_creation: default_project_creation_access)
        end

        it "does not include group with access level #{params[:role]} in list" do
          group = create(:group, project_creation_level: group_project_creation_level)
          group.add_member(user, role)
          get :index

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).to include({
            'id' => group.id,
            'full_path' => group.full_path
          })
        end
      end
    end

    context "when having developer role" do
      using RSpec::Parameterized::TableSyntax

      where(:default_project_creation_access, :project_creation_level, :is_visible) do
        ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS           | nil                                                   | false
        ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS           | ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS | true
        ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS | nil                                                   | true
        ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS | ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS           | false
      end

      with_them do
        before do
          stub_application_setting(default_project_creation: default_project_creation_access)
        end

        it "#{params[:is_visible] ? 'includes' : 'does not include'} group with access level #{params[:role]} in list" do
          group = create(:group, project_creation_level: project_creation_level)
          group.add_member(user, :developer)

          get :index

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).send(is_visible ? 'to' : 'not_to', include({
            'id' => group.id,
            'full_path' => group.full_path
          }))
        end
      end
    end

    context "with an anonymous user" do
      before do
        sign_out(user)
      end

      it "redirects to sign-in page" do
        get :index

        expect(response).to redirect_to(new_user_session_path)
      end
    end
  end
end