group_links_controller_spec.rb 4.2 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'spec_helper'

5
RSpec.describe Projects::GroupLinksController do
6
  let(:group) { create(:group, :private) }
barthc's avatar
barthc committed
7
  let(:group2) { create(:group, :private) }
8
  let(:project) { create(:project, :private, group: group2) }
9 10 11
  let(:user) { create(:user) }

  before do
12
    project.add_maintainer(user)
13 14 15 16 17 18
    sign_in(user)
  end

  describe '#create' do
    shared_context 'link project to group' do
      before do
blackst0ne's avatar
blackst0ne committed
19 20 21 22 23 24
        post(:create, params: {
                        namespace_id: project.namespace,
                        project_id: project,
                        link_group_id: group.id,
                        link_group_access: ProjectGroupLink.default_access
                      })
25 26 27
      end
    end

28 29
    context 'when project is not allowed to be shared with a group' do
      before do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
30
        group.update(share_with_group_lock: false)
31 32 33 34 35
      end

      include_context 'link project to group'

      it 'responds with status 404' do
36
        expect(response).to have_gitlab_http_status(:not_found)
37 38 39
      end
    end

charlieablett's avatar
charlieablett committed
40
    context 'when user has access to group they want to link project to' do
41 42 43 44
      before do
        group.add_developer(user)
      end

45 46 47 48 49 50
      include_context 'link project to group'

      it 'links project with selected group' do
        expect(group.shared_projects).to include project
      end

51
      it 'redirects to project group links page' do
52
        expect(response).to redirect_to(
53
          project_project_members_path(project)
54 55 56 57
        )
      end
    end

charlieablett's avatar
charlieablett committed
58
    context 'when user doers not have access to group they want to link to' do
59 60 61
      include_context 'link project to group'

      it 'renders 404' do
62
        expect(response).to have_gitlab_http_status(:not_found)
63 64 65
      end

      it 'does not share project with that group' do
66
        expect(group.shared_projects).not_to include project
67 68
      end
    end
barthc's avatar
barthc committed
69

70 71 72 73 74 75
    context 'when user does not have access to the public group' do
      let(:group) { create(:group, :public) }

      include_context 'link project to group'

      it 'renders 404' do
76
        expect(response).to have_gitlab_http_status(:not_found)
77 78 79 80 81 82 83
      end

      it 'does not share project with that group' do
        expect(group.shared_projects).not_to include project
      end
    end

barthc's avatar
barthc committed
84 85
    context 'when project group id equal link group id' do
      before do
86 87
        group2.add_developer(user)

blackst0ne's avatar
blackst0ne committed
88 89 90 91 92 93
        post(:create, params: {
                        namespace_id: project.namespace,
                        project_id: project,
                        link_group_id: group2.id,
                        link_group_access: ProjectGroupLink.default_access
                      })
barthc's avatar
barthc committed
94 95 96 97 98 99 100 101
      end

      it 'does not share project with selected group' do
        expect(group2.shared_projects).not_to include project
      end

      it 'redirects to project group links page' do
        expect(response).to redirect_to(
102
          project_project_members_path(project)
barthc's avatar
barthc committed
103 104 105 106 107 108
        )
      end
    end

    context 'when link group id is not present' do
      before do
blackst0ne's avatar
blackst0ne committed
109 110 111 112 113
        post(:create, params: {
                        namespace_id: project.namespace,
                        project_id: project,
                        link_group_access: ProjectGroupLink.default_access
                      })
barthc's avatar
barthc committed
114 115 116 117
      end

      it 'redirects to project group links page' do
        expect(response).to redirect_to(
118
          project_project_members_path(project)
barthc's avatar
barthc committed
119 120 121 122
        )
        expect(flash[:alert]).to eq('Please select a group.')
      end
    end
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

    context 'when link is not persisted in the database' do
      before do
        allow(::Projects::GroupLinks::CreateService).to receive_message_chain(:new, :execute)
          .and_return({ status: :error, http_status: 409, message: 'error' })

        post(:create, params: {
                        namespace_id: project.namespace,
                        project_id: project,
                        link_group_id: group.id,
                        link_group_access: ProjectGroupLink.default_access
                      })
      end

      it 'redirects to project group links page' do
        expect(response).to redirect_to(
          project_project_members_path(project)
        )
        expect(flash[:alert]).to eq('error')
      end
    end
144 145
  end
end