def test_set_login_view_without_blueprints(self): with self.app.app_context(): @self.app.route('/app_login') def app_login(): return 'Login Form Goes Here!' @self.app.route('/protected') @login_required def protected(): return u'Access Granted' set_login_view('app_login') with self.app.test_client() as c: result = c.get('/protected') self.assertEqual(result.status_code, 302) expected = 'http://localhost/app_login?next=%2Fprotected' self.assertEqual(result.location, expected)
def test_unauthorized_uses_blueprint_login_view(self): with self.app.app_context(): first = Blueprint('first', 'first') second = Blueprint('second', 'second') @self.app.route('/app_login') def app_login(): return 'Login Form Goes Here!' @self.app.route('/first_login') def first_login(): return 'Login Form Goes Here!' @self.app.route('/second_login') def second_login(): return 'Login Form Goes Here!' @self.app.route('/protected') @login_required def protected(): return u'Access Granted' @first.route('/protected') @login_required def first_protected(): return u'Access Granted' @second.route('/protected') @login_required def second_protected(): return u'Access Granted' self.app.register_blueprint(first, url_prefix='/first') self.app.register_blueprint(second, url_prefix='/second') set_login_view('app_login') set_login_view('first_login', blueprint=first) set_login_view('second_login', blueprint=second) with self.app.test_client() as c: result = c.get('/protected') self.assertEqual(result.status_code, 302) expected = ('http://localhost/' 'app_login?next=%2Fprotected') self.assertEqual(result.location, expected) result = c.get('/first/protected') self.assertEqual(result.status_code, 302) expected = ('http://localhost/' 'first_login?next=%2Ffirst%2Fprotected') self.assertEqual(result.location, expected) result = c.get('/second/protected') self.assertEqual(result.status_code, 302) expected = ('http://localhost/' 'second_login?next=%2Fsecond%2Fprotected') self.assertEqual(result.location, expected)