Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | |
michael@0 | 3 | """ |
michael@0 | 4 | test installing and managing webapps in a profile |
michael@0 | 5 | """ |
michael@0 | 6 | |
michael@0 | 7 | import os |
michael@0 | 8 | import shutil |
michael@0 | 9 | import unittest |
michael@0 | 10 | from tempfile import mkdtemp |
michael@0 | 11 | |
michael@0 | 12 | from mozprofile.webapps import WebappCollection, Webapp, WebappFormatException |
michael@0 | 13 | |
michael@0 | 14 | here = os.path.dirname(os.path.abspath(__file__)) |
michael@0 | 15 | |
michael@0 | 16 | class WebappTest(unittest.TestCase): |
michael@0 | 17 | """Tests reading, installing and cleaning webapps |
michael@0 | 18 | from a profile. |
michael@0 | 19 | """ |
michael@0 | 20 | manifest_path_1 = os.path.join(here, 'files', 'webapps1.json') |
michael@0 | 21 | manifest_path_2 = os.path.join(here, 'files', 'webapps2.json') |
michael@0 | 22 | |
michael@0 | 23 | def setUp(self): |
michael@0 | 24 | self.profile = mkdtemp(prefix='test_webapp') |
michael@0 | 25 | self.webapps_dir = os.path.join(self.profile, 'webapps') |
michael@0 | 26 | self.webapps_json_path = os.path.join(self.webapps_dir, 'webapps.json') |
michael@0 | 27 | |
michael@0 | 28 | def tearDown(self): |
michael@0 | 29 | shutil.rmtree(self.profile) |
michael@0 | 30 | |
michael@0 | 31 | def test_read_json_manifest(self): |
michael@0 | 32 | """Tests WebappCollection.read_json""" |
michael@0 | 33 | # Parse a list of webapp objects and verify it worked |
michael@0 | 34 | manifest_json_1 = WebappCollection.read_json(self.manifest_path_1) |
michael@0 | 35 | self.assertEqual(len(manifest_json_1), 7) |
michael@0 | 36 | for app in manifest_json_1: |
michael@0 | 37 | self.assertIsInstance(app, Webapp) |
michael@0 | 38 | for key in Webapp.required_keys: |
michael@0 | 39 | self.assertIn(key, app) |
michael@0 | 40 | |
michael@0 | 41 | # Parse a dictionary of webapp objects and verify it worked |
michael@0 | 42 | manifest_json_2 = WebappCollection.read_json(self.manifest_path_2) |
michael@0 | 43 | self.assertEqual(len(manifest_json_2), 5) |
michael@0 | 44 | for app in manifest_json_2: |
michael@0 | 45 | self.assertIsInstance(app, Webapp) |
michael@0 | 46 | for key in Webapp.required_keys: |
michael@0 | 47 | self.assertIn(key, app) |
michael@0 | 48 | |
michael@0 | 49 | def test_invalid_webapp(self): |
michael@0 | 50 | """Tests a webapp with a missing required key""" |
michael@0 | 51 | webapps = WebappCollection(self.profile) |
michael@0 | 52 | # Missing the required key "description", exception should be raised |
michael@0 | 53 | self.assertRaises(WebappFormatException, webapps.append, { 'name': 'foo' }) |
michael@0 | 54 | |
michael@0 | 55 | def test_webapp_collection(self): |
michael@0 | 56 | """Tests the methods of the WebappCollection object""" |
michael@0 | 57 | webapp_1 = { 'name': 'test_app_1', |
michael@0 | 58 | 'description': 'a description', |
michael@0 | 59 | 'manifestURL': 'http://example.com/1/manifest.webapp', |
michael@0 | 60 | 'appStatus': 1 } |
michael@0 | 61 | |
michael@0 | 62 | webapp_2 = { 'name': 'test_app_2', |
michael@0 | 63 | 'description': 'another description', |
michael@0 | 64 | 'manifestURL': 'http://example.com/2/manifest.webapp', |
michael@0 | 65 | 'appStatus': 2 } |
michael@0 | 66 | |
michael@0 | 67 | webapp_3 = { 'name': 'test_app_2', |
michael@0 | 68 | 'description': 'a third description', |
michael@0 | 69 | 'manifestURL': 'http://example.com/3/manifest.webapp', |
michael@0 | 70 | 'appStatus': 3 } |
michael@0 | 71 | |
michael@0 | 72 | webapps = WebappCollection(self.profile) |
michael@0 | 73 | self.assertEqual(len(webapps), 0) |
michael@0 | 74 | |
michael@0 | 75 | # WebappCollection should behave like a list |
michael@0 | 76 | def invalid_index(): |
michael@0 | 77 | webapps[0] |
michael@0 | 78 | self.assertRaises(IndexError, invalid_index) |
michael@0 | 79 | |
michael@0 | 80 | # Append a webapp object |
michael@0 | 81 | webapps.append(webapp_1) |
michael@0 | 82 | self.assertTrue(len(webapps), 1) |
michael@0 | 83 | self.assertIsInstance(webapps[0], Webapp) |
michael@0 | 84 | self.assertEqual(len(webapps[0]), len(webapp_1)) |
michael@0 | 85 | self.assertEqual(len(set(webapps[0].items()) & set(webapp_1.items())), len(webapp_1)) |
michael@0 | 86 | |
michael@0 | 87 | # Remove a webapp object |
michael@0 | 88 | webapps.remove(webapp_1) |
michael@0 | 89 | self.assertEqual(len(webapps), 0) |
michael@0 | 90 | |
michael@0 | 91 | # Extend a list of webapp objects |
michael@0 | 92 | webapps.extend([webapp_1, webapp_2]) |
michael@0 | 93 | self.assertEqual(len(webapps), 2) |
michael@0 | 94 | self.assertTrue(webapp_1 in webapps) |
michael@0 | 95 | self.assertTrue(webapp_2 in webapps) |
michael@0 | 96 | self.assertNotEquals(webapps[0], webapps[1]) |
michael@0 | 97 | |
michael@0 | 98 | # Insert a webapp object |
michael@0 | 99 | webapps.insert(1, webapp_3) |
michael@0 | 100 | self.assertEqual(len(webapps), 3) |
michael@0 | 101 | self.assertEqual(webapps[1], webapps[2]) |
michael@0 | 102 | for app in webapps: |
michael@0 | 103 | self.assertIsInstance(app, Webapp) |
michael@0 | 104 | |
michael@0 | 105 | # Assigning an invalid type (must be accepted by the dict() constructor) should throw |
michael@0 | 106 | def invalid_type(): |
michael@0 | 107 | webapps[2] = 1 |
michael@0 | 108 | self.assertRaises(WebappFormatException, invalid_type) |
michael@0 | 109 | |
michael@0 | 110 | def test_install_webapps(self): |
michael@0 | 111 | """Test installing webapps into a profile that has no prior webapps""" |
michael@0 | 112 | webapps = WebappCollection(self.profile, apps=self.manifest_path_1) |
michael@0 | 113 | self.assertFalse(os.path.exists(self.webapps_dir)) |
michael@0 | 114 | |
michael@0 | 115 | # update the webapp manifests for the first time |
michael@0 | 116 | webapps.update_manifests() |
michael@0 | 117 | self.assertFalse(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) |
michael@0 | 118 | self.assertTrue(os.path.isfile(self.webapps_json_path)) |
michael@0 | 119 | |
michael@0 | 120 | webapps_json = webapps.read_json(self.webapps_json_path, description="fake description") |
michael@0 | 121 | self.assertEqual(len(webapps_json), 7) |
michael@0 | 122 | for app in webapps_json: |
michael@0 | 123 | self.assertIsInstance(app, Webapp) |
michael@0 | 124 | |
michael@0 | 125 | manifest_json_1 = webapps.read_json(self.manifest_path_1) |
michael@0 | 126 | manifest_json_2 = webapps.read_json(self.manifest_path_2) |
michael@0 | 127 | self.assertEqual(len(webapps_json), len(manifest_json_1)) |
michael@0 | 128 | for app in webapps_json: |
michael@0 | 129 | self.assertTrue(app in manifest_json_1) |
michael@0 | 130 | |
michael@0 | 131 | # Remove one of the webapps from WebappCollection after it got installed |
michael@0 | 132 | removed_app = manifest_json_1[2] |
michael@0 | 133 | webapps.remove(removed_app) |
michael@0 | 134 | # Add new webapps to the collection |
michael@0 | 135 | webapps.extend(manifest_json_2) |
michael@0 | 136 | |
michael@0 | 137 | # update the webapp manifests a second time |
michael@0 | 138 | webapps.update_manifests() |
michael@0 | 139 | self.assertFalse(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) |
michael@0 | 140 | self.assertTrue(os.path.isfile(self.webapps_json_path)) |
michael@0 | 141 | |
michael@0 | 142 | webapps_json = webapps.read_json(self.webapps_json_path, description="a description") |
michael@0 | 143 | self.assertEqual(len(webapps_json), 11) |
michael@0 | 144 | |
michael@0 | 145 | # The new apps should be added |
michael@0 | 146 | for app in webapps_json: |
michael@0 | 147 | self.assertIsInstance(app, Webapp) |
michael@0 | 148 | self.assertTrue(os.path.isfile(os.path.join(self.webapps_dir, app['name'], 'manifest.webapp'))) |
michael@0 | 149 | # The removed app should not exist in the manifest |
michael@0 | 150 | self.assertNotIn(removed_app, webapps_json) |
michael@0 | 151 | self.assertFalse(os.path.exists(os.path.join(self.webapps_dir, removed_app['name']))) |
michael@0 | 152 | |
michael@0 | 153 | # Cleaning should delete the webapps directory entirely since there was nothing there before |
michael@0 | 154 | webapps.clean() |
michael@0 | 155 | self.assertFalse(os.path.isdir(self.webapps_dir)) |
michael@0 | 156 | |
michael@0 | 157 | def test_install_webapps_preexisting(self): |
michael@0 | 158 | """Tests installing webapps when the webapps directory already exists""" |
michael@0 | 159 | manifest_json_2 = WebappCollection.read_json(self.manifest_path_2) |
michael@0 | 160 | |
michael@0 | 161 | # Synthesize a pre-existing webapps directory |
michael@0 | 162 | os.mkdir(self.webapps_dir) |
michael@0 | 163 | shutil.copyfile(self.manifest_path_2, self.webapps_json_path) |
michael@0 | 164 | for app in manifest_json_2: |
michael@0 | 165 | app_path = os.path.join(self.webapps_dir, app['name']) |
michael@0 | 166 | os.mkdir(app_path) |
michael@0 | 167 | f = open(os.path.join(app_path, 'manifest.webapp'), 'w') |
michael@0 | 168 | f.close() |
michael@0 | 169 | |
michael@0 | 170 | webapps = WebappCollection(self.profile, apps=self.manifest_path_1) |
michael@0 | 171 | self.assertTrue(os.path.exists(self.webapps_dir)) |
michael@0 | 172 | |
michael@0 | 173 | # update webapp manifests for the first time |
michael@0 | 174 | webapps.update_manifests() |
michael@0 | 175 | # A backup should be created |
michael@0 | 176 | self.assertTrue(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) |
michael@0 | 177 | |
michael@0 | 178 | # Both manifests should remain installed |
michael@0 | 179 | webapps_json = webapps.read_json(self.webapps_json_path, description='a fake description') |
michael@0 | 180 | self.assertEqual(len(webapps_json), 12) |
michael@0 | 181 | for app in webapps_json: |
michael@0 | 182 | self.assertIsInstance(app, Webapp) |
michael@0 | 183 | self.assertTrue(os.path.isfile(os.path.join(self.webapps_dir, app['name'], 'manifest.webapp'))) |
michael@0 | 184 | |
michael@0 | 185 | # Upon cleaning the backup should be restored |
michael@0 | 186 | webapps.clean() |
michael@0 | 187 | self.assertFalse(os.path.isdir(os.path.join(self.profile, webapps.backup_dir))) |
michael@0 | 188 | |
michael@0 | 189 | # The original webapps should still be installed |
michael@0 | 190 | webapps_json = webapps.read_json(self.webapps_json_path) |
michael@0 | 191 | for app in webapps_json: |
michael@0 | 192 | self.assertIsInstance(app, Webapp) |
michael@0 | 193 | self.assertTrue(os.path.isfile(os.path.join(self.webapps_dir, app['name'], 'manifest.webapp'))) |
michael@0 | 194 | self.assertEqual(webapps_json, manifest_json_2) |
michael@0 | 195 | |
michael@0 | 196 | if __name__ == '__main__': |
michael@0 | 197 | unittest.main() |