michael@0: #!/usr/bin/env python michael@0: michael@0: """ michael@0: test installing and managing webapps in a profile michael@0: """ michael@0: michael@0: import os michael@0: import shutil michael@0: import unittest michael@0: from tempfile import mkdtemp michael@0: michael@0: from mozprofile.webapps import WebappCollection, Webapp, WebappFormatException michael@0: michael@0: here = os.path.dirname(os.path.abspath(__file__)) michael@0: michael@0: class WebappTest(unittest.TestCase): michael@0: """Tests reading, installing and cleaning webapps michael@0: from a profile. michael@0: """ michael@0: manifest_path_1 = os.path.join(here, 'files', 'webapps1.json') michael@0: manifest_path_2 = os.path.join(here, 'files', 'webapps2.json') michael@0: michael@0: def setUp(self): michael@0: self.profile = mkdtemp(prefix='test_webapp') michael@0: self.webapps_dir = os.path.join(self.profile, 'webapps') michael@0: self.webapps_json_path = os.path.join(self.webapps_dir, 'webapps.json') michael@0: michael@0: def tearDown(self): michael@0: shutil.rmtree(self.profile) michael@0: michael@0: def test_read_json_manifest(self): michael@0: """Tests WebappCollection.read_json""" michael@0: # Parse a list of webapp objects and verify it worked michael@0: manifest_json_1 = WebappCollection.read_json(self.manifest_path_1) michael@0: self.assertEqual(len(manifest_json_1), 7) michael@0: for app in manifest_json_1: michael@0: self.assertIsInstance(app, Webapp) michael@0: for key in Webapp.required_keys: michael@0: self.assertIn(key, app) michael@0: michael@0: # Parse a dictionary of webapp objects and verify it worked michael@0: manifest_json_2 = WebappCollection.read_json(self.manifest_path_2) michael@0: self.assertEqual(len(manifest_json_2), 5) michael@0: for app in manifest_json_2: michael@0: self.assertIsInstance(app, Webapp) michael@0: for key in Webapp.required_keys: michael@0: self.assertIn(key, app) michael@0: michael@0: def test_invalid_webapp(self): michael@0: """Tests a webapp with a missing required key""" michael@0: webapps = WebappCollection(self.profile) michael@0: # Missing the required key "description", exception should be raised michael@0: self.assertRaises(WebappFormatException, webapps.append, { 'name': 'foo' }) michael@0: michael@0: def test_webapp_collection(self): michael@0: """Tests the methods of the WebappCollection object""" michael@0: webapp_1 = { 'name': 'test_app_1', michael@0: 'description': 'a description', michael@0: 'manifestURL': 'http://example.com/1/manifest.webapp', michael@0: 'appStatus': 1 } michael@0: michael@0: webapp_2 = { 'name': 'test_app_2', michael@0: 'description': 'another description', michael@0: 'manifestURL': 'http://example.com/2/manifest.webapp', michael@0: 'appStatus': 2 } michael@0: michael@0: webapp_3 = { 'name': 'test_app_2', michael@0: 'description': 'a third description', michael@0: 'manifestURL': 'http://example.com/3/manifest.webapp', michael@0: 'appStatus': 3 } michael@0: michael@0: webapps = WebappCollection(self.profile) michael@0: self.assertEqual(len(webapps), 0) michael@0: michael@0: # WebappCollection should behave like a list michael@0: def invalid_index(): michael@0: webapps[0] michael@0: self.assertRaises(IndexError, invalid_index) michael@0: michael@0: # Append a webapp object michael@0: webapps.append(webapp_1) michael@0: self.assertTrue(len(webapps), 1) michael@0: self.assertIsInstance(webapps[0], Webapp) michael@0: self.assertEqual(len(webapps[0]), len(webapp_1)) michael@0: self.assertEqual(len(set(webapps[0].items()) & set(webapp_1.items())), len(webapp_1)) michael@0: michael@0: # Remove a webapp object michael@0: webapps.remove(webapp_1) michael@0: self.assertEqual(len(webapps), 0) michael@0: michael@0: # Extend a list of webapp objects michael@0: webapps.extend([webapp_1, webapp_2]) michael@0: self.assertEqual(len(webapps), 2) michael@0: self.assertTrue(webapp_1 in webapps) michael@0: self.assertTrue(webapp_2 in webapps) michael@0: self.assertNotEquals(webapps[0], webapps[1]) michael@0: michael@0: # Insert a webapp object michael@0: webapps.insert(1, webapp_3) michael@0: self.assertEqual(len(webapps), 3) michael@0: self.assertEqual(webapps[1], webapps[2]) michael@0: for app in webapps: michael@0: self.assertIsInstance(app, Webapp) michael@0: michael@0: # Assigning an invalid type (must be accepted by the dict() constructor) should throw michael@0: def invalid_type(): michael@0: webapps[2] = 1 michael@0: self.assertRaises(WebappFormatException, invalid_type) michael@0: michael@0: def test_install_webapps(self): michael@0: """Test installing webapps into a profile that has no prior webapps""" michael@0: webapps = WebappCollection(self.profile, apps=self.manifest_path_1) michael@0: self.assertFalse(os.path.exists(self.webapps_dir)) michael@0: michael@0: # update the webapp manifests for the first time michael@0: webapps.update_manifests() michael@0: self.assertFalse(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) michael@0: self.assertTrue(os.path.isfile(self.webapps_json_path)) michael@0: michael@0: webapps_json = webapps.read_json(self.webapps_json_path, description="fake description") michael@0: self.assertEqual(len(webapps_json), 7) michael@0: for app in webapps_json: michael@0: self.assertIsInstance(app, Webapp) michael@0: michael@0: manifest_json_1 = webapps.read_json(self.manifest_path_1) michael@0: manifest_json_2 = webapps.read_json(self.manifest_path_2) michael@0: self.assertEqual(len(webapps_json), len(manifest_json_1)) michael@0: for app in webapps_json: michael@0: self.assertTrue(app in manifest_json_1) michael@0: michael@0: # Remove one of the webapps from WebappCollection after it got installed michael@0: removed_app = manifest_json_1[2] michael@0: webapps.remove(removed_app) michael@0: # Add new webapps to the collection michael@0: webapps.extend(manifest_json_2) michael@0: michael@0: # update the webapp manifests a second time michael@0: webapps.update_manifests() michael@0: self.assertFalse(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) michael@0: self.assertTrue(os.path.isfile(self.webapps_json_path)) michael@0: michael@0: webapps_json = webapps.read_json(self.webapps_json_path, description="a description") michael@0: self.assertEqual(len(webapps_json), 11) michael@0: michael@0: # The new apps should be added michael@0: for app in webapps_json: michael@0: self.assertIsInstance(app, Webapp) michael@0: self.assertTrue(os.path.isfile(os.path.join(self.webapps_dir, app['name'], 'manifest.webapp'))) michael@0: # The removed app should not exist in the manifest michael@0: self.assertNotIn(removed_app, webapps_json) michael@0: self.assertFalse(os.path.exists(os.path.join(self.webapps_dir, removed_app['name']))) michael@0: michael@0: # Cleaning should delete the webapps directory entirely since there was nothing there before michael@0: webapps.clean() michael@0: self.assertFalse(os.path.isdir(self.webapps_dir)) michael@0: michael@0: def test_install_webapps_preexisting(self): michael@0: """Tests installing webapps when the webapps directory already exists""" michael@0: manifest_json_2 = WebappCollection.read_json(self.manifest_path_2) michael@0: michael@0: # Synthesize a pre-existing webapps directory michael@0: os.mkdir(self.webapps_dir) michael@0: shutil.copyfile(self.manifest_path_2, self.webapps_json_path) michael@0: for app in manifest_json_2: michael@0: app_path = os.path.join(self.webapps_dir, app['name']) michael@0: os.mkdir(app_path) michael@0: f = open(os.path.join(app_path, 'manifest.webapp'), 'w') michael@0: f.close() michael@0: michael@0: webapps = WebappCollection(self.profile, apps=self.manifest_path_1) michael@0: self.assertTrue(os.path.exists(self.webapps_dir)) michael@0: michael@0: # update webapp manifests for the first time michael@0: webapps.update_manifests() michael@0: # A backup should be created michael@0: self.assertTrue(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) michael@0: michael@0: # Both manifests should remain installed michael@0: webapps_json = webapps.read_json(self.webapps_json_path, description='a fake description') michael@0: self.assertEqual(len(webapps_json), 12) michael@0: for app in webapps_json: michael@0: self.assertIsInstance(app, Webapp) michael@0: self.assertTrue(os.path.isfile(os.path.join(self.webapps_dir, app['name'], 'manifest.webapp'))) michael@0: michael@0: # Upon cleaning the backup should be restored michael@0: webapps.clean() michael@0: self.assertFalse(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) michael@0: michael@0: # The original webapps should still be installed michael@0: webapps_json = webapps.read_json(self.webapps_json_path) michael@0: for app in webapps_json: michael@0: self.assertIsInstance(app, Webapp) michael@0: self.assertTrue(os.path.isfile(os.path.join(self.webapps_dir, app['name'], 'manifest.webapp'))) michael@0: self.assertEqual(webapps_json, manifest_json_2) michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()