diff options
-rw-r--r-- | tests/vaulttests.py | 79 | ||||
-rw-r--r-- | tutorius/vault.py | 74 |
2 files changed, 150 insertions, 3 deletions
diff --git a/tests/vaulttests.py b/tests/vaulttests.py index c6bd852..5fdb7f9 100644 --- a/tests/vaulttests.py +++ b/tests/vaulttests.py @@ -187,9 +187,7 @@ class VaultInterfaceTest(unittest.TestCase): # Note : Temporary only test query that return ALL tutorials in the vault. # TODO : Test with varying parameters - vault = Vault() - - tutorial_list = vault.query() + tutorial_list = Vault.query() if tutorial_list.__len__() < 2: assert False, 'Error, list doesnt have enough tutorial in it : ' + str(tutorial_list.__len__()) + ' element' @@ -259,6 +257,81 @@ class VaultInterfaceTest(unittest.TestCase): # TODO : Compare the initial and reloaded metadata when vault.Query() will accept specifc argument # (so we can specifiy that we want only the metadata for this particular tutorial + def test_update_metadata(self): + """ + This test verify that the vault interface funciton update_metadata succesfully update a tutorial metadata. + """ + # Create and save a tutorial + tutorial = Tutorial('test') + Vault.saveTutorial(tutorial, self.test_metadata_dict) + + bundler = TutorialBundler(self.save_test_guid) + + # Create a metadata dictionnary different than the initial one + test_new_metadata_dict = {} + test_new_metadata_dict['name'] = 'TestTutorial_modified' + test_new_metadata_dict['guid'] = str(self.save_test_guid) + test_new_metadata_dict['version'] = '3' + test_new_metadata_dict['description'] = 'This is a test tutorial modified' + test_new_metadata_dict['rating'] = '4.5' + test_new_metadata_dict['category'] = 'Test_modified' + test_new_metadata_dict['publish_state'] = 'true' + new_activities_dict = {} + new_activities_dict['org.laptop.tutoriusactivity'] = '2' + new_activities_dict['org.laptop.writus'] = '2' + test_new_metadata_dict['activities'] = new_activities_dict + + # Test update_metadata + Vault.update_metadata(self.save_test_guid, test_new_metadata_dict) + + # Get the metadata back + tutorial_list = Vault.query() + + new_metadata_found = False + + # Test that the metadata in the tutorial is indeed the new one + for tuto_dictionnary in tutorial_list: + if tuto_dictionnary['name'] == 'TestTutorial_modified': + # Flag that indicate that the modified metadata has been found + new_metadata_found = True + related = tuto_dictionnary['activities'] + assert tuto_dictionnary['version'] == '3' + assert tuto_dictionnary['description'] == 'This is a test tutorial modified' + assert tuto_dictionnary['rating'] == '4.5' + assert tuto_dictionnary['category'] == 'Test_modified' + assert tuto_dictionnary['publish_state'] == 'true' + assert related.has_key('org.laptop.tutoriusactivity') + assert related.has_key('org.laptop.writus') + + # If false, new metadata hasn't been found in the vault + assert new_metadata_found == True, 'Modified metadata not found in the vault' + + + def test_get_tutorial_archive(self): + """ + This test verify that the vault interface function get_tutorial_archive zip a tutorial and + return the zipped archive object succesfully. + """ + # Create and save a tutorial + tutorial = Tutorial('test') + Vault.saveTutorial(tutorial, self.test_metadata_dict) + + bundler = TutorialBundler(self.save_test_guid) + + # Test get_tutorial_archive() + archive_object = Vault.get_tutorial_archive(self.save_test_guid) + + # Write the archive data as a new file + zip_path = str(self.save_test_guid) + '_test_.zip' + file = open(zip_path, 'wt') + file.write(archive_object) + file.close() + + # Test if new zip is valid + assert zipfile.is_zipfile(zip_path) + # Remove test file + os.remove(zip_path) + def test_add_delete_get_path_resource(self): """ diff --git a/tutorius/vault.py b/tutorius/vault.py index 7ec0a23..c0409da 100644 --- a/tutorius/vault.py +++ b/tutorius/vault.py @@ -342,6 +342,80 @@ class Vault(object): else: return False + + @staticmethod + def update_metadata(guid, metadata_dict): + """ + Modify the metadata of given tutorial found whit his guid. + @params guid of the tutorial to update the metadata. + @params metadata_dict the new metadata to update from + """ + + bundle = TutorialBundler(guid) + tutorial_path = bundle.get_tutorial_path(guid) + # Create the metadata file + ini_file_path = os.path.join(tutorial_path, "meta.ini") + parser = SafeConfigParser() + parser.add_section(INI_METADATA_SECTION) + for key, value in metadata_dict.items(): + if key != 'activities': + parser.set(INI_METADATA_SECTION, key, value) + else: + related_activities_dict = value + parser.add_section(INI_ACTIVITY_SECTION) + for related_key, related_value in related_activities_dict.items(): + parser.set(INI_ACTIVITY_SECTION, related_key, related_value) + + # Delete the old metadata file + if os.path.isfile(ini_file_path): + os.remove(ini_file_path) + + # Write the new metadata file to disk + with open(ini_file_path, 'wb') as configfile: + parser.write(configfile) + + if os.path.isfile(ini_file_path): + return True + else: + return False + + + @staticmethod + def get_tutorial_archive(guid): + """ + Zip the files of the tutorial indentified with is guid and returns it. + @params guid the guid of the tutorial to zip + @returns a zip archive containing the files of the tutorial + """ + bundle = TutorialBundler(guid) + bundle_path = bundle.get_tutorial_path(guid) + + #Zip the tutorials files in the pkzip file format in a temp file + archive_list = [os.path.join(bundle_path, 'meta.ini'), os.path.join(bundle_path, 'tutorial.xml')] #, os.path.join(bundle_path, RESOURCES_FOLDER)] + + zfilename = str(guid) + ".zip" + + zout = zipfile.ZipFile(os.path.join(bundle_path, zfilename), "w") + for fname in archive_list: + fname_splitted = fname.rsplit('/') + file_only_name = fname_splitted[fname_splitted.__len__() - 1] + zout.write(fname, file_only_name) + zout.close() + + # test if the temp file is a valid pkzip file + assert zipfile.is_zipfile(os.path.join(bundle_path, zfilename)) == True, "The zipping of the tutorial files failed." + + # open the temp archive file in read mode + zin = open(os.path.join(bundle_path, zfilename), "r") + archive_data = zin.read() + zin.close() + + # delete temp file + os.remove(os.path.join(bundle_path, zfilename)) + + # return zip object data + return archive_data + @staticmethod def add_resource(tutorial_guid, file_path): |