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