2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-18 04:56:10 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 14:08:28 -04:00
|
|
|
RSpec.describe Projects::GroupLinksController do
|
2020-09-30 08:09:53 -04:00
|
|
|
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) }
|
2016-04-18 04:56:10 -04:00
|
|
|
|
|
|
|
before do
|
2021-04-12 14:12:15 -04:00
|
|
|
travel_to DateTime.new(2019, 4, 1)
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2016-04-18 04:56:10 -04:00
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2021-04-12 14:12:15 -04:00
|
|
|
after do
|
|
|
|
travel_back
|
|
|
|
end
|
|
|
|
|
2016-04-18 04:56:10 -04:00
|
|
|
describe '#create' do
|
|
|
|
shared_context 'link project to group' do
|
|
|
|
before do
|
2018-12-17 17:52:17 -05:00
|
|
|
post(:create, params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
link_group_id: group.id,
|
|
|
|
link_group_access: ProjectGroupLink.default_access
|
|
|
|
})
|
2016-04-18 04:56:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-22 09:15:31 -04:00
|
|
|
context 'when project is not allowed to be shared with a group' do
|
|
|
|
before do
|
2021-03-30 08:10:51 -04:00
|
|
|
group.update!(share_with_group_lock: false)
|
2018-05-22 09:15:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
include_context 'link project to group'
|
|
|
|
|
|
|
|
it 'responds with status 404' do
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-05-22 09:15:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-07 10:09:52 -05:00
|
|
|
context 'when user has access to group they want to link project to' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
group.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2016-04-18 04:56:10 -04:00
|
|
|
include_context 'link project to group'
|
|
|
|
|
|
|
|
it 'links project with selected group' do
|
|
|
|
expect(group.shared_projects).to include project
|
|
|
|
end
|
|
|
|
|
2016-05-23 14:16:35 -04:00
|
|
|
it 'redirects to project group links page' do
|
2016-04-18 04:56:10 -04:00
|
|
|
expect(response).to redirect_to(
|
2017-07-07 10:40:41 -04:00
|
|
|
project_project_members_path(project)
|
2016-04-18 04:56:10 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-07 10:09:52 -05:00
|
|
|
context 'when user doers not have access to group they want to link to' do
|
2016-04-18 04:56:10 -04:00
|
|
|
include_context 'link project to group'
|
|
|
|
|
|
|
|
it 'renders 404' do
|
2020-03-31 17:08:05 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2016-04-18 04:56:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not share project with that group' do
|
2016-05-23 19:37:59 -04:00
|
|
|
expect(group.shared_projects).not_to include project
|
2016-04-18 04:56:10 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-01 13:23:39 -04:00
|
|
|
|
2019-02-12 07:29:47 -05:00
|
|
|
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
|
2020-03-31 17:08:05 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2019-02-12 07:29:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not share project with that group' do
|
|
|
|
expect(group.shared_projects).not_to include project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-01 13:23:39 -04:00
|
|
|
context 'when project group id equal link group id' do
|
|
|
|
before do
|
2019-02-12 07:29:47 -05:00
|
|
|
group2.add_developer(user)
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post(:create, params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
link_group_id: group2.id,
|
|
|
|
link_group_access: ProjectGroupLink.default_access
|
|
|
|
})
|
2016-09-01 13:23:39 -04:00
|
|
|
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(
|
2017-07-07 10:40:41 -04:00
|
|
|
project_project_members_path(project)
|
2016-09-01 13:23:39 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when link group id is not present' do
|
|
|
|
before do
|
2018-12-17 17:52:17 -05:00
|
|
|
post(:create, params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
link_group_access: ProjectGroupLink.default_access
|
|
|
|
})
|
2016-09-01 13:23:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to project group links page' do
|
|
|
|
expect(response).to redirect_to(
|
2017-07-07 10:40:41 -04:00
|
|
|
project_project_members_path(project)
|
2016-09-01 13:23:39 -04:00
|
|
|
)
|
|
|
|
expect(flash[:alert]).to eq('Please select a group.')
|
|
|
|
end
|
|
|
|
end
|
2019-02-12 07:29:47 -05:00
|
|
|
|
|
|
|
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
|
2016-04-18 04:56:10 -04:00
|
|
|
end
|
2020-09-30 08:09:53 -04:00
|
|
|
|
|
|
|
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 }
|
|
|
|
|
|
|
|
before do
|
|
|
|
travel_to Time.now.utc.beginning_of_day
|
|
|
|
|
|
|
|
put(
|
|
|
|
:update,
|
|
|
|
params: {
|
|
|
|
namespace_id: project.namespace.to_param,
|
|
|
|
project_id: project.to_param,
|
|
|
|
id: link.id,
|
|
|
|
group_link: { group_access: Gitlab::Access::GUEST, expires_at: expiry_date }
|
|
|
|
},
|
|
|
|
format: :json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when `expires_at` is set' do
|
|
|
|
it 'returns correct json response' do
|
|
|
|
expect(json_response).to eq({ "expires_in" => "about 1 month", "expires_soon" => false })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when `expires_at` is not set' do
|
|
|
|
let(:expiry_date) { nil }
|
|
|
|
|
|
|
|
it 'returns empty json response' do
|
|
|
|
expect(json_response).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-18 04:56:10 -04:00
|
|
|
end
|