Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Jun 23, 2019
2 parents 53e04c9 + d59045b commit 2355ded
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
6 changes: 5 additions & 1 deletion masonite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import re
from cgi import MiniFieldStorage
from http import cookies
from urllib.parse import parse_qsl
from urllib.parse import parse_qsl, urlencode

import tldextract
from cryptography.fernet import InvalidToken
Expand Down Expand Up @@ -649,6 +649,10 @@ def redirect(self, route=None, params={}, name=None, controller=None, status=302
self.status(status)
return self

def then_back(self, url=None):
self.redirect_url = self.redirect_url + '?' + urlencode({'back': url or self.path}, safe='/')
return self

def redirect_to(self, route_name, params={}, status=302):
"""Redirect to a named route.
Expand Down
17 changes: 16 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 @@ -198,3 +197,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())
14 changes: 13 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 Expand Up @@ -684,3 +683,16 @@ def test_request_gets_input_with_dotdict(self):
self.assertEqual(self.request.input('key.user'), '1')
self.assertEqual(self.request.input('key.nothing'), False)
self.assertEqual(self.request.input('key.nothing', default='test'), 'test')

def test_then_back(self):
self.request.redirect('/login')
self.assertEqual(self.request.redirect_url, '/login')

self.request.redirect('/login').then_back('/dashboard')

self.assertEqual(self.request.redirect_url, '/login?back=/dashboard')

self.request.path = '/dashboard'
self.request.redirect('/login').then_back()

self.assertEqual(self.request.redirect_url, '/login?back=/dashboard')
6 changes: 5 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,7 @@ 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')

0 comments on commit 2355ded

Please sign in to comment.