1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozprofile/tests/test_webapps.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,197 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +""" 1.7 +test installing and managing webapps in a profile 1.8 +""" 1.9 + 1.10 +import os 1.11 +import shutil 1.12 +import unittest 1.13 +from tempfile import mkdtemp 1.14 + 1.15 +from mozprofile.webapps import WebappCollection, Webapp, WebappFormatException 1.16 + 1.17 +here = os.path.dirname(os.path.abspath(__file__)) 1.18 + 1.19 +class WebappTest(unittest.TestCase): 1.20 + """Tests reading, installing and cleaning webapps 1.21 + from a profile. 1.22 + """ 1.23 + manifest_path_1 = os.path.join(here, 'files', 'webapps1.json') 1.24 + manifest_path_2 = os.path.join(here, 'files', 'webapps2.json') 1.25 + 1.26 + def setUp(self): 1.27 + self.profile = mkdtemp(prefix='test_webapp') 1.28 + self.webapps_dir = os.path.join(self.profile, 'webapps') 1.29 + self.webapps_json_path = os.path.join(self.webapps_dir, 'webapps.json') 1.30 + 1.31 + def tearDown(self): 1.32 + shutil.rmtree(self.profile) 1.33 + 1.34 + def test_read_json_manifest(self): 1.35 + """Tests WebappCollection.read_json""" 1.36 + # Parse a list of webapp objects and verify it worked 1.37 + manifest_json_1 = WebappCollection.read_json(self.manifest_path_1) 1.38 + self.assertEqual(len(manifest_json_1), 7) 1.39 + for app in manifest_json_1: 1.40 + self.assertIsInstance(app, Webapp) 1.41 + for key in Webapp.required_keys: 1.42 + self.assertIn(key, app) 1.43 + 1.44 + # Parse a dictionary of webapp objects and verify it worked 1.45 + manifest_json_2 = WebappCollection.read_json(self.manifest_path_2) 1.46 + self.assertEqual(len(manifest_json_2), 5) 1.47 + for app in manifest_json_2: 1.48 + self.assertIsInstance(app, Webapp) 1.49 + for key in Webapp.required_keys: 1.50 + self.assertIn(key, app) 1.51 + 1.52 + def test_invalid_webapp(self): 1.53 + """Tests a webapp with a missing required key""" 1.54 + webapps = WebappCollection(self.profile) 1.55 + # Missing the required key "description", exception should be raised 1.56 + self.assertRaises(WebappFormatException, webapps.append, { 'name': 'foo' }) 1.57 + 1.58 + def test_webapp_collection(self): 1.59 + """Tests the methods of the WebappCollection object""" 1.60 + webapp_1 = { 'name': 'test_app_1', 1.61 + 'description': 'a description', 1.62 + 'manifestURL': 'http://example.com/1/manifest.webapp', 1.63 + 'appStatus': 1 } 1.64 + 1.65 + webapp_2 = { 'name': 'test_app_2', 1.66 + 'description': 'another description', 1.67 + 'manifestURL': 'http://example.com/2/manifest.webapp', 1.68 + 'appStatus': 2 } 1.69 + 1.70 + webapp_3 = { 'name': 'test_app_2', 1.71 + 'description': 'a third description', 1.72 + 'manifestURL': 'http://example.com/3/manifest.webapp', 1.73 + 'appStatus': 3 } 1.74 + 1.75 + webapps = WebappCollection(self.profile) 1.76 + self.assertEqual(len(webapps), 0) 1.77 + 1.78 + # WebappCollection should behave like a list 1.79 + def invalid_index(): 1.80 + webapps[0] 1.81 + self.assertRaises(IndexError, invalid_index) 1.82 + 1.83 + # Append a webapp object 1.84 + webapps.append(webapp_1) 1.85 + self.assertTrue(len(webapps), 1) 1.86 + self.assertIsInstance(webapps[0], Webapp) 1.87 + self.assertEqual(len(webapps[0]), len(webapp_1)) 1.88 + self.assertEqual(len(set(webapps[0].items()) & set(webapp_1.items())), len(webapp_1)) 1.89 + 1.90 + # Remove a webapp object 1.91 + webapps.remove(webapp_1) 1.92 + self.assertEqual(len(webapps), 0) 1.93 + 1.94 + # Extend a list of webapp objects 1.95 + webapps.extend([webapp_1, webapp_2]) 1.96 + self.assertEqual(len(webapps), 2) 1.97 + self.assertTrue(webapp_1 in webapps) 1.98 + self.assertTrue(webapp_2 in webapps) 1.99 + self.assertNotEquals(webapps[0], webapps[1]) 1.100 + 1.101 + # Insert a webapp object 1.102 + webapps.insert(1, webapp_3) 1.103 + self.assertEqual(len(webapps), 3) 1.104 + self.assertEqual(webapps[1], webapps[2]) 1.105 + for app in webapps: 1.106 + self.assertIsInstance(app, Webapp) 1.107 + 1.108 + # Assigning an invalid type (must be accepted by the dict() constructor) should throw 1.109 + def invalid_type(): 1.110 + webapps[2] = 1 1.111 + self.assertRaises(WebappFormatException, invalid_type) 1.112 + 1.113 + def test_install_webapps(self): 1.114 + """Test installing webapps into a profile that has no prior webapps""" 1.115 + webapps = WebappCollection(self.profile, apps=self.manifest_path_1) 1.116 + self.assertFalse(os.path.exists(self.webapps_dir)) 1.117 + 1.118 + # update the webapp manifests for the first time 1.119 + webapps.update_manifests() 1.120 + self.assertFalse(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) 1.121 + self.assertTrue(os.path.isfile(self.webapps_json_path)) 1.122 + 1.123 + webapps_json = webapps.read_json(self.webapps_json_path, description="fake description") 1.124 + self.assertEqual(len(webapps_json), 7) 1.125 + for app in webapps_json: 1.126 + self.assertIsInstance(app, Webapp) 1.127 + 1.128 + manifest_json_1 = webapps.read_json(self.manifest_path_1) 1.129 + manifest_json_2 = webapps.read_json(self.manifest_path_2) 1.130 + self.assertEqual(len(webapps_json), len(manifest_json_1)) 1.131 + for app in webapps_json: 1.132 + self.assertTrue(app in manifest_json_1) 1.133 + 1.134 + # Remove one of the webapps from WebappCollection after it got installed 1.135 + removed_app = manifest_json_1[2] 1.136 + webapps.remove(removed_app) 1.137 + # Add new webapps to the collection 1.138 + webapps.extend(manifest_json_2) 1.139 + 1.140 + # update the webapp manifests a second time 1.141 + webapps.update_manifests() 1.142 + self.assertFalse(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) 1.143 + self.assertTrue(os.path.isfile(self.webapps_json_path)) 1.144 + 1.145 + webapps_json = webapps.read_json(self.webapps_json_path, description="a description") 1.146 + self.assertEqual(len(webapps_json), 11) 1.147 + 1.148 + # The new apps should be added 1.149 + for app in webapps_json: 1.150 + self.assertIsInstance(app, Webapp) 1.151 + self.assertTrue(os.path.isfile(os.path.join(self.webapps_dir, app['name'], 'manifest.webapp'))) 1.152 + # The removed app should not exist in the manifest 1.153 + self.assertNotIn(removed_app, webapps_json) 1.154 + self.assertFalse(os.path.exists(os.path.join(self.webapps_dir, removed_app['name']))) 1.155 + 1.156 + # Cleaning should delete the webapps directory entirely since there was nothing there before 1.157 + webapps.clean() 1.158 + self.assertFalse(os.path.isdir(self.webapps_dir)) 1.159 + 1.160 + def test_install_webapps_preexisting(self): 1.161 + """Tests installing webapps when the webapps directory already exists""" 1.162 + manifest_json_2 = WebappCollection.read_json(self.manifest_path_2) 1.163 + 1.164 + # Synthesize a pre-existing webapps directory 1.165 + os.mkdir(self.webapps_dir) 1.166 + shutil.copyfile(self.manifest_path_2, self.webapps_json_path) 1.167 + for app in manifest_json_2: 1.168 + app_path = os.path.join(self.webapps_dir, app['name']) 1.169 + os.mkdir(app_path) 1.170 + f = open(os.path.join(app_path, 'manifest.webapp'), 'w') 1.171 + f.close() 1.172 + 1.173 + webapps = WebappCollection(self.profile, apps=self.manifest_path_1) 1.174 + self.assertTrue(os.path.exists(self.webapps_dir)) 1.175 + 1.176 + # update webapp manifests for the first time 1.177 + webapps.update_manifests() 1.178 + # A backup should be created 1.179 + self.assertTrue(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) 1.180 + 1.181 + # Both manifests should remain installed 1.182 + webapps_json = webapps.read_json(self.webapps_json_path, description='a fake description') 1.183 + self.assertEqual(len(webapps_json), 12) 1.184 + for app in webapps_json: 1.185 + self.assertIsInstance(app, Webapp) 1.186 + self.assertTrue(os.path.isfile(os.path.join(self.webapps_dir, app['name'], 'manifest.webapp'))) 1.187 + 1.188 + # Upon cleaning the backup should be restored 1.189 + webapps.clean() 1.190 + self.assertFalse(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) 1.191 + 1.192 + # The original webapps should still be installed 1.193 + webapps_json = webapps.read_json(self.webapps_json_path) 1.194 + for app in webapps_json: 1.195 + self.assertIsInstance(app, Webapp) 1.196 + self.assertTrue(os.path.isfile(os.path.join(self.webapps_dir, app['name'], 'manifest.webapp'))) 1.197 + self.assertEqual(webapps_json, manifest_json_2) 1.198 + 1.199 +if __name__ == '__main__': 1.200 + unittest.main()