Welcome to mirror list, hosted at ThFree Co, Russian Federation.

group_links_controller_spec.rb « projects « controllers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4510e9e646e9a603b8ac4a6a705b929f8720d4fa (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
110
111
112
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::GroupLinksController, feature_category: :system_access do
  let_it_be(:group) { create(:group, :private) }
  let_it_be(:group2) { create(:group, :private) }
  let_it_be(:project) { create(:project, :private, group: group2) }
  let_it_be(:user) { create(:user) }

  before do
    travel_to DateTime.new(2019, 4, 1)
    project.add_maintainer(user)
    sign_in(user)
  end

  after do
    travel_back
  end

  describe '#update' do
    let_it_be(:link) do
      create(
        :project_group_link,
        {
          project: project,
          group: group
        }
      )
    end

    let(:expiry_date) { 1.month.from_now.to_date }
    let(:group_access) { Gitlab::Access::GUEST }

    subject(:update_link) do
      put(
        :update,
        params: {
          namespace_id: project.namespace.to_param,
          project_id: project.to_param,
          id: link.id,
          group_link: { group_access: group_access, expires_at: expiry_date }
        },
        format: :json
      )
    end

    before do
      travel_to Time.now.utc.beginning_of_day
    end

    context 'when `expires_at` is set' do
      it 'returns correct json response' do
        update_link

        expect(json_response).to eq({ "expires_in" => controller.helpers.time_ago_with_tooltip(expiry_date), "expires_soon" => false })
      end
    end

    context 'when `expires_at` is not set' do
      let(:expiry_date) { nil }

      it 'returns empty json response' do
        update_link

        expect(json_response).to be_empty
      end
    end

    it "returns an error when link is not updated" do
      allow(::Projects::GroupLinks::UpdateService).to receive_message_chain(:new, :execute)
        .and_return(ServiceResponse.error(message: '404 Not Found', reason: :not_found))

      update_link

      expect(response).to have_gitlab_http_status(:not_found)
      expect(json_response['message']).to eq('404 Not Found')
    end
  end

  describe '#destroy' do
    let(:group_owner) { create(:user) }
    let(:group_access) { Gitlab::Access::DEVELOPER }
    let(:format) { :html }

    let!(:link) do
      create(:project_group_link, project: project, group: group, group_access: group_access)
    end

    subject(:destroy_link) do
      post(:destroy, params: { namespace_id: project.namespace.to_param,
                               project_id: project.to_param,
                               id: link.id }, format: format)
    end

    shared_examples 'success response' do
      it 'deletes the project group link' do
        expect { destroy_link }.to change { project.reload.project_group_links.count }

        expect(response).to redirect_to(project_project_members_path(project))
        expect(response).to have_gitlab_http_status(:found)
      end
    end

    context 'when user is group owner' do
      before do
        link.group.add_owner(group_owner)
        sign_in(group_owner)
      end

      context 'when user is not project maintainer' do
        it 'deletes the project group link and redirects to group show page' do
          destroy_link

          expect(response).to redirect_to(group_path(group))
          expect(response).to have_gitlab_http_status(:found)
        end
      end

      context 'when user is a project maintainer' do
        before do
          project.add_maintainer(group_owner)
        end

        it 'deletes the project group link and redirects to group show page' do
          destroy_link

          expect(response).to redirect_to(group_path(group))
          expect(response).to have_gitlab_http_status(:found)
        end
      end
    end

    context 'when user is not a group owner' do
      context 'when user is a project maintainer' do
        before do
          sign_in(user)
        end

        it_behaves_like 'success response'

        it "returns an error when link is not destroyed" do
          allow(::Projects::GroupLinks::DestroyService).to receive_message_chain(:new, :execute)
            .and_return(ServiceResponse.error(message: 'The error message'))

          expect { destroy_link }.not_to change { project.reload.project_group_links.count }
          expect(flash[:alert]).to eq('The project-group link could not be removed.')
        end

        context 'when format is js' do
          let(:format) { :js }

          it "returns an error when link is not destroyed" do
            allow(::Projects::GroupLinks::DestroyService).to receive_message_chain(:new, :execute)
              .and_return(ServiceResponse.error(message: '404 Not Found', reason: :not_found))

            expect { destroy_link }.not_to change { project.reload.project_group_links.count }
            expect(response).to have_gitlab_http_status(:not_found)
            expect(json_response['message']).to eq('404 Not Found')
          end
        end
      end

      context 'when user is not a project maintainer' do
        before do
          project.add_developer(user)
          sign_in(user)
        end

        it 'renders 404' do
          destroy_link

          expect(response).to have_gitlab_http_status(:not_found)
        end
      end
    end
  end
end