<s> """<STR_LIT>""" <EOL> from distlib . database import Distribution <EOL> from mock import MagicMock , patch <EOL> from pyramid . httpexceptions import HTTPBadRequest , HTTPNotFound , HTTPForbidden <EOL> from . import MockServerTest , make_package <EOL> from pypicloud . views import api <EOL> class TestApi ( MockServerTest ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestApi , self ) . setUp ( ) <EOL> self . access = self . request . access = MagicMock ( ) <EOL> def test_list_packages ( self ) : <EOL> """<STR_LIT>""" <EOL> p1 = make_package ( ) <EOL> self . db . upload ( p1 . filename , None ) <EOL> pkgs = api . all_packages ( self . request ) <EOL> self . assertEqual ( pkgs [ '<STR_LIT>' ] , [ p1 . name ] ) <EOL> def test_list_packages_no_perm ( self ) : <EOL> """<STR_LIT>""" <EOL> p1 = make_package ( ) <EOL> self . db . upload ( p1 . filename , None ) <EOL> self . access . has_permission . return_value = False <EOL> pkgs = api . all_packages ( self . request ) <EOL> self . assertEqual ( pkgs [ '<STR_LIT>' ] , [ ] ) <EOL> def test_list_packages_verbose ( self ) : <EOL> """<STR_LIT>""" <EOL> p1 = make_package ( ) <EOL> p1 = self . db . upload ( p1 . filename , None ) <EOL> pkgs = api . all_packages ( self . request , True ) <EOL> self . assertEqual ( pkgs [ '<STR_LIT>' ] , [ { <EOL> '<STR_LIT:name>' : p1 . name , <EOL> '<STR_LIT>' : p1 . version , <EOL> '<STR_LIT>' : p1 . version , <EOL> '<STR_LIT>' : p1 . last_modified , <EOL> } ] ) <EOL> def test_delete_missing ( self ) : <EOL> """<STR_LIT>""" <EOL> context = MagicMock ( ) <EOL> context . name = '<STR_LIT>' <EOL> context . version = '<STR_LIT>' <EOL> ret = api . delete_package ( context , self . request ) <EOL> self . assertTrue ( isinstance ( ret , HTTPBadRequest ) ) <EOL> def test_register_not_allowed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . named_subpaths = { '<STR_LIT:username>' : '<STR_LIT:a>' } <EOL> self . access . allow_register = False <EOL> self . access . need_admin . return_value = False <EOL> ret = api . register ( self . request , '<STR_LIT:b>' ) <EOL> self . assertTrue ( isinstance ( ret , HTTPNotFound ) ) <EOL> def test_register ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . named_subpaths = { '<STR_LIT:username>' : '<STR_LIT:a>' } <EOL> self . access . need_admin . return_value = False <EOL> api . register ( self . request , '<STR_LIT:b>' ) <EOL> self . access . register . assert_called_with ( '<STR_LIT:a>' , '<STR_LIT:b>' ) <EOL> def test_register_set_admin ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . named_subpaths = { '<STR_LIT:username>' : '<STR_LIT:a>' } <EOL> self . access . need_admin . return_value = True <EOL> api . register ( self . request , '<STR_LIT:b>' ) <EOL> self . access . register . assert_called_with ( '<STR_LIT:a>' , '<STR_LIT:b>' ) <EOL> self . access . approve_user . assert_called_with ( '<STR_LIT:a>' ) <EOL> self . access . set_user_admin . assert_called_with ( '<STR_LIT:a>' , True ) <EOL> def test_change_password ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . userid = '<STR_LIT:u>' <EOL> api . change_password ( self . request , '<STR_LIT:a>' , '<STR_LIT:b>' ) <EOL> self . access . edit_user_password . assert_called_with ( '<STR_LIT:u>' , '<STR_LIT:b>' ) <EOL> def test_change_password_no_verify ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . userid = '<STR_LIT:u>' <EOL> self . access . verify_user . return_value = False <EOL> ret = api . change_password ( self . request , '<STR_LIT:a>' , '<STR_LIT:b>' ) <EOL> self . assertTrue ( isinstance ( ret , HTTPForbidden ) ) <EOL> self . access . verify_user . assert_called_with ( '<STR_LIT:u>' , '<STR_LIT:a>' ) <EOL> def test_download ( self ) : <EOL> """<STR_LIT>""" <EOL> db = self . request . db = MagicMock ( ) <EOL> context = MagicMock ( ) <EOL> ret = api . download_package ( context , self . request ) <EOL> db . fetch . assert_called_with ( context . filename ) <EOL> db . download_response . assert_called_with ( db . fetch ( ) ) <EOL> self . assertEqual ( ret , db . download_response ( ) ) <EOL> def test_download_fallback_no_cache ( self ) : <EOL> """<STR_LIT>""" <EOL> db = self . request . db = MagicMock ( ) <EOL> self . request . registry . fallback = '<STR_LIT:none>' <EOL> db . fetch . return_value = None <EOL> context = MagicMock ( ) <EOL> ret = api . download_package ( context , self . request ) <EOL> self . assertEqual ( ret . status_code , <NUM_LIT> ) <EOL> def test_download_fallback_cache_no_perm ( self ) : <EOL> """<STR_LIT>""" <EOL> db = self . request . db = MagicMock ( ) <EOL> self . request . registry . fallback = '<STR_LIT>' <EOL> self . request . access . can_update_cache . return_value = False <EOL> db . fetch . return_value = None <EOL> context = MagicMock ( ) <EOL> ret = api . download_package ( context , self . request ) <EOL> self . assertEqual ( ret , self . request . forbid ( ) ) <EOL> def test_download_fallback_cache_missing ( self ) : <EOL> """<STR_LIT>""" <EOL> db = self . request . db = MagicMock ( ) <EOL> locator = self . request . locator = MagicMock ( ) <EOL> self . request . registry . fallback = '<STR_LIT>' <EOL> self . request . registry . fallback_url = '<STR_LIT>' <EOL> self . request . access . can_update_cache . return_value = True <EOL> db . fetch . return_value = None <EOL> context = MagicMock ( ) <EOL> locator ( ) . get_project . return_value = { <EOL> context . filename : None , <EOL> '<STR_LIT>' : { } , <EOL> } <EOL> ret = api . download_package ( context , self . request ) <EOL> self . assertEqual ( ret . status_code , <NUM_LIT> ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_download_fallback_cache ( self , fetch_dist ) : <EOL> """<STR_LIT>""" <EOL> db = self . request . db = MagicMock ( ) <EOL> locator = self . request . locator = MagicMock ( ) <EOL> self . request . registry . fallback = '<STR_LIT>' <EOL> self . request . registry . fallback_url = '<STR_LIT>' <EOL> self . request . access . can_update_cache . return_value = True <EOL> db . fetch . return_value = None <EOL> fetch_dist . return_value = ( MagicMock ( ) , MagicMock ( ) ) <EOL> context = MagicMock ( ) <EOL> context . filename = '<STR_LIT>' <EOL> dist = MagicMock ( ) <EOL> url = '<STR_LIT>' % context . filename <EOL> locator . get_project . return_value = { <EOL> '<STR_LIT>' : dist , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : set ( [ url ] ) <EOL> } <EOL> } <EOL> ret = api . download_package ( context , self . request ) <EOL> fetch_dist . assert_called_with ( self . request , dist . name , url ) <EOL> self . assertEqual ( ret . body , fetch_dist ( ) [ <NUM_LIT:1> ] ) <EOL> def test_fetch_requirements_no_perm ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . access . can_update_cache . return_value = False <EOL> requirements = '<STR_LIT>' <EOL> ret = api . fetch_requirements ( self . request , requirements ) <EOL> self . assertEqual ( ret . status_code , <NUM_LIT> ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_fetch_requirements ( self , fetch_dist ) : <EOL> """<STR_LIT>""" <EOL> requirements = '<STR_LIT>' <EOL> locator = self . request . locator = MagicMock ( ) <EOL> ret = api . fetch_requirements ( self . request , requirements ) <EOL> dist = locator . locate ( ) <EOL> fetch_dist . assert_called_with ( self . request , dist . name , dist . source_url ) <EOL> self . assertEqual ( ret , { '<STR_LIT>' : [ fetch_dist ( ) [ <NUM_LIT:0> ] ] } ) </s>
