def test_get_hash_exists(): db = Db() random_key = helper.random_string(10) random_value = helper.random_string(12) # Test with explicit False value as 3rd param db.add_hash(random_key, random_value, False) assert db.get_hash(random_key) == random_value, 'Lookup for hash that exists did not return value'
def test_check_hash_exists(): db = Db() random_key = helper.random_string(10) random_value = helper.random_string(12) # Test with explicit False value as 3rd param db.add_hash(random_key, random_value, False) assert db.check_hash( random_key) == True, 'Lookup for hash did not return True'
def test_create_directory_recursive_success(): filesystem = FileSystem() folder = '%s/%s/%s' % (helper.temp_dir(), helper.random_string(10), helper.random_string(10)) status = filesystem.create_directory(folder) # Needs to be a subdirectory assert helper.temp_dir() != folder assert status == True assert os.path.isdir(folder) == True assert os.path.exists(folder) == True shutil.rmtree(folder)
def test_encrypt_decrypt_bad_passphrase(): "should fail when trying to decrypt with a bad passphrase" encrypt_filenames = helper.docs_filenames(['doc1.odt', 'doc2.odt', 'spreadsheet.ods']) passphrase1 = helper.random_string(30) passphrase2 = helper.random_string(30) # encrypt assert not os.path.exists(output_dir) supercipher.encrypt(encrypt_filenames, output_file, None, passphrase1) assert os.path.isfile(output_file) # decrypt supercipher.decrypt(output_file, output_dir, passphrase2) assert not os.path.exists(output_dir)
def test_add_hash_explicit_write(): db = Db() random_key = helper.random_string(10) random_value = helper.random_string(12) # Test with explicit True value as 3rd param db.add_hash(random_key, random_value, True) assert db.check_hash(random_key) == True, 'Lookup for hash did not return True' # Instnatiate new db class to confirm random_key exists db2 = Db() assert db2.check_hash(random_key) == True
def test_add_hash_default_do_not_write(): db = Db() random_key = helper.random_string(10) random_value = helper.random_string(12) # Test with default False value as 3rd param db.add_hash(random_key, random_value) assert db.check_hash(random_key) == True, 'Lookup for hash did not return True' # Instnatiate new db class to confirm random_key does not exist db2 = Db() assert db2.check_hash(random_key) == False
def test_add_hash_explicit_write(): db = Db() random_key = helper.random_string(10) random_value = helper.random_string(12) # Test with explicit True value as 3rd param db.add_hash(random_key, random_value, True) assert db.check_hash( random_key) == True, 'Lookup for hash did not return True' # Instnatiate new db class to confirm random_key exists db2 = Db() assert db2.check_hash(random_key) == True
def test_add_hash_default_do_not_write(): db = Db() random_key = helper.random_string(10) random_value = helper.random_string(12) # Test with default False value as 3rd param db.add_hash(random_key, random_value) assert db.check_hash( random_key) == True, 'Lookup for hash did not return True' # Instnatiate new db class to confirm random_key does not exist db2 = Db() assert db2.check_hash(random_key) == False
def test_set_original_name(): files = ['plain.jpg', 'audio.m4a', 'photo.nef', 'video.mov'] for file in files: ext = os.path.splitext(file)[1] temporary_folder, folder = helper.create_working_folder() random_file_name = '%s%s' % (helper.random_string(10), ext) origin = '%s/%s' % (folder, random_file_name) file_path = helper.get_file(file) if file_path is False: file_path = helper.download_file(file, folder) shutil.copyfile(file_path, origin) media = Media.get_class_by_file(origin, [Audio, Media, Photo, Video]) metadata = media.get_metadata() media.set_original_name() metadata_updated = media.get_metadata() shutil.rmtree(folder) assert metadata['original_name'] is None, metadata['original_name'] assert metadata_updated['original_name'] == random_file_name, metadata_updated['original_name']
def test_get_hash_does_not_exist(): db = Db() random_key = helper.random_string(10) assert db.get_hash( random_key ) is None, 'Lookup for hash that should not exist did not return None'
def test_gnupg_pubkey_encryption(): "should be able to encrypt with GnuPG.pubkey_encrypt, and decrypt with GnuPGP.pubkey_decrypt with valid pubkey and seckey" passphrase = helper.random_string(128) plaintext = helper.random_string(256) # make file with random data open(plaintext_path, 'w').write(plaintext) # encrypt gpg.pubkey_encrypt(plaintext_path, '6F6467FDF4462C38FE597CD0CA6C5413CF7BCA9E') helper.delete_file(plaintext_path) # decrypt gpg.pubkey_decrypt(ciphertext_path) new_plaintext = open(plaintext_path, 'r').read() assert plaintext == new_plaintext
def test_check_hash_does_not_exist(): db = Db() random_key = helper.random_string(10) assert db.check_hash( random_key ) == False, 'Lookup for hash that should not exist returned True'
def test_delete_directory_if_empty_when_not_empty(): filesystem = FileSystem() folder = '%s/%s/%s' % (helper.temp_dir(), helper.random_string(10), helper.random_string(10)) os.makedirs(folder) parent_folder = os.path.dirname(folder) assert os.path.isdir(folder) == True assert os.path.exists(folder) == True assert os.path.isdir(parent_folder) == True assert os.path.exists(parent_folder) == True filesystem.delete_directory_if_empty(parent_folder) assert os.path.isdir(folder) == True assert os.path.exists(folder) == True assert os.path.isdir(parent_folder) == True assert os.path.exists(parent_folder) == True shutil.rmtree(parent_folder)
def test_set_original_name_with_arg(): temporary_folder, folder = helper.create_working_folder() random_file_name = '%s.txt' % helper.random_string(10) origin = '%s/%s' % (folder, random_file_name) shutil.copyfile(helper.get_file('valid.txt'), origin) new_name = helper.random_string(15) text = Text(origin) metadata = text.get_metadata() text.set_original_name(new_name) metadata_updated = text.get_metadata() shutil.rmtree(folder) assert metadata['original_name'] is None, metadata['original_name'] assert metadata_updated['original_name'] == new_name, metadata_updated[ 'original_name']
def test_gnupg_pubkey_encryption_missing_seckey(): "when encrypting with GnuPG.pubkey_encrypt, should fail to decrypt with GnuPGP.pubkey_decrypt if seckey is missing" passphrase = helper.random_string(128) plaintext = helper.random_string(256) # make file with random data open(plaintext_path, 'w').write(plaintext) # encrypt gpg.pubkey_encrypt(plaintext_path, '77D4E195BE81A10047B06E4747AA62EF2712261B') helper.delete_file(plaintext_path) # decrypt try: gpg.pubkey_decrypt(ciphertext_path) new_plaintext = open(plaintext_path, 'r').read() except MissingSeckey: assert True else: assert False
def test_get_location_coordinates_does_not_exists(): db = Db() latitude, longitude, name = helper.get_test_location() name = '%s-%s' % (name, helper.random_string(10)) latitude = helper.random_coordinate(latitude, 1) longitude = helper.random_coordinate(longitude, 1) location = db.get_location_coordinates(name) assert location is None
def test_delete_directory_if_empty(): filesystem = FileSystem() folder = '%s/%s' % (helper.temp_dir(), helper.random_string(10)) os.makedirs(folder) assert os.path.isdir(folder) == True assert os.path.exists(folder) == True filesystem.delete_directory_if_empty(folder) assert os.path.isdir(folder) == False assert os.path.exists(folder) == False
def test_delete_directory_if_empty(): filesystem = FileSystem() folder = os.path.join(helper.temp_dir(), helper.random_string(10)) os.makedirs(folder) assert os.path.isdir(folder) == True assert os.path.exists(folder) == True filesystem.delete_directory_if_empty(folder) assert os.path.isdir(folder) == False assert os.path.exists(folder) == False
def test_encrypt_decrypt(): "should be able to encrypt a file with a random passphrase and decrypt it again" encrypt_filenames = helper.docs_filenames(['doc1.odt']) passphrase = helper.random_string(30) # encrypt assert not os.path.exists(output_dir) supercipher.encrypt(encrypt_filenames, output_file, None, passphrase) assert os.path.isfile(output_file) # decrypt supercipher.decrypt(output_file, output_dir, passphrase) assert os.path.isdir(output_dir) assert os.path.isfile('{0}/doc1.odt'.format(output_dir))
def test_encrypt_decrypt_missing_seckey(): "should fail to decrypt when trying to decrypt files encrypted using random passphrase and a seckey that is missing" encrypt_filenames = helper.docs_filenames(['doc1.odt', 'doc2.odt', 'spreadsheet.ods']) passphrase = helper.random_string(30) # we have the public key but not the secret key pubkey = '77D4E195BE81A10047B06E4747AA62EF2712261B' # encrypt assert not os.path.exists(output_dir) supercipher.encrypt(encrypt_filenames, output_file, pubkey, passphrase) assert os.path.isfile(output_file) # decrypt supercipher.decrypt(output_file, output_dir, passphrase) assert not os.path.exists(output_dir)
def test_get_location_coordinates_exists(): db = Db() latitude, longitude, name = helper.get_test_location() name = '%s-%s' % (name, helper.random_string(10)) latitude = helper.random_coordinate(latitude, 1) longitude = helper.random_coordinate(longitude, 1) db.add_location(latitude, longitude, name) location = db.get_location_coordinates(name) assert location is not None assert location[0] == latitude assert location[1] == longitude
def test_encrypt_decrypt_missing_pubkey(): "should fail to encrypt when trying to encrypt files using random passphrase and a pubkey that is missing" encrypt_filenames = helper.docs_filenames(['doc1.odt', 'doc2.odt', 'spreadsheet.ods']) passphrase = helper.random_string(30) # we do not have this public key pubkey = '0B1491929806596254700155FD720AD9EBA34B1C' # encrypt assert not os.path.exists(output_dir) try: supercipher.encrypt(encrypt_filenames, output_file, pubkey, passphrase) except MissingPubkey: assert True else: assert False assert not os.path.isfile(output_file)
def test_set_original_name_with_arg(): temporary_folder, folder = helper.create_working_folder() origin = '%s/%s' % (folder, 'plain.jpg') file = helper.get_file('plain.jpg') shutil.copyfile(file, origin) new_name = helper.random_string(15) media = Media.get_class_by_file(origin, [Photo]) metadata_before = media.get_metadata() result = media.set_original_name(new_name) metadata_after = media.get_metadata() assert metadata_before['original_name'] is None, metadata_before assert metadata_after['original_name'] == new_name, metadata_after assert result is True, result
def test_encrypt_decrypt_pubkey(): "should be able to encrypt/decrypt files using random passphrase and a pubkey" encrypt_filenames = helper.docs_filenames(['doc1.odt', 'doc2.odt', 'spreadsheet.ods']) passphrase = helper.random_string(30) # we have the public and secret key pubkey = '6F6467FDF4462C38FE597CD0CA6C5413CF7BCA9E' # encrypt assert not os.path.exists(output_dir) supercipher.encrypt(encrypt_filenames, output_file, pubkey, passphrase) assert os.path.isfile(output_file) # decrypt supercipher.decrypt(output_file, output_dir, passphrase) assert os.path.isdir(output_dir) assert os.path.isfile('{0}/doc1.odt'.format(output_dir)) assert os.path.isfile('{0}/doc2.odt'.format(output_dir)) assert os.path.isfile('{0}/spreadsheet.ods'.format(output_dir))
def test_encrypt_decrypt_files_dirs(): "should be able to encrypt files and directories with a random passphrase and decrypt it again" encrypt_filenames = helper.docs_filenames(['doc1.odt', 'doc2.odt', 'spreadsheet.ods', 'more_files']) passphrase = helper.random_string(30) # encrypt assert not os.path.exists(output_dir) supercipher.encrypt(encrypt_filenames, output_file, None, passphrase) assert os.path.isfile(output_file) # decrypt supercipher.decrypt(output_file, output_dir, passphrase) assert os.path.isdir(output_dir) assert os.path.isfile('{0}/doc1.odt'.format(output_dir)) assert os.path.isfile('{0}/doc2.odt'.format(output_dir)) assert os.path.isfile('{0}/spreadsheet.ods'.format(output_dir)) assert os.path.isdir('{0}/more_files'.format(output_dir)) assert os.path.isfile('{0}/more_files/image.png'.format(output_dir)) assert os.path.isfile('{0}/more_files/text.txt'.format(output_dir)) assert os.path.isdir('{0}/more_files/nested'.format(output_dir)) assert os.path.isfile('{0}/more_files/nested/nested_doc.odt'.format(output_dir))
def test_get_hash_does_not_exist(): db = Db() random_key = helper.random_string(10) assert db.get_hash(random_key) is None, 'Lookup for hash that should not exist did not return None'
def test_check_hash_does_not_exist(): db = Db() random_key = helper.random_string(10) assert db.check_hash(random_key) == False, 'Lookup for hash that should not exist returned True'