def test_upload_data_to_server_smaller_db(rotkehlchen_instance, db_password): """Test that if the server has bigger DB size no upload happens""" last_ts = rotkehlchen_instance.data.db.get_last_data_upload_ts() assert last_ts == 0 # Write anything in the DB to set a non-zero last_write_ts rotkehlchen_instance.data.db.set_settings( ModifiableDBSettings(main_currency=A_EUR)) _, our_hash = rotkehlchen_instance.data.compress_and_encrypt_db( db_password) remote_hash = get_different_hash(our_hash) patched_put = patch.object( rotkehlchen_instance.premium.session, 'put', return_value=None, ) patched_get = create_patched_requests_get_for_premium( session=rotkehlchen_instance.premium.session, metadata_last_modify_ts=0, metadata_data_hash=remote_hash, # larger DB than ours metadata_data_size=9999999999, saved_data='foo', ) with patched_get, patched_put as put_mock: rotkehlchen_instance.premium_sync_manager.maybe_upload_data_to_server() # The upload mock should not have been called since the hash is the same assert not put_mock.called
def test_upload_data_to_server(rotkehlchen_instance, username, db_password): """Test our side of uploading data to the server""" last_ts = rotkehlchen_instance.data.db.get_last_data_upload_ts() assert last_ts == 0 # Write anything in the DB to set a non-zero last_write_ts rotkehlchen_instance.data.db.set_settings(ModifiableDBSettings(main_currency=A_GBP)) last_write_ts = rotkehlchen_instance.data.db.get_last_write_ts() _, our_hash = rotkehlchen_instance.data.compress_and_encrypt_db(db_password) remote_hash = get_different_hash(our_hash) def mock_succesfull_upload_data_to_server( url, # pylint: disable=unused-argument data, timeout, # pylint: disable=unused-argument ): # Can't compare data blobs as they are encrypted and as such can be # different each time assert 'data_blob' in data assert data['original_hash'] == our_hash assert data['last_modify_ts'] == last_write_ts assert 'index' in data assert len(data['data_blob']) == data['length'] assert 'nonce' in data assert data['compression'] == 'zlib' return MockResponse(200, '{"success": true}') patched_put = patch.object( rotkehlchen_instance.premium.session, 'put', side_effect=mock_succesfull_upload_data_to_server, ) patched_get = create_patched_requests_get_for_premium( session=rotkehlchen_instance.premium.session, metadata_last_modify_ts=0, metadata_data_hash=remote_hash, # Smaller Remote DB size metadata_data_size=2, saved_data='foo', ) now = ts_now() with patched_get, patched_put: rotkehlchen_instance.premium_sync_manager.maybe_upload_data_to_server() last_ts = rotkehlchen_instance.data.db.get_last_data_upload_ts() msg = 'The last data upload timestamp should have been saved in the db as now' assert last_ts >= now and last_ts - now < 50, msg last_ts = rotkehlchen_instance.premium_sync_manager.last_data_upload_ts msg = 'The last data upload timestamp should also be in memory' assert last_ts >= now and last_ts - now < 50, msg # and now logout and login again and make sure that the last_data_upload_ts is correct rotkehlchen_instance.logout() rotkehlchen_instance.data.unlock(username, db_password, create_new=False) assert last_ts == rotkehlchen_instance.premium_sync_manager.last_data_upload_ts assert last_ts == rotkehlchen_instance.data.db.get_last_data_upload_ts()