Skip to content

Commit

Permalink
Merge pull request #129 from MasoniteFramework/1.6-staging
Browse files Browse the repository at this point in the history
1.6.7 (Next Minor)
  • Loading branch information
josephmancuso committed May 16, 2018
2 parents cd1d411 + 65c7ce5 commit 05f1dda
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 152 deletions.
4 changes: 3 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ source = masonite
ignore_errors = True
omit =
*/tests/*
masonite/commands/*
masonite/commands/*
masonite/contracts/*
masonite/snippets/*
3 changes: 3 additions & 0 deletions masonite/contracts/SessionContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def has(self): pass

@abstractmethod
def all(self): pass

@abstractmethod
def delete(self): pass

@abstractmethod
def flash(self): pass
Expand Down
9 changes: 9 additions & 0 deletions masonite/drivers/SessionCookieDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def all(self):
"""

return self.__collect_data()

def delete(self, key):
data = self.__collect_data()

if self.request.get_cookie('s_{}'.format(key)):
self.request.delete_cookie('s_{}'.format(key))
return True

return False

def __collect_data(self):
"""
Expand Down
8 changes: 8 additions & 0 deletions masonite/drivers/SessionMemoryDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ def reset(self, flash_only=False):
if ip in self._session:
self._session[ip] = {}

def delete(self, key):
data = self.__collect_data()

if key in data:
del data[key]
return True

return False
def __get_client_address(self):
"""
Get ip from the client
Expand Down
33 changes: 28 additions & 5 deletions masonite/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,40 @@ def render(self, template, dictionary={}):
if self.container.has('Cache') and self.__cached_template_exists() and not self.__is_expired_cache():
return self.__get_cached_template()

if template in self.composers:
self.dictionary.update(self.composers[template])

if '*' in self.composers:
self.dictionary.update(self.composers['*'])
# Check if composers are even set for a speed improvement
if self.composers:
self._update_from_composers()

self.rendered_template = self.env.get_template(filename).render(
self.dictionary)

return self

def _update_from_composers(self):

# Check if the template is directly specified in the composer
if self.template in self.composers:
self.dictionary.update(self.composers[self.template])

# Check if there is just an astericks in the composer
if '*' in self.composers:
self.dictionary.update(self.composers['*'])

# We will append onto this string for an easier way to search through wildcard routes
compiled_string = ''

# Check for wildcard view composers
for template in self.template.split('/'):
# Append the template onto the compiled_string
compiled_string += template
if '{}*'.format(compiled_string) in self.composers:
self.dictionary.update(self.composers['{}*'.format(compiled_string)])
else:
# Add a slash to symbolize going into a deeper directory structure
compiled_string += '/'



def composer(self, composer_name, dictionary):
"""
Updates composer dictionary
Expand Down
1 change: 1 addition & 0 deletions resources/templates/mail/welcome.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
to: {{ to }}
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'masonite.contracts',
'masonite.helpers',
],
version='1.6.6',
version='1.6.7',
install_requires=[
'validator.py==1.2.5',
'cryptography==2.2.2',
Expand Down
154 changes: 154 additions & 0 deletions tests/providers/test_route_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
from masonite.app import App
from masonite.routes import Route
from masonite.request import Request
from masonite.providers.RouteProvider import RouteProvider
from masonite.view import View
from masonite.helpers.routes import get


class TestRouteProvider:

def setup_method(self):
self.app = App()
WSGI_REQUEST = {
'wsgi.version': (1, 0),
'wsgi.multithread': False,
'wsgi.multiprocess': True,
'wsgi.run_once': False,
'SERVER_SOFTWARE': 'gunicorn/19.7.1',
'REQUEST_METHOD': 'GET',
'QUERY_STRING': 'application=Masonite',
'RAW_URI': '/',
'SERVER_PROTOCOL': 'HTTP/1.1',
'HTTP_HOST': '127.0.0.1:8000',
'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_UPGRADE_INSECURE_REQUESTS': '1',
'HTTP_COOKIE': 'setcookie=value',
'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7',
'HTTP_ACCEPT_LANGUAGE': 'en-us',
'HTTP_ACCEPT_ENCODING': 'gzip, deflate',
'HTTP_CONNECTION': 'keep-alive',
'wsgi.url_scheme': 'http',
'REMOTE_ADDR': '127.0.0.1',
'REMOTE_PORT': '62241',
'SERVER_NAME': '127.0.0.1',
'SERVER_PORT': '8000',
'PATH_INFO': '/',
'SCRIPT_NAME': ''
}
self.app.bind('Environ', WSGI_REQUEST)
self.app.bind('WebRoutes', [])
self.app.bind('Route', Route(self.app.make('Environ')))
self.app.bind('Request', Request(self.app.make('Environ')).load_app(self.app))
self.app.bind('Headers', [])
self.app.bind('HttpMiddleware', [])
view = View(self.app)
self.app.bind('View', view.render)
self.provider = RouteProvider()
self.provider.app = self.app


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_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_provider_runs_through_routes(self):
self.app.make('Route').url = '/test'
self.app.bind('WebRoutes', [get('/test', 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').header('Content-Type') == 'text/html; charset=utf-8'


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_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', 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('Response') == 'Route not found. Error 404'

def test_controller_returns_json_response_for_dict(self):
self.app.make('Route').url = '/view'
self.app.bind('WebRoutes', [get('/view', Controller.returns_a_dict)])

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') == '{"id": 1}'
assert self.app.make('Request').header('Content-Type') == 'application/json; charset=utf-8'

class Controller:

def show():
return 'test'

def returns_a_view(View):
return View('index')

def returns_a_dict():
return {'id': 1}

class Middleware:

def before(): pass

def after(): pass
9 changes: 9 additions & 0 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,12 @@ def test_reset_session():
SESSION.set('flash_', 'test_pep')
SESSION.reset()
assert SESSION.get('reset_username') is None

def test_delete_session():
for driver in ('memory', 'cookie'):
SESSION = container.make('SessionManager').driver(driver)
SESSION.set('test1', 'value')
SESSION.set('test2', 'value')
assert SESSION.delete('test1')
assert SESSION.has('test1') is False
assert SESSION.delete('test1') is False
Loading

0 comments on commit 05f1dda

Please sign in to comment.