backup_service_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe BackupService, type: :service do
  4. subject(:service_call) { described_class.new.call(backup) }
  5. let!(:user) { Fabricate(:user) }
  6. let!(:attachment) { Fabricate(:media_attachment, account: user.account) }
  7. let!(:status) { Fabricate(:status, account: user.account, text: 'Hello', visibility: :public, media_attachments: [attachment]) }
  8. let!(:private_status) { Fabricate(:status, account: user.account, text: 'secret', visibility: :private) }
  9. let!(:favourite) { Fabricate(:favourite, account: user.account) }
  10. let!(:bookmark) { Fabricate(:bookmark, account: user.account) }
  11. let!(:backup) { Fabricate(:backup, user: user) }
  12. def read_zip_file(backup, filename)
  13. file = Paperclip.io_adapters.for(backup.dump)
  14. Zip::File.open(file) do |zipfile|
  15. entry = zipfile.glob(filename).first
  16. return entry.get_input_stream.read
  17. end
  18. end
  19. context 'when the user has an avatar and header' do
  20. before do
  21. user.account.update!(avatar: attachment_fixture('avatar.gif'))
  22. user.account.update!(header: attachment_fixture('emojo.png'))
  23. end
  24. it 'stores them as expected' do
  25. service_call
  26. json = export_json(:actor)
  27. avatar_path = json.dig('icon', 'url')
  28. header_path = json.dig('image', 'url')
  29. expect(avatar_path).to_not be_nil
  30. expect(header_path).to_not be_nil
  31. expect(read_zip_file(backup, avatar_path)).to be_present
  32. expect(read_zip_file(backup, header_path)).to be_present
  33. end
  34. end
  35. it 'marks the backup as processed and exports files' do
  36. expect { service_call }.to process_backup
  37. expect_outbox_export
  38. expect_likes_export
  39. expect_bookmarks_export
  40. end
  41. def process_backup
  42. change(backup, :processed).from(false).to(true)
  43. end
  44. def expect_outbox_export
  45. json = export_json(:outbox)
  46. aggregate_failures do
  47. expect(json['@context']).to_not be_nil
  48. expect(json['type']).to eq 'OrderedCollection'
  49. expect(json['totalItems']).to eq 2
  50. expect(json['orderedItems'][0]['@context']).to be_nil
  51. expect(json['orderedItems'][0]).to include_create_item(status)
  52. expect(json['orderedItems'][1]).to include_create_item(private_status)
  53. end
  54. end
  55. def expect_likes_export
  56. json = export_json(:likes)
  57. aggregate_failures do
  58. expect(json['type']).to eq 'OrderedCollection'
  59. expect(json['orderedItems']).to eq [ActivityPub::TagManager.instance.uri_for(favourite.status)]
  60. end
  61. end
  62. def expect_bookmarks_export
  63. json = export_json(:bookmarks)
  64. aggregate_failures do
  65. expect(json['type']).to eq 'OrderedCollection'
  66. expect(json['orderedItems']).to eq [ActivityPub::TagManager.instance.uri_for(bookmark.status)]
  67. end
  68. end
  69. def export_json(type)
  70. Oj.load(read_zip_file(backup, "#{type}.json"))
  71. end
  72. def include_create_item(status)
  73. include({
  74. 'type' => 'Create',
  75. 'object' => include({
  76. 'id' => ActivityPub::TagManager.instance.uri_for(status),
  77. 'content' => "<p>#{status.text}</p>",
  78. }),
  79. })
  80. end
  81. end