summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-28 09:09:06 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-28 09:09:06 +0000
commit7e8278c0f46cf6058efad5afd0aef177977bd663 (patch)
tree7ac46710921145bb782bcb208ea896e1548b168b /spec
parentbbf6581214128ae12a6ff32f66a0d03ee57a2e91 (diff)
downloadgitlab-ce-7e8278c0f46cf6058efad5afd0aef177977bd663.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r--spec/fixtures/group_export.tar.gzbin4551 -> 2795 bytes
-rw-r--r--spec/frontend/lib/utils/url_utility_spec.js107
-rw-r--r--spec/frontend/repository/utils/title_spec.js25
-rw-r--r--spec/lib/gitlab/import_export/group_object_builder_spec.rb44
-rw-r--r--spec/lib/gitlab/import_export/group_relation_factory_spec.rb120
-rw-r--r--spec/lib/gitlab/import_export/group_tree_restorer_spec.rb80
-rw-r--r--spec/services/groups/import_export/import_service_spec.rb38
-rw-r--r--spec/workers/group_import_worker_spec.rb29
8 files changed, 442 insertions, 1 deletions
diff --git a/spec/fixtures/group_export.tar.gz b/spec/fixtures/group_export.tar.gz
index 83e360d7cc2..d76c6ddba25 100644
--- a/spec/fixtures/group_export.tar.gz
+++ b/spec/fixtures/group_export.tar.gz
Binary files differ
diff --git a/spec/frontend/lib/utils/url_utility_spec.js b/spec/frontend/lib/utils/url_utility_spec.js
index 048736d75f6..989de1a8337 100644
--- a/spec/frontend/lib/utils/url_utility_spec.js
+++ b/spec/frontend/lib/utils/url_utility_spec.js
@@ -1,5 +1,20 @@
import * as urlUtils from '~/lib/utils/url_utility';
+const shas = {
+ valid: [
+ 'ad9be38573f9ee4c4daec22673478c2dd1d81cd8',
+ '76e07a692f65a2f4fd72f107a3e83908bea9b7eb',
+ '9dd8f215b1e8605b1d59eaf9df1178081cda0aaf',
+ 'f2e0be58c4091b033203bae1cc0302febd54117d',
+ ],
+ invalid: [
+ 'zd9be38573f9ee4c4daec22673478c2dd1d81cd8',
+ ':6e07a692f65a2f4fd72f107a3e83908bea9b7eb',
+ '-dd8f215b1e8605b1d59eaf9df1178081cda0aaf',
+ ' 2e0be58c4091b033203bae1cc0302febd54117d',
+ ],
+};
+
const setWindowLocation = value => {
Object.defineProperty(window, 'location', {
writable: true,
@@ -154,6 +169,44 @@ describe('URL utility', () => {
});
});
+ describe('urlContainsSha', () => {
+ it('returns true when there is a valid 40-character SHA1 hash in the URL', () => {
+ shas.valid.forEach(sha => {
+ expect(
+ urlUtils.urlContainsSha({ url: `http://urlstuff/${sha}/moreurlstuff` }),
+ ).toBeTruthy();
+ });
+ });
+
+ it('returns false when there is not a valid 40-character SHA1 hash in the URL', () => {
+ shas.invalid.forEach(str => {
+ expect(urlUtils.urlContainsSha({ url: `http://urlstuff/${str}/moreurlstuff` })).toBeFalsy();
+ });
+ });
+ });
+
+ describe('getShaFromUrl', () => {
+ let validUrls = [];
+ let invalidUrls = [];
+
+ beforeAll(() => {
+ validUrls = shas.valid.map(sha => `http://urlstuff/${sha}/moreurlstuff`);
+ invalidUrls = shas.invalid.map(str => `http://urlstuff/${str}/moreurlstuff`);
+ });
+
+ it('returns the valid 40-character SHA1 hash from the URL', () => {
+ validUrls.forEach((url, idx) => {
+ expect(urlUtils.getShaFromUrl({ url })).toBe(shas.valid[idx]);
+ });
+ });
+
+ it('returns null from a URL with no valid 40-character SHA1 hash', () => {
+ invalidUrls.forEach(url => {
+ expect(urlUtils.getShaFromUrl({ url })).toBeNull();
+ });
+ });
+ });
+
describe('setUrlFragment', () => {
it('should set fragment when url has no fragment', () => {
const url = urlUtils.setUrlFragment('/home/feature', 'usage');
@@ -174,6 +227,44 @@ describe('URL utility', () => {
});
});
+ describe('updateHistory', () => {
+ const state = { key: 'prop' };
+ const title = 'TITLE';
+ const url = 'URL';
+ const win = {
+ history: {
+ pushState: jest.fn(),
+ replaceState: jest.fn(),
+ },
+ };
+
+ beforeEach(() => {
+ win.history.pushState.mockReset();
+ win.history.replaceState.mockReset();
+ });
+
+ it('should call replaceState if the replace option is true', () => {
+ urlUtils.updateHistory({ state, title, url, replace: true, win });
+
+ expect(win.history.replaceState).toHaveBeenCalledWith(state, title, url);
+ expect(win.history.pushState).not.toHaveBeenCalled();
+ });
+
+ it('should call pushState if the replace option is missing', () => {
+ urlUtils.updateHistory({ state, title, url, win });
+
+ expect(win.history.replaceState).not.toHaveBeenCalled();
+ expect(win.history.pushState).toHaveBeenCalledWith(state, title, url);
+ });
+
+ it('should call pushState if the replace option is false', () => {
+ urlUtils.updateHistory({ state, title, url, replace: false, win });
+
+ expect(win.history.replaceState).not.toHaveBeenCalled();
+ expect(win.history.pushState).toHaveBeenCalledWith(state, title, url);
+ });
+ });
+
describe('getBaseURL', () => {
beforeEach(() => {
setWindowLocation({
@@ -331,6 +422,22 @@ describe('URL utility', () => {
});
});
+ describe('urlIsDifferent', () => {
+ beforeEach(() => {
+ setWindowLocation('current');
+ });
+
+ it('should compare against the window location if no compare value is provided', () => {
+ expect(urlUtils.urlIsDifferent('different')).toBeTruthy();
+ expect(urlUtils.urlIsDifferent('current')).toBeFalsy();
+ });
+
+ it('should use the provided compare value', () => {
+ expect(urlUtils.urlIsDifferent('different', 'current')).toBeTruthy();
+ expect(urlUtils.urlIsDifferent('current', 'current')).toBeFalsy();
+ });
+ });
+
describe('setUrlParams', () => {
it('adds new params as query string', () => {
const url = 'https://gitlab.com/test';
diff --git a/spec/frontend/repository/utils/title_spec.js b/spec/frontend/repository/utils/title_spec.js
index 63035933424..a1213c13be8 100644
--- a/spec/frontend/repository/utils/title_spec.js
+++ b/spec/frontend/repository/utils/title_spec.js
@@ -1,4 +1,4 @@
-import { setTitle } from '~/repository/utils/title';
+import { setTitle, updateRefPortionOfTitle } from '~/repository/utils/title';
describe('setTitle', () => {
it.each`
@@ -13,3 +13,26 @@ describe('setTitle', () => {
expect(document.title).toEqual(`${title} · master · GitLab Org / GitLab · GitLab`);
});
});
+
+describe('updateRefPortionOfTitle', () => {
+ const sha = 'abc';
+ const testCases = [
+ [
+ 'updates the title with the SHA',
+ { title: 'part 1 · part 2 · part 3' },
+ 'part 1 · abc · part 3',
+ ],
+ ["makes no change if there's no title", { foo: null }, undefined],
+ [
+ "makes no change if the title doesn't split predictably",
+ { title: 'part 1 - part 2 - part 3' },
+ 'part 1 - part 2 - part 3',
+ ],
+ ];
+
+ it.each(testCases)('%s', (desc, doc, title) => {
+ updateRefPortionOfTitle(sha, doc);
+
+ expect(doc.title).toEqual(title);
+ });
+});
diff --git a/spec/lib/gitlab/import_export/group_object_builder_spec.rb b/spec/lib/gitlab/import_export/group_object_builder_spec.rb
new file mode 100644
index 00000000000..7950a937608
--- /dev/null
+++ b/spec/lib/gitlab/import_export/group_object_builder_spec.rb
@@ -0,0 +1,44 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::ImportExport::GroupObjectBuilder do
+ let(:group) { create(:group) }
+ let(:base_attributes) do
+ {
+ 'title' => 'title',
+ 'description' => 'description',
+ 'group' => group
+ }
+ end
+
+ context 'labels' do
+ let(:label_attributes) { base_attributes.merge('type' => 'GroupLabel') }
+
+ it 'finds the existing group label' do
+ group_label = create(:group_label, base_attributes)
+
+ expect(described_class.build(Label, label_attributes)).to eq(group_label)
+ end
+
+ it 'creates a new label' do
+ label = described_class.build(Label, label_attributes)
+
+ expect(label.persisted?).to be true
+ end
+ end
+
+ context 'milestones' do
+ it 'finds the existing group milestone' do
+ milestone = create(:milestone, base_attributes)
+
+ expect(described_class.build(Milestone, base_attributes)).to eq(milestone)
+ end
+
+ it 'creates a new milestone' do
+ milestone = described_class.build(Milestone, base_attributes)
+
+ expect(milestone.persisted?).to be true
+ end
+ end
+end
diff --git a/spec/lib/gitlab/import_export/group_relation_factory_spec.rb b/spec/lib/gitlab/import_export/group_relation_factory_spec.rb
new file mode 100644
index 00000000000..9208b2ad203
--- /dev/null
+++ b/spec/lib/gitlab/import_export/group_relation_factory_spec.rb
@@ -0,0 +1,120 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::ImportExport::GroupRelationFactory do
+ let(:group) { create(:group) }
+ let(:members_mapper) { double('members_mapper').as_null_object }
+ let(:user) { create(:admin) }
+ let(:excluded_keys) { [] }
+ let(:created_object) do
+ described_class.create(relation_sym: relation_sym,
+ relation_hash: relation_hash,
+ members_mapper: members_mapper,
+ object_builder: Gitlab::ImportExport::GroupObjectBuilder,
+ user: user,
+ importable: group,
+ excluded_keys: excluded_keys)
+ end
+
+ context 'label object' do
+ let(:relation_sym) { :group_label }
+ let(:id) { random_id }
+ let(:original_group_id) { random_id }
+
+ let(:relation_hash) do
+ {
+ 'id' => 123456,
+ 'title' => 'Bruffefunc',
+ 'color' => '#1d2da4',
+ 'project_id' => nil,
+ 'created_at' => '2019-11-20T17:02:20.546Z',
+ 'updated_at' => '2019-11-20T17:02:20.546Z',
+ 'template' => false,
+ 'description' => 'Description',
+ 'group_id' => original_group_id,
+ 'type' => 'GroupLabel',
+ 'priorities' => [],
+ 'textColor' => '#FFFFFF'
+ }
+ end
+
+ it 'does not have the original ID' do
+ expect(created_object.id).not_to eq(id)
+ end
+
+ it 'does not have the original group_id' do
+ expect(created_object.group_id).not_to eq(original_group_id)
+ end
+
+ it 'has the new group_id' do
+ expect(created_object.group_id).to eq(group.id)
+ end
+
+ context 'excluded attributes' do
+ let(:excluded_keys) { %w[description] }
+
+ it 'are removed from the imported object' do
+ expect(created_object.description).to be_nil
+ end
+ end
+ end
+
+ context 'Notes user references' do
+ let(:relation_sym) { :notes }
+ let(:new_user) { create(:user) }
+ let(:exported_member) do
+ {
+ 'id' => 111,
+ 'access_level' => 30,
+ 'source_id' => 1,
+ 'source_type' => 'Namespace',
+ 'user_id' => 3,
+ 'notification_level' => 3,
+ 'created_at' => '2016-11-18T09:29:42.634Z',
+ 'updated_at' => '2016-11-18T09:29:42.634Z',
+ 'user' => {
+ 'id' => 999,
+ 'email' => new_user.email,
+ 'username' => new_user.username
+ }
+ }
+ end
+
+ let(:relation_hash) do
+ {
+ 'id' => 4947,
+ 'note' => 'note',
+ 'noteable_type' => 'Epic',
+ 'author_id' => 999,
+ 'created_at' => '2016-11-18T09:29:42.634Z',
+ 'updated_at' => '2016-11-18T09:29:42.634Z',
+ 'project_id' => 1,
+ 'attachment' => {
+ 'url' => nil
+ },
+ 'noteable_id' => 377,
+ 'system' => true,
+ 'author' => {
+ 'name' => 'Administrator'
+ },
+ 'events' => []
+ }
+ end
+
+ let(:members_mapper) do
+ Gitlab::ImportExport::MembersMapper.new(
+ exported_members: [exported_member],
+ user: user,
+ importable: group)
+ end
+
+ it 'maps the right author to the imported note' do
+ expect(created_object.author).to eq(new_user)
+ end
+ end
+
+ def random_id
+ rand(1000..10000)
+ end
+end
diff --git a/spec/lib/gitlab/import_export/group_tree_restorer_spec.rb b/spec/lib/gitlab/import_export/group_tree_restorer_spec.rb
new file mode 100644
index 00000000000..0c55ed715cc
--- /dev/null
+++ b/spec/lib/gitlab/import_export/group_tree_restorer_spec.rb
@@ -0,0 +1,80 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::ImportExport::GroupTreeRestorer do
+ include ImportExport::CommonUtil
+
+ let(:shared) { Gitlab::ImportExport::Shared.new(group) }
+
+ describe 'restore group tree' do
+ before(:context) do
+ # Using an admin for import, so we can check assignment of existing members
+ user = create(:admin, username: 'root')
+ create(:user, username: 'adriene.mcclure')
+ create(:user, username: 'gwendolyn_robel')
+
+ RSpec::Mocks.with_temporary_scope do
+ @group = create(:group, name: 'group', path: 'group')
+ @shared = Gitlab::ImportExport::Shared.new(@group)
+
+ setup_import_export_config('group_exports/complex')
+
+ group_tree_restorer = described_class.new(user: user, shared: @shared, group: @group, group_hash: nil)
+
+ @restored_group_json = group_tree_restorer.restore
+ end
+ end
+
+ context 'JSON' do
+ it 'restores models based on JSON' do
+ expect(@restored_group_json).to be_truthy
+ end
+
+ it 'has the group description' do
+ expect(Group.find_by_path('group').description).to eq('Group Description')
+ end
+
+ it 'has group labels' do
+ expect(@group.labels.count).to eq(10)
+ end
+
+ it 'has issue boards' do
+ expect(@group.boards.count).to eq(2)
+ end
+
+ it 'has badges' do
+ expect(@group.badges.count).to eq(1)
+ end
+
+ it 'has milestones' do
+ expect(@group.milestones.count).to eq(5)
+ end
+
+ it 'has group children' do
+ expect(@group.children.count).to eq(2)
+ end
+
+ it 'has group members' do
+ expect(@group.members.map(&:user).map(&:username)).to contain_exactly('root', 'adriene.mcclure', 'gwendolyn_robel')
+ end
+ end
+ end
+
+ context 'group.json file access check' do
+ let(:user) { create(:user) }
+ let!(:group) { create(:group, name: 'group2', path: 'group2') }
+ let(:group_tree_restorer) { described_class.new(user: user, shared: shared, group: group, group_hash: nil) }
+ let(:restored_group_json) { group_tree_restorer.restore }
+
+ it 'does not read a symlink' do
+ Dir.mktmpdir do |tmpdir|
+ setup_symlink(tmpdir, 'group.json')
+ allow(shared).to receive(:export_path).and_call_original
+
+ expect(group_tree_restorer.restore).to eq(false)
+ expect(shared.errors).to include('Incorrect JSON format')
+ end
+ end
+ end
+end
diff --git a/spec/services/groups/import_export/import_service_spec.rb b/spec/services/groups/import_export/import_service_spec.rb
new file mode 100644
index 00000000000..bac266d08da
--- /dev/null
+++ b/spec/services/groups/import_export/import_service_spec.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Groups::ImportExport::ImportService do
+ describe '#execute' do
+ let(:user) { create(:admin) }
+ let(:group) { create(:group) }
+ let(:service) { described_class.new(group: group, user: user) }
+ let(:import_file) { fixture_file_upload('spec/fixtures/group_export.tar.gz') }
+
+ subject { service.execute }
+
+ before do
+ ImportExportUpload.create(group: group, import_file: import_file)
+ end
+
+ context 'when user has correct permissions' do
+ it 'imports group structure successfully' do
+ expect(subject).to be_truthy
+ end
+
+ it 'removes import file' do
+ subject
+
+ expect(group.import_export_upload.import_file.file).to be_nil
+ end
+ end
+
+ context 'when user does not have correct permissions' do
+ let(:user) { create(:user) }
+
+ it 'raises exception' do
+ expect { subject }.to raise_error(StandardError)
+ end
+ end
+ end
+end
diff --git a/spec/workers/group_import_worker_spec.rb b/spec/workers/group_import_worker_spec.rb
new file mode 100644
index 00000000000..0783ac4df4e
--- /dev/null
+++ b/spec/workers/group_import_worker_spec.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe GroupImportWorker do
+ let!(:user) { create(:user) }
+ let!(:group) { create(:group) }
+
+ subject { described_class.new }
+
+ describe '#perform' do
+ context 'when it succeeds' do
+ it 'calls the ImportService' do
+ expect_any_instance_of(::Groups::ImportExport::ImportService).to receive(:execute)
+
+ subject.perform(user.id, group.id)
+ end
+ end
+
+ context 'when it fails' do
+ it 'raises an exception when params are invalid' do
+ expect_any_instance_of(::Groups::ImportExport::ImportService).not_to receive(:execute)
+
+ expect { subject.perform(1234, group.id) }.to raise_exception(ActiveRecord::RecordNotFound)
+ expect { subject.perform(user.id, 1234) }.to raise_exception(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
+end