2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::GithubImport::Importer::NoteImporter do
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:client) { double(:client) }
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:created_at) { Time.new(2017, 1, 1, 12, 00) }
|
|
|
|
let(:updated_at) { Time.new(2017, 1, 1, 12, 15) }
|
2021-06-08 01:23:25 +05:30
|
|
|
let(:note_body) { 'This is my note' }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
let(:github_note) do
|
|
|
|
Gitlab::GithubImport::Representation::Note.new(
|
|
|
|
noteable_id: 1,
|
|
|
|
noteable_type: 'Issue',
|
|
|
|
author: Gitlab::GithubImport::Representation::User.new(id: 4, login: 'alice'),
|
2021-06-08 01:23:25 +05:30
|
|
|
note: note_body,
|
2018-03-17 18:26:18 +05:30
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at,
|
|
|
|
github_id: 1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:importer) { described_class.new(github_note, project, client) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
context 'when the noteable exists' do
|
|
|
|
let!(:issue_row) { create(:issue, project: project, iid: 1) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(importer)
|
|
|
|
.to receive(:find_noteable_id)
|
|
|
|
.and_return(issue_row.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the author could be found' do
|
|
|
|
it 'imports the note with the found author as the note author' do
|
|
|
|
expect(importer.user_finder)
|
|
|
|
.to receive(:author_id_for)
|
|
|
|
.with(github_note)
|
|
|
|
.and_return([user.id, true])
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(ApplicationRecord)
|
|
|
|
.to receive(:legacy_bulk_insert)
|
2018-03-17 18:26:18 +05:30
|
|
|
.with(
|
|
|
|
Note.table_name,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
noteable_type: 'Issue',
|
|
|
|
noteable_id: issue_row.id,
|
|
|
|
project_id: project.id,
|
|
|
|
author_id: user.id,
|
|
|
|
note: 'This is my note',
|
2022-01-26 12:08:38 +05:30
|
|
|
discussion_id: match(/\A[0-9a-f]{40}\z/),
|
2018-03-17 18:26:18 +05:30
|
|
|
system: false,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
importer.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the note author could not be found' do
|
|
|
|
it 'imports the note with the project creator as the note author' do
|
|
|
|
expect(importer.user_finder)
|
|
|
|
.to receive(:author_id_for)
|
|
|
|
.with(github_note)
|
|
|
|
.and_return([project.creator_id, false])
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(ApplicationRecord)
|
|
|
|
.to receive(:legacy_bulk_insert)
|
2018-03-17 18:26:18 +05:30
|
|
|
.with(
|
|
|
|
Note.table_name,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
noteable_type: 'Issue',
|
|
|
|
noteable_id: issue_row.id,
|
|
|
|
project_id: project.id,
|
|
|
|
author_id: project.creator_id,
|
|
|
|
note: "*Created by: alice*\n\nThis is my note",
|
2022-01-26 12:08:38 +05:30
|
|
|
discussion_id: match(/\A[0-9a-f]{40}\z/),
|
2018-03-17 18:26:18 +05:30
|
|
|
system: false,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
importer.execute
|
|
|
|
end
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
context 'when the note have invalid chars' do
|
|
|
|
let(:note_body) { %{There were an invalid char "\u0000" <= right here} }
|
|
|
|
|
|
|
|
it 'removes invalid chars' do
|
|
|
|
expect(importer.user_finder)
|
|
|
|
.to receive(:author_id_for)
|
|
|
|
.with(github_note)
|
|
|
|
.and_return([user.id, true])
|
|
|
|
|
|
|
|
expect { importer.execute }
|
|
|
|
.to change(project.notes, :count)
|
|
|
|
.by(1)
|
|
|
|
|
|
|
|
expect(project.notes.last.note)
|
|
|
|
.to eq('There were an invalid char "" <= right here')
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the noteable does not exist' do
|
|
|
|
it 'does not import the note' do
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(ApplicationRecord).not_to receive(:legacy_bulk_insert)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
importer.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the import fails due to a foreign key error' do
|
|
|
|
it 'does not raise any errors' do
|
|
|
|
issue_row = create(:issue, project: project, iid: 1)
|
|
|
|
|
|
|
|
allow(importer)
|
|
|
|
.to receive(:find_noteable_id)
|
|
|
|
.and_return(issue_row.id)
|
|
|
|
|
|
|
|
allow(importer.user_finder)
|
|
|
|
.to receive(:author_id_for)
|
|
|
|
.with(github_note)
|
|
|
|
.and_return([user.id, true])
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(ApplicationRecord)
|
|
|
|
.to receive(:legacy_bulk_insert)
|
2018-03-17 18:26:18 +05:30
|
|
|
.and_raise(ActiveRecord::InvalidForeignKey, 'invalid foreign key')
|
|
|
|
|
|
|
|
expect { importer.execute }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'produces a valid Note' do
|
|
|
|
issue_row = create(:issue, project: project, iid: 1)
|
|
|
|
|
|
|
|
allow(importer)
|
|
|
|
.to receive(:find_noteable_id)
|
|
|
|
.and_return(issue_row.id)
|
|
|
|
|
|
|
|
allow(importer.user_finder)
|
|
|
|
.to receive(:author_id_for)
|
|
|
|
.with(github_note)
|
|
|
|
.and_return([user.id, true])
|
|
|
|
|
|
|
|
importer.execute
|
|
|
|
|
|
|
|
expect(project.notes.take).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#find_noteable_id' do
|
|
|
|
it 'returns the ID of the noteable' do
|
2020-01-01 13:55:28 +05:30
|
|
|
expect_next_instance_of(Gitlab::GithubImport::IssuableFinder) do |instance|
|
|
|
|
expect(instance).to receive(:database_id).and_return(10)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
expect(importer.find_noteable_id).to eq(10)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|