Bug 1445944 - [mozprofile] Convert mozprofile unittests to the pytest format r=davehunt

This is a much cleaner and easier to understand test format. It will also make
it easier to add tests for the upcoming ChromeProfile changes.

MozReview-Commit-ID: DizKGt0qkPF

--HG--
rename : testing/mozbase/mozprofile/tests/addonid.py => testing/mozbase/mozprofile/tests/test_addonid.py
rename : testing/mozbase/mozprofile/tests/bug758250.py => testing/mozbase/mozprofile/tests/test_bug758250.py
rename : testing/mozbase/mozprofile/tests/permissions.py => testing/mozbase/mozprofile/tests/test_permissions.py
rename : testing/mozbase/mozprofile/tests/server_locations.py => testing/mozbase/mozprofile/tests/test_server_locations.py
extra : rebase_source : 07953fd02a8592ed31e1972d646ff93bfd25d80b
This commit is contained in:
Andrew Halberstadt 2018-04-13 11:06:10 -04:00
Родитель 3d3df5c730
Коммит d0deb3a47c
15 изменённых файлов: 1210 добавлений и 1267 удалений

Просмотреть файл

@ -1,57 +0,0 @@
#!/usr/bin/env python
from __future__ import absolute_import
import mozprofile
import os
import shutil
import tempfile
import unittest
import mozunit
here = os.path.dirname(os.path.abspath(__file__))
class Bug758250(unittest.TestCase):
"""
use of --profile in mozrunner just blows away addon sources:
https://bugzilla.mozilla.org/show_bug.cgi?id=758250
"""
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
self.addon = os.path.join(here, 'addons', 'empty')
def tearDown(self):
# remove vestiges
shutil.rmtree(self.tmpdir)
def test_profile_addon_cleanup(self):
# sanity check: the empty addon should be here
self.assertTrue(os.path.exists(self.addon))
self.assertTrue(os.path.isdir(self.addon))
self.assertTrue(os.path.exists(os.path.join(self.addon, 'install.rdf')))
# because we are testing data loss, let's make sure we make a copy
shutil.rmtree(self.tmpdir)
shutil.copytree(self.addon, self.tmpdir)
self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'install.rdf')))
# make a starter profile
profile = mozprofile.FirefoxProfile()
path = profile.profile
# make a new profile based on the old
newprofile = mozprofile.FirefoxProfile(profile=path, addons=[self.tmpdir])
newprofile.cleanup()
# the source addon *should* still exist
self.assertTrue(os.path.exists(self.tmpdir))
self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'install.rdf')))
if __name__ == '__main__':
mozunit.main()

Просмотреть файл

@ -1,57 +0,0 @@
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import
import mozfile
import os
import shutil
import sqlite3
import tempfile
import unittest
import mozunit
from mozprofile.permissions import Permissions
class PermissionsTest(unittest.TestCase):
locations = """http://mochi.test:8888 primary,privileged
http://127.0.0.1:80 noxul
http://127.0.0.1:8888 privileged
"""
def setUp(self):
self.profile_dir = tempfile.mkdtemp()
self.locations_file = mozfile.NamedTemporaryFile()
self.locations_file.write(self.locations)
self.locations_file.flush()
def tearDown(self):
if self.profile_dir:
shutil.rmtree(self.profile_dir)
if self.locations_file:
self.locations_file.close()
def test_schema_version(self):
perms = Permissions(self.profile_dir, self.locations_file.name)
perms_db_filename = os.path.join(self.profile_dir, 'permissions.sqlite')
perms.write_db(self.locations_file)
stmt = 'PRAGMA user_version;'
con = sqlite3.connect(perms_db_filename)
cur = con.cursor()
cur.execute(stmt)
entries = cur.fetchall()
schema_version = entries[0][0]
self.assertEqual(schema_version, 5)
if __name__ == '__main__':
mozunit.main()

Просмотреть файл

@ -1,12 +1,11 @@
[DEFAULT]
subsuite = mozbase, os == "linux"
[addonid.py]
[server_locations.py]
[test_addonid.py]
[test_server_locations.py]
[test_preferences.py]
[permissions.py]
[bug758250.py]
[test_permissions.py]
[test_bug758250.py]
[test_nonce.py]
[bug785146.py]
[test_clone_cleanup.py]
[test_profile.py]
[test_profile_view.py]

Просмотреть файл

@ -1,205 +0,0 @@
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import
import mozfile
import os
import shutil
import sqlite3
import tempfile
import unittest
import mozunit
from mozprofile.permissions import Permissions
class PermissionsTest(unittest.TestCase):
locations = """http://mochi.test:8888 primary,privileged
http://127.0.0.1:80 noxul
http://127.0.0.1:8888 privileged
"""
profile_dir = None
locations_file = None
def setUp(self):
self.profile_dir = tempfile.mkdtemp()
self.locations_file = mozfile.NamedTemporaryFile()
self.locations_file.write(self.locations)
self.locations_file.flush()
def tearDown(self):
if self.profile_dir:
shutil.rmtree(self.profile_dir)
if self.locations_file:
self.locations_file.close()
def write_perm_db(self, version=3):
permDB = sqlite3.connect(os.path.join(self.profile_dir, "permissions.sqlite"))
cursor = permDB.cursor()
cursor.execute("PRAGMA user_version=%d;" % version)
if version == 5:
cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts (
id INTEGER PRIMARY KEY,
origin TEXT,
type TEXT,
permission INTEGER,
expireType INTEGER,
expireTime INTEGER,
modificationTime INTEGER)""")
elif version == 4:
cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts (
id INTEGER PRIMARY KEY,
host TEXT,
type TEXT,
permission INTEGER,
expireType INTEGER,
expireTime INTEGER,
modificationTime INTEGER,
appId INTEGER,
isInBrowserElement INTEGER)""")
elif version == 3:
cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts (
id INTEGER PRIMARY KEY,
host TEXT,
type TEXT,
permission INTEGER,
expireType INTEGER,
expireTime INTEGER,
appId INTEGER,
isInBrowserElement INTEGER)""")
elif version == 2:
cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts (
id INTEGER PRIMARY KEY,
host TEXT,
type TEXT,
permission INTEGER,
expireType INTEGER,
expireTime INTEGER)""")
else:
raise Exception("version must be 2, 3, 4 or 5")
permDB.commit()
cursor.close()
def test_create_permissions_db(self):
perms = Permissions(self.profile_dir, self.locations_file.name)
perms_db_filename = os.path.join(self.profile_dir, 'permissions.sqlite')
select_stmt = 'select origin, type, permission from moz_hosts'
con = sqlite3.connect(perms_db_filename)
cur = con.cursor()
cur.execute(select_stmt)
entries = cur.fetchall()
self.assertEqual(len(entries), 3)
self.assertEqual(entries[0][0], 'http://mochi.test:8888')
self.assertEqual(entries[0][1], 'allowXULXBL')
self.assertEqual(entries[0][2], 1)
self.assertEqual(entries[1][0], 'http://127.0.0.1')
self.assertEqual(entries[1][1], 'allowXULXBL')
self.assertEqual(entries[1][2], 2)
self.assertEqual(entries[2][0], 'http://127.0.0.1:8888')
self.assertEqual(entries[2][1], 'allowXULXBL')
self.assertEqual(entries[2][2], 1)
perms._locations.add_host('a.b.c', port='8081', scheme='https', options='noxul')
cur.execute(select_stmt)
entries = cur.fetchall()
self.assertEqual(len(entries), 4)
self.assertEqual(entries[3][0], 'https://a.b.c:8081')
self.assertEqual(entries[3][1], 'allowXULXBL')
self.assertEqual(entries[3][2], 2)
# when creating a DB we should default to user_version==5
cur.execute('PRAGMA user_version')
entries = cur.fetchall()
self.assertEqual(entries[0][0], 5)
perms.clean_db()
# table should be removed
cur.execute("select * from sqlite_master where type='table'")
entries = cur.fetchall()
self.assertEqual(len(entries), 0)
def test_nw_prefs(self):
perms = Permissions(self.profile_dir, self.locations_file.name)
prefs, user_prefs = perms.network_prefs(False)
self.assertEqual(len(user_prefs), 0)
self.assertEqual(len(prefs), 0)
prefs, user_prefs = perms.network_prefs(True)
self.assertEqual(len(user_prefs), 2)
self.assertEqual(user_prefs[0], ('network.proxy.type', 2))
self.assertEqual(user_prefs[1][0], 'network.proxy.autoconfig_url')
origins_decl = "var knownOrigins = (function () { return ['http://mochi.test:8888', " \
"'http://127.0.0.1:80', 'http://127.0.0.1:8888'].reduce"
self.assertTrue(origins_decl in user_prefs[1][1])
proxy_check = ("'http': 'PROXY mochi.test:8888'",
"'https': 'PROXY mochi.test:4443'",
"'ws': 'PROXY mochi.test:4443'",
"'wss': 'PROXY mochi.test:4443'")
self.assertTrue(all(c in user_prefs[1][1] for c in proxy_check))
def verify_user_version(self, version):
"""Verifies that we call INSERT statements using the correct number
of columns for existing databases.
"""
self.write_perm_db(version=version)
Permissions(self.profile_dir, self.locations_file.name)
perms_db_filename = os.path.join(self.profile_dir, 'permissions.sqlite')
select_stmt = 'select * from moz_hosts'
con = sqlite3.connect(perms_db_filename)
cur = con.cursor()
cur.execute(select_stmt)
entries = cur.fetchall()
self.assertEqual(len(entries), 3)
columns = {
1: 6,
2: 6,
3: 8,
4: 9,
5: 7,
}[version]
self.assertEqual(len(entries[0]), columns)
for x in range(4, columns):
self.assertEqual(entries[0][x], 0)
def test_existing_permissions_db_v2(self):
self.verify_user_version(2)
def test_existing_permissions_db_v3(self):
self.verify_user_version(3)
def test_existing_permissions_db_v4(self):
self.verify_user_version(4)
def test_existing_permissions_db_v5(self):
self.verify_user_version(5)
if __name__ == '__main__':
mozunit.main()

Просмотреть файл

