2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe Gitlab::ImportExport::MembersMapper do
|
2016-06-22 15:30:34 +05:30
|
|
|
describe 'map members' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:user) { create(:admin) }
|
|
|
|
let(:project) { create(:project, :public, name: 'searchable_project') }
|
|
|
|
let(:user2) { create(:user) }
|
2016-06-22 15:30:34 +05:30
|
|
|
let(:exported_user_id) { 99 }
|
|
|
|
let(:exported_members) do
|
|
|
|
[{
|
|
|
|
"id" => 2,
|
|
|
|
"access_level" => 40,
|
|
|
|
"source_id" => 14,
|
|
|
|
"source_type" => "Project",
|
|
|
|
"notification_level" => 3,
|
|
|
|
"created_at" => "2016-03-11T10:21:44.822Z",
|
|
|
|
"updated_at" => "2016-03-11T10:21:44.822Z",
|
|
|
|
"created_by_id" => nil,
|
|
|
|
"invite_email" => nil,
|
|
|
|
"invite_token" => nil,
|
|
|
|
"invite_accepted_at" => nil,
|
|
|
|
"user" =>
|
|
|
|
{
|
|
|
|
"id" => exported_user_id,
|
|
|
|
"email" => user2.email,
|
2017-08-17 22:00:37 +05:30
|
|
|
"username" => 'test'
|
2019-09-04 21:01:54 +05:30
|
|
|
},
|
2020-01-01 13:55:28 +05:30
|
|
|
"user_id" => 19
|
2016-09-13 17:45:13 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
"id" => 3,
|
|
|
|
"access_level" => 40,
|
|
|
|
"source_id" => 14,
|
|
|
|
"source_type" => "Project",
|
|
|
|
"user_id" => nil,
|
|
|
|
"notification_level" => 3,
|
|
|
|
"created_at" => "2016-03-11T10:21:44.822Z",
|
|
|
|
"updated_at" => "2016-03-11T10:21:44.822Z",
|
|
|
|
"created_by_id" => 1,
|
|
|
|
"invite_email" => 'invite@test.com',
|
|
|
|
"invite_token" => 'token',
|
|
|
|
"invite_accepted_at" => nil
|
2016-06-22 15:30:34 +05:30
|
|
|
}]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:members_mapper) do
|
|
|
|
described_class.new(
|
2020-01-01 13:55:28 +05:30
|
|
|
exported_members: exported_members, user: user, importable: project)
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'includes the exported user ID in the map' do
|
|
|
|
expect(members_mapper.map.keys).to include(exported_user_id)
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
it 'maps a project member' do
|
|
|
|
expect(members_mapper.map[exported_user_id]).to eq(user2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to importer project member if it does not exist' do
|
|
|
|
expect(members_mapper.map[-1]).to eq(user.id)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'has invited members with no user' do
|
|
|
|
members_mapper.map
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(ProjectMember.find_by_invite_email('invite@test.com')).not_to be_nil
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'authorizes the users to the project' do
|
2016-09-13 17:45:13 +05:30
|
|
|
members_mapper.map
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(user.authorized_project?(project)).to be true
|
|
|
|
expect(user2.authorized_project?(project)).to be true
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
it 'maps an owner as a maintainer' do
|
|
|
|
exported_members.first['access_level'] = ProjectMember::OWNER
|
|
|
|
|
|
|
|
expect(members_mapper.map[exported_user_id]).to eq(user2.id)
|
|
|
|
expect(ProjectMember.find_by_user_id(user2.id).access_level).to eq(ProjectMember::MAINTAINER)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes old user_id from member_hash to avoid conflict with user key' do
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(ProjectMember)
|
|
|
|
.to receive(:create)
|
2019-09-04 21:01:54 +05:30
|
|
|
.twice
|
|
|
|
.with(hash_excluding('user_id'))
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
members_mapper.map
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'user is not an admin' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:user) { create(:user) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'does not map a project member' do
|
|
|
|
expect(members_mapper.map[exported_user_id]).to eq(user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to importer project member if it does not exist' do
|
|
|
|
expect(members_mapper.map[-1]).to eq(user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'chooses the one with an email first' do
|
|
|
|
let(:user3) { create(:user, username: 'test') }
|
|
|
|
|
|
|
|
it 'maps the project member that has a matching email first' do
|
|
|
|
expect(members_mapper.map[exported_user_id]).to eq(user2.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'importer same as group member' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:user2) { create(:admin) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:group) { create(:group) }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project, :public, name: 'searchable_project', namespace: group) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:members_mapper) do
|
|
|
|
described_class.new(
|
2020-01-01 13:55:28 +05:30
|
|
|
exported_members: exported_members, user: user2, importable: project)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_users([user, user2], GroupMember::DEVELOPER)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'maps the project member' do
|
|
|
|
expect(members_mapper.map[exported_user_id]).to eq(user2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'maps the project member if it already exists' do
|
2018-11-18 11:00:15 +05:30
|
|
|
project.add_maintainer(user2)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(members_mapper.map[exported_user_id]).to eq(user2.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'importing group members' do
|
|
|
|
let(:group) { create(:group) }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project, namespace: group) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:members_mapper) do
|
|
|
|
described_class.new(
|
2020-01-01 13:55:28 +05:30
|
|
|
exported_members: exported_members, user: user, importable: project)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_users([user, user2], GroupMember::DEVELOPER)
|
|
|
|
user.update(email: 'invite@test.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'maps the importer' do
|
|
|
|
expect(members_mapper.map[-1]).to eq(user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'maps the group member' do
|
|
|
|
expect(members_mapper.map[exported_user_id]).to eq(user2.id)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
context 'when importer mapping fails' do
|
|
|
|
let(:exception_message) { 'Something went wrong' }
|
|
|
|
|
|
|
|
it 'includes importer specific error message' do
|
|
|
|
expect(ProjectMember).to receive(:create!).and_raise(StandardError.new(exception_message))
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
expect { members_mapper.map }.to raise_error(StandardError, "Error adding importer user to Project members. #{exception_message}")
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|