<s> import datetime <EOL> import pretend <EOL> import pytest <EOL> from webob . multidict import MultiDict <EOL> from pyramid . httpexceptions import HTTPNotFound <EOL> from warehouse import views <EOL> from warehouse . views import ( <EOL> forbidden , index , httpexception_view , robotstxt , current_user_indicator , <EOL> search , health <EOL> ) <EOL> from . . common . db . accounts import UserFactory <EOL> from . . common . db . classifiers import ClassifierFactory <EOL> from . . common . db . packaging import ( <EOL> ProjectFactory , ReleaseFactory , FileFactory , <EOL> ) <EOL> def test_httpexception_view ( ) : <EOL> response = context = pretend . stub ( ) <EOL> request = pretend . stub ( ) <EOL> assert httpexception_view ( context , request ) is response <EOL> class TestForbiddenView : <EOL> def test_logged_in_returns_exception ( self ) : <EOL> exc , request = pretend . stub ( ) , pretend . stub ( authenticated_userid = <NUM_LIT:1> ) <EOL> resp = forbidden ( exc , request ) <EOL> assert resp is exc <EOL> def test_logged_out_redirects_login ( self ) : <EOL> exc = pretend . stub ( ) <EOL> request = pretend . stub ( <EOL> authenticated_userid = None , <EOL> path_qs = "<STR_LIT>" , <EOL> route_url = pretend . call_recorder ( <EOL> lambda route , _query : "<STR_LIT>" <EOL> ) , <EOL> ) <EOL> resp = forbidden ( exc , request ) <EOL> assert resp . status_code == <NUM_LIT> <EOL> assert resp . headers [ "<STR_LIT>" ] == "<STR_LIT>" <EOL> def test_robotstxt ( pyramid_request ) : <EOL> assert robotstxt ( pyramid_request ) == { } <EOL> assert pyramid_request . response . content_type == "<STR_LIT>" <EOL> class TestIndex : <EOL> def test_index ( self , db_request ) : <EOL> project = ProjectFactory . create ( ) <EOL> release1 = ReleaseFactory . create ( project = project ) <EOL> release1 . created = datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> release2 = ReleaseFactory . create ( project = project ) <EOL> release2 . created = datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> FileFactory . create ( <EOL> release = release1 , <EOL> filename = "<STR_LIT>" . format ( project . name , release1 . version ) , <EOL> python_version = "<STR_LIT:source>" , <EOL> ) <EOL> UserFactory . create ( ) <EOL> assert index ( db_request ) == { <EOL> '<STR_LIT>' : [ release2 , release1 ] , <EOL> '<STR_LIT>' : [ release2 ] , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } <EOL> def test_esi_current_user_indicator ( ) : <EOL> assert current_user_indicator ( pretend . stub ( ) ) == { } <EOL> class TestSearch : <EOL> @ pytest . mark . parametrize ( "<STR_LIT>" , [ None , <NUM_LIT:1> , <NUM_LIT:5> ] ) <EOL> def test_with_a_query ( self , monkeypatch , db_request , page ) : <EOL> params = MultiDict ( { "<STR_LIT:q>" : "<STR_LIT>" } ) <EOL> if page is not None : <EOL> params [ "<STR_LIT>" ] = page <EOL> db_request . params = params <EOL> sort = pretend . stub ( ) <EOL> suggest = pretend . stub ( <EOL> sort = pretend . call_recorder ( lambda * a , ** kw : sort ) , <EOL> ) <EOL> es_query = pretend . stub ( <EOL> suggest = pretend . call_recorder ( lambda * a , ** kw : suggest ) , <EOL> ) <EOL> db_request . es = pretend . stub ( <EOL> query = pretend . call_recorder ( lambda * a , ** kw : es_query ) <EOL> ) <EOL> page_obj = pretend . stub ( page_count = ( page or <NUM_LIT:1> ) + <NUM_LIT:10> ) <EOL> page_cls = pretend . call_recorder ( lambda * a , ** kw : page_obj ) <EOL> monkeypatch . setattr ( views , "<STR_LIT>" , page_cls ) <EOL> url_maker = pretend . stub ( ) <EOL> url_maker_factory = pretend . call_recorder ( lambda request : url_maker ) <EOL> monkeypatch . setattr ( views , "<STR_LIT>" , url_maker_factory ) <EOL> assert search ( db_request ) == { <EOL> "<STR_LIT>" : page_obj , <EOL> "<STR_LIT>" : params . get ( "<STR_LIT:q>" , '<STR_LIT>' ) , <EOL> "<STR_LIT>" : params . get ( "<STR_LIT:o>" , '<STR_LIT>' ) , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] , <EOL> } <EOL> assert page_cls . calls == [ <EOL> pretend . call ( suggest , url_maker = url_maker , page = page or <NUM_LIT:1> ) , <EOL> ] <EOL> assert url_maker_factory . calls == [ pretend . call ( db_request ) ] <EOL> assert db_request . es . query . calls == [ <EOL> pretend . call ( <EOL> "<STR_LIT>" , <EOL> query = "<STR_LIT>" , <EOL> fields = [ <EOL> "<STR_LIT>" , "<STR_LIT:version>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:description>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> ) , <EOL> ] <EOL> assert es_query . suggest . calls == [ <EOL> pretend . call ( <EOL> name = "<STR_LIT>" , <EOL> term = { "<STR_LIT>" : "<STR_LIT:name>" } , <EOL> text = "<STR_LIT>" , <EOL> ) , <EOL> ] <EOL> @ pytest . mark . parametrize ( "<STR_LIT>" , [ None , <NUM_LIT:1> , <NUM_LIT:5> ] ) <EOL> def test_with_an_ordering ( self , monkeypatch , db_request , page ) : <EOL> params = MultiDict ( { "<STR_LIT:q>" : "<STR_LIT>" , "<STR_LIT:o>" : "<STR_LIT>" } ) <EOL> if page is not None : <EOL> params [ "<STR_LIT>" ] = page <EOL> db_request . params = params <EOL> sort = pretend . stub ( ) <EOL> suggest = pretend . stub ( <EOL> sort = pretend . call_recorder ( lambda * a , ** kw : sort ) , <EOL> ) <EOL> es_query = pretend . stub ( <EOL> suggest = pretend . call_recorder ( lambda * a , ** kw : suggest ) , <EOL> ) <EOL> db_request . es = pretend . stub ( <EOL> query = pretend . call_recorder ( lambda * a , ** kw : es_query ) <EOL> ) <EOL> page_obj = pretend . stub ( page_count = ( page or <NUM_LIT:1> ) + <NUM_LIT:10> ) <EOL> page_cls = pretend . call_recorder ( lambda * a , ** kw : page_obj ) <EOL> monkeypatch . setattr ( views , "<STR_LIT>" , page_cls ) <EOL> url_maker = pretend . stub ( ) <EOL> url_maker_factory = pretend . call_recorder ( lambda request : url_maker ) <EOL> monkeypatch . setattr ( views , "<STR_LIT>" , url_maker_factory ) <EOL> assert search ( db_request ) == { <EOL> "<STR_LIT>" : page_obj , <EOL> "<STR_LIT>" : params . get ( "<STR_LIT:q>" , '<STR_LIT>' ) , <EOL> "<STR_LIT>" : params . get ( "<STR_LIT:o>" , '<STR_LIT>' ) , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] , <EOL> } <EOL> assert page_cls . calls == [ <EOL> pretend . call ( sort , url_maker = url_maker , page = page or <NUM_LIT:1> ) , <EOL> ] <EOL> assert url_maker_factory . calls == [ pretend . call ( db_request ) ] <EOL> assert db_request . es . query . calls == [ <EOL> pretend . call ( <EOL> "<STR_LIT>" , <EOL> query = "<STR_LIT>" , <EOL> fields = [ <EOL> "<STR_LIT>" , "<STR_LIT:version>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:description>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> ) , <EOL> ] <EOL> assert es_query . suggest . calls == [ <EOL> pretend . call ( <EOL> name = "<STR_LIT>" , <EOL> term = { "<STR_LIT>" : "<STR_LIT:name>" } , <EOL> text = "<STR_LIT>" , <EOL> ) , <EOL> ] <EOL> assert suggest . sort . calls == [ <EOL> pretend . call ( "<STR_LIT>" ) <EOL> ] <EOL> @ pytest . mark . parametrize ( "<STR_LIT>" , [ None , <NUM_LIT:1> , <NUM_LIT:5> ] ) <EOL> def test_with_classifiers ( self , monkeypatch , db_request , page ) : <EOL> params = MultiDict ( [ <EOL> ( "<STR_LIT:q>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT:c>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT:c>" , "<STR_LIT>" ) , <EOL> ] ) <EOL> if page is not None : <EOL> params [ "<STR_LIT>" ] = page <EOL> db_request . params = params <EOL> es_query = pretend . stub ( <EOL> suggest = pretend . call_recorder ( lambda * a , ** kw : es_query ) , <EOL> filter = pretend . call_recorder ( lambda * a , ** kw : es_query ) , <EOL> sort = pretend . call_recorder ( lambda * a , ** kw : es_query ) , <EOL> ) <EOL> db_request . es = pretend . stub ( <EOL> query = pretend . call_recorder ( lambda * a , ** kw : es_query ) <EOL> ) <EOL> classifier1 = ClassifierFactory . create ( classifier = "<STR_LIT>" ) <EOL> classifier2 = ClassifierFactory . create ( classifier = "<STR_LIT>" ) <EOL> classifier3 = ClassifierFactory . create ( classifier = "<STR_LIT>" ) <EOL> page_obj = pretend . stub ( page_count = ( page or <NUM_LIT:1> ) + <NUM_LIT:10> ) <EOL> page_cls = pretend . call_recorder ( lambda * a , ** kw : page_obj ) <EOL> monkeypatch . setattr ( views , "<STR_LIT>" , page_cls ) <EOL> url_maker = pretend . stub ( ) <EOL> url_maker_factory = pretend . call_recorder ( lambda request : url_maker ) <EOL> monkeypatch . setattr ( views , "<STR_LIT>" , url_maker_factory ) <EOL> assert search ( db_request ) == { <EOL> "<STR_LIT>" : page_obj , <EOL> "<STR_LIT>" : params . get ( "<STR_LIT:q>" , '<STR_LIT>' ) , <EOL> "<STR_LIT>" : params . get ( "<STR_LIT:o>" , '<STR_LIT>' ) , <EOL> "<STR_LIT>" : params . getall ( "<STR_LIT:c>" ) , <EOL> "<STR_LIT>" : [ <EOL> ( '<STR_LIT>' , [ classifier3 . classifier ] ) , <EOL> ( '<STR_LIT:foo>' , [ <EOL> classifier1 . classifier , <EOL> classifier2 . classifier , <EOL> ] ) <EOL> ] , <EOL> } <EOL> assert page_cls . calls == [ <EOL> pretend . call ( es_query , url_maker = url_maker , page = page or <NUM_LIT:1> ) , <EOL> ] <EOL> assert url_maker_factory . calls == [ pretend . call ( db_request ) ] <EOL> assert db_request . es . query . calls == [ <EOL> pretend . call ( <EOL> "<STR_LIT>" , <EOL> query = "<STR_LIT>" , <EOL> fields = [ <EOL> "<STR_LIT>" , "<STR_LIT:version>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:description>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> ) , <EOL> ] <EOL> assert es_query . suggest . calls == [ <EOL> pretend . call ( <EOL> name = "<STR_LIT>" , <EOL> term = { "<STR_LIT>" : "<STR_LIT:name>" } , <EOL> text = "<STR_LIT>" , <EOL> ) , <EOL> ] <EOL> assert es_query . filter . calls == [ <EOL> pretend . call ( '<STR_LIT>' , classifiers = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> ] <EOL> @ pytest . mark . parametrize ( "<STR_LIT>" , [ None , <NUM_LIT:1> , <NUM_LIT:5> ] ) <EOL> def test_without_a_query ( self , monkeypatch , db_request , page ) : <EOL> params = MultiDict ( ) <EOL> if page is not None : <EOL> params [ "<STR_LIT>" ] = page <EOL> db_request . params = params <EOL> es_query = pretend . stub ( ) <EOL> db_request . es = pretend . stub ( query = lambda * a , ** kw : es_query ) <EOL> page_obj = pretend . stub ( page_count = ( page or <NUM_LIT:1> ) + <NUM_LIT:10> ) <EOL> page_cls = pretend . call_recorder ( lambda * a , ** kw : page_obj ) <EOL> monkeypatch . setattr ( views , "<STR_LIT>" , page_cls ) <EOL> url_maker = pretend . stub ( ) <EOL> url_maker_factory = pretend . call_recorder ( lambda request : url_maker ) <EOL> monkeypatch . setattr ( views , "<STR_LIT>" , url_maker_factory ) <EOL> assert search ( db_request ) == { <EOL> "<STR_LIT>" : page_obj , <EOL> "<STR_LIT>" : params . get ( "<STR_LIT:q>" , '<STR_LIT>' ) , <EOL> "<STR_LIT>" : params . get ( "<STR_LIT:o>" , '<STR_LIT>' ) , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] , <EOL> } <EOL> assert page_cls . calls == [ <EOL> pretend . call ( es_query , url_maker = url_maker , page = page or <NUM_LIT:1> ) , <EOL> ] <EOL> assert url_maker_factory . calls == [ pretend . call ( db_request ) ] <EOL> def test_raises_404_with_pagenum_too_high ( self , monkeypatch , db_request ) : <EOL> params = MultiDict ( { "<STR_LIT>" : <NUM_LIT:15> } ) <EOL> db_request . params = params <EOL> es_query = pretend . stub ( ) <EOL> db_request . es = pretend . stub ( query = lambda * a , ** kw : es_query ) <EOL> page_obj = pretend . stub ( page_count = <NUM_LIT:10> ) <EOL> page_cls = pretend . call_recorder ( lambda * a , ** kw : page_obj ) <EOL> monkeypatch . setattr ( views , "<STR_LIT>" , page_cls ) <EOL> url_maker = pretend . stub ( ) <EOL> url_maker_factory = pretend . call_recorder ( lambda request : url_maker ) <EOL> monkeypatch . setattr ( views , "<STR_LIT>" , url_maker_factory ) <EOL> with pytest . raises ( HTTPNotFound ) : <EOL> search ( db_request ) <EOL> assert page_cls . calls == [ <EOL> pretend . call ( es_query , url_maker = url_maker , page = <NUM_LIT:15> or <NUM_LIT:1> ) , <EOL> ] <EOL> assert url_maker_factory . calls == [ pretend . call ( db_request ) ] <EOL> def test_health ( ) : <EOL> request = pretend . stub ( <EOL> db = pretend . stub ( <EOL> execute = pretend . call_recorder ( lambda q : None ) , <EOL> ) , <EOL> ) <EOL> assert health ( request ) == "<STR_LIT:OK>" <EOL> assert request . db . execute . calls == [ pretend . call ( "<STR_LIT>" ) ] </s>
