<s> from django . conf import settings <EOL> import mock <EOL> import urlparse <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 . utils import make_xpi <EOL> from olympia . versions . compare import version_int <EOL> from olympia . zadmin import tasks <EOL> def RequestMock ( response = '<STR_LIT>' , headers = None ) : <EOL> """<STR_LIT>""" <EOL> res = mock . Mock ( ) <EOL> res . read . return_value = response <EOL> res . contents = response <EOL> res . text = response <EOL> res . iter_lines . side_effect = lambda chunk_size = <NUM_LIT:1> : ( response . split ( '<STR_LIT:\n>' ) <EOL> . __iter__ ( ) ) <EOL> res . iter_content . side_effect = lambda chunk_size = <NUM_LIT:1> : ( response , ) . __iter__ ( ) <EOL> def lines ( ) : <EOL> return [ l + '<STR_LIT:\n>' for l in response . split ( '<STR_LIT:\n>' ) [ : - <NUM_LIT:1> ] ] <EOL> res . readlines . side_effect = lines <EOL> res . iter_lines . side_effect = lambda : lines ( ) . __iter__ ( ) <EOL> res . headers = headers or { } <EOL> res . headers [ '<STR_LIT>' ] = len ( response ) <EOL> return res <EOL> def make_langpack ( version ) : <EOL> versions = ( version , '<STR_LIT>' % version ) <EOL> for version in versions : <EOL> AppVersion . objects . get_or_create ( application = amo . FIREFOX . id , <EOL> version = version , <EOL> version_int = version_int ( version ) ) <EOL> return make_xpi ( { <EOL> '<STR_LIT>' : """<STR_LIT>""" . format ( * versions ) <EOL> } ) . read ( ) <EOL> class TestLangpackFetcher ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> LISTING = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> super ( TestLangpackFetcher , self ) . setUp ( ) <EOL> request_patch = mock . patch ( '<STR_LIT>' ) <EOL> self . mock_request = request_patch . start ( ) <EOL> self . addCleanup ( request_patch . stop ) <EOL> def get_langpacks ( self ) : <EOL> return ( Addon . objects . no_cache ( ) <EOL> . filter ( addonuser__user__email = settings . LANGPACK_OWNER_EMAIL , <EOL> type = amo . ADDON_LPAPP ) ) <EOL> def fetch_langpacks ( self , version ) : <EOL> path = settings . LANGPACK_PATH_DEFAULT % ( '<STR_LIT>' , version ) <EOL> base_url = urlparse . urljoin ( settings . LANGPACK_DOWNLOAD_BASE , path ) <EOL> list_url = urlparse . urljoin ( base_url , settings . LANGPACK_MANIFEST_PATH ) <EOL> langpack_url = urlparse . urljoin ( base_url , '<STR_LIT>' ) <EOL> responses = { list_url : RequestMock ( self . LISTING ) , <EOL> langpack_url : RequestMock ( make_langpack ( version ) ) } <EOL> self . mock_request . reset_mock ( ) <EOL> self . mock_request . side_effect = lambda url , ** kw : responses . get ( url ) <EOL> tasks . fetch_langpacks ( path ) <EOL> self . mock_request . assert_has_calls ( <EOL> [ mock . call ( list_url , verify = settings . CA_CERT_BUNDLE_PATH ) , <EOL> mock . call ( langpack_url , verify = settings . CA_CERT_BUNDLE_PATH ) ] ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_fetch_new_langpack ( self , mock_sign_file ) : <EOL> assert self . get_langpacks ( ) . count ( ) == <NUM_LIT:0> <EOL> self . fetch_langpacks ( amo . FIREFOX . latest_version ) <EOL> langpacks = self . get_langpacks ( ) <EOL> assert langpacks . count ( ) == <NUM_LIT:1> <EOL> addon = langpacks [ <NUM_LIT:0> ] <EOL> assert addon . default_locale == '<STR_LIT>' <EOL> assert addon . target_locale == '<STR_LIT>' <EOL> assert addon . _current_version <EOL> assert addon . current_version . version == amo . FIREFOX . latest_version <EOL> assert addon . status == amo . STATUS_PUBLIC <EOL> assert addon . current_version . files . all ( ) [ <NUM_LIT:0> ] . status == amo . STATUS_PUBLIC <EOL> mock_sign_file . assert_called_once_with ( <EOL> addon . current_version . files . get ( ) , settings . SIGNING_SERVER ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_fetch_updated_langpack ( self , mock_sign_file ) : <EOL> versions = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . fetch_langpacks ( versions [ <NUM_LIT:0> ] ) <EOL> assert self . get_langpacks ( ) . count ( ) == <NUM_LIT:1> <EOL> self . fetch_langpacks ( versions [ <NUM_LIT:1> ] ) <EOL> langpacks = self . get_langpacks ( ) <EOL> assert langpacks . count ( ) == <NUM_LIT:1> <EOL> addon = langpacks [ <NUM_LIT:0> ] <EOL> assert addon . versions . count ( ) == <NUM_LIT:2> <EOL> version = addon . versions . get ( version = versions [ <NUM_LIT:1> ] ) <EOL> assert version . files . all ( ) [ <NUM_LIT:0> ] . status == amo . STATUS_PUBLIC <EOL> mock_sign_file . assert_called_with ( <EOL> version . files . get ( ) , settings . SIGNING_SERVER ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_fetch_duplicate_langpack ( self , mock_sign_file ) : <EOL> self . fetch_langpacks ( amo . FIREFOX . latest_version ) <EOL> langpacks = self . get_langpacks ( ) <EOL> assert langpacks . count ( ) == <NUM_LIT:1> <EOL> assert langpacks [ <NUM_LIT:0> ] . versions . count ( ) == <NUM_LIT:1> <EOL> assert ( langpacks [ <NUM_LIT:0> ] . versions . all ( ) [ <NUM_LIT:0> ] . version == <EOL> amo . FIREFOX . latest_version ) <EOL> self . fetch_langpacks ( amo . FIREFOX . latest_version ) <EOL> langpacks = self . get_langpacks ( ) <EOL> assert langpacks . count ( ) == <NUM_LIT:1> <EOL> addon = langpacks [ <NUM_LIT:0> ] <EOL> assert addon . versions . count ( ) == <NUM_LIT:1> <EOL> assert ( addon . versions . all ( ) [ <NUM_LIT:0> ] . version == <EOL> amo . FIREFOX . latest_version ) <EOL> mock_sign_file . assert_called_once_with ( <EOL> addon . current_version . files . get ( ) , settings . SIGNING_SERVER ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_fetch_updated_langpack_beta ( self , mock_sign_file ) : <EOL> versions = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . fetch_langpacks ( versions [ <NUM_LIT:0> ] ) <EOL> assert self . get_langpacks ( ) . count ( ) == <NUM_LIT:1> <EOL> self . fetch_langpacks ( versions [ <NUM_LIT:1> ] ) <EOL> langpacks = self . get_langpacks ( ) <EOL> assert langpacks . count ( ) == <NUM_LIT:1> <EOL> addon = langpacks [ <NUM_LIT:0> ] <EOL> assert addon . versions . count ( ) == <NUM_LIT:2> <EOL> version = addon . versions . get ( version = versions [ <NUM_LIT:1> ] ) <EOL> assert version . files . all ( ) [ <NUM_LIT:0> ] . status == amo . STATUS_BETA <EOL> mock_sign_file . assert_called_with ( <EOL> version . files . get ( ) , settings . PRELIMINARY_SIGNING_SERVER ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_fetch_new_langpack_beta ( self , mock_sign_file ) : <EOL> self . fetch_langpacks ( '<STR_LIT>' ) <EOL> assert self . get_langpacks ( ) . count ( ) == <NUM_LIT:0> <EOL> assert not mock_sign_file . called <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_fetch_langpack_wrong_owner ( self , mock_sign_file ) : <EOL> Addon . objects . create ( guid = '<STR_LIT>' , <EOL> type = amo . ADDON_LPAPP ) <EOL> self . fetch_langpacks ( amo . FIREFOX . latest_version ) <EOL> assert self . get_langpacks ( ) . count ( ) == <NUM_LIT:0> <EOL> assert not mock_sign_file . called <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_fetch_langpack_invalid_path_fails ( self , mock_sign_file ) : <EOL> self . mock_request . return_value = None <EOL> with self . assertRaises ( ValueError ) as exc : <EOL> tasks . fetch_langpacks ( '<STR_LIT>' ) <EOL> assert str ( exc . exception ) == '<STR_LIT>' <EOL> assert not mock_sign_file . called </s>
