def test_is_named_route(self): app = App() app.bind('Request', self.request) app.bind('WebRoutes', [ get('/test/url', None).name('test.url'), get('/test/url/@id', None).name('test.id') ]) request = app.make('Request').load_app(app) request.path = '/test/url' assert request.is_named_route('test.url') request.path = '/test/url/1' assert request.is_named_route('test.id', {'id': 1})
def test_request_url_from_controller(self): app = App() app.bind('Request', self.request) app.bind('WebRoutes', [ get('/test/url', 'TestController@show').name('test.url'), get('/test/url/@id', 'ControllerTest@show').name('test.id'), get('/test/url/controller/@id', TestController.show).name('test.controller'), ]) request = app.make('Request').load_app(app) assert request.url_from_controller('TestController@show') == '/test/url' assert request.url_from_controller('ControllerTest@show', {'id': 1}) == '/test/url/1' assert request.url_from_controller(TestController.show, {'id': 1}) == '/test/url/controller/1'
def test_request_route_returns_url(self): app = App() app.bind('Request', self.request) app.bind('WebRoutes', [ get('/test/url', None).name('test.url'), get('/test/url/@id', None).name('test.id') ]) request = app.make('Request').load_app(app) assert request.route('test.url') == '/test/url' assert request.route('test.id', {'id': 1}) == '/test/url/1' assert request.route('test.id', [1]) == '/test/url/1' with pytest.raises(RouteException): assert request.route('not.exists', [1])
def test_confirm_controller_failure(self): self.auth = Auth(self.request, MockVerifyUser()) timestamp_plus_11 = datetime.datetime.now() - datetime.timedelta( minutes=11) params = { 'id': Sign().sign('{0}::{1}'.format(1, timestamp_plus_11.timestamp())) } self.request.set_params(params) user = self.auth.once().login_by_id(1) self.request.set_user(user) self.app.bind('Request', self.request) self.app.make('Request').load_app(self.app) # Create the route route = get('/email/verify/@id', ConfirmController.confirm_email) ConfirmController.get_user = self.get_user # Resolve the controller constructor controller = self.app.resolve(route.controller) # Resolve the method response = self.app.resolve( getattr(controller, route.controller_method)) self.reset_method() assert response.rendered_template == 'error'
def test_sets_request_params(self): self.app.make('Route').url = '/test/1' self.app.bind('WebRoutes', [get('/test/@id', ControllerTest.test)]) self.provider.boot(self.app.make('Route'), self.app.make('Request'), self.app.make(Response)) assert self.app.make('Request').param('id') == '1'
def test_provider_runs_through_routes(self): self.app.make('Route').url = '/test' self.app.bind('WebRoutes', [get('/test', ControllerTest.test)]) self.provider.boot(self.app.make('Route'), self.app.make('Request'), self.app.make(Response)) assert self.app.make('Request').header( 'Content-Type') == 'text/html; charset=utf-8'
def test_controller_that_return_a_view_with_trailing_slash(self): self.app.make('Route').url = '/view/' self.app.bind('WebRoutes', [get('/view/', ControllerTest.test)]) self.provider.boot(self.app.make('Route'), self.app.make('Request'), self.app.make(Response)) assert self.app.make('Response') == 'test' self.app.make('Route').url = '/view' self.app.bind('WebRoutes', [get('/view/', ControllerTest.test)]) print(self.app.providers) self.provider.boot(self.app.make('Route'), self.app.make('Request'), self.app.make(Response)) assert self.app.make('Response') == 'test'
def test_url_with_dots_finds_route(self): self.app.make('Route').url = '/test/user.endpoint' self.app.bind('WebRoutes', [get('/test/@endpoint', ControllerTest.test)]) self.provider.boot(self.app.make('Route'), self.app.make('Request'), self.app.make(Response)) assert self.app.make('Request').param('endpoint') == 'user.endpoint'
def test_controller_returns_json_response_for_dict(self): self.app.make('Route').url = '/view' self.app.bind('WebRoutes', [get('/view', ControllerTest.returns_a_dict)]) self.provider.boot(self.app.make('Route'), self.app.make('Request'), self.app.make(Response)) assert self.app.make('Request').header( 'Content-Type') == 'application/json; charset=utf-8'
def test_custom_route_compiler_returns_param(self): self.app.make('Route').url = '/test/1' self.app.make('Route').compile('signed', r'([\w.-]+)') self.app.bind('WebRoutes', [get('/test/@id:signed', ControllerTest.param)]) self.provider.boot(self.app.make('Route'), self.app.make('Request'), self.app.make(Response)) assert self.app.make('Response') == '1'
def test_controller_that_returns_a_view(self): self.app.make('Route').url = '/view' self.app.bind('WebRoutes', [get('/view', ControllerTest.test)]) self.provider.boot( self.app.make('Route'), self.app.make('Request'), self.app.make(Response) ) assert self.app.make('Response') == 'test' self.app.make('Route').url = '/view/' self.app.bind('WebRoutes', [get('/view', ControllerTest.test)]) self.provider.boot( self.app.make('Route'), self.app.make('Request'), self.app.make(Response) )
def test_route_runs_str_middleware(self): self.app.make('Route').url = '/view' self.app.bind('RouteMiddleware', middleware.ROUTE_MIDDLEWARE) self.app.bind( 'WebRoutes', [get('/view', ControllerTest.returns_a_dict).middleware('test')]) self.provider.boot(self.app.make('Route'), self.app.make('Request'), self.app.make(Response)) assert self.app.make('Request').path == 'test/middleware/before/ran'
def test_route_subdomain_ignores_routes(self): self.app.make('Route').url = '/test' self.app.make('Environ')['HTTP_HOST'] = 'subb.domain.com' self.app.bind('WebRoutes', [get('/test', ControllerTest.test)]) request = self.app.make('Request') request.activate_subdomains() self.provider.boot(self.app.make('Route'), self.app.make('Request'), self.app.make(Response)) assert self.app.make('Response') == 'Route not found. Error 404'
def test_request_redirection(self): app = App() app.bind('Request', self.request) app.bind('WebRoutes', [ get('/test/url', 'TestController@show').name('test.url'), get('/test/url/@id', 'TestController@testing').name('test.id'), get('/test/url/object', TestController.show).name('test.object') ]) request = app.make('Request').load_app(app) assert request.redirect('/test/url/@id', {'id': 1}).redirect_url == '/test/url/1' request.redirect_url = None assert request.redirect(name='test.url').redirect_url == '/test/url' request.redirect_url = None assert request.redirect(name='test.id', params={'id': 1}).redirect_url == '/test/url/1' request.redirect_url = None assert request.redirect(controller='TestController@show').redirect_url == '/test/url' request.redirect_url = None assert request.redirect(controller=TestController.show).redirect_url == '/test/url/object' request.redirect_url = None assert request.redirect('some/url').redirect_url == '/some/url'
def test_url_with_dashes_finds_route(self): self.app.make('Route').url = '/test/user-endpoint' self.app.bind('WebRoutes', [get('/test/@endpoint', ControllerTest.show)]) self.provider.boot( self.app.make('WebRoutes'), self.app.make('Route'), self.app.make('Request'), self.app.make('Environ'), self.app.make('Headers'), ) assert self.app.make('Request').param('endpoint') == 'user-endpoint'
def test_controller_does_not_return_with_non_matching_end_slash(self): self.app.make('Route').url = '/view' self.app.bind('WebRoutes', [get('/view/', Controller.returns_a_view)]) self.provider.boot( self.app.make('WebRoutes'), self.app.make('Route'), self.app.make('Request'), self.app.make('Environ'), self.app.make('Headers'), ) assert self.app.make('Response') == 'Route not found. Error 404'
def test_sets_request_params(self): self.app.make('Route').url = '/test/1' self.app.bind('WebRoutes', [get('/test/@id', Controller.show)]) self.provider.boot( self.app.make('WebRoutes'), self.app.make('Route'), self.app.make('Request'), self.app.make('Environ'), self.app.make('Headers'), ) assert self.app.make('Request').param('id') == '1'
def test_controller_that_returns_a_view(self): self.app.make('Route').url = '/view' self.app.bind('WebRoutes', [get('/view', Controller.returns_a_view)]) self.provider.boot( self.app.make('WebRoutes'), self.app.make('Route'), self.app.make('Request'), self.app.make('Environ'), self.app.make('Headers'), ) assert self.app.make('Response') == 'hey'
def test_object_controller_constructor_resolves_container(self): self.app.bind('Request', Request) # Create the route route = get('/url', ControllerTest.show) # Resolve the controller constructor controller = self.app.resolve(route.controller) # Resolve the method response = self.app.resolve( getattr(controller, route.controller_method)) self.assertIsInstance(route.controller, ControllerTest.__class__) self.assertEqual(route.controller_method, 'show') self.assertIsInstance(response, Request)
def test_string_controller_constructor_resolves_container(self): self.app.bind('Request', Request) # Create the route route = get('/url', 'ControllerTest@show') # Resolve the controller constructor controller = self.app.resolve(route.controller) # Resolve the method response = self.app.resolve(getattr(controller, route.controller_method)) assert isinstance(route.controller, ControllerTest.__class__) assert route.controller_method == 'show' assert isinstance(response, Request.__class__)
def test_route_runs_middleware_with_list(self): self.app.make('Route').url = '/view' self.app.bind('RouteMiddleware', middleware.ROUTE_MIDDLEWARE) self.app.bind('WebRoutes', [ get('/view', ControllerTest.returns_a_dict).middleware('middleware.test') ] ) self.provider.boot( self.app.make('WebRoutes'), self.app.make('Route'), self.app.make('Request'), self.app.make('Environ'), self.app.make('Headers'), ) assert self.app.make('Request').path == 'test/middleware/before/ran' assert self.app.make('Request').attribute == True
def test_get_sets_route(): assert routes.get('test', None) assert isinstance(routes.get('test', None), Get)
from config import application, providers from pydoc import locate from app.http.test_controllers.TestController import TestController from cgi import MiniFieldStorage from masonite.request import Request from masonite.app import App from masonite.routes import Get, Route from masonite.helpers.routes import flatten_routes, get, group from masonite.helpers.time import cookie_expire_time from masonite.testsuite.TestSuite import generate_wsgi, TestSuite WEB_ROUTES = flatten_routes([ get('/test', 'Controller@show').name('test'), group('/a', [ get('/account', 'Controller@show').name('a_account'), ]) ]) wsgi_request = generate_wsgi() class TestRequest: def setup_method(self): self.request = Request(wsgi_request).key( 'NCTpkICMlTXie5te9nJniMj9aVbPM6lsjeq5iDZ0dqY=') def test_request_is_callable(self): """ Request should be callable """ if callable(self.request): assert True
''' Web Routes ''' from masonite.helpers.routes import get, post, group ROUTES = [ get('/', 'HomeController@index').name('welcome'), get('/login', 'LoginController@show').name('login'), post('/login', 'LoginController@store'), get('/register', 'RegisterController@show'), post('/register', 'RegisterController@store'), get('/questions/@id', 'QuestionController@show'), # auth middleware get('/logout', 'LoginController@logout').middleware('auth'), get('/ask', 'QuestionController@create').middleware('auth'), group('/questions', [ post('', 'QuestionController@store').middleware('auth'), post('/@id/answers', 'AnswerController@store').middleware('auth'), get('/@id/upvote', 'QuestionController@upvote').middleware('auth'), get('/@id/downvote', 'QuestionController@downvote').middleware('auth'), ]), get('/me/questions', 'QuestionController@questions').middleware('auth'), get('/me/answers', 'AnswerController@answers').middleware('auth'), ]
''' Web Routes ''' from masonite.helpers.routes import get, post from masonite.routes import RouteGroup as group ROUTES = [ get('/', 'HomeController@index').name('home'), get('/login', 'LoginController@show').name('login'), post('/login', 'LoginController@store'), get('/register', 'RegisterController@show').name('register'), post('/register', 'RegisterController@store').name('register'), get('/questions/@id:int', 'QuestionController@show').name('questions.show'), get('/categories/@id:int/questions', 'CategoryController@index').name('categories.questions.index'), get('/users/@id:int', 'UserController@show').name('users.show'), group( [ get('/logout', 'LoginController@logout').name('logout'), # Question Routes group([ get('/create', 'QuestionController@create').name('create'), post('', 'QuestionController@store').name('store'), post('/@id/answers', 'AnswerController@store').name('answers'), post('/@id/answers/@answer_id/accept', 'QuestionController@accept_answer').name('accept'), get('/@id/upvote', 'QuestionController@upvote').name('upvote'), get('/@id/downvote', 'QuestionController@downvote').name('downvote'), ], prefix='/questions',