@ -1,156 +0,0 @@
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import
import mozfile
import unittest
import mozunit
from mozprofile.permissions import ServerLocations, \
MissingPrimaryLocationError, MultiplePrimaryLocationsError, \
DuplicateLocationError, BadPortLocationError, LocationsSyntaxError
class ServerLocationsTest(unittest.TestCase):
"""test server locations"""
locations = """# This is the primary location from which tests run.
#
http://mochi.test:8888 primary,privileged
# a few test locations
http://127.0.0.1:80 privileged
http://127.0.0.1:8888 privileged
https://test:80 privileged
http://example.org:80 privileged
http://test1.example.org privileged
"""
locations_no_primary = """http://secondary.test:80 privileged
http://tertiary.test:8888 privileged
"""
locations_bad_port = """http://mochi.test:8888 primary,privileged
http://127.0.0.1:80 privileged
http://127.0.0.1:8888 privileged
http://test:badport privileged
http://example.org:80 privileged
"""
def compare_location(self, location, scheme, host, port, options):
self.assertEqual(location.scheme, scheme)
self.assertEqual(location.host, host)
self.assertEqual(location.port, port)
self.assertEqual(location.options, options)
def create_temp_file(self, contents):
f = mozfile.NamedTemporaryFile()
f.write(contents)
f.flush()
return f
def test_server_locations(self):
# write a permissions file
f = self.create_temp_file(self.locations)
# read the locations
locations = ServerLocations(f.name)
# ensure that they're what we expect
self.assertEqual(len(locations), 6)
i = iter(locations)
self.compare_location(next(i), 'http', 'mochi.test', '8888',
['primary', 'privileged'])
self.compare_location(next(i), 'http', '127.0.0.1', '80',
['privileged'])
self.compare_location(next(i), 'http', '127.0.0.1', '8888',
['privileged'])
self.compare_location(next(i), 'https', 'test', '80', ['privileged'])
self.compare_location(next(i), 'http', 'example.org', '80',
['privileged'])
self.compare_location(next(i), 'http', 'test1.example.org', '8888',
['privileged'])
locations.add_host('mozilla.org')
self.assertEqual(len(locations), 7)
self.compare_location(next(i), 'http', 'mozilla.org', '80',
['privileged'])
# test some errors
self.assertRaises(MultiplePrimaryLocationsError, locations.add_host,
'primary.test', options='primary')
# We no longer throw these DuplicateLocation Error
try:
locations.add_host('127.0.0.1')
except DuplicateLocationError:
self.assertTrue(False, "Should no longer throw DuplicateLocationError")
self.assertRaises(BadPortLocationError, locations.add_host, '127.0.0.1',
port='abc')
# test some errors in locations file
f = self.create_temp_file(self.locations_no_primary)
exc = None
try:
ServerLocations(f.name)
except LocationsSyntaxError as e:
exc = e
self.assertNotEqual(exc, None)
self.assertEqual(exc.err.__class__, MissingPrimaryLocationError)
self.assertEqual(exc.lineno, 3)
# test bad port in a locations file to ensure lineno calculated
# properly.
f = self.create_temp_file(self.locations_bad_port)
exc = None
try:
ServerLocations(f.name)
except LocationsSyntaxError as e:
exc = e
self.assertNotEqual(exc, None)
self.assertEqual(exc.err.__class__, BadPortLocationError)
self.assertEqual(exc.lineno, 4)
def test_server_locations_callback(self):
class CallbackTest(object):
last_locations = None
def callback(self, locations):
self.last_locations = locations
c = CallbackTest()
f = self.create_temp_file(self.locations)
locations = ServerLocations(f.name, c.callback)
# callback should be for all locations in file
self.assertEqual(len(c.last_locations), 6)
# validate arbitrary one
self.compare_location(c.last_locations[2], 'http', '127.0.0.1', '8888',
['privileged'])
locations.add_host('a.b.c')
# callback should be just for one location
self.assertEqual(len(c.last_locations), 1)
self.compare_location(c.last_locations[0], 'http', 'a.b.c', '80',
['privileged'])
# read a second file, which should generate a callback with both
# locations.
f = self.create_temp_file(self.locations_no_primary)
locations.read(f.name)
self.assertEqual(len(c.last_locations), 2)
if __name__ == '__main__':
mozunit.main()

Просмотреть файл

