diaspora/spec/lib/archive_importer_spec.rb
cmrd Senya f85f167f50 Implement archive import backend
This implements archive import feature.

The feature is divided in two main subfeatures: archive validation and archive import.

Archive validation performs different validation on input user archive. This can be
used without actually running import, e.g. when user wants to check the archive
before import from the frontend. Validators may add messages and modify the archive.

Validators are separated in two types: critical validators and non-critical validators.

If validations by critical validators fail it means we can't import archive.

If non-critical validations fail, we can import archive, but some warning messages
are rendered.

Also validators may change archive contents, e.g. when some entity can't be
imported it may be removed from the archive.

Validators' job is to take away complexity from the importer and perform the validations
which are not implemented in other parts of the system, e.g. DB validations or
diaspora_federation entity validations.

Archive importer then takes the modified archive from the validator and imports it.

In order to incapsulate high-level migration logic a MigrationService is
introduced. MigrationService links ArchiveValidator, ArchiveImporter and
AccountMigration.

Also here is introduced a rake task which may be used by podmins to run archive
import.
2019-04-26 18:41:27 +03:00

148 lines
4.2 KiB
Ruby

# frozen_string_literal: true
require "integration/federation/federation_helper"
describe ArchiveImporter do
describe "#import" do
let(:target) { FactoryGirl.create(:user) }
let(:archive_importer) {
archive_importer = ArchiveImporter.new(archive_hash)
archive_importer.user = target
archive_importer
}
context "with tag following" do
let(:archive_hash) {
{
"user" => {
"profile" => {
"entity_data" => {
"author" => "old_id@old_pod.nowhere"
}
},
"followed_tags" => ["testtag"]
}
}
}
it "imports tag" do
archive_importer.import
expect(target.tag_followings.first.tag.name).to eq("testtag")
end
end
context "with subscription" do
let(:status_message) { FactoryGirl.create(:status_message) }
let(:archive_hash) {
{
"user" => {
"profile" => {
"entity_data" => {
"author" => "old_id@old_pod.nowhere"
}
},
"post_subscriptions" => [status_message.guid]
}
}
}
it "imports tag" do
archive_importer.import
expect(target.participations.first.target).to eq(status_message)
end
end
context "with duplicates" do
let(:archive_hash) {
{
"user" => {
"auto_follow_back_aspect" => "Friends",
"profile" => {
"entity_data" => {
"author" => "old_id@old_pod.nowhere"
}
},
"contact_groups" => [{
"chat_enabled" => true,
"name" => "Friends"
}],
"followed_tags" => [target.tag_followings.first.tag.name],
"post_subscriptions" => [target.participations.first.target.guid]
}
}
}
before do
DataGenerator.create(target, %i[tag_following subscription])
end
it "doesn't fail" do
expect {
archive_importer.import
}.not_to raise_error
end
end
context "with non-fetchable subscription" do
let(:archive_hash) {
{
"user" => {
"profile" => {
"entity_data" => {
"author" => "old_id@old_pod.nowhere"
}
},
"post_subscriptions" => ["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"]
}
}
}
before do
stub_request(:get, %r{https*://old_pod\.nowhere/\.well-known/webfinger\?resource=acct:old_id@old_pod\.nowhere})
.to_return(status: 404, body: "", headers: {})
stub_request(:get, %r{https*://old_pod\.nowhere/\.well-known/host-meta})
.to_return(status: 404, body: "", headers: {})
end
it "doesn't fail" do
expect {
archive_importer.import
}.not_to raise_error
end
end
end
describe "#create_user" do
let(:archive_importer) { ArchiveImporter.new(archive_hash) }
let(:archive_hash) {
{
"user" => {
"profile" => {
"entity_data" => {
"author" => "old_id@old_pod.nowhere"
}
},
"email" => "user@example.com",
"strip_exif" => false,
"show_community_spotlight_in_stream" => false,
"language" => "ru",
"disable_mail" => false,
"auto_follow_back" => true
}
}
}
it "creates user" do
expect {
archive_importer.create_user(username: "new_name", password: "123456")
}.to change(User, :count).by(1)
expect(archive_importer.user.email).to eq("user@example.com")
expect(archive_importer.user.strip_exif).to eq(false)
expect(archive_importer.user.show_community_spotlight_in_stream).to eq(false)
expect(archive_importer.user.language).to eq("ru")
expect(archive_importer.user.disable_mail).to eq(false)
expect(archive_importer.user.auto_follow_back).to eq(true)
end
end
end