Skip to content

Commit

Permalink
Merge pull request #357 from MasoniteFramework/master
Browse files Browse the repository at this point in the history
Next Minor
  • Loading branch information
josephmancuso authored Jan 12, 2021
2 parents ad93677 + 9429b49 commit 2ac2062
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/masonite/drivers/authentication/AuthJwtDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def save(self, _, **kwargs):
serialized_dictionary = model.serialize()
serialized_dictionary.update({"expired": cookie_expire_time("5 minutes")})
token = self.jwt.encode(serialized_dictionary, KEY, algorithm="HS256")
token = bytes(token).decode("utf-8")
if isinstance(token, bytes):
token = bytes(token).decode("utf-8")
self.request.cookie("token", token)

def delete(self):
Expand Down
3 changes: 2 additions & 1 deletion src/masonite/helpers/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ def __new__(cls, *args):


def deprecated(message):
warnings.simplefilter("default", DeprecationWarning)

def deprecated_decorator(func):
def deprecated_func(*args, **kwargs):
warnings.warn(
"{} is a deprecated function. {}".format(func.__name__, message),
category=DeprecationWarning,
stacklevel=2,
)
warnings.simplefilter("default", DeprecationWarning)
return func(*args, **kwargs)

return deprecated_func
Expand Down
5 changes: 3 additions & 2 deletions src/masonite/providers/RouteProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def boot(self, router: Route, request: Request, response: Response):

"""No Response was found in the for loop so let's set an arbitrary response now.
"""
response.view("Route not found. Error 404", status=404)
# If the route exists but not the method is incorrect
if request.is_status(404) and request.route_exists(request.path):
if request.route_exists(request.path):
response.view("Method not allowed", status=405)
else:
response.view("Route not found. Error 404", status=404)
11 changes: 10 additions & 1 deletion src/masonite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,16 @@ def __getattr__(self, key):
raise AttributeError("class 'Request' has no attribute {}".format(key))

def with_errors(self, errors):
self.session.flash("errors", errors)
"""Easily attach errors message to session request."""
return self.with_flash("error", errors)

def with_success(self, success):
"""Easily attach success message to session request."""
return self.with_flash("success", success)

def with_flash(self, key, value):
"""Easily attach data to session request."""
self.session.flash(key, value)
return self

def reset_redirections(self):
Expand Down
19 changes: 18 additions & 1 deletion tests/core/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_can_redirect_with_inputs(self):

def test_can_redirect_with_bytes_inputs(self):
for driver in ('memory', 'cookie'):

request = self.container.make('Request')
session = self.container.make('SessionManager').driver(driver)
request.request_variables = {
Expand All @@ -167,3 +167,20 @@ def test_intended_returns_correct_url(self):
# Assert redirect intended method resets the redirection
request.redirect_intended()
self.assertEqual(request.session.get('__intend'), None)

def test_with_flash(self):
request = self.container.make('Request')
request.redirect('/dashboard').with_flash('success', 'Ok')
self.assertEqual(request.session.get('success'), 'Ok')
request.redirect('/dashboard').with_flash('any_key', 'any_value')
self.assertEqual(request.session.get('any_key'), 'any_value')

def test_with_errors(self):
request = self.container.make('Request')
request.redirect('/dashboard').with_errors('Form error')
self.assertEqual(request.session.get('error'), 'Form error')

def test_with_success(self):
request = self.container.make('Request')
request.redirect('/dashboard').with_success('Created !')
self.assertEqual(request.session.get('success'), 'Created !')

0 comments on commit 2ac2062

Please sign in to comment.