Skip to content

Commit

Permalink
Merge pull request #790 from MasoniteFramework/master
Browse files Browse the repository at this point in the history
Next Minor
  • Loading branch information
josephmancuso committed Jun 24, 2019
2 parents 8a10eeb + eb1f683 commit cab173c
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion masonite/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__title__ = 'masonite'
__description__ = 'The core for the Masonite framework'
__url__ = 'https://github.com/MasoniteFramework/masonite'
__version__ = '2.2.1'
__version__ = '2.2.2'
__author__ = 'Joseph Mancuso'
__author_email__ = 'joe@masoniteproject.com'
__licence__ = 'MIT'
4 changes: 4 additions & 0 deletions masonite/auth/Auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def login(self, name, password):
Returns:
object|bool -- Returns the current authenticated user object or False or None if there is none.
"""

if not isinstance(password, str):
raise TypeError("Cannot login with password '{}' of type: {}".format(password, type(password)))

auth_column = self.auth_model.__auth__

try:
Expand Down
3 changes: 3 additions & 0 deletions masonite/helpers/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ def create_matchurl(url, route):
string -- compiled regex string
"""

if route._compiled_regex is None:
route.compile_route_to_regex()

if not url.endswith('/'):
return route._compiled_regex
elif url == '/':
Expand Down
3 changes: 3 additions & 0 deletions masonite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ def _set_header(self, key, value, http_prefix):
self.environ[key] = str(value)
self._headers.update({key: str(value)})

def has_raw_header(self, key):
return key in self._headers

def get_headers(self):
"""Return all current headers to be set.
Expand Down
2 changes: 1 addition & 1 deletion masonite/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def make_headers(self, content_type="text/html; charset=utf-8"):
self.request.header('Content-Length', str(len(self.to_bytes())))

# If the user did not change it directly
if not self.request.header('Content-Type'):
if not self.request.has_raw_header('Content-Type'):
self.request.header('Content-Type', content_type)

def data(self):
Expand Down
15 changes: 10 additions & 5 deletions masonite/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,16 @@ def compile_route_to_regex(self):
regex += Route.route_compilers[regex_route.split(':')[
1]]
except KeyError:
raise InvalidRouteCompileException(
'Route compiler "{}" is not an available route compiler. '
'Verify you spelled it correctly or that you have added it using the compile() method.'.format(
regex_route.split(':')[1])
)
if hasattr(self, '_compiled_regex'):
raise InvalidRouteCompileException(
'Route compiler "{}" is not an available route compiler. '
'Verify you spelled it correctly or that you have added it using the compile() method.'.format(
regex_route.split(':')[1])
)
self._compiled_regex = None
self._compiled_regex_end = None
return

else:
regex += Route.route_compilers['default']

Expand Down
19 changes: 18 additions & 1 deletion masonite/testing/TestCase.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import io
import json
import subprocess
import unittest
from contextlib import contextmanager
from urllib.parse import urlencode
Expand Down Expand Up @@ -151,6 +150,8 @@ def delete(self, url, params={}):
return self.json('DELETE', url, params)

def actingAs(self, user):
if not user:
raise TypeError("Cannot act as a user of type: {}".format(type(user)))
self.acting_user = user
return self

Expand Down Expand Up @@ -198,3 +199,19 @@ def withCsrf(self):

def withoutCsrf(self):
self._with_csrf = False

def assertDatabaseHas(self, schema, value):
from config.database import DB

table = schema.split('.')[0]
column = schema.split('.')[1]

self.assertTrue(DB.table(table).where(column, value).first())

def assertDatabaseNotHas(self, schema, value):
from config.database import DB

table = schema.split('.')[0]
column = schema.split('.')[1]

self.assertFalse(DB.table(table).where(column, value).first())
6 changes: 6 additions & 0 deletions tests/core/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ def test_login_user(self):
self.assertTrue(self.request.get_cookie('token'))
self.assertEqual(self.auth.user().name, 'testuser123')

def test_login_with_no_password(self):
with self.assertRaises(TypeError):
for driver in ('cookie', 'jwt'):
self.auth.driver = driver
self.assertTrue(self.auth.login('nopassword@email.com', None))

# def test_can_login_with_second_password(self):
# self.auth.auth_model.__password__ = 'second_password'
# self.assertTrue(self.auth.login('user@email.com', 'pass123'))
Expand Down
1 change: 0 additions & 1 deletion tests/core/test_requests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import unittest
from cgi import MiniFieldStorage
from pydoc import locate

import pytest
from masonite.app import App
Expand Down
4 changes: 3 additions & 1 deletion tests/core/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from masonite.app import App
from masonite.routes import Get, Head, Post, Match, Put, Patch, Delete, Connect, Options, Trace, RouteGroup, Redirect
from masonite.helpers.routes import group, flatten_routes
from masonite.helpers.routes import create_matchurl
from masonite.testsuite.TestSuite import generate_wsgi
from masonite.exceptions import InvalidRouteCompileException, RouteException
from app.http.controllers.subdirectory.SubController import SubController
Expand Down Expand Up @@ -61,7 +62,8 @@ def test_route_can_add_compilers(self):
self.assertEqual(get_route.compile_route_to_regex(), r'^\/test\/[0-9]{4}\/$')

with self.assertRaises(InvalidRouteCompileException):
get_route = Get().route('test/@route:slug', None)
get_route = Get().route('test/@route:none', None)
create_matchurl('/test/1', get_route)

def test_route_gets_controllers(self):
self.assertTrue(Get().route('test/url', 'TestController@show'))
Expand Down
10 changes: 9 additions & 1 deletion tests/testing/test_route_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_json(self):
def test_json_response(self):
self.assertTrue(self.json('GET', '/unit/test/json/response').hasJson('count', 5))

def test_json_response(self):
def test_json_response_dictionary(self):
self.assertTrue(self.json('GET', '/unit/test/json/response').hasJson({
'count': 5
}))
Expand Down Expand Up @@ -95,3 +95,11 @@ def test_csrf(self):
self.withCsrf()
with self.assertRaises(InvalidCSRFToken):
self.assertTrue(self.post('/unit/test/json', {'test': 'testing'}).contains('testing'))

def test_database_has(self):
self.assertDatabaseHas('users.email', 'user@example.com')
self.assertDatabaseNotHas('users.email', 'joe@example.com')

def test_acting_as_none(self):
with self.assertRaises(TypeError):
self.actingAs(User.find(10)).get('/helloworld')

0 comments on commit cab173c

Please sign in to comment.