github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/tests/system/tests/sharing_referenced_by.rb (about)

     1  require_relative '../boot'
     2  require 'minitest/autorun'
     3  require 'pry-rescue/minitest' unless ENV['CI']
     4  
     5  def file_has_album_reference(file, album_id)
     6    return false if file.referenced_by.nil?
     7    file.referenced_by.any? do |ref|
     8       ref["type"] == Album.doctype && ref["id"] == album_id
     9    end
    10  end
    11  
    12  describe "A photo" do
    13    it "can be shared in a folder or an album" do
    14      Helpers.scenario "referenced_by"
    15      Helpers.start_mailhog
    16  
    17      recipient_name = "Bob"
    18  
    19      # Create the instance
    20      inst = Instance.create name: "Alice"
    21      inst_recipient = Instance.create name: recipient_name
    22  
    23      # Create the folder with a photo
    24      folder = Folder.create inst
    25      folder.couch_id.wont_be_empty
    26      file_name = "../fixtures/wet-cozy_20160910__M4Dz.jpg"
    27      opts = CozyFile.options_from_fixture(file_name, dir_id: folder.couch_id)
    28      file = CozyFile.create inst, opts
    29  
    30      # Create an album with this file
    31      album = Album.create inst
    32      album.add inst, file
    33      file = CozyFile.find inst, file.couch_id
    34      assert file_has_album_reference(file, album.couch_id)
    35  
    36      # Create the sharing
    37      contact = Contact.create inst, given_name: recipient_name
    38      sharing = Sharing.new
    39      sharing.rules << Rule.sync(folder)
    40      sharing.members << inst << contact
    41      inst.register sharing
    42  
    43      # Accept the sharing
    44      sleep 1
    45      inst_recipient.accept sharing
    46      sleep 2
    47  
    48      # Check there is no reference on the recipient's file
    49      file_path = "/#{Helpers::SHARED_WITH_ME}/#{folder.name}/#{file.name}"
    50      file_recipient = CozyFile.find_by_path inst_recipient, file_path
    51      assert_equal file.name, file_recipient.name
    52      assert_nil file_recipient.referenced_by
    53  
    54      # Create an album with the file by the recipient
    55      album_recipient = Album.create inst_recipient
    56      album_recipient.add inst_recipient, file_recipient
    57      file_recipient = CozyFile.find inst_recipient, file_recipient.couch_id
    58      assert file_has_album_reference(file_recipient, album_recipient.couch_id)
    59  
    60      # Make an update on the recipient's file and check on the sharer
    61      file_recipient.rename inst_recipient, "#{Faker::Internet.slug}.jpg"
    62      sleep 11
    63      file = CozyFile.find inst, file.couch_id
    64      assert_equal file_recipient.name, file.name
    65      refute file_has_album_reference(file, album_recipient.couch_id)
    66  
    67      # Remove the sharer's file
    68      file.remove inst
    69      sleep 6
    70      file = CozyFile.find inst, file.couch_id
    71      assert file.trashed
    72  
    73      # The recipient still has the file, but in a special folder
    74      no_longer_shared = Folder.find inst_recipient, Folder::NO_LONGER_SHARED_DIR
    75      file_path = "#{no_longer_shared.path}/#{file.name}"
    76      file_recipient = CozyFile.find_by_path inst_recipient, file_path
    77      refute file_recipient.trashed
    78      assert file_has_album_reference(file_recipient, album_recipient.couch_id)
    79  
    80      # Create a picture
    81      file_name = "../fixtures/wet-cozy_20160910__M4Dz.jpg"
    82      opts = CozyFile.options_from_fixture file_name
    83      file = CozyFile.create inst, opts
    84  
    85      # Create an album with the picture
    86      album = Album.create inst
    87      album.add inst, file
    88      file = CozyFile.find inst, file.couch_id
    89      assert file_has_album_reference(file, album.couch_id)
    90  
    91      # Create the sharing
    92      contact = Contact.create inst, given_name: recipient_name
    93      sharing = Sharing.new
    94      sharing.rules = Rule.create_from_album(album, "sync")
    95      sharing.members << inst << contact
    96      inst.register sharing
    97  
    98      # Manually set the xor_key
    99      doc = Helpers.couch.get_doc inst.domain, Sharing.doctype, sharing.couch_id
   100      key = Sharing.make_xor_key
   101      doc["credentials"][0]["xor_key"] = key
   102      Helpers.couch.update_doc inst.domain, Sharing.doctype, doc
   103  
   104      # Accept the sharing
   105      sleep 1
   106      inst_recipient.accept sharing
   107      sleep 4
   108  
   109      # Check the recipient has the shared album and photo
   110      xored_id = Sharing.xor_id album.couch_id, key
   111      album_recipient = Album.find inst_recipient, xored_id
   112      assert_equal album.name, album_recipient.name
   113      file_path = "/#{Helpers::SHARED_WITH_ME}/#{album.name}/#{file.name}"
   114      file_recipient = CozyFile.find_by_path inst_recipient, file_path
   115      assert_equal file.name, file_recipient.name
   116      assert file_has_album_reference(file_recipient, xored_id)
   117  
   118      # Remove the photo from the recipient's album
   119      album_recipient.remove_photo inst_recipient, file_recipient
   120      file_recipient.rename inst_recipient, "#{Faker::Internet.slug}.jpg"
   121      sleep 5
   122  
   123      # The photo should still exist, but not in the sharer's album anymore
   124      file = CozyFile.find_by_path inst, "/#{file.name}"
   125      refute file_has_album_reference(file, album.couch_id)
   126      refute file.trashed
   127  
   128      # Add again the photo in the album
   129      album.add inst, file
   130      sleep 12
   131  
   132      # Check that the recipient has received again the photo
   133      file_path = "/#{Helpers::SHARED_WITH_ME}/#{album.name}/#{file.name}"
   134      file_recipient = CozyFile.find_by_path inst_recipient, file_path
   135      assert_equal file.name, file_recipient.name
   136      assert file_has_album_reference(file_recipient, xored_id)
   137  
   138      assert_equal inst.check, []
   139      assert_equal inst_recipient.check, []
   140  
   141      inst.remove
   142      inst_recipient.remove
   143    end
   144  end