def test_request_loads_app(self): app = App() app.bind('Request', self.request) app.make('Request').load_app(app) self.assertEqual(self.request.app(), app) self.assertEqual(app.make('Request').app(), app)
def test_request_loads_app(self): app = App() app.bind('Request', self.request) app.make('Request').load_app(app) assert self.request.app() == app assert app.make('Request').app() == app
class TestS3Upload: def setup_method(self): self.app = App() self.app.bind('Application', application) self.app.bind('StorageConfig', storage) self.app.bind('UploadDiskDriver', UploadDiskDriver) self.app.bind('UploadManager', UploadManager(self.app)) self.app.bind('Upload', UploadManager(self.app)) self.app.bind('UploadS3Driver', UploadS3Driver) def test_upload_file_for_s3(self): assert self.app.make('Upload').driver('s3').store( ImageMock()) == 'test.jpg' def test_upload_manage_accept_files(self): """ This test is responsible for checking if you upload a file correctly with a valid extension. """ assert UploadManager(self.app).driver('s3').accept( 'jpg', 'png').store(ImageMock()) def test_upload_manage_accept_files_error(self): """ This test should return an error because it is an invalid extension. """ with pytest.raises(FileTypeException): UploadManager(self.app).driver('s3').accept('png').store( ImageMock()) def test_upload_store_prepend(self): assert self.app.make('UploadManager').driver('s3').store_prepend( ImageMock(), 'hey') == 'heytest.jpg'
class TestServiceProvider: def setup_method(self): self.app = App() self.provider = ServiceProvider() self.provider.load_app(self.app).boot() def test_service_provider_loads_app(self): assert self.provider.app == self.app def test_can_call_container_with_self_parameter(self): self.app.bind('Request', object) self.app.bind('Get', object) assert self.app.resolve(ContainerTest().boot) == self.app.make('Request') def test_can_call_container_with_annotations_from_variable(self): request = Request(generate_wsgi()) self.app.bind('Request', request) self.app.bind('Get', Get().route('url', None)) assert self.app.resolve(ContainerTest().testboot) == self.app.make('Request') def test_can_call_container_with_annotation_with_self_parameter(self): self.app.bind('Request', Request) self.app.bind('Get', Get().route('url', None)) assert self.app.resolve(ContainerTest().testboot) == self.app.make('Request')
def test_cache_sets_times(): container = App() container.bind('CacheConfig', cache) container.bind('CacheDiskDriver', CacheDiskDriver) container.bind('CacheManager', CacheManager(container)) container.bind('Application', container) container.bind('Cache', container.make('CacheManager').driver('disk')) cache_driver = container.make('Cache') cache_driver.store_for('key_for_1_minute', 'value', 1, 'minute') cache_driver.store_for('key_for_1_hour', 'value', 1, 'hour') cache_driver.store_for('key_for_1_day', 'value', 1, 'day') cache_driver.store_for('key_for_1_month', 'value', 1, 'month') cache_driver.store_for('key_for_1_year', 'value', 1, 'year') assert cache_driver.is_valid('key_for_1_minute') assert cache_driver.is_valid('key_for_1_hour') assert cache_driver.is_valid('key_for_1_day') assert cache_driver.is_valid('key_for_1_month') assert cache_driver.is_valid('key_for_1_year') assert cache_driver.get('key_for_1_minute') == 'value' assert cache_driver.get('key_for_1_hour') == 'value' assert cache_driver.get('key_for_1_day') == 'value' assert cache_driver.get('key_for_1_month') == 'value' assert cache_driver.get('key_for_1_year') == 'value' for cache_file in glob.glob('bootstrap/cache/key*'): os.remove(cache_file)
class TestAsyncDriver: def setup_method(self): self.app = App() self.app.bind('QueueAsyncDriver', QueueAsyncDriver) self.app.bind('QueueAmqpDriver', QueueAmqpDriver) self.app.bind('QueueConfig', queue) self.app.bind('Queueable', Queueable) self.app.bind('Container', self.app) self.app.bind('QueueManager', QueueManager(self.app)) self.drivers = ['async'] if env('RUN_AMQP'): self.drivers.append('amqp') def test_async_driver_pushes_to_queue(self): for driver in self.drivers: assert self.app.make('QueueManager').driver(driver).push( Job) is None def test_async_driver_can_run_any_callback_method(self): for driver in self.drivers: assert self.app.make('QueueManager').driver(driver).push( Random, callback="send") is None def test_async_driver_can_run_any_method(self): for driver in self.drivers: assert self.app.make('QueueManager').driver(driver).push( Random().send) is None
def create(self): from masonite.app import App from config import providers """ |-------------------------------------------------------------------------- | Instantiate Container And Perform Important Bindings |-------------------------------------------------------------------------- | | Some Service providers need important bindings like the WSGI application | and the application configuration file before they boot. | """ container = App() container.bind('WSGI', self.app) # container.bind('Application', application) container.bind('Container', container) container.bind('Providers', []) container.bind('WSGIProviders', []) """ |-------------------------------------------------------------------------- | Bind all service providers |-------------------------------------------------------------------------- | | Let's register everything into the Service Container. Once everything is | in the container we can run through all the boot methods. For reasons | some providers don't need to execute with every request and should | only run once when the server is started. Providers will be ran | once if the wsgi attribute on a provider is False. | """ for provider in providers.PROVIDERS: located_provider = provider() located_provider.load_app(container).register() if located_provider.wsgi: container.make('WSGIProviders').append(located_provider) else: container.make('Providers').append(located_provider) for provider in container.make('Providers'): container.resolve(provider.boot) """ |-------------------------------------------------------------------------- | Get the application from the container |-------------------------------------------------------------------------- | | Some providers may change the WSGI Server like wrapping the WSGI server | in a Whitenoise container for an example. Let's get a WSGI instance | from the container and pass it to the application variable. This | will allow WSGI servers to pick it up from the command line | """ return container
def test_request_loads_app(): app = App() app.bind('Request', REQUEST) app.make('Request').load_app(app) assert REQUEST.app() == app assert app.make('Request').app() == app
def create_container(self): container = App() container.bind('WSGI', object) container.bind('Application', application) container.bind('Providers', providers) """ |-------------------------------------------------------------------------- | Bind all service providers |-------------------------------------------------------------------------- | | Let's register everything into the Service Container. Once everything is | in the container we can run through all the boot methods. For reasons | some providers don't need to execute with every request and should | only run once when the server is started. Providers will be ran | once if the wsgi attribute on a provider is False. | """ for provider in container.make('Providers').PROVIDERS: provider().load_app(container).register() for provider in container.make('Providers').PROVIDERS: located_provider = provider().load_app(container) if located_provider.wsgi is False: container.resolve(located_provider.boot) """ |-------------------------------------------------------------------------- | Startup the Service Container |-------------------------------------------------------------------------- | | Instantiate the Service Container so we can bind classes into it and | bind the environ variable that is created by the WSGI server into | the container. | """ container.bind('Environ', generate_wsgi()) """ |-------------------------------------------------------------------------- | Execute All Service Providers |-------------------------------------------------------------------------- | | Run all service provider boot methods if the wsgi attribute is true. | """ for provider in container.make('Providers').PROVIDERS: located_provider = provider().load_app(container) container.bind('Response', 'test') if located_provider.wsgi is True: container.resolve(located_provider.boot) self.container = container return self
def __init__(self, app: App): """Upload Disk Driver Constructor. Arguments: StorageConfig {config.storage} -- Storage configuration. Application {masonite.app.App} -- The application container. """ self.config = app.make('StorageConfig') self.appconfig = app.make('Application')
class TestNotifiable: def setup_method(self): self.app = App() self.app.bind('Container', self.app) self.app.bind('ViewClass', View(self.app)) # self.app.make('ViewClass').add_environment('notifications/snippets') self.app.bind('View', View(self.app).render) self.app.bind('MailConfig', MockMailConfig) self.app.bind('MailTerminalDriver', MailTerminalDriver) self.app.bind('MailMailgunDriver', MailMailgunDriver) self.app.bind('MailManager', MailManager(self.app)) self.app.bind( 'Mail', self.app.make('MailManager').driver(MockMailConfig.DRIVER)) # Setup and test Queueing self.app.bind('QueueAsyncDriver', QueueAsyncDriver) self.app.bind('QueueConfig', queue) self.app.bind('Container', self.app) self.app.bind('QueueManager', QueueManager(self.app)) self.app.swap(Queue, self.app.make('QueueManager').driver('async')) self.notification = WelcomeNotification self.notify = Notify(self.app) def test_notification_sends_mail(self): assert self.notify.mail(WelcomeNotification, to='*****@*****.**') is None def test_notification_sends_slack(self): assert self.notify.slack(WelcomeNotification) is None def test_notify_returns_called_notification(self): self.notify.mail(WelcomeNotification) assert self.notify.called_notifications def test_notification_sets_protected_members(self): assert self.notify.mail(WelcomeNotification, to='*****@*****.**') is None assert self.notify.called_notifications[0]._to == '*****@*****.**' def test_mail_notification_appends_template(self): assert self.notify.mail(WelcomeNotification, to='*****@*****.**') is None assert 'We greatly value your service!' in self.notify.called_notifications[ 0].template def test_mail_notification_should_queue(self): assert self.notify.mail(ShouldQueueWelcomeNotification, to='*****@*****.**') is None def test_can_send_with_via_method(self): notifications = self.notify.via('mail').send( ShouldQueueWelcomeNotification, to="*****@*****.**").called_notifications assert isinstance(notifications[0], ShouldQueueWelcomeNotification)
def __init__(self, app: App): """Cache disk driver constructor. Arguments: CacheConfig {config.cache} -- Cache configuration module. Application {config.application} -- Application configuration module. """ self.config = app.make('CacheConfig') self.appconfig = app.make('Application') self.cache_forever = None
def test_render_from_container_as_view_class(): container = App() ViewClass = View(container) container.bind('ViewClass', ViewClass) container.bind('View', ViewClass.render) container.make('ViewClass').share({'test': 'test'}) view = container.make('View') assert view('test').rendered_template == 'test'
def test_composers_load_all_views_with_astericks(): container = App() ViewClass = View(container) container.bind('ViewClass', ViewClass) container.bind('View', ViewClass.render) container.make('ViewClass').composer('*', {'test': 'test'}) assert container.make('ViewClass').composers == {'*': {'test': 'test'}} view = container.make('View') assert view('test').rendered_template == 'test'
def test_composers(): container = App() ViewClass = View(container) container.bind('ViewClass', ViewClass) container.bind('View', ViewClass.render) container.make('ViewClass').composer('test', {'test': 'test'}) assert container.make('ViewClass').composers == {'test': {'test': 'test'}} view = container.make('View') assert view('test').rendered_template == 'test'
def test_upload_manager_switches_drivers(): container = App() container.bind('Test', object) container.bind('StorageConfig', storage) container.bind('UploadDiskDriver', UploadDiskDriver) container.bind('UploadTestDriver', object) container.bind('Application', application) container.bind('UploadManager', UploadManager(container)) assert isinstance( container.make('UploadManager').driver('disk'), UploadDiskDriver) assert isinstance(container.make('UploadManager').driver('test'), object)
class TestMiddleware: def setup_method(self): self.app = App() self.app.bind('Environ', generate_wsgi()) self.app.bind('Application', application) self.app.make('Environ') self.app.bind('StatusCode', '404 Not Found Error') self.app.bind('Request', Request(self.app.make('Environ')).load_app(self.app)) self.app.simple(Response(self.app)) self.app.bind('Csrf', Csrf(self.app.make('Request'))) self.app.bind('Route', Route(self.app.make('Environ'))) self.app.bind('ViewClass', View(self.app)) self.app.bind( 'WebRoutes', [Get().route('/', 'TestController@show').middleware('test')]) self.app.bind('HttpMiddleware', [MiddlewareHttpTest]) self.app.bind('RouteMiddleware', {'test': MiddlewareTest}) self.provider = RouteProvider() self.provider.app = self.app def test_route_middleware_runs(self): self.app.resolve(self.provider.boot) assert self.app.make('Request').path == '/test/middleware' def test_http_middleware_runs(self): self.app.resolve(self.provider.boot) assert self.app.make('Request').path == '/test/middleware' assert self.app.make('Request').environ['HTTP_TEST'] == 'test'
class TestS3Upload(unittest.TestCase): def setUp(self): self.app = App() self.app.bind('Container', self.app) self.app.bind('Application', application) self.app.bind('StorageConfig', storage) self.app.bind('UploadDiskDriver', UploadDiskDriver) self.app.bind('UploadManager', UploadManager(self.app)) self.app.bind('Upload', UploadManager(self.app)) self.app.bind('UploadS3Driver', UploadS3Driver) def test_upload_file_for_s3(self): self.assertEqual( len(self.app.make('Upload').driver('s3').store(ImageMock())), 29) def test_upload_open_file_for_s3(self): self.assertTrue( self.app.make('Upload').driver('s3').accept('yml').store( open('.travis.yml'))) def test_upload_manage_accept_files(self): """ This test is responsible for checking if you upload a file correctly with a valid extension. """ self.assertTrue( UploadManager(self.app).driver('s3').accept( 'jpg', 'png').store(ImageMock())) def test_upload_manage_accept_files_error(self): """ This test should return an error because it is an invalid extension. """ with self.assertRaises(FileTypeException): UploadManager(self.app).driver('s3').accept('png').store( ImageMock()) def test_upload_with_new_filename(self): self.assertEqual( self.app.make('UploadManager').driver('s3').store( ImageMock(), filename='newname.jpg'), 'newname.jpg') def test_upload_with_new_filename_and_location_in_s3(self): self.assertEqual( self.app.make('UploadManager').driver('s3').store( ImageMock(), filename='newname.jpg', location='3/2'), 'newname.jpg')
def test_service_provider_sets_on_app_object(): app = App() provider = ServiceProvider() provider.load_app(app).register() assert 'Request' in app.providers assert app.make('Request') == object
def test_cache_throws_exception_with_incorrect_cache_type(): container = App() view = View(container) container.bind('CacheConfig', cache) container.bind('CacheDiskDriver', CacheDiskDriver) container.bind('CacheManager', CacheManager(container)) container.bind('Application', container) container.bind('Cache', container.make('CacheManager').driver('disk')) container.bind('View', view.render) view = container.make('View') with pytest.raises(ValueError): view('test_exception', {'test': 'test'}).cache_for(1, 'monthss')
def test_maintenance_mode_middleware_is_not_down(self): app = App() app.bind('Request', self.request) app.bind('StatusCode', '200 OK') request = app.make('Request').load_app(app) self.middleware.before() assert request.get_status_code() == '200 OK'
def test_request_sets_int_status_code(self): app = App() app.bind('Request', self.request) request = app.make('Request').load_app(app) request.status(500) assert request.get_status_code() == '500 Internal Server Error'
def test_can_call_container_with_self_parameter(): app = App() app.bind('Request', object) app.bind('Get', object) assert app.resolve(ContainerTest().boot) == app.make('Request')
def test_request_sets_invalid_int_status_code(self): with self.assertRaises(InvalidHTTPStatusCode): app = App() app.bind('Request', self.request) request = app.make('Request').load_app(app) request.status(600)
def test_request_gets_int_status(self): app = App() app.bind('Request', self.request) request = app.make('Request').load_app(app) request.status(500) self.assertEqual(request.get_status(), 500)
def test_can_call_container_with_annotation_with_self_parameter(): app = App() app.bind('Request', Request) app.bind('Get', Get().route('url', None)) assert app.resolve(ContainerTest().testboot) == app.make('Request')
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) self.assertEqual( request.redirect('/test/url/@id', { 'id': 1 }).redirect_url, '/test/url/1') request.redirect_url = None self.assertEqual( request.redirect(name='test.url').redirect_url, '/test/url') request.redirect_url = None self.assertEqual( request.redirect(name='test.id', params={ 'id': 1 }).redirect_url, '/test/url/1') request.redirect_url = None self.assertEqual( request.redirect(controller='TestController@show').redirect_url, '/test/url') request.redirect_url = None self.assertEqual( request.redirect(controller=TestController.show).redirect_url, '/test/url/object') request.redirect_url = None self.assertEqual( request.redirect('some/url').redirect_url, '/some/url')
def test_request_sets_status_code(): app = App() app.bind('Request', REQUEST) request = app.make('Request').load_app(app) request.status('200 OK') assert request.get_status_code() == '200 OK'
def test_request_gets_all_headers(self): app = App() app.bind('Request', Request(wsgi_request)) request = app.make('Request').load_app(app) request.header('TEST1', 'set_this_item') self.assertEqual(request.get_headers(), [('TEST1', 'set_this_item')])
def test_is_status_code(self): app = App() app.bind('Request', self.request) request = app.make('Request').load_app(app) request.status(500) self.assertEqual(request.is_status(500), True)
def __init__(self, Request, app:App): ''' Inject Any Dependencies From The Service Container ''' self.request = Request self.response = app.make('Response')
container.bind('WSGIProviders', []) """ |-------------------------------------------------------------------------- | Bind all service providers |-------------------------------------------------------------------------- | | Let's register everything into the Service Container. Once everything is | in the container we can run through all the boot methods. For reasons | some providers don't need to execute with every request and should | only run once when the server is started. Providers will be ran | once if the wsgi attribute on a provider is False. | """ for provider in container.make('ProvidersConfig').PROVIDERS: located_provider = provider() located_provider.load_app(container).register() if located_provider.wsgi: container.make('WSGIProviders').append(located_provider) else: container.make('Providers').append(located_provider) for provider in container.make('Providers'): container.resolve(provider.boot) """ |-------------------------------------------------------------------------- | Get the application from the container |-------------------------------------------------------------------------- |