BigW Consortium Gitlab

members_mapper_spec.rb 4.18 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::ImportExport::MembersMapper, services: true do
4
  describe 'map members' do
5
    let(:user) { create(:admin) }
6
    let(:project) { create(:empty_project, :public, name: 'searchable_project') }
7
    let(:user2) { create(:user) }
8
    let(:exported_user_id) { 99 }
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    let(:exported_members) do
      [{
         "id" => 2,
         "access_level" => 40,
         "source_id" => 14,
         "source_type" => "Project",
         "user_id" => 19,
         "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" =>
           {
25
             "id" => exported_user_id,
26
             "email" => user2.email,
27
             "username" => 'test'
28
           }
29 30 31 32 33 34 35 36 37 38 39 40 41 42
       },
       {
         "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
43 44
       }]
    end
45 46

    let(:members_mapper) do
47
      described_class.new(
48
        exported_members: exported_members, user: user, project: project)
49 50
    end

51 52 53 54
    it 'includes the exported user ID in the map' do
      expect(members_mapper.map.keys).to include(exported_user_id)
    end

55
    it 'maps a project member' do
56
      expect(members_mapper.map[exported_user_id]).to eq(user2.id)
57 58 59
    end

    it 'defaults to importer project member if it does not exist' do
60
      expect(members_mapper.map[-1]).to eq(user.id)
61 62
    end

63 64 65 66 67
    it 'has invited members with no user' do
      members_mapper.map

      expect(ProjectMember.find_by_invite_email('invite@test.com')).not_to be_nil
    end
68 69 70 71 72 73 74

    it 'authorizes the users to the project' do
      members_mapper.map

      expect(user.authorized_project?(project)).to be true
      expect(user2.authorized_project?(project)).to be true
    end
75

76
    context 'user is not an admin' do
77
      let(:user) { create(:user) }
78

79 80 81 82 83 84 85 86
      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
87 88

    context 'chooses the one with an email first' do
James Lopez committed
89
      let(:user3) { create(:user, username: 'test') }
90 91 92 93 94

      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
95 96

    context 'importer same as group member' do
97
      let(:user2) { create(:admin) }
98 99 100 101 102 103 104 105
      let(:group) { create(:group) }
      let(:project) { create(:empty_project, :public, name: 'searchable_project', namespace: group) }
      let(:members_mapper) do
        described_class.new(
          exported_members: exported_members, user: user2, project: project)
      end

      before do
James Lopez committed
106
        group.add_users([user, user2], GroupMember::DEVELOPER)
107 108 109 110 111 112 113
      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
James Lopez committed
114
        project.add_master(user2)
115 116 117 118

        expect(members_mapper.map[exported_user_id]).to eq(user2.id)
      end
    end
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

    context 'importing group members' do
      let(:group) { create(:group) }
      let(:project) { create(:empty_project, namespace: group) }
      let(:members_mapper) do
        described_class.new(
          exported_members: exported_members, user: user, project: project)
      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
    end
141 142
  end
end