Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user info table #24

Merged
merged 6 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ codecov
flake8
pytest
pytest-asyncio
notebook==5.7.2
3 changes: 2 additions & 1 deletion nativeauthenticator/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ async def get(self):

async def post(self):
username = self.get_body_argument('username', strip=False)
user = self.authenticator.get_or_create_user(username)
password = self.get_body_argument('password', strip=False)
user = self.authenticator.get_or_create_user(username, password)
html = self.render_template(
'signup.html',
result=bool(user),
Expand Down
20 changes: 16 additions & 4 deletions nativeauthenticator/nativeauthenticator.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
from jupyterhub import orm
from jupyterhub.orm import User
from jupyterhub.auth import Authenticator

from sqlalchemy import inspect
from tornado import gen

from .handlers import SignUpHandler
from .orm import UserInfo


class NativeAuthenticator(Authenticator):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

inspector = inspect(self.db.bind)
if 'users_info' not in inspector.get_table_names():
UserInfo.__table__.create(self.db.bind)

@gen.coroutine
def authenticate(self, handler, data):
return data['username']

def get_or_create_user(self, username):
user = orm.User.find(self.db, username)
def get_or_create_user(self, username, password):
user = User.find(self.db, username)
if not user:
user = orm.User(name=username, admin=False)
user = User(name=username, admin=False)
self.db.add(user)

user_info = UserInfo(username=username, password=password)
self.db.add(user_info)
return user

def get_handlers(self, app):
Expand Down
20 changes: 20 additions & 0 deletions nativeauthenticator/orm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from sqlalchemy import (
Column, Integer, String
)
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class UserInfo(Base):
__tablename__ = 'users_info'
id = Column(Integer, primary_key=True, autoincrement=True)
username = Column(String, nullable=False)
password = Column(String, nullable=False)

@classmethod
def find(cls, db, username):
"""Find a user info record by name.
Returns None if not found.
"""
return db.query(cls).filter(cls.username == username).first()
19 changes: 16 additions & 3 deletions nativeauthenticator/tests/test_authenticator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from unittest.mock import Mock

from jupyterhub.tests.mocking import MockHub

from nativeauthenticator import NativeAuthenticator

Expand All @@ -10,14 +10,27 @@ def tmpcwd(tmpdir):
tmpdir.chdir()


@pytest.fixture
def app():
hub = MockHub()
hub.init_db()
return hub


# use pytest-asyncio
pytestmark = pytest.mark.asyncio
# run each test in a temporary working directory
pytestmark = pytestmark(pytest.mark.usefixtures("tmpcwd"))


async def test_basic(tmpcwd):
auth = NativeAuthenticator()
async def test_basic(tmpcwd, app):
auth = NativeAuthenticator(db=app.db)
response = await auth.authenticate(Mock(), {'username': 'name',
'password': '123'})
assert response == 'name'


async def test_handlers(app):
auth = NativeAuthenticator(db=app.db)
handlers = auth.get_handlers(app)
assert handlers[1][0] == '/signup'