summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/lib/gitlab/import_export/relation_factory_shared_examples.rb
blob: 9656f3c3520a6c635b204c45289c1aa72f0b6bc2 (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
# frozen_string_literal: true

# required context:
# - importable: group or project
# - relation_hash: a note relation that's being imported
# - created_object: the object created with the relation factory
RSpec.shared_examples 'Notes user references' do
  let(:relation_sym) { :notes }
  let(:mapped_user) { create(:user) }
  let(:exported_member) do
    {
      'id' => 111,
      'access_level' => 30,
      'source_id' => 1,
      'source_type' => importable.instance_of?(Project) ? 'Project' : '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,
        'public_email' => mapped_user.email,
        'username' => mapped_user.username
      }
    }
  end

  let(:members_mapper) do
    Gitlab::ImportExport::MembersMapper.new(
      exported_members: [exported_member].compact,
      user: importer_user,
      importable: importable
    )
  end

  shared_examples 'sets the note author to the importer user' do
    it { expect(created_object.author).to eq(importer_user) }
  end

  shared_examples 'sets the note author to the mapped user' do
    it { expect(created_object.author).to eq(mapped_user) }
  end

  shared_examples 'does not add original autor note' do
    it { expect(created_object.note).not_to include('*By Administrator') }
  end

  shared_examples 'adds original autor note' do
    it { expect(created_object.note).to include('*By Administrator') }
  end

  context 'when the importer is admin' do
    let(:importer_user) { create(:admin) }

    context 'and the note author is not mapped' do
      let(:exported_member) { nil }

      include_examples 'sets the note author to the importer user'

      include_examples 'adds original autor note'
    end

    context 'and the note author is the importer user' do
      let(:mapped_user) { importer_user }

      include_examples 'sets the note author to the mapped user'

      include_examples 'does not add original autor note'
    end

    context 'and the note author exists in the target instance' do
      let(:mapped_user) { create(:user) }

      include_examples 'sets the note author to the mapped user'

      include_examples 'does not add original autor note'
    end
  end

  context 'when the importer is not admin' do
    let(:importer_user) { create(:user) }

    context 'and the note author is not mapped' do
      let(:exported_member) { nil }

      include_examples 'sets the note author to the importer user'

      include_examples 'adds original autor note'
    end

    context 'and the note author is the importer user' do
      let(:mapped_user) { importer_user }

      include_examples 'sets the note author to the importer user'

      include_examples 'adds original autor note'
    end

    context 'and the note author exists in the target instance' do
      let(:mapped_user) { create(:user) }

      include_examples 'sets the note author to the importer user'

      include_examples 'adds original autor note'
    end
  end
end