<s> import json <EOL> import os <EOL> import zipfile <EOL> from tempfile import NamedTemporaryFile <EOL> import mock <EOL> import pytest <EOL> from django import forms <EOL> from olympia import amo <EOL> from olympia . amo . tests import TestCase <EOL> from olympia . addons . models import Addon <EOL> from olympia . applications . models import AppVersion <EOL> from olympia . files import utils <EOL> from olympia . files . models import File <EOL> from olympia . versions . models import Version <EOL> pytestmark = pytest . mark . django_db <EOL> def test_is_beta ( ) : <EOL> assert not utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> assert utils . is_beta ( '<STR_LIT>' ) <EOL> class TestFindJetpacks ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> super ( TestFindJetpacks , self ) . setUp ( ) <EOL> File . objects . update ( jetpack_version = '<STR_LIT:1.0>' ) <EOL> self . file = File . objects . filter ( version__addon = <NUM_LIT> ) . get ( ) <EOL> def test_success ( self ) : <EOL> files = utils . find_jetpacks ( '<STR_LIT:1.0>' , '<STR_LIT>' ) <EOL> assert files == [ self . file ] <EOL> def test_skip_autorepackage ( self ) : <EOL> Addon . objects . update ( auto_repackage = False ) <EOL> assert utils . find_jetpacks ( '<STR_LIT:1.0>' , '<STR_LIT>' ) == [ ] <EOL> def test_minver ( self ) : <EOL> files = utils . find_jetpacks ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert files == [ self . file ] <EOL> assert not files [ <NUM_LIT:0> ] . needs_upgrade <EOL> def test_maxver ( self ) : <EOL> files = utils . find_jetpacks ( '<STR_LIT>' , '<STR_LIT:1.0>' ) <EOL> assert files == [ self . file ] <EOL> assert not files [ <NUM_LIT:0> ] . needs_upgrade <EOL> def test_unreviewed_files_plus_reviewed_file ( self ) : <EOL> v = Version . objects . create ( addon_id = <NUM_LIT> ) <EOL> new_file = File . objects . create ( version = v , jetpack_version = '<STR_LIT:1.0>' ) <EOL> Version . objects . create ( addon_id = <NUM_LIT> ) <EOL> new_file2 = File . objects . create ( version = v , jetpack_version = '<STR_LIT:1.0>' ) <EOL> assert new_file . status == amo . STATUS_UNREVIEWED <EOL> assert new_file2 . status == amo . STATUS_UNREVIEWED <EOL> files = utils . find_jetpacks ( '<STR_LIT:1.0>' , '<STR_LIT>' ) <EOL> assert files == [ self . file , new_file , new_file2 ] <EOL> assert all ( f . needs_upgrade for f in files ) <EOL> new_file . update ( status = amo . STATUS_PUBLIC ) <EOL> files = utils . find_jetpacks ( '<STR_LIT:1.0>' , '<STR_LIT>' ) <EOL> assert files == [ self . file , new_file , new_file2 ] <EOL> assert not files [ <NUM_LIT:0> ] . needs_upgrade <EOL> assert all ( f . needs_upgrade for f in files [ <NUM_LIT:1> : ] ) <EOL> class TestExtractor ( TestCase ) : <EOL> def os_path_exists_for ( self , path_to_accept ) : <EOL> """<STR_LIT>""" <EOL> return lambda path : path . endswith ( path_to_accept ) <EOL> def test_no_manifest ( self ) : <EOL> with self . assertRaises ( forms . ValidationError ) as exc : <EOL> utils . Extractor . parse ( '<STR_LIT>' ) <EOL> assert exc . exception . message == ( <EOL> "<STR_LIT>" ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_parse_install_rdf ( self , exists_mock , rdf_extractor , <EOL> package_json_extractor , <EOL> manifest_json_extractor ) : <EOL> exists_mock . side_effect = self . os_path_exists_for ( '<STR_LIT>' ) <EOL> utils . Extractor . parse ( '<STR_LIT>' ) <EOL> assert rdf_extractor . called <EOL> assert not package_json_extractor . called <EOL> assert not manifest_json_extractor . called <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_parse_package_json ( self , exists_mock , rdf_extractor , <EOL> package_json_extractor , <EOL> manifest_json_extractor ) : <EOL> exists_mock . side_effect = self . os_path_exists_for ( '<STR_LIT>' ) <EOL> utils . Extractor . parse ( '<STR_LIT>' ) <EOL> assert not rdf_extractor . called <EOL> assert package_json_extractor . called <EOL> assert not manifest_json_extractor . called <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_parse_manifest_json ( self , exists_mock , rdf_extractor , <EOL> package_json_extractor , <EOL> manifest_json_extractor ) : <EOL> exists_mock . side_effect = self . os_path_exists_for ( '<STR_LIT>' ) <EOL> utils . Extractor . parse ( '<STR_LIT>' ) <EOL> assert not rdf_extractor . called <EOL> assert not package_json_extractor . called <EOL> assert manifest_json_extractor . called <EOL> class TestPackageJSONExtractor ( TestCase ) : <EOL> def parse ( self , base_data ) : <EOL> return utils . PackageJSONExtractor ( '<STR_LIT>' , <EOL> json . dumps ( base_data ) ) . parse ( ) <EOL> def create_appversion ( self , name , version ) : <EOL> return AppVersion . objects . create ( application = amo . APPS [ name ] . id , <EOL> version = version ) <EOL> def test_instanciate_without_data ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT:id>' : '<STR_LIT>' } <EOL> with NamedTemporaryFile ( ) as file_ : <EOL> file_ . write ( json . dumps ( data ) ) <EOL> file_ . flush ( ) <EOL> pje = utils . PackageJSONExtractor ( file_ . name ) <EOL> assert pje . data == data <EOL> def test_guid ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . parse ( { '<STR_LIT:id>' : '<STR_LIT>' } ) [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> def test_name_for_guid_if_no_id ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . parse ( { '<STR_LIT:name>' : '<STR_LIT>' } ) [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> def test_type ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . parse ( { } ) [ '<STR_LIT:type>' ] == amo . ADDON_EXTENSION <EOL> def test_no_restart ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . parse ( { } ) [ '<STR_LIT>' ] is True <EOL> def test_name_from_title_with_name ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT:name>' : '<STR_LIT>' } <EOL> assert self . parse ( data ) [ '<STR_LIT:name>' ] == '<STR_LIT>' <EOL> def test_name_from_name_without_title ( self ) : <EOL> """<STR_LIT>""" <EOL> assert ( <EOL> self . parse ( { '<STR_LIT:name>' : '<STR_LIT>' } ) [ '<STR_LIT:name>' ] == '<STR_LIT>' ) <EOL> def test_version ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . parse ( { '<STR_LIT:version>' : '<STR_LIT>' } ) [ '<STR_LIT:version>' ] == '<STR_LIT>' <EOL> def test_homepage ( self ) : <EOL> """<STR_LIT>""" <EOL> assert ( <EOL> self . parse ( { '<STR_LIT>' : '<STR_LIT>' } ) [ '<STR_LIT>' ] == <EOL> '<STR_LIT>' ) <EOL> def test_summary ( self ) : <EOL> """<STR_LIT>""" <EOL> assert ( <EOL> self . parse ( { '<STR_LIT:description>' : '<STR_LIT>' } ) [ '<STR_LIT>' ] == '<STR_LIT>' ) <EOL> def test_apps ( self ) : <EOL> """<STR_LIT>""" <EOL> firefox_version = self . create_appversion ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> thunderbird_version = self . create_appversion ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> data = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } } <EOL> apps = self . parse ( data ) [ '<STR_LIT>' ] <EOL> apps_dict = dict ( ( app . appdata . short , app ) for app in apps ) <EOL> assert sorted ( apps_dict . keys ( ) ) == [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert apps_dict [ '<STR_LIT>' ] . min == firefox_version <EOL> assert apps_dict [ '<STR_LIT>' ] . max == firefox_version <EOL> assert apps_dict [ '<STR_LIT>' ] . min == thunderbird_version <EOL> assert apps_dict [ '<STR_LIT>' ] . max == thunderbird_version <EOL> def test_unknown_apps_are_ignored ( self ) : <EOL> """<STR_LIT>""" <EOL> self . create_appversion ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . create_appversion ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> apps = self . parse ( data ) [ '<STR_LIT>' ] <EOL> engines = [ app . appdata . short for app in apps ] <EOL> assert sorted ( engines ) == [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def test_invalid_app_versions_are_ignored ( self ) : <EOL> """<STR_LIT>""" <EOL> firefox_version = self . create_appversion ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> apps = self . parse ( data ) [ '<STR_LIT>' ] <EOL> assert len ( apps ) == <NUM_LIT:1> <EOL> assert apps [ <NUM_LIT:0> ] . appdata . short == '<STR_LIT>' <EOL> assert apps [ <NUM_LIT:0> ] . min == firefox_version <EOL> assert apps [ <NUM_LIT:0> ] . max == firefox_version <EOL> def test_fennec_is_treated_as_android ( self ) : <EOL> """<STR_LIT>""" <EOL> android_version = self . create_appversion ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> apps = self . parse ( data ) [ '<STR_LIT>' ] <EOL> assert apps [ <NUM_LIT:0> ] . appdata . short == '<STR_LIT>' <EOL> assert apps [ <NUM_LIT:0> ] . min == android_version <EOL> assert apps [ <NUM_LIT:0> ] . max == android_version <EOL> def test_is_webextension ( self ) : <EOL> """<STR_LIT>""" <EOL> assert '<STR_LIT>' not in self . parse ( { } ) <EOL> class TestManifestJSONExtractor ( TestCase ) : <EOL> def parse ( self , base_data ) : <EOL> return utils . ManifestJSONExtractor ( '<STR_LIT>' , <EOL> json . dumps ( base_data ) ) . parse ( ) <EOL> def create_appversion ( self , name , version ) : <EOL> return AppVersion . objects . create ( application = amo . APPS [ name ] . id , <EOL> version = version ) <EOL> def test_instanciate_without_data ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { '<STR_LIT:id>' : '<STR_LIT>' } <EOL> with NamedTemporaryFile ( ) as file_ : <EOL> file_ . write ( json . dumps ( data ) ) <EOL> file_ . flush ( ) <EOL> mje = utils . ManifestJSONExtractor ( file_ . name ) <EOL> assert mje . data == data <EOL> def test_guid ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . parse ( <EOL> { '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : '<STR_LIT>' } } } ) [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> def test_name_for_guid_if_no_id ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . parse ( { '<STR_LIT:name>' : '<STR_LIT>' } ) [ '<STR_LIT>' ] is None <EOL> def test_type ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . parse ( { } ) [ '<STR_LIT:type>' ] == amo . ADDON_EXTENSION <EOL> def test_no_restart ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . parse ( { } ) [ '<STR_LIT>' ] is True <EOL> def test_name ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . parse ( { '<STR_LIT:name>' : '<STR_LIT>' } ) [ '<STR_LIT:name>' ] == '<STR_LIT>' <EOL> def test_version ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . parse ( { '<STR_LIT:version>' : '<STR_LIT>' } ) [ '<STR_LIT:version>' ] == '<STR_LIT>' <EOL> def test_homepage ( self ) : <EOL> """<STR_LIT>""" <EOL> assert ( <EOL> self . parse ( { '<STR_LIT>' : '<STR_LIT>' } ) [ '<STR_LIT>' ] == <EOL> '<STR_LIT>' ) <EOL> def test_summary ( self ) : <EOL> """<STR_LIT>""" <EOL> assert ( <EOL> self . parse ( { '<STR_LIT:description>' : '<STR_LIT>' } ) [ '<STR_LIT>' ] == '<STR_LIT>' ) <EOL> def test_apps_use_provided_versions ( self ) : <EOL> """<STR_LIT>""" <EOL> firefox_min_version = self . create_appversion ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> firefox_max_version = self . create_appversion ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . create_appversion ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . create_appversion ( '<STR_LIT>' , '<STR_LIT:*>' ) <EOL> data = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } } <EOL> apps = self . parse ( data ) [ '<STR_LIT>' ] <EOL> assert len ( apps ) == <NUM_LIT:1> <EOL> app = apps [ <NUM_LIT:0> ] <EOL> assert app . appdata == amo . FIREFOX <EOL> assert app . min == firefox_min_version <EOL> assert app . max == firefox_max_version <EOL> def test_apps_use_default_versions_if_none_provided ( self ) : <EOL> """<STR_LIT>""" <EOL> firefox_min_version = self . create_appversion ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> firefox_max_version = self . create_appversion ( '<STR_LIT>' , '<STR_LIT:*>' ) <EOL> data = { '<STR_LIT>' : { '<STR_LIT>' : { '<STR_LIT:id>' : '<STR_LIT>' } } } <EOL> apps = self . parse ( data ) [ '<STR_LIT>' ] <EOL> assert len ( apps ) == <NUM_LIT:1> <EOL> app = apps [ <NUM_LIT:0> ] <EOL> assert app . appdata == amo . FIREFOX <EOL> assert app . min == firefox_min_version <EOL> assert app . max == firefox_max_version <EOL> def test_invalid_app_versions_are_ignored ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } } <EOL> assert not self . parse ( data ) [ '<STR_LIT>' ] <EOL> def test_is_webextension ( self ) : <EOL> assert self . parse ( { } ) [ '<STR_LIT>' ] <EOL> def test_zip_folder_content ( ) : <EOL> extension_file = '<STR_LIT>' <EOL> temp_filename , temp_folder = None , None <EOL> try : <EOL> temp_folder = utils . extract_zip ( extension_file ) <EOL> assert sorted ( os . listdir ( temp_folder ) ) == [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> temp_filename = amo . tests . get_temp_filename ( ) <EOL> utils . zip_folder_content ( temp_folder , temp_filename ) <EOL> with zipfile . ZipFile ( temp_filename , mode = '<STR_LIT:r>' ) as new : <EOL> with zipfile . ZipFile ( extension_file , mode = '<STR_LIT:r>' ) as orig : <EOL> assert sorted ( new . namelist ( ) ) == sorted ( orig . namelist ( ) ) <EOL> finally : <EOL> if temp_folder is not None and os . path . exists ( temp_folder ) : <EOL> amo . utils . rm_local_tmp_dir ( temp_folder ) <EOL> if temp_filename is not None and os . path . exists ( temp_filename ) : <EOL> os . unlink ( temp_filename ) <EOL> def test_repack ( ) : <EOL> extension_file = '<STR_LIT>' <EOL> with amo . tests . copy_file_to_temp ( extension_file ) as temp_filename : <EOL> with utils . repack ( temp_filename ) as folder_path : <EOL> assert sorted ( os . listdir ( folder_path ) ) == [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> with open ( os . path . join ( folder_path , '<STR_LIT>' ) , '<STR_LIT:w>' ) as file_ : <EOL> file_ . write ( '<STR_LIT>' ) <EOL> assert not os . path . exists ( folder_path ) <EOL> assert os . path . exists ( temp_filename ) <EOL> with zipfile . ZipFile ( temp_filename , mode = '<STR_LIT:r>' ) as zf : <EOL> assert '<STR_LIT>' in zf . namelist ( ) <EOL> assert zf . read ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> @ pytest . fixture <EOL> def file_obj ( ) : <EOL> addon = amo . tests . addon_factory ( ) <EOL> addon . update ( guid = '<STR_LIT>' ) <EOL> version = addon . current_version <EOL> return version . all_files [ <NUM_LIT:0> ] <EOL> def test_bump_version_in_install_rdf ( file_obj ) : <EOL> with amo . tests . copy_file ( '<STR_LIT>' , <EOL> file_obj . file_path ) : <EOL> utils . update_version_number ( file_obj , '<STR_LIT>' ) <EOL> parsed = utils . parse_xpi ( file_obj . file_path ) <EOL> assert parsed [ '<STR_LIT:version>' ] == '<STR_LIT>' <EOL> def test_bump_version_in_alt_install_rdf ( file_obj ) : <EOL> with amo . tests . copy_file ( '<STR_LIT>' , <EOL> file_obj . file_path ) : <EOL> utils . update_version_number ( file_obj , '<STR_LIT>' ) <EOL> parsed = utils . parse_xpi ( file_obj . file_path ) <EOL> assert parsed [ '<STR_LIT:version>' ] == '<STR_LIT>' <EOL> def test_bump_version_in_package_json ( file_obj ) : <EOL> with amo . tests . copy_file ( <EOL> '<STR_LIT>' , <EOL> file_obj . file_path ) : <EOL> utils . update_version_number ( file_obj , '<STR_LIT>' ) <EOL> parsed = utils . parse_xpi ( file_obj . file_path ) <EOL> assert parsed [ '<STR_LIT:version>' ] == '<STR_LIT>' <EOL> def test_bump_version_in_manifest_json ( file_obj ) : <EOL> with amo . tests . copy_file ( <EOL> '<STR_LIT>' , <EOL> file_obj . file_path ) : <EOL> utils . update_version_number ( file_obj , '<STR_LIT>' ) <EOL> parsed = utils . parse_xpi ( file_obj . file_path ) <EOL> assert parsed [ '<STR_LIT:version>' ] == '<STR_LIT>' </s>