@ -3,11 +3,9 @@
from __future__ import absolute_import
import os
import tempfile
import unittest
import shutil
import mozunit
import pytest
from mozprofile import addons
@ -15,40 +13,11 @@ from mozprofile import addons
here = os.path.dirname(os.path.abspath(__file__))
class AddonIDTest(unittest.TestCase):
""" Test finding the addon id in a variety of install.rdf styles """
"""Test finding the addon id in a variety of install.rdf styles"""
def make_install_rdf(self, filecontents):
path = tempfile.mkdtemp()
f = open(os.path.join(path, "install.rdf"), "w")
f.write(filecontents)
f.close()
return path
def test_addonID(self):
testlist = self.get_test_list()
for t in testlist:
try:
p = self.make_install_rdf(t)
a = addons.AddonManager(os.path.join(p, "profile"))
addon_id = a.addon_details(p)['id']
self.assertEqual(addon_id, "winning", "We got the addon id")
finally:
shutil.rmtree(p)
def test_addonID_xpi(self):
a = addons.AddonManager("profile")
addon = a.addon_details(os.path.join(here, "addons", "empty.xpi"))
self.assertEqual(addon['id'], "test-empty@quality.mozilla.org", "We got the addon id")
def get_test_list(self):
""" This just returns a hardcoded list of install.rdf snippets for testing.
When adding snippets for testing, remember that the id we're looking for
is "winning" (no quotes). So, make sure you have that id in your snippet
if you want it to pass.
"""
tests = [
"""<?xml version="1.0"?>
ADDON_ID_TESTS = [
"""
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
@ -108,7 +77,8 @@ class AddonIDTest(unittest.TestCase):
</em:targetApplication>
</Description>
</RDF>""",
"""<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
"""
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
<em:targetApplication>
@ -128,7 +98,8 @@ class AddonIDTest(unittest.TestCase):
<em:unpack>true</em:unpack>
</Description>
</RDF>""",
"""<RDF xmlns="http://www.mozilla.org/2004/em-rdf#"
"""
<RDF xmlns="http://www.mozilla.org/2004/em-rdf#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description about="urn:mozilla:install-manifest">
<id>winning</id>
@ -138,7 +109,8 @@ class AddonIDTest(unittest.TestCase):
Windmill Testing Framework client source</description>
</rdf:Description>
</RDF>""",
"""<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
"""
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:foobar="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
<foobar:targetApplication>
@ -157,8 +129,30 @@ class AddonIDTest(unittest.TestCase):
Windmill Testing Framework client source</foobar:description>
<foobar:unpack>true</foobar:unpack>
</Description>
</RDF>"""]
return tests
</RDF>""",
]
@pytest.fixture(params=ADDON_ID_TESTS, ids=[str(i) for i in range(0, len(ADDON_ID_TESTS))])
def profile(request, tmpdir):
test = request.param
path = tmpdir.mkdtemp().strpath
with open(os.path.join(path, "install.rdf"), "w") as fh:
fh.write(test)
return path
def test_addonID(profile):
a = addons.AddonManager(os.path.join(profile, "profile"))
addon_id = a.addon_details(profile)['id']
assert addon_id == "winning"
def test_addonID_xpi():
a = addons.AddonManager("profile")
addon = a.addon_details(os.path.join(here, "addons", "empty.xpi"))
assert addon['id'] == "test-empty@quality.mozilla.org"
if __name__ == '__main__':

Просмотреть файл

@ -7,344 +7,360 @@
from __future__ import absolute_import, unicode_literals
import os
import tempfile
import unittest
import zipfile
import mozunit
import pytest
import mozfile
import mozlog.unstructured as mozlog
import mozprofile
from addon_stubs import generate_addon
here = os.path.dirname(os.path.abspath(__file__))
class TestAddonsManager(unittest.TestCase):
""" Class to test mozprofile.addons.AddonManager """
def setUp(self):
self.logger = mozlog.getLogger('mozprofile.addons')
self.logger.setLevel(mozlog.ERROR)
self.profile = mozprofile.profile.Profile()
self.am = self.profile.addons
self.profile_path = self.profile.profile
self.tmpdir = tempfile.mkdtemp()
self.addCleanup(mozfile.remove, self.tmpdir)
def test_install_multiple_same_source(self):
# Generate installer stubs for all possible types of addons
addon_xpi = generate_addon('test-addon-1@mozilla.org',
path=self.tmpdir)
addon_folder = generate_addon('test-addon-1@mozilla.org',
path=self.tmpdir,
xpi=False)
# The same folder should not be installed twice
self.am.install([addon_folder, addon_folder])
self.assertEqual(self.am.installed_addons, [addon_folder])
self.am.clean()
# The same XPI file should not be installed twice
self.am.install([addon_xpi, addon_xpi])
self.assertEqual(self.am.installed_addons, [addon_xpi])
self.am.clean()
# Even if it is the same id the add-on should be installed twice, if
# specified via XPI and folder
self.am.install([addon_folder, addon_xpi])
self.assertEqual(len(self.am.installed_addons), 2)
self.assertIn(addon_folder, self.am.installed_addons)
self.assertIn(addon_xpi, self.am.installed_addons)
self.am.clean()
def test_install_webextension_from_dir(self):
addon = os.path.join(here, 'addons', 'apply-css.xpi')
zipped = zipfile.ZipFile(addon)
try:
zipped.extractall(self.tmpdir)
finally:
zipped.close()
self.am.install(self.tmpdir)
self.assertEqual(len(self.am.installed_addons), 1)
self.assertTrue(os.path.isdir(self.am.installed_addons[0]))
def test_install_webextension(self):
addon = os.path.join(here, 'addons', 'apply-css.xpi')
self.am.install(addon)
self.assertEqual(len(self.am.installed_addons), 1)
self.assertTrue(os.path.isfile(self.am.installed_addons[0]))
self.assertEqual('apply-css.xpi', os.path.basename(self.am.installed_addons[0]))
details = self.am.addon_details(self.am.installed_addons[0])
self.assertEqual('test-webext@quality.mozilla.org', details['id'])
def test_install_webextension_sans_id(self):
addon = os.path.join(here, 'addons', 'apply-css-sans-id.xpi')
self.am.install(addon)
self.assertEqual(len(self.am.installed_addons), 1)
self.assertTrue(os.path.isfile(self.am.installed_addons[0]))
self.assertEqual('apply-css-sans-id.xpi', os.path.basename(self.am.installed_addons[0]))
details = self.am.addon_details(self.am.installed_addons[0])
self.assertIn('@temporary-addon', details['id'])
def test_install_xpi(self):
addons_to_install = []
addons_installed = []
# Generate installer stubs and install them
for ext in ['test-addon-1@mozilla.org', 'test-addon-2@mozilla.org']:
temp_addon = generate_addon(ext, path=self.tmpdir)
addons_to_install.append(self.am.addon_details(temp_addon)['id'])
self.am.install(temp_addon)
# Generate a list of addons installed in the profile
addons_installed = [str(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
self.profile.profile, 'extensions'))]
self.assertEqual(addons_to_install.sort(), addons_installed.sort())
def test_install_folder(self):
# Generate installer stubs for all possible types of addons
addons = []
addons.append(generate_addon('test-addon-1@mozilla.org',
path=self.tmpdir))
addons.append(generate_addon('test-addon-2@mozilla.org',
path=self.tmpdir,
xpi=False))
addons.append(generate_addon('test-addon-3@mozilla.org',
path=self.tmpdir,
name='addon-3'))
addons.append(generate_addon('test-addon-4@mozilla.org',
path=self.tmpdir,
name='addon-4',
xpi=False))
addons.sort()
self.am.install(self.tmpdir)
self.assertEqual(self.am.installed_addons, addons)
def test_install_unpack(self):
# Generate installer stubs for all possible types of addons
addon_xpi = generate_addon('test-addon-unpack@mozilla.org',
path=self.tmpdir)
addon_folder = generate_addon('test-addon-unpack@mozilla.org',
path=self.tmpdir,
xpi=False)
addon_no_unpack = generate_addon('test-addon-1@mozilla.org',
path=self.tmpdir)
# Test unpack flag for add-on as XPI
self.am.install(addon_xpi)
self.assertEqual(self.am.installed_addons, [addon_xpi])
self.am.clean()
# Test unpack flag for add-on as folder
self.am.install(addon_folder)
self.assertEqual(self.am.installed_addons, [addon_folder])
self.am.clean()
# Test forcing unpack an add-on
self.am.install(addon_no_unpack, unpack=True)
self.assertEqual(self.am.installed_addons, [addon_no_unpack])
self.am.clean()
def test_install_after_reset(self):
# Installing the same add-on after a reset should not cause a failure
addon = generate_addon('test-addon-1@mozilla.org',
path=self.tmpdir, xpi=False)
# We cannot use self.am because profile.reset() creates a new instance
self.profile.addons.install(addon)
self.profile.reset()
self.profile.addons.install(addon)
self.assertEqual(self.profile.addons.installed_addons, [addon])
def test_install_backup(self):
staged_path = os.path.join(self.profile_path, 'extensions')
# Generate installer stubs for all possible types of addons
addon_xpi = generate_addon('test-addon-1@mozilla.org',
path=self.tmpdir)
addon_folder = generate_addon('test-addon-1@mozilla.org',
path=self.tmpdir,
xpi=False)
addon_name = generate_addon('test-addon-1@mozilla.org',
path=self.tmpdir,
name='test-addon-1-dupe@mozilla.org')
# Test backup of xpi files
self.am.install(addon_xpi)
self.assertIsNone(self.am.backup_dir)
self.am.install(addon_xpi)
self.assertIsNotNone(self.am.backup_dir)
self.assertEqual(os.listdir(self.am.backup_dir),
['test-addon-1@mozilla.org.xpi'])
self.am.clean()
self.assertEqual(os.listdir(staged_path),
['test-addon-1@mozilla.org.xpi'])
self.am.clean()
# Test backup of folders
self.am.install(addon_folder)
self.assertIsNone(self.am.backup_dir)
self.am.install(addon_folder)
self.assertIsNotNone(self.am.backup_dir)
self.assertEqual(os.listdir(self.am.backup_dir),
['test-addon-1@mozilla.org'])
self.am.clean()
self.assertEqual(os.listdir(staged_path),
['test-addon-1@mozilla.org'])
self.am.clean()
# Test backup of xpi files with another file name
self.am.install(addon_name)
self.assertIsNone(self.am.backup_dir)
self.am.install(addon_xpi)
self.assertIsNotNone(self.am.backup_dir)
self.assertEqual(os.listdir(self.am.backup_dir),
['test-addon-1@mozilla.org.xpi'])
self.am.clean()
self.assertEqual(os.listdir(staged_path),
['test-addon-1@mozilla.org.xpi'])
self.am.clean()
def test_install_invalid_addons(self):
# Generate installer stubs for all possible types of addons
addons = []
addons.append(generate_addon('test-addon-invalid-no-manifest@mozilla.org',
path=self.tmpdir,
xpi=False))
addons.append(generate_addon('test-addon-invalid-no-id@mozilla.org',
path=self.tmpdir))
self.am.install(self.tmpdir)
self.assertEqual(self.am.installed_addons, [])
@unittest.skip("Feature not implemented as part of AddonManger")
def test_install_error(self):
""" Check install raises an error with an invalid addon"""
temp_addon = generate_addon('test-addon-invalid-version@mozilla.org')
# This should raise an error here
self.am.install(temp_addon)
def test_addon_details(self):
# Generate installer stubs for a valid and invalid add-on manifest
valid_addon = generate_addon('test-addon-1@mozilla.org',
path=self.tmpdir)
invalid_addon = generate_addon('test-addon-invalid-not-wellformed@mozilla.org',
path=self.tmpdir)
# Check valid add-on
details = self.am.addon_details(valid_addon)
self.assertEqual(details['id'], 'test-addon-1@mozilla.org')
self.assertEqual(details['name'], 'Test Add-on 1')
self.assertEqual(details['unpack'], False)
self.assertEqual(details['version'], '0.1')
# Check invalid add-on
self.assertRaises(mozprofile.addons.AddonFormatError,
self.am.addon_details, invalid_addon)
# Check invalid path
self.assertRaises(IOError,
self.am.addon_details, '')
# Check invalid add-on format
addon_path = os.path.join(os.path.join(here, 'files'), 'not_an_addon.txt')
self.assertRaises(mozprofile.addons.AddonFormatError,
self.am.addon_details, addon_path)
@unittest.skip("Bug 900154")
def test_clean_addons(self):
addon_one = generate_addon('test-addon-1@mozilla.org')
addon_two = generate_addon('test-addon-2@mozilla.org')
self.am.install(addon_one)
installed_addons = [str(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
self.profile.profile, 'extensions'))]
# Create a new profile based on an existing profile
# Install an extra addon in the new profile
# Cleanup addons
duplicate_profile = mozprofile.profile.Profile(profile=self.profile.profile,
addons=addon_two)
duplicate_profile.addons.clean()
addons_after_cleanup = [str(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
duplicate_profile.profile, 'extensions'))]
# New addons installed should be removed by clean_addons()
self.assertEqual(installed_addons, addons_after_cleanup)
def test_noclean(self):
"""test `restore=True/False` functionality"""
profile = tempfile.mkdtemp()
tmpdir = tempfile.mkdtemp()
try:
# empty initially
self.assertFalse(bool(os.listdir(profile)))
# make an addon
addons = [
generate_addon('test-addon-1@mozilla.org', path=tmpdir),
os.path.join(here, 'addons', 'empty.xpi'),
]
# install it with a restore=True AddonManager
am = mozprofile.addons.AddonManager(profile, restore=True)
for addon in addons:
am.install(addon)
# now its there
self.assertEqual(os.listdir(profile), ['extensions'])
staging_folder = os.path.join(profile, 'extensions')
self.assertTrue(os.path.exists(staging_folder))
self.assertEqual(len(os.listdir(staging_folder)), 2)
del am
self.assertEqual(os.listdir(profile), ['extensions'])
self.assertTrue(os.path.exists(staging_folder))
self.assertEqual(os.listdir(staging_folder), [])
finally:
mozfile.rmtree(tmpdir)
mozfile.rmtree(profile)
def test_remove_addon(self):
addons = []
addons.append(generate_addon('test-addon-1@mozilla.org',
path=self.tmpdir))
addons.append(generate_addon('test-addon-2@mozilla.org',
path=self.tmpdir))
self.am.install(self.tmpdir)
extensions_path = os.path.join(self.profile_path, 'extensions')
staging_path = os.path.join(extensions_path)
for addon in self.am._addons:
self.am.remove_addon(addon)
self.assertEqual(os.listdir(staging_path), [])
self.assertEqual(os.listdir(extensions_path), [])
@pytest.fixture
def profile():
return mozprofile.Profile()
@pytest.fixture
def am(profile):
return profile.addons
def test_install_multiple_same_source(tmpdir, am):
path = tmpdir.strpath
# Generate installer stubs for all possible types of addons
addon_xpi = generate_addon('test-addon-1@mozilla.org',
path=path)
addon_folder = generate_addon('test-addon-1@mozilla.org',
path=path,
xpi=False)
# The same folder should not be installed twice
am.install([addon_folder, addon_folder])
assert am.installed_addons == [addon_folder]
am.clean()
# The same XPI file should not be installed twice
am.install([addon_xpi, addon_xpi])
assert am.installed_addons == [addon_xpi]
am.clean()
# Even if it is the same id the add-on should be installed twice, if
# specified via XPI and folder
am.install([addon_folder, addon_xpi])
assert len(am.installed_addons) == 2
assert addon_folder in am.installed_addons
assert addon_xpi in am.installed_addons
am.clean()
def test_install_webextension_from_dir(tmpdir, am):
tmpdir = tmpdir.strpath
addon = os.path.join(here, 'addons', 'apply-css.xpi')
zipped = zipfile.ZipFile(addon)
try:
zipped.extractall(tmpdir)
finally:
zipped.close()
am.install(tmpdir)
assert len(am.installed_addons) == 1
assert os.path.isdir(am.installed_addons[0])
def test_install_webextension(am):
addon = os.path.join(here, 'addons', 'apply-css.xpi')
am.install(addon)
assert len(am.installed_addons) == 1
assert os.path.isfile(am.installed_addons[0])
assert 'apply-css.xpi' == os.path.basename(am.installed_addons[0])
details = am.addon_details(am.installed_addons[0])
assert 'test-webext@quality.mozilla.org' == details['id']
def test_install_webextension_sans_id(am):
addon = os.path.join(here, 'addons', 'apply-css-sans-id.xpi')
am.install(addon)
assert len(am.installed_addons) == 1
assert os.path.isfile(am.installed_addons[0])
assert 'apply-css-sans-id.xpi' == os.path.basename(am.installed_addons[0])
details = am.addon_details(am.installed_addons[0])
assert '@temporary-addon' in details['id']
def test_install_xpi(tmpdir, am):
tmpdir = tmpdir.strpath
addons_to_install = []
addons_installed = []
# Generate installer stubs and install them
for ext in ['test-addon-1@mozilla.org', 'test-addon-2@mozilla.org']:
temp_addon = generate_addon(ext, path=tmpdir)
addons_to_install.append(am.addon_details(temp_addon)['id'])
am.install(temp_addon)
# Generate a list of addons installed in the profile
addons_installed = [str(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
am.profile, 'extensions'))]
assert addons_to_install.sort() == addons_installed.sort()
def test_install_folder(tmpdir, am):
tmpdir = tmpdir.strpath
# Generate installer stubs for all possible types of addons
addons = []
addons.append(generate_addon('test-addon-1@mozilla.org',
path=tmpdir))
addons.append(generate_addon('test-addon-2@mozilla.org',
path=tmpdir,
xpi=False))
addons.append(generate_addon('test-addon-3@mozilla.org',
path=tmpdir,
name='addon-3'))
addons.append(generate_addon('test-addon-4@mozilla.org',
path=tmpdir,
name='addon-4',
xpi=False))
addons.sort()
am.install(tmpdir)
assert am.installed_addons == addons
def test_install_unpack(tmpdir, am):
tmpdir = tmpdir.strpath
# Generate installer stubs for all possible types of addons
addon_xpi = generate_addon('test-addon-unpack@mozilla.org',
path=tmpdir)
addon_folder = generate_addon('test-addon-unpack@mozilla.org',
path=tmpdir,
xpi=False)
addon_no_unpack = generate_addon('test-addon-1@mozilla.org',
path=tmpdir)
# Test unpack flag for add-on as XPI
am.install(addon_xpi)
assert am.installed_addons == [addon_xpi]
am.clean()
# Test unpack flag for add-on as folder
am.install(addon_folder)
assert am.installed_addons == [addon_folder]
am.clean()
# Test forcing unpack an add-on
am.install(addon_no_unpack, unpack=True)
assert am.installed_addons == [addon_no_unpack]
am.clean()
def test_install_after_reset(tmpdir, profile):
tmpdir = tmpdir.strpath
am = profile.addons
# Installing the same add-on after a reset should not cause a failure
addon = generate_addon('test-addon-1@mozilla.org',
path=tmpdir, xpi=False)
# We cannot use am because profile.reset() creates a new instance
am.install(addon)
profile.reset()
am.install(addon)
assert am.installed_addons == [addon]
def test_install_backup(tmpdir, am):
tmpdir = tmpdir.strpath
staged_path = os.path.join(am.profile, 'extensions')
# Generate installer stubs for all possible types of addons
addon_xpi = generate_addon('test-addon-1@mozilla.org',
path=tmpdir)
addon_folder = generate_addon('test-addon-1@mozilla.org',
path=tmpdir,
xpi=False)
addon_name = generate_addon('test-addon-1@mozilla.org',
path=tmpdir,
name='test-addon-1-dupe@mozilla.org')
# Test backup of xpi files
am.install(addon_xpi)
assert am.backup_dir is None
am.install(addon_xpi)
assert am.backup_dir is not None
assert os.listdir(am.backup_dir) == ['test-addon-1@mozilla.org.xpi']
am.clean()
assert os.listdir(staged_path) == ['test-addon-1@mozilla.org.xpi']
am.clean()
# Test backup of folders
am.install(addon_folder)
assert am.backup_dir is None
am.install(addon_folder)
assert am.backup_dir is not None
assert os.listdir(am.backup_dir) == ['test-addon-1@mozilla.org']
am.clean()
assert os.listdir(staged_path) == ['test-addon-1@mozilla.org']
am.clean()
# Test backup of xpi files with another file name
am.install(addon_name)
assert am.backup_dir is None
am.install(addon_xpi)
assert am.backup_dir is not None
assert os.listdir(am.backup_dir) == ['test-addon-1@mozilla.org.xpi']
am.clean()
assert os.listdir(staged_path) == ['test-addon-1@mozilla.org.xpi']
am.clean()
def test_install_invalid_addons(tmpdir, am):
tmpdir = tmpdir.strpath
# Generate installer stubs for all possible types of addons
addons = []
addons.append(generate_addon('test-addon-invalid-no-manifest@mozilla.org',
path=tmpdir,
xpi=False))
addons.append(generate_addon('test-addon-invalid-no-id@mozilla.org',
path=tmpdir))
am.install(tmpdir)
assert am.installed_addons == []
@pytest.mark.xfail(reason="feature not implemented as part of AddonManger")
def test_install_error(am):
""" Check install raises an error with an invalid addon"""
temp_addon = generate_addon('test-addon-invalid-version@mozilla.org')
# This should raise an error here
with pytest.raises(Exception):
am.install(temp_addon)
def test_addon_details(tmpdir, am):
tmpdir = tmpdir.strpath
# Generate installer stubs for a valid and invalid add-on manifest
valid_addon = generate_addon('test-addon-1@mozilla.org',
path=tmpdir)
invalid_addon = generate_addon('test-addon-invalid-not-wellformed@mozilla.org',
path=tmpdir)
# Check valid add-on
details = am.addon_details(valid_addon)
assert details['id'] == 'test-addon-1@mozilla.org'
assert details['name'] == 'Test Add-on 1'
assert not details['unpack']
assert details['version'] == '0.1'
# Check invalid add-on
with pytest.raises(mozprofile.addons.AddonFormatError):
am.addon_details(invalid_addon)
# Check invalid path
with pytest.raises(IOError):
am.addon_details('')
# Check invalid add-on format
addon_path = os.path.join(os.path.join(here, 'files'), 'not_an_addon.txt')
with pytest.raises(mozprofile.addons.AddonFormatError):
am.addon_details(addon_path)
def test_clean_addons(am):
addon_one = generate_addon('test-addon-1@mozilla.org')
addon_two = generate_addon('test-addon-2@mozilla.org')
am.install(addon_one)
installed_addons = [str(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
am.profile, 'extensions'))]
# Create a new profile based on an existing profile
# Install an extra addon in the new profile
# Cleanup addons
duplicate_profile = mozprofile.profile.Profile(profile=am.profile,
addons=addon_two)
duplicate_profile.addons.clean()
addons_after_cleanup = [str(x[:-len('.xpi')]) for x in os.listdir(os.path.join(
duplicate_profile.profile, 'extensions'))]
# New addons installed should be removed by clean_addons()
assert installed_addons == addons_after_cleanup
def test_noclean(tmpdir):
"""test `restore=True/False` functionality"""
profile = tmpdir.mkdtemp().strpath
tmpdir = tmpdir.mkdtemp().strpath
# empty initially
assert not bool(os.listdir(profile))
# make an addon
addons = [
generate_addon('test-addon-1@mozilla.org', path=tmpdir),
os.path.join(here, 'addons', 'empty.xpi'),
]
# install it with a restore=True AddonManager
am = mozprofile.addons.AddonManager(profile, restore=True)
for addon in addons:
am.install(addon)
# now its there
assert os.listdir(profile) == ['extensions']
staging_folder = os.path.join(profile, 'extensions')
assert os.path.exists(staging_folder)
assert len(os.listdir(staging_folder)) == 2
del am
assert os.listdir(profile) == ['extensions']
assert os.path.exists(staging_folder)
assert os.listdir(staging_folder) == []
def test_remove_addon(tmpdir, am):
tmpdir = tmpdir.strpath
addons = []
addons.append(generate_addon('test-addon-1@mozilla.org',
path=tmpdir))
addons.append(generate_addon('test-addon-2@mozilla.org',
path=tmpdir))
am.install(tmpdir)
extensions_path = os.path.join(am.profile, 'extensions')
staging_path = os.path.join(extensions_path)
for addon in am._addons:
am.remove_addon(addon)
assert os.listdir(staging_path) == []
assert os.listdir(extensions_path) == []
if __name__ == '__main__':

Просмотреть файл

@ -0,0 +1,47 @@
#!/usr/bin/env python
from __future__ import absolute_import
import mozprofile
import os
import shutil
import mozunit
here = os.path.dirname(os.path.abspath(__file__))
"""
use of --profile in mozrunner just blows away addon sources:
https://bugzilla.mozilla.org/show_bug.cgi?id=758250
"""
def test_profile_addon_cleanup(tmpdir):
tmpdir = tmpdir.mkdtemp().strpath
addon = os.path.join(here, 'addons', 'empty')
# sanity check: the empty addon should be here
assert os.path.exists(addon)
assert os.path.isdir(addon)
assert os.path.exists(os.path.join(addon, 'install.rdf'))
# because we are testing data loss, let's make sure we make a copy
shutil.rmtree(tmpdir)
shutil.copytree(addon, tmpdir)
assert os.path.exists(os.path.join(tmpdir, 'install.rdf'))
# make a starter profile
profile = mozprofile.FirefoxProfile()
path = profile.profile
# make a new profile based on the old
newprofile = mozprofile.FirefoxProfile(profile=path, addons=[tmpdir])
newprofile.cleanup()
# the source addon *should* still exist
assert os.path.exists(tmpdir)
assert os.path.exists(os.path.join(tmpdir, 'install.rdf'))
if __name__ == '__main__':
mozunit.main()

Просмотреть файл

@ -6,75 +6,78 @@
from __future__ import absolute_import
import os
import tempfile
import unittest
import mozfile
import mozunit
import pytest
from mozprofile.profile import Profile
"""
test cleanup logic for the clone functionality
see https://bugzilla.mozilla.org/show_bug.cgi?id=642843
"""
class CloneCleanupTest(unittest.TestCase):
"""
test cleanup logic for the clone functionality
see https://bugzilla.mozilla.org/show_bug.cgi?id=642843
"""
def setUp(self):
# make a profile with one preference
path = tempfile.mktemp()
self.addCleanup(mozfile.remove, path)
self.profile = Profile(path,
preferences={'foo': 'bar'},
restore=False)
user_js = os.path.join(self.profile.profile, 'user.js')
self.assertTrue(os.path.exists(user_js))
@pytest.fixture
def profile(tmpdir):
# make a profile with one preference
path = tmpdir.mkdtemp().strpath
profile = Profile(path,
preferences={'foo': 'bar'},
restore=False)
user_js = os.path.join(profile.profile, 'user.js')
assert os.path.exists(user_js)
return profile
def test_restore_true(self):
counter = [0]
def _feedback(dir, content):
# Called by shutil.copytree on each visited directory.
# Used here to display info.
#
# Returns the items that should be ignored by
# shutil.copytree when copying the tree, so always returns
# an empty list.
counter[0] += 1
return []
def test_restore_true(profile):
counter = [0]
# make a clone of this profile with restore=True
clone = Profile.clone(self.profile.profile, restore=True,
ignore=_feedback)
self.addCleanup(mozfile.remove, clone.profile)
def _feedback(dir, content):
# Called by shutil.copytree on each visited directory.
# Used here to display info.
#
# Returns the items that should be ignored by
# shutil.copytree when copying the tree, so always returns
# an empty list.
counter[0] += 1
return []
# make a clone of this profile with restore=True
clone = Profile.clone(profile.profile, restore=True,
ignore=_feedback)
try:
clone.cleanup()
# clone should be deleted
self.assertFalse(os.path.exists(clone.profile))
self.assertTrue(counter[0] > 0)
assert not os.path.exists(clone.profile)
assert counter[0] > 0
finally:
mozfile.remove(clone.profile)
def test_restore_false(self):
# make a clone of this profile with restore=False
clone = Profile.clone(self.profile.profile, restore=False)
self.addCleanup(mozfile.remove, clone.profile)
def test_restore_false(profile):
# make a clone of this profile with restore=False
clone = Profile.clone(profile.profile, restore=False)
try:
clone.cleanup()
# clone should still be around on the filesystem
self.assertTrue(os.path.exists(clone.profile))
assert os.path.exists(clone.profile)
finally:
mozfile.remove(clone.profile)
def test_cleanup_on_garbage_collected(self):
clone = Profile.clone(self.profile.profile)
self.addCleanup(mozfile.remove, clone.profile)
profile_dir = clone.profile
self.assertTrue(os.path.exists(profile_dir))
del clone
# clone should be deleted
self.assertFalse(os.path.exists(profile_dir))
def test_cleanup_on_garbage_collected(profile):
clone = Profile.clone(profile.profile)
profile_dir = clone.profile
assert os.path.exists(profile_dir)
del clone
# clone should be deleted
assert not os.path.exists(profile_dir)
if __name__ == '__main__':

Просмотреть файл

@ -8,9 +8,6 @@ see https://bugzilla.mozilla.org/show_bug.cgi?id=722804
from __future__ import absolute_import
import os
import tempfile
import unittest
import mozfile
import mozunit
@ -18,37 +15,33 @@ from mozprofile.prefs import Preferences
from mozprofile.profile import Profile
class PreferencesNonceTest(unittest.TestCase):
def test_nonce(tmpdir):
# make a profile with one preference
path = tmpdir.strpath
profile = Profile(path,
preferences={'foo': 'bar'},
restore=False)
user_js = os.path.join(profile.profile, 'user.js')
assert os.path.exists(user_js)
def test_nonce(self):
# ensure the preference is correct
prefs = Preferences.read_prefs(user_js)
assert dict(prefs) == {'foo': 'bar'}
# make a profile with one preference
path = tempfile.mktemp()
self.addCleanup(mozfile.remove, path)
profile = Profile(path,
preferences={'foo': 'bar'},
restore=False)
user_js = os.path.join(profile.profile, 'user.js')
self.assertTrue(os.path.exists(user_js))
del profile
# ensure the preference is correct
prefs = Preferences.read_prefs(user_js)
self.assertEqual(dict(prefs), {'foo': 'bar'})
# augment the profile with a second preference
profile = Profile(path,
preferences={'fleem': 'baz'},
restore=True)
prefs = Preferences.read_prefs(user_js)
assert dict(prefs) == {'foo': 'bar', 'fleem': 'baz'}
del profile
# augment the profile with a second preference
profile = Profile(path,
preferences={'fleem': 'baz'},
restore=True)
prefs = Preferences.read_prefs(user_js)
self.assertEqual(dict(prefs), {'foo': 'bar', 'fleem': 'baz'})
# cleanup the profile;
# this should remove the new preferences but not the old
profile.cleanup()
prefs = Preferences.read_prefs(user_js)
self.assertEqual(dict(prefs), {'foo': 'bar'})
# cleanup the profile;
# this should remove the new preferences but not the old
profile.cleanup()
prefs = Preferences.read_prefs(user_js)
assert dict(prefs) == {'foo': 'bar'}
if __name__ == '__main__':

Просмотреть файл

@ -0,0 +1,212 @@
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import
import os
import sqlite3
import mozunit
import pytest
from mozprofile.permissions import Permissions
LOCATIONS = """http://mochi.test:8888 primary,privileged
http://127.0.0.1:80 noxul
http://127.0.0.1:8888 privileged
"""
@pytest.fixture
def locations_file(tmpdir):
locations_file = tmpdir.join('locations.txt')
locations_file.write(LOCATIONS)
return locations_file.strpath
@pytest.fixture
def perms(tmpdir, locations_file):
return Permissions(tmpdir.mkdir('profile').strpath, locations_file)
def test_create_permissions_db(perms):
profile_dir = perms._profileDir
perms_db_filename = os.path.join(profile_dir, 'permissions.sqlite')
select_stmt = 'select origin, type, permission from moz_hosts'
con = sqlite3.connect(perms_db_filename)
cur = con.cursor()
cur.execute(select_stmt)
entries = cur.fetchall()
assert len(entries) == 3
assert entries[0][0] == 'http://mochi.test:8888'
assert entries[0][1] == 'allowXULXBL'
assert entries[0][2] == 1
assert entries[1][0] == 'http://127.0.0.1'
assert entries[1][1] == 'allowXULXBL'
assert entries[1][2] == 2
assert entries[2][0] == 'http://127.0.0.1:8888'
assert entries[2][1] == 'allowXULXBL'
assert entries[2][2] == 1
perms._locations.add_host('a.b.c', port='8081', scheme='https', options='noxul')
cur.execute(select_stmt)
entries = cur.fetchall()
assert len(entries) == 4
assert entries[3][0] == 'https://a.b.c:8081'
assert entries[3][1] == 'allowXULXBL'
assert entries[3][2] == 2
# when creating a DB we should default to user_version==5
cur.execute('PRAGMA user_version')
entries = cur.fetchall()
assert entries[0][0] == 5
perms.clean_db()
# table should be removed
cur.execute("select * from sqlite_master where type='table'")
entries = cur.fetchall()
assert len(entries) == 0
def test_nw_prefs(perms):
prefs, user_prefs = perms.network_prefs(False)
assert len(user_prefs) == 0
assert len(prefs) == 0
prefs, user_prefs = perms.network_prefs(True)
assert len(user_prefs) == 2
assert user_prefs[0] == ('network.proxy.type', 2)
assert user_prefs[1][0] == 'network.proxy.autoconfig_url'
origins_decl = "var knownOrigins = (function () { return ['http://mochi.test:8888', " \
"'http://127.0.0.1:80', 'http://127.0.0.1:8888'].reduce"
assert origins_decl in user_prefs[1][1]
proxy_check = ("'http': 'PROXY mochi.test:8888'",
"'https': 'PROXY mochi.test:4443'",
"'ws': 'PROXY mochi.test:4443'",
"'wss': 'PROXY mochi.test:4443'")
assert all(c in user_prefs[1][1] for c in proxy_check)
@pytest.fixture
def perms_db_filename(tmpdir):
return tmpdir.join('permissions.sqlite').strpath
@pytest.fixture
def permDB(perms_db_filename):
permDB = sqlite3.connect(perms_db_filename)
yield permDB
permDB.cursor().close()
@pytest.fixture(params=range(2, 6))
def version(request, perms_db_filename, permDB, locations_file):
version = request.param
cursor = permDB.cursor()
cursor.execute("PRAGMA user_version=%d;" % version)
if version == 5:
cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts (
id INTEGER PRIMARY KEY,
origin TEXT,
type TEXT,
permission INTEGER,
expireType INTEGER,
expireTime INTEGER,
modificationTime INTEGER)""")
elif version == 4:
cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts (
id INTEGER PRIMARY KEY,
host TEXT,
type TEXT,
permission INTEGER,
expireType INTEGER,
expireTime INTEGER,
modificationTime INTEGER,
appId INTEGER,
isInBrowserElement INTEGER)""")
elif version == 3:
cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts (
id INTEGER PRIMARY KEY,
host TEXT,
type TEXT,
permission INTEGER,
expireType INTEGER,
expireTime INTEGER,
appId INTEGER,
isInBrowserElement INTEGER)""")
elif version == 2:
cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts (
id INTEGER PRIMARY KEY,
host TEXT,
type TEXT,
permission INTEGER,
expireType INTEGER,
expireTime INTEGER)""")
else:
raise Exception("version must be 2, 3, 4 or 5")
permDB.commit()
# Create a permissions object to read the db
Permissions(os.path.dirname(perms_db_filename), locations_file)
return version
def test_verify_user_version(version, permDB):
"""Verifies that we call INSERT statements using the correct number
of columns for existing databases.
"""
select_stmt = 'select * from moz_hosts'
cur = permDB.cursor()
cur.execute(select_stmt)
entries = cur.fetchall()
assert len(entries) == 3
columns = {
1: 6,
2: 6,
3: 8,
4: 9,
5: 7,
}[version]
assert len(entries[0]) == columns
for x in range(4, columns):
assert entries[0][x] == 0
def test_schema_version(perms, locations_file):
profile_dir = perms._profileDir
perms_db_filename = os.path.join(profile_dir, 'permissions.sqlite')
perms.write_db(open(locations_file, 'w+b'))
stmt = 'PRAGMA user_version;'
con = sqlite3.connect(perms_db_filename)
cur = con.cursor()
cur.execute(stmt)
entries = cur.fetchall()
schema_version = entries[0][0]
assert schema_version == 5
if __name__ == '__main__':
mozunit.main()

Просмотреть файл

@ -11,9 +11,9 @@ import mozhttpd
import os
import shutil
import tempfile
import unittest
import mozunit
import pytest
from mozprofile.cli import MozProfileCLI
from mozprofile.prefs import Preferences, PreferencesReadError
@ -22,21 +22,20 @@ from mozprofile.profile import Profile
here = os.path.dirname(os.path.abspath(__file__))
class PreferencesTest(unittest.TestCase):
"""test mozprofile preference handling"""
# preferences from files/prefs_with_comments.js
_prefs_with_comments = {'browser.startup.homepage': 'http://planet.mozilla.org',
'zoom.minPercent': 30,
'zoom.maxPercent': 300,
'webgl.verbose': 'false'}
# preferences from files/prefs_with_comments.js
_prefs_with_comments = {'browser.startup.homepage': 'http://planet.mozilla.org',
'zoom.minPercent': 30,
'zoom.maxPercent': 300,
'webgl.verbose': 'false'}
def run_command(self, *args):
"""
invokes mozprofile command line via the CLI factory
- args : command line arguments (equivalent of sys.argv[1:])
"""
@pytest.fixture
def run_command():
"""
invokes mozprofile command line via the CLI factory
- args : command line arguments (equivalent of sys.argv[1:])
"""
def inner(*args):
# instantiate the factory
cli = MozProfileCLI(list(args))
@ -45,350 +44,366 @@ class PreferencesTest(unittest.TestCase):
# return path to profile
return profile.profile
return inner
def compare_generated(self, _prefs, commandline):
"""
writes out to a new profile with mozprofile command line
reads the generated preferences with prefs.py
compares the results
cleans up
"""
profile = self.run_command(*commandline)
@pytest.fixture
def compare_generated(run_command):
"""
writes out to a new profile with mozprofile command line
reads the generated preferences with prefs.py
compares the results
cleans up
"""
def inner(prefs, commandline):
profile = run_command(*commandline)
prefs_file = os.path.join(profile, 'user.js')
self.assertTrue(os.path.exists(prefs_file))
assert os.path.exists(prefs_file)
read = Preferences.read_prefs(prefs_file)
if isinstance(_prefs, dict):
if isinstance(prefs, dict):
read = dict(read)
self.assertEqual(_prefs, read)
assert prefs == read
shutil.rmtree(profile)
return inner
def test_basic_prefs(self):
"""test setting a pref from the command line entry point"""
_prefs = {"browser.startup.homepage": "http://planet.mozilla.org/"}
commandline = []
_prefs = _prefs.items()
for pref, value in _prefs:
commandline += ["--pref", "%s:%s" % (pref, value)]
self.compare_generated(_prefs, commandline)
def test_basic_prefs(compare_generated):
"""test setting a pref from the command line entry point"""
def test_ordered_prefs(self):
"""ensure the prefs stay in the right order"""
_prefs = [("browser.startup.homepage", "http://planet.mozilla.org/"),
("zoom.minPercent", 30),
("zoom.maxPercent", 300),
("webgl.verbose", 'false')]
commandline = []
for pref, value in _prefs:
commandline += ["--pref", "%s:%s" % (pref, value)]
_prefs = [(i, Preferences.cast(j)) for i, j in _prefs]
self.compare_generated(_prefs, commandline)
_prefs = {"browser.startup.homepage": "http://planet.mozilla.org/"}
commandline = []
_prefs = _prefs.items()
for pref, value in _prefs:
commandline += ["--pref", "%s:%s" % (pref, value)]
compare_generated(_prefs, commandline)
def test_ini(self):
# write the .ini file
_ini = """[DEFAULT]
def test_ordered_prefs(compare_generated):
"""ensure the prefs stay in the right order"""
_prefs = [("browser.startup.homepage", "http://planet.mozilla.org/"),
("zoom.minPercent", 30),
("zoom.maxPercent", 300),
("webgl.verbose", 'false')]
commandline = []
for pref, value in _prefs:
commandline += ["--pref", "%s:%s" % (pref, value)]
_prefs = [(i, Preferences.cast(j)) for i, j in _prefs]
compare_generated(_prefs, commandline)
def test_ini(compare_generated):
# write the .ini file
_ini = """[DEFAULT]
browser.startup.homepage = http://planet.mozilla.org/
[foo]
browser.startup.homepage = http://github.com/
"""
try:
fd, name = tempfile.mkstemp(suffix='.ini')
os.write(fd, _ini)
os.close(fd)
commandline = ["--preferences", name]
try:
fd, name = tempfile.mkstemp(suffix='.ini')
os.write(fd, _ini)
os.close(fd)
commandline = ["--preferences", name]
# test the [DEFAULT] section
_prefs = {'browser.startup.homepage': 'http://planet.mozilla.org/'}
self.compare_generated(_prefs, commandline)
# test the [DEFAULT] section
_prefs = {'browser.startup.homepage': 'http://planet.mozilla.org/'}
compare_generated(_prefs, commandline)
# test a specific section
_prefs = {'browser.startup.homepage': 'http://github.com/'}
commandline[-1] = commandline[-1] + ':foo'
self.compare_generated(_prefs, commandline)
# test a specific section
_prefs = {'browser.startup.homepage': 'http://github.com/'}
commandline[-1] = commandline[-1] + ':foo'
compare_generated(_prefs, commandline)
finally:
# cleanup
os.remove(name)
finally:
# cleanup
os.remove(name)
def test_ini_keep_case(self):
"""
Read a preferences config file with a preference in camel-case style.
Check that the read preference name has not been lower-cased
"""
# write the .ini file
_ini = """[DEFAULT]
def test_ini_keep_case(compare_generated):
"""
Read a preferences config file with a preference in camel-case style.
Check that the read preference name has not been lower-cased
"""
# write the .ini file
_ini = """[DEFAULT]
general.warnOnAboutConfig = False
"""
try:
fd, name = tempfile.mkstemp(suffix='.ini')
os.write(fd, _ini)
os.close(fd)
commandline = ["--preferences", name]
try:
fd, name = tempfile.mkstemp(suffix='.ini')
os.write(fd, _ini)
os.close(fd)
commandline = ["--preferences", name]
# test the [DEFAULT] section
_prefs = {'general.warnOnAboutConfig': 'False'}
self.compare_generated(_prefs, commandline)
# test the [DEFAULT] section
_prefs = {'general.warnOnAboutConfig': 'False'}
compare_generated(_prefs, commandline)
finally:
# cleanup
os.remove(name)
finally:
# cleanup
os.remove(name)
def test_reset_should_remove_added_prefs(self):
"""Check that when we call reset the items we expect are updated"""
profile = Profile()
prefs_file = os.path.join(profile.profile, 'user.js')
# we shouldn't have any initial preferences
initial_prefs = Preferences.read_prefs(prefs_file)
self.assertFalse(initial_prefs)
initial_prefs = open(prefs_file).read().strip()
self.assertFalse(initial_prefs)
def test_reset_should_remove_added_prefs():
"""Check that when we call reset the items we expect are updated"""
profile = Profile()
prefs_file = os.path.join(profile.profile, 'user.js')
# add some preferences
prefs1 = [("mr.t.quotes", "i aint getting on no plane!")]
profile.set_preferences(prefs1)
self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
lines = open(prefs_file).read().strip().splitlines()
self.assertTrue(any(line.startswith('#MozRunner Prefs Start') for line in lines))
self.assertTrue(any(line.startswith('#MozRunner Prefs End') for line in lines))
# we shouldn't have any initial preferences
initial_prefs = Preferences.read_prefs(prefs_file)
assert not initial_prefs
initial_prefs = open(prefs_file).read().strip()
assert not initial_prefs
profile.reset()
self.assertNotEqual(prefs1,
Preferences.read_prefs(os.path.join(profile.profile, 'user.js')),
"I pity the fool who left my pref")
# add some preferences
prefs1 = [("mr.t.quotes", "i aint getting on no plane!")]
profile.set_preferences(prefs1)
assert prefs1 == Preferences.read_prefs(prefs_file)
lines = open(prefs_file).read().strip().splitlines()
assert any(line.startswith('#MozRunner Prefs Start') for line in lines)
assert any(line.startswith('#MozRunner Prefs End') for line in lines)
def test_reset_should_keep_user_added_prefs(self):
"""Check that when we call reset the items we expect are updated"""
profile = Profile()
prefs_file = os.path.join(profile.profile, 'user.js')
profile.reset()
assert prefs1 != Preferences.read_prefs(os.path.join(profile.profile, 'user.js'))
# we shouldn't have any initial preferences
initial_prefs = Preferences.read_prefs(prefs_file)
self.assertFalse(initial_prefs)
initial_prefs = open(prefs_file).read().strip()
self.assertFalse(initial_prefs)
# add some preferences
prefs1 = [("mr.t.quotes", "i aint getting on no plane!")]
profile.set_persistent_preferences(prefs1)
self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
lines = open(prefs_file).read().strip().splitlines()
self.assertTrue(any(line.startswith('#MozRunner Prefs Start') for line in lines))
self.assertTrue(any(line.startswith('#MozRunner Prefs End') for line in lines))
def test_reset_should_keep_user_added_prefs():
"""Check that when we call reset the items we expect are updated"""
profile = Profile()
prefs_file = os.path.join(profile.profile, 'user.js')
profile.reset()
self.assertEqual(prefs1,
Preferences.read_prefs(os.path.join(profile.profile, 'user.js')),
"I pity the fool who left my pref")
# we shouldn't have any initial preferences
initial_prefs = Preferences.read_prefs(prefs_file)
assert not initial_prefs
initial_prefs = open(prefs_file).read().strip()
assert not initial_prefs
def test_magic_markers(self):
"""ensure our magic markers are working"""
# add some preferences
prefs1 = [("mr.t.quotes", "i aint getting on no plane!")]
profile.set_persistent_preferences(prefs1)
assert prefs1 == Preferences.read_prefs(prefs_file)
lines = open(prefs_file).read().strip().splitlines()
assert any(line.startswith('#MozRunner Prefs Start') for line in lines)
assert any(line.startswith('#MozRunner Prefs End') for line in lines)
profile = Profile()
prefs_file = os.path.join(profile.profile, 'user.js')
profile.reset()
assert prefs1 == Preferences.read_prefs(os.path.join(profile.profile, 'user.js'))
# we shouldn't have any initial preferences
initial_prefs = Preferences.read_prefs(prefs_file)
self.assertFalse(initial_prefs)
initial_prefs = open(prefs_file).read().strip()
self.assertFalse(initial_prefs)
# add some preferences
prefs1 = [("browser.startup.homepage", "http://planet.mozilla.org/"),
("zoom.minPercent", 30)]
profile.set_preferences(prefs1)
self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
lines = open(prefs_file).read().strip().splitlines()
self.assertTrue(bool([line for line in lines
if line.startswith('#MozRunner Prefs Start')]))
self.assertTrue(bool([line for line in lines
if line.startswith('#MozRunner Prefs End')]))
def test_magic_markers():
"""ensure our magic markers are working"""
# add some more preferences
prefs2 = [("zoom.maxPercent", 300),
("webgl.verbose", 'false')]
profile.set_preferences(prefs2)
self.assertEqual(prefs1 + prefs2, Preferences.read_prefs(prefs_file))
lines = open(prefs_file).read().strip().splitlines()
self.assertTrue(len([line for line in lines
if line.startswith('#MozRunner Prefs Start')]) == 2)
self.assertTrue(len([line for line in lines
if line.startswith('#MozRunner Prefs End')]) == 2)
profile = Profile()
prefs_file = os.path.join(profile.profile, 'user.js')
# now clean it up
profile.clean_preferences()
final_prefs = Preferences.read_prefs(prefs_file)
self.assertFalse(final_prefs)
lines = open(prefs_file).read().strip().splitlines()
self.assertTrue('#MozRunner Prefs Start' not in lines)
self.assertTrue('#MozRunner Prefs End' not in lines)
# we shouldn't have any initial preferences
initial_prefs = Preferences.read_prefs(prefs_file)
assert not initial_prefs
initial_prefs = open(prefs_file).read().strip()
assert not initial_prefs
def test_preexisting_preferences(self):
"""ensure you don't clobber preexisting preferences"""
# add some preferences
prefs1 = [("browser.startup.homepage", "http://planet.mozilla.org/"),
("zoom.minPercent", 30)]
profile.set_preferences(prefs1)
assert prefs1 == Preferences.read_prefs(prefs_file)
lines = open(prefs_file).read().strip().splitlines()
assert bool([line for line in lines
if line.startswith('#MozRunner Prefs Start')])
assert bool([line for line in lines
if line.startswith('#MozRunner Prefs End')])
# make a pretend profile
tempdir = tempfile.mkdtemp()
# add some more preferences
prefs2 = [("zoom.maxPercent", 300),
("webgl.verbose", 'false')]
profile.set_preferences(prefs2)
assert prefs1 + prefs2 == Preferences.read_prefs(prefs_file)
lines = open(prefs_file).read().strip().splitlines()
assert len([line for line in lines
if line.startswith('#MozRunner Prefs Start')]) == 2
assert len([line for line in lines
if line.startswith('#MozRunner Prefs End')]) == 2
try:
# make a user.js
contents = """
# now clean it up
profile.clean_preferences()
final_prefs = Preferences.read_prefs(prefs_file)
assert not final_prefs
lines = open(prefs_file).read().strip().splitlines()
assert '#MozRunner Prefs Start' not in lines
assert '#MozRunner Prefs End' not in lines
def test_preexisting_preferences():
"""ensure you don't clobber preexisting preferences"""
# make a pretend profile
tempdir = tempfile.mkdtemp()
try:
# make a user.js
contents = """
user_pref("webgl.enabled_for_all_sites", true);
user_pref("webgl.force-enabled", true);
"""
user_js = os.path.join(tempdir, 'user.js')
f = open(user_js, 'w')
f.write(contents)
f.close()
user_js = os.path.join(tempdir, 'user.js')
f = open(user_js, 'w')
f.write(contents)
f.close()
# make sure you can read it
prefs = Preferences.read_prefs(user_js)
original_prefs = [('webgl.enabled_for_all_sites', True), ('webgl.force-enabled', True)]
self.assertTrue(prefs == original_prefs)
# make sure you can read it
prefs = Preferences.read_prefs(user_js)
original_prefs = [('webgl.enabled_for_all_sites', True), ('webgl.force-enabled', True)]
assert prefs == original_prefs
# now read this as a profile
profile = Profile(tempdir, preferences={"browser.download.dir": "/home/jhammel"})
# now read this as a profile
profile = Profile(tempdir, preferences={"browser.download.dir": "/home/jhammel"})
# make sure the new pref is now there
new_prefs = original_prefs[:] + [("browser.download.dir", "/home/jhammel")]
prefs = Preferences.read_prefs(user_js)
self.assertTrue(prefs == new_prefs)
# make sure the new pref is now there
new_prefs = original_prefs[:] + [("browser.download.dir", "/home/jhammel")]
prefs = Preferences.read_prefs(user_js)
assert prefs == new_prefs
# clean up the added preferences
profile.cleanup()
del profile
# clean up the added preferences
profile.cleanup()
del profile
# make sure you have the original preferences
prefs = Preferences.read_prefs(user_js)
self.assertTrue(prefs == original_prefs)
finally:
shutil.rmtree(tempdir)
# make sure you have the original preferences
prefs = Preferences.read_prefs(user_js)
assert prefs == original_prefs
finally:
shutil.rmtree(tempdir)
def test_can_read_prefs_with_multiline_comments(self):
"""
Ensure that multiple comments in the file header do not break reading
the prefs (https://bugzilla.mozilla.org/show_bug.cgi?id=1233534).
"""
user_js = tempfile.NamedTemporaryFile(suffix='.js', delete=False)
self.addCleanup(mozfile.remove, user_js.name)
def test_can_read_prefs_with_multiline_comments():
"""
Ensure that multiple comments in the file header do not break reading
the prefs (https://bugzilla.mozilla.org/show_bug.cgi?id=1233534).
"""
user_js = tempfile.NamedTemporaryFile(suffix='.js', delete=False)
try:
with user_js:
user_js.write("""
# Mozilla User Preferences
/* Do not edit this file.
*
* If you make changes to this file while the application is running,
* the changes will be overwritten when the application exits.
*
* To make a manual change to preferences, you can visit the URL about:config
*/
*
* If you make changes to this file while the application is running,
* the changes will be overwritten when the application exits.
*
* To make a manual change to preferences, you can visit the URL about:config
*/
user_pref("webgl.enabled_for_all_sites", true);
user_pref("webgl.force-enabled", true);
""")
self.assertEqual(
Preferences.read_prefs(user_js.name),
[('webgl.enabled_for_all_sites', True),
('webgl.force-enabled', True)]
)
assert Preferences.read_prefs(user_js.name) == [
('webgl.enabled_for_all_sites', True),
('webgl.force-enabled', True)
]
finally:
mozfile.remove(user_js.name)
def test_json(self):
_prefs = {"browser.startup.homepage": "http://planet.mozilla.org/"}
json = '{"browser.startup.homepage": "http://planet.mozilla.org/"}'
# just repr it...could use the json module but we don't need it here
with mozfile.NamedTemporaryFile(suffix='.json') as f:
f.write(json)
f.flush()
def test_json(compare_generated):
_prefs = {"browser.startup.homepage": "http://planet.mozilla.org/"}
json = '{"browser.startup.homepage": "http://planet.mozilla.org/"}'
commandline = ["--preferences", f.name]
self.compare_generated(_prefs, commandline)
# just repr it...could use the json module but we don't need it here
with mozfile.NamedTemporaryFile(suffix='.json') as f:
f.write(json)
f.flush()
def test_json_datatypes(self):
# minPercent is at 30.1 to test if non-integer data raises an exception
json = """{"zoom.minPercent": 30.1, "zoom.maxPercent": 300}"""
commandline = ["--preferences", f.name]
compare_generated(_prefs, commandline)
with mozfile.NamedTemporaryFile(suffix='.json') as f:
f.write(json)
f.flush()
with self.assertRaises(PreferencesReadError):
Preferences.read_json(f._path)
def test_json_datatypes():
# minPercent is at 30.1 to test if non-integer data raises an exception
json = """{"zoom.minPercent": 30.1, "zoom.maxPercent": 300}"""
def test_prefs_write(self):
"""test that the Preferences.write() method correctly serializes preferences"""
with mozfile.NamedTemporaryFile(suffix='.json') as f:
f.write(json)
f.flush()
_prefs = {'browser.startup.homepage': "http://planet.mozilla.org",
'zoom.minPercent': 30,
'zoom.maxPercent': 300}
with pytest.raises(PreferencesReadError):
Preferences.read_json(f._path)
# make a Preferences manager with the testing preferences
preferences = Preferences(_prefs)
# write them to a temporary location
path = None
read_prefs = None
try:
with mozfile.NamedTemporaryFile(suffix='.js', delete=False) as f:
path = f.name
preferences.write(f, _prefs)
def test_prefs_write():
"""test that the Preferences.write() method correctly serializes preferences"""
# read them back and ensure we get what we put in
read_prefs = dict(Preferences.read_prefs(path))
_prefs = {'browser.startup.homepage': "http://planet.mozilla.org",
'zoom.minPercent': 30,
'zoom.maxPercent': 300}
finally:
# cleanup
if path and os.path.exists(path):
os.remove(path)
# make a Preferences manager with the testing preferences
preferences = Preferences(_prefs)
self.assertEqual(read_prefs, _prefs)
# write them to a temporary location
path = None
read_prefs = None
try:
with mozfile.NamedTemporaryFile(suffix='.js', delete=False) as f:
path = f.name
preferences.write(f, _prefs)
def test_read_prefs_with_comments(self):
"""test reading preferences from a prefs.js file that contains comments"""
# read them back and ensure we get what we put in
read_prefs = dict(Preferences.read_prefs(path))
path = os.path.join(here, 'files', 'prefs_with_comments.js')
self.assertEqual(dict(Preferences.read_prefs(path)), self._prefs_with_comments)
finally:
# cleanup
if path and os.path.exists(path):
os.remove(path)
def test_read_prefs_with_interpolation(self):
"""test reading preferences from a prefs.js file whose values
require interpolation"""
assert read_prefs == _prefs
expected_prefs = {
"browser.foo": "http://server-name",
"zoom.minPercent": 30,
"webgl.verbose": "false",
"browser.bar": "somethingxyz"
}
values = {
"server": "server-name",
"abc": "something"
}
path = os.path.join(here, 'files', 'prefs_with_interpolation.js')
read_prefs = Preferences.read_prefs(path, interpolation=values)
self.assertEqual(dict(read_prefs), expected_prefs)
def test_read_prefs_ttw(self):
"""test reading preferences through the web via mozhttpd"""
def test_read_prefs_with_comments():
"""test reading preferences from a prefs.js file that contains comments"""
# create a MozHttpd instance
docroot = os.path.join(here, 'files')
host = '127.0.0.1'
port = 8888
httpd = mozhttpd.MozHttpd(host=host, port=port, docroot=docroot)
path = os.path.join(here, 'files', 'prefs_with_comments.js')
assert dict(Preferences.read_prefs(path)) == _prefs_with_comments
# create a preferences instance
prefs = Preferences()
try:
# start server
httpd.start(block=False)
def test_read_prefs_with_interpolation():
"""test reading preferences from a prefs.js file whose values
require interpolation"""
# read preferences through the web
read = prefs.read_prefs('http://%s:%d/prefs_with_comments.js' % (host, port))
self.assertEqual(dict(read), self._prefs_with_comments)
finally:
httpd.stop()
expected_prefs = {
"browser.foo": "http://server-name",
"zoom.minPercent": 30,
"webgl.verbose": "false",
"browser.bar": "somethingxyz"
}
values = {
"server": "server-name",
"abc": "something"
}
path = os.path.join(here, 'files', 'prefs_with_interpolation.js')
read_prefs = Preferences.read_prefs(path, interpolation=values)
assert dict(read_prefs) == expected_prefs
def test_read_prefs_ttw():
"""test reading preferences through the web via mozhttpd"""
# create a MozHttpd instance
docroot = os.path.join(here, 'files')
host = '127.0.0.1'
port = 8888
httpd = mozhttpd.MozHttpd(host=host, port=port, docroot=docroot)
# create a preferences instance
prefs = Preferences()
try:
# start server
httpd.start(block=False)
# read preferences through the web
read = prefs.read_prefs('http://%s:%d/prefs_with_comments.js' % (host, port))
assert dict(read) == _prefs_with_comments
finally:
httpd.stop()
if __name__ == '__main__':

Просмотреть файл

@ -6,29 +6,30 @@
from __future__ import absolute_import
import unittest
import os
import mozunit
import pytest
from mozprofile import Profile
class TestProfile(unittest.TestCase):
def test_with_profile_should_cleanup():
with Profile() as profile:
assert os.path.exists(profile.profile)
def test_with_profile_should_cleanup(self):
# profile is cleaned
assert not os.path.exists(profile.profile)
def test_with_profile_should_cleanup_even_on_exception():
with pytest.raises(ZeroDivisionError):
with Profile() as profile:
self.assertTrue(os.path.exists(profile.profile))
# profile is cleaned
self.assertFalse(os.path.exists(profile.profile))
assert os.path.exists(profile.profile)
1 / 0 # will raise ZeroDivisionError
def test_with_profile_should_cleanup_even_on_exception(self):
with self.assertRaises(ZeroDivisionError):
with Profile() as profile:
self.assertTrue(os.path.exists(profile.profile))
1 / 0 # will raise ZeroDivisionError
# profile is cleaned
self.assertFalse(os.path.exists(profile.profile))
# profile is cleaned
assert not os.path.exists(profile.profile)
if __name__ == '__main__':

Просмотреть файл

@ -6,76 +6,68 @@
from __future__ import absolute_import
import mozfile
import mozprofile
import os
import tempfile
import unittest
import mozunit
here = os.path.dirname(os.path.abspath(__file__))
class TestProfilePrint(unittest.TestCase):
def test_profileprint(tmpdir):
"""
test the summary function
"""
def test_profileprint(self):
"""
test the summary function
"""
keys = set(['Files', 'Path', 'user.js'])
ff_prefs = mozprofile.FirefoxProfile.preferences # shorthand
pref_string = '\n'.join(['%s: %s' % (key, ff_prefs[key])
for key in sorted(ff_prefs.keys())])
keys = set(['Files', 'Path', 'user.js'])
ff_prefs = mozprofile.FirefoxProfile.preferences # shorthand
pref_string = '\n'.join(['%s: %s' % (key, ff_prefs[key])
for key in sorted(ff_prefs.keys())])
tmpdir = tmpdir.strpath
profile = mozprofile.FirefoxProfile(tmpdir)
parts = profile.summary(return_parts=True)
parts = dict(parts)
tempdir = tempfile.mkdtemp()
try:
profile = mozprofile.FirefoxProfile(tempdir)
parts = profile.summary(return_parts=True)
parts = dict(parts)
assert parts['Path'] == tmpdir
assert set(parts.keys()) == keys
assert pref_string == parts['user.js'].strip()
self.assertEqual(parts['Path'], tempdir)
self.assertEqual(set(parts.keys()), keys)
self.assertEqual(pref_string, parts['user.js'].strip())
except BaseException:
raise
finally:
mozfile.rmtree(tempdir)
def test_str_cast():
"""Test casting to a string."""
profile = mozprofile.Profile()
assert str(profile) == profile.summary().encode("utf-8")
def test_str_cast(self):
"""Test casting to a string."""
profile = mozprofile.Profile()
self.assertEqual(str(profile), profile.summary().encode("utf-8"))
def test_unicode_cast(self):
"""Test casting to a unicode string."""
profile = mozprofile.Profile()
self.assertEqual(unicode(profile), profile.summary())
def test_unicode_cast():
"""Test casting to a unicode string."""
profile = mozprofile.Profile()
assert unicode(profile) == profile.summary()
def test_profile_diff(self):
profile1 = mozprofile.Profile()
profile2 = mozprofile.Profile(preferences=dict(foo='bar'))
# diff a profile against itself; no difference
self.assertEqual([], mozprofile.diff(profile1, profile1))
def test_profile_diff():
profile1 = mozprofile.Profile()
profile2 = mozprofile.Profile(preferences=dict(foo='bar'))
# diff two profiles
diff = dict(mozprofile.diff(profile1, profile2))
self.assertEqual(diff.keys(), ['user.js'])
lines = [line.strip() for line in diff['user.js'].splitlines()]
self.assertTrue('+foo: bar' in lines)
# diff a profile against itself; no difference
assert mozprofile.diff(profile1, profile1) == []
# diff a blank vs FirefoxProfile
ff_profile = mozprofile.FirefoxProfile()
diff = dict(mozprofile.diff(profile2, ff_profile))
self.assertEqual(diff.keys(), ['user.js'])
lines = [line.strip() for line in diff['user.js'].splitlines()]
self.assertTrue('-foo: bar' in lines)
ff_pref_lines = ['+%s: %s' % (key, value)
for key, value in mozprofile.FirefoxProfile.preferences.items()]
self.assertTrue(set(ff_pref_lines).issubset(lines))
# diff two profiles
diff = dict(mozprofile.diff(profile1, profile2))
assert diff.keys() == ['user.js']
lines = [line.strip() for line in diff['user.js'].splitlines()]
assert '+foo: bar' in lines
# diff a blank vs FirefoxProfile
ff_profile = mozprofile.FirefoxProfile()
diff = dict(mozprofile.diff(profile2, ff_profile))
assert diff.keys() == ['user.js']
lines = [line.strip() for line in diff['user.js'].splitlines()]
assert '-foo: bar' in lines
ff_pref_lines = ['+%s: %s' % (key, value)
for key, value in mozprofile.FirefoxProfile.preferences.items()]
assert set(ff_pref_lines).issubset(lines)
if __name__ == '__main__':

Просмотреть файл

@ -0,0 +1,146 @@
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import
import mozunit
import pytest
from mozprofile.permissions import ServerLocations, \
MissingPrimaryLocationError, MultiplePrimaryLocationsError, \
BadPortLocationError, LocationsSyntaxError
LOCATIONS = """# This is the primary location from which tests run.
#
http://mochi.test:8888 primary,privileged
# a few test locations
http://127.0.0.1:80 privileged
http://127.0.0.1:8888 privileged
https://test:80 privileged
http://example.org:80 privileged
http://test1.example.org privileged
"""
LOCATIONS_NO_PRIMARY = """http://secondary.test:80 privileged
http://tertiary.test:8888 privileged
"""
LOCATIONS_BAD_PORT = """http://mochi.test:8888 primary,privileged
http://127.0.0.1:80 privileged
http://127.0.0.1:8888 privileged
http://test:badport privileged
http://example.org:80 privileged
"""
def compare_location(location, scheme, host, port, options):
assert location.scheme == scheme
assert location.host == host
assert location.port == port
assert location.options == options
@pytest.fixture
def create_temp_file(tmpdir):
def inner(contents):
f = tmpdir.mkdtemp().join('locations.txt')
f.write(contents)
return f.strpath
return inner
def test_server_locations(create_temp_file):
# write a permissions file
f = create_temp_file(LOCATIONS)
# read the locations
locations = ServerLocations(f)
# ensure that they're what we expect
assert len(locations) == 6
i = iter(locations)
compare_location(next(i), 'http', 'mochi.test', '8888', ['primary', 'privileged'])
compare_location(next(i), 'http', '127.0.0.1', '80', ['privileged'])
compare_location(next(i), 'http', '127.0.0.1', '8888', ['privileged'])
compare_location(next(i), 'https', 'test', '80', ['privileged'])
compare_location(next(i), 'http', 'example.org', '80', ['privileged'])
compare_location(next(i), 'http', 'test1.example.org', '8888', ['privileged'])
locations.add_host('mozilla.org')
assert len(locations) == 7
compare_location(next(i), 'http', 'mozilla.org', '80', ['privileged'])
# test some errors
with pytest.raises(MultiplePrimaryLocationsError):
locations.add_host('primary.test', options='primary')
# assert we don't throw DuplicateLocationError
locations.add_host('127.0.0.1')
with pytest.raises(BadPortLocationError):
locations.add_host('127.0.0.1', port='abc')
# test some errors in locations file
f = create_temp_file(LOCATIONS_NO_PRIMARY)
exc = None
try:
ServerLocations(f)
except LocationsSyntaxError as e:
exc = e
assert exc is not None
assert exc.err.__class__ == MissingPrimaryLocationError
assert exc.lineno == 3
# test bad port in a locations file to ensure lineno calculated
# properly.
f = create_temp_file(LOCATIONS_BAD_PORT)
exc = None
try:
ServerLocations(f)
except LocationsSyntaxError as e:
exc = e
assert exc is not None
assert exc.err.__class__ == BadPortLocationError
assert exc.lineno == 4
def test_server_locations_callback(create_temp_file):
class CallbackTest(object):
last_locations = None
def callback(self, locations):
self.last_locations = locations
c = CallbackTest()
f = create_temp_file(LOCATIONS)
locations = ServerLocations(f, c.callback)
# callback should be for all locations in file
assert len(c.last_locations) == 6
# validate arbitrary one
compare_location(c.last_locations[2], 'http', '127.0.0.1', '8888', ['privileged'])
locations.add_host('a.b.c')
# callback should be just for one location
assert len(c.last_locations) == 1
compare_location(c.last_locations[0], 'http', 'a.b.c', '80', ['privileged'])
# read a second file, which should generate a callback with both
# locations.
f = create_temp_file(LOCATIONS_NO_PRIMARY)
locations.read(f)
assert len(c.last_locations) == 2
if __name__ == '__main__':
mozunit.main()