backup_service_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe BackupService 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. body = export_json_raw(:outbox)
  46. json = Oj.load(body)
  47. aggregate_failures do
  48. expect(body.scan('@context').count).to eq 1
  49. expect(body.scan('orderedItems').count).to eq 1
  50. expect(json['@context']).to_not be_nil
  51. expect(json['type']).to eq 'OrderedCollection'
  52. expect(json['totalItems']).to eq 2
  53. expect(json['orderedItems'][0]['@context']).to be_nil
  54. expect(json['orderedItems'][0]).to include_create_item(status)
  55. expect(json['orderedItems'][1]).to include_create_item(private_status)
  56. end
  57. end
  58. def expect_likes_export
  59. json = export_json(:likes)
  60. aggregate_failures do
  61. expect(json['type']).to eq 'OrderedCollection'
  62. expect(json['orderedItems']).to eq [ActivityPub::TagManager.instance.uri_for(favourite.status)]
  63. end
  64. end
  65. def expect_bookmarks_export
  66. json = export_json(:bookmarks)
  67. aggregate_failures do
  68. expect(json['type']).to eq 'OrderedCollection'
  69. expect(json['orderedItems']).to eq [ActivityPub::TagManager.instance.uri_for(bookmark.status)]
  70. end
  71. end
  72. def export_json_raw(type)
  73. read_zip_file(backup, "#{type}.json")
  74. end
  75. def export_json(type)
  76. Oj.load(export_json_raw(type))
  77. end
  78. def include_create_item(status)
  79. include({
  80. 'type' => 'Create',
  81. 'object' => include({
  82. 'id' => ActivityPub::TagManager.instance.uri_for(status),
  83. 'content' => "<p>#{status.text}</p>",
  84. }),
  85. })
  86. end
  87. end