diff --git a/masonite/__version__.py b/masonite/__version__.py index 1570ef557..01187fb8b 100644 --- a/masonite/__version__.py +++ b/masonite/__version__.py @@ -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' diff --git a/masonite/auth/Auth.py b/masonite/auth/Auth.py index 8cbefbdd2..d16aa766b 100644 --- a/masonite/auth/Auth.py +++ b/masonite/auth/Auth.py @@ -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: diff --git a/masonite/helpers/routes.py b/masonite/helpers/routes.py index cfdbd8056..ae77a525c 100644 --- a/masonite/helpers/routes.py +++ b/masonite/helpers/routes.py @@ -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 == '/': diff --git a/masonite/request.py b/masonite/request.py index 9bb91f9a9..d985ee63e 100644 --- a/masonite/request.py +++ b/masonite/request.py @@ -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. diff --git a/masonite/response.py b/masonite/response.py index c094f4ed7..f7fb13916 100644 --- a/masonite/response.py +++ b/masonite/response.py @@ -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): diff --git a/masonite/routes.py b/masonite/routes.py index d5c0b344e..6a3ced529 100644 --- a/masonite/routes.py +++ b/masonite/routes.py @@ -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'] diff --git a/masonite/testing/TestCase.py b/masonite/testing/TestCase.py index 74c112409..aaa965464 100644 --- a/masonite/testing/TestCase.py +++ b/masonite/testing/TestCase.py @@ -1,6 +1,5 @@ import io import json -import subprocess import unittest from contextlib import contextmanager from urllib.parse import urlencode @@ -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 @@ -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()) diff --git a/tests/core/test_auth.py b/tests/core/test_auth.py index 09d17f46f..24983e1a5 100644 --- a/tests/core/test_auth.py +++ b/tests/core/test_auth.py @@ -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')) diff --git a/tests/core/test_requests.py b/tests/core/test_requests.py index 2f7d8d29e..acb1a2d97 100644 --- a/tests/core/test_requests.py +++ b/tests/core/test_requests.py @@ -1,6 +1,5 @@ import unittest from cgi import MiniFieldStorage -from pydoc import locate import pytest from masonite.app import App diff --git a/tests/core/test_routes.py b/tests/core/test_routes.py index 908a98a7c..edde9d554 100644 --- a/tests/core/test_routes.py +++ b/tests/core/test_routes.py @@ -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 @@ -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')) diff --git a/tests/testing/test_route_tests.py b/tests/testing/test_route_tests.py index 2ce5638c7..a3077be59 100644 --- a/tests/testing/test_route_tests.py +++ b/tests/testing/test_route_tests.py @@ -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 })) @@ -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')