group_links_controller_spec.rb 3.01 KB
Newer Older
1 2 3 4
require 'spec_helper'

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

  before do
10
    project.add_maintainer(user)
11 12 13 14 15 16
    sign_in(user)
  end

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

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

      include_context 'link project to group'

      it 'responds with status 404' do
        expect(response).to have_gitlab_http_status(404)
      end
    end

38
    context 'when user has access to group he want to link project to' do
39 40 41 42
      before do
        group.add_developer(user)
      end

43 44 45 46 47 48
      include_context 'link project to group'

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

49
      it 'redirects to project group links page' do
50
        expect(response).to redirect_to(
51
          project_project_members_path(project)
52 53 54 55 56 57 58 59 60 61 62 63
        )
      end
    end

    context 'when user doers not have access to group he want to link to' do
      include_context 'link project to group'

      it 'renders 404' do
        expect(response.status).to eq 404
      end

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

    context 'when project group id equal link group id' do
      before do
70 71
        group2.add_developer(user)

blackst0ne's avatar
blackst0ne committed
72 73 74 75 76 77
        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
78 79 80 81 82 83 84 85
      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(
86
          project_project_members_path(project)
barthc's avatar
barthc committed
87 88 89 90 91 92
        )
      end
    end

    context 'when link group id is not present' do
      before do
blackst0ne's avatar
blackst0ne committed
93 94 95 96 97
        post(:create, params: {
                        namespace_id: project.namespace,
                        project_id: project,
                        link_group_access: ProjectGroupLink.default_access
                      })
barthc's avatar
barthc committed
98 99 100 101
      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
        )
        expect(flash[:alert]).to eq('Please select a group.')
      end
    end
107 108
  end
end