1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe Import::AvailableNamespacesController do
6  let_it_be(:user) { create(:user) }
7
8  before do
9    sign_in(user)
10  end
11
12  describe "GET index" do
13    context "when having group with role never allowed to create projects" do
14      using RSpec::Parameterized::TableSyntax
15
16      where(
17        role: [:guest, :reporter],
18        default_project_creation_access: [::Gitlab::Access::MAINTAINER_PROJECT_ACCESS, ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS],
19        group_project_creation_level: [nil, ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS, ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS])
20
21      with_them do
22        before do
23          stub_application_setting(default_project_creation: default_project_creation_access)
24        end
25
26        it "does not include group with access level #{params[:role]} in list" do
27          group = create(:group, project_creation_level: group_project_creation_level)
28          group.add_user(user, role)
29          get :index
30
31          expect(response).to have_gitlab_http_status(:ok)
32          expect(json_response).not_to include({
33            'id' => group.id,
34            'full_path' => group.full_path
35          })
36        end
37      end
38    end
39
40    context "when having group with role always allowed to create projects" do
41      using RSpec::Parameterized::TableSyntax
42
43      where(
44        role: [:maintainer, :owner],
45        default_project_creation_access: [::Gitlab::Access::MAINTAINER_PROJECT_ACCESS, ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS],
46        group_project_creation_level: [nil, ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS, ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS])
47
48      with_them do
49        before do
50          stub_application_setting(default_project_creation: default_project_creation_access)
51        end
52
53        it "does not include group with access level #{params[:role]} in list" do
54          group = create(:group, project_creation_level: group_project_creation_level)
55          group.add_user(user, role)
56          get :index
57
58          expect(response).to have_gitlab_http_status(:ok)
59          expect(json_response).to include({
60            'id' => group.id,
61            'full_path' => group.full_path
62          })
63        end
64      end
65    end
66
67    context "when having developer role" do
68      using RSpec::Parameterized::TableSyntax
69
70      where(:default_project_creation_access, :project_creation_level, :is_visible) do
71        ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS           | nil                                                   | false
72        ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS           | ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS | true
73        ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS | nil                                                   | true
74        ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS | ::Gitlab::Access::MAINTAINER_PROJECT_ACCESS           | false
75      end
76
77      with_them do
78        before do
79          stub_application_setting(default_project_creation: default_project_creation_access)
80        end
81
82        it "#{params[:is_visible] ? 'includes' : 'does not include'} group with access level #{params[:role]} in list" do
83          group = create(:group, project_creation_level: project_creation_level)
84          group.add_user(user, :developer)
85
86          get :index
87
88          expect(response).to have_gitlab_http_status(:ok)
89          expect(json_response).send(is_visible ? 'to' : 'not_to', include({
90            'id' => group.id,
91            'full_path' => group.full_path
92          }))
93        end
94      end
95    end
96
97    context "with an anonymous user" do
98      before do
99        sign_out(user)
100      end
101
102      it "redirects to sign-in page" do
103        get :index
104
105        expect(response).to redirect_to(new_user_session_path)
106      end
107    end
108  end
109end
110