Skip to content

Commit

Permalink
Merge branch 'navbar_and_log_out' of github.com:classrank/ClassRank i…
Browse files Browse the repository at this point in the history
…nto navbar_and_log_out

Conflicts:
	config.json.example
  • Loading branch information
joshuamorton committed Feb 13, 2016
2 parents dea5913 + e64d68e commit 96ad76f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 26 deletions.
8 changes: 6 additions & 2 deletions classrank/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ class BaseHandler(tornado.web.RequestHandler):

def initialize(self):
self.db = self.application.db
self.pages = self.application.settings['pages']

def render(self, template_name, **kwargs):
"""
binds certain global settings so that they are always passed into the template
"""
kwargs['pages'] = self.pages
if(self.get_current_user()):
pages = self.application.settings['logged_in_pages']
else:
pages = self.application.settings['logged_out_pages']

kwargs['pages'] = pages
return super(BaseHandler, self).render(template_name, **kwargs)

def get_current_user(self):
Expand Down
12 changes: 12 additions & 0 deletions classrank/handlers/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tornado.escape
from tornado.web import authenticated

from classrank.database.wrapper import Query, NoResultFound, IntegrityError
from . import BaseHandler
Expand Down Expand Up @@ -59,3 +60,14 @@ def authorize(self, user):
self.set_secure_cookie("user", tornado.escape.json_encode(user))
else:
self.clear_cookie("user")

class LogoutHandler(BaseHandler):
@authenticated
def get(self):
self.clear_cookie("user")
return self.redirect("/")

@authenticated
def post(self):
self.clear_cookie("user")
return self.redirect("/")
1 change: 1 addition & 0 deletions classrank/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
routes = [
(r'/', splash.SplashHandler),
(r'/login/?', auth.LoginHandler),
(r'/logout/?', auth.LogoutHandler),
(r'/register/?', auth.RegistrationHandler),
(r'/welcome/?', welcome.WelcomeHandler),
]
7 changes: 1 addition & 6 deletions classrank/templates/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
<a class="navbar-brand" href="/">ClassRank</a>
<div class="nav navbar-nav">
{% for item in pages %}
<a class="nav-item nav-link" href="#">{{item}}</a>
{% end %}
{% if current_user %}
<a class="nav-item nav-link" href="#">Logout</a>
{% else %}
<a class="nav-item nav-link" href="#">Login</a>
<a class="nav-item nav-link" href="/{{item}}">{{item}}</a>
{% end %}
</div>
</nav>
5 changes: 3 additions & 2 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"autoreload": true,
"cookie_secret": "This is actually a secret cookie!986425&(!@",
"pages": ["Dashboard", "Search", "Rate", "Privacy", "Settings"],
"login_url": "/login",
"logged_in_pages": ["dashboard", "search", "rate", "privacy", "settings", "logout"],
"logged_out_pages": ["login", "register"],
"login_url": "/login"
"db_config": {
"school": [
{"name": "Georgia Institute of Technology", "abbreviation": "gatech"}
Expand Down
36 changes: 26 additions & 10 deletions test/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def get_app(self):
"static_path": os.path.join(os.path.dirname(__file__), "../classrank/static"),
"template_path": os.path.join(os.path.dirname(__file__),
"../classrank/templates"),
"pages": ["Page", "Other Page"],
"logged_in_pages": ["dashboard", "search", "rate", "privacy", "settings", "logout"],
"logged_out_pages": ["login", "register"],
"cookie_secret": test_cookie_secret,
"login_url": "/login"
}
Expand Down Expand Up @@ -75,19 +76,21 @@ def create_example_user(self):
return body

def test_login_post_success(self):
body = self.create_example_user()

with patch('classrank.handlers._authenticate.hash_pw') as hash_pass:
with patch(
'classrank.handlers.BaseHandler.get_current_user') as authenticator:
authenticator.return_value = "tester"
hash_pass.return_value = "secret"

response = self.fetch("/login", method="POST", body=body)
hash_pass, response = self.login()

self.assertEqual(b"You're allowed here!", response.body)
self.assertEqual(("password", "salt"), hash_pass.call_args[0])

def test_logout(self):
self.login()

with patch('classrank.handlers.BaseHandler.get_current_user') as auth:
auth.return_value = "tester"
response = self.fetch("/logout", method="GET")

self.assertIn("ClassRank".encode('utf-8'), response.body)


def test_register_existing_user(self):
self.create_example_user()
body = urllib.parse.urlencode({"email": "test@test.com", "school": "test",
Expand All @@ -96,3 +99,16 @@ def test_register_existing_user(self):
response = self.fetch("/register", method="POST", body=body)

self.assertEqual(self.fetch('/register').body, response.body)

def login(self):
body = self.create_example_user()

with patch('classrank.handlers._authenticate.hash_pw') as hash_pass:
with patch(
'classrank.handlers.BaseHandler.get_current_user') as authenticator:
authenticator.return_value = "tester"
hash_pass.return_value = "secret"

return hash_pass, self.fetch("/login", method="POST", body=body)


12 changes: 6 additions & 6 deletions test/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ class TestBaseHandler(unittest.TestCase):
def setUp(self):
self.app = MagicMock()
self.app.db = "database"
self.app.settings = {"pages": None}
self.app.settings = {"logged_in_pages": None, "logged_out_pages": None, "cookie_secret": test_cookie_secret}

def test_base_handler(self):
handler = BaseHandler(self.app, Mock())
self.assertEqual(handler.db, "database")
self.assertEqual(handler.pages, None)

template = "file.html"
with patch.object(RequestHandler, "render", return_value="a template") as r:
handler.render(template)
args, kwargs = r.call_args
self.assertTupleEqual((args, kwargs), (
("file.html",), {"pages": None}))
with patch.object(BaseHandler, 'get_current_user', return_value=None) as g:
handler.render(template)
args, kwargs = r.call_args
self.assertTupleEqual((args, kwargs), (("file.html",), {"pages": None}))

0 comments on commit 96ad76f

Please sign in to comment.