Skip to content

Commit

Permalink
tests: rest auth views
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidalgarcia committed Mar 10, 2020
1 parent d06fd76 commit 9ce2323
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 45 deletions.
6 changes: 0 additions & 6 deletions examples/app-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ mkdir static

# Install specific dependencies
pip install -r requirements.txt
npm install -g [email protected] [email protected] [email protected] [email protected]

# Install assets
flask npm
cd static
npm install
cd ..
flask collect
flask webpack buildall

Expand Down
2 changes: 1 addition & 1 deletion invenio_accounts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _generate_token_url(endpoint, token):


def default_reset_password_link_func(user):
"""Return the confirmation link that will be sent to a user via email."""
"""Return the reset password link that will be sent to a user via email."""
token = generate_reset_password_token(user)
endpoint = current_app.config['ACCOUNTS_RESET_PASSWORD_ENDPOINT'] or \
get_security_endpoint_name('reset_password')
Expand Down
44 changes: 19 additions & 25 deletions invenio_accounts/views/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def create_blueprint(app):
view_func=authentication_views['confirm_email'].as_view(
'confirm_email'))

# TODO: Check this
if app.config['ACCOUNTS_SESSION_ACTIVITY_ENABLED']:
blueprint.add_url_rule(
'/sessions',
Expand All @@ -130,11 +129,11 @@ def create_blueprint(app):


class FlaskParser(FlaskParserBase):
"""."""
"""Parser to add FieldError to validation errors."""

# TODO: Add error codes to all messages (e.g. 'user-already-exists')
def handle_error(self, error, *args, **kwargs):
"""."""
"""Handle errors during parsing."""
if isinstance(error, ValidationError):
_errors = []
for field, messages in error.messages.items():
Expand Down Expand Up @@ -165,15 +164,14 @@ def unique_user_email(email):


def default_user_payload(user):
"""."""
"""Parse user payload."""
return {
'id': user.id,
'email': user.email,
'confirmed_at':
user.confirmed_at.isoformat() if user.confirmed_at else None,
'last_login_at':
user.last_login_at.isoformat() if user.last_login_at else None,
# TODO: Check roles
'roles': [role_to_dict(role) for role in user.roles],
}

Expand All @@ -189,7 +187,15 @@ def _commit(response=None):
return response


class LoginView(MethodView):
class UserViewMixin(object):
"""Mixin class for get user operations."""

def get_user(self, email=None, **kwargs):
"""Retrieve a user by the provided arguments."""
return current_datastore.get_user(email)


class LoginView(MethodView, UserViewMixin):
"""View to login a user."""

decorators = [user_already_authenticated]
Expand All @@ -215,10 +221,6 @@ def verify_login(self, user, password=None, **kwargs):
if not user.is_active:
_abort(get_message('DISABLED_ACCOUNT')[0])

def get_user(self, email=None, **kwargs):
"""Retrieve a user by the provided arguments."""
return current_datastore.get_user(email)

def login_user(self, user):
"""Perform any login actions."""
return login_user(user)
Expand Down Expand Up @@ -294,7 +296,7 @@ def post(self, **kwargs):
return self.success_response(user)


class ForgotPasswordView(MethodView):
class ForgotPasswordView(MethodView, UserViewMixin):
"""View to get a link to reset the user password."""

decorators = [user_already_authenticated]
Expand All @@ -305,10 +307,6 @@ class ForgotPasswordView(MethodView):
'email': fields.Email(required=True, validate=[user_exists]),
}

def get_user(self, email=None, **kwargs):
"""Retrieve a user by the provided arguments."""
return current_datastore.get_user(email)

@classmethod
def send_reset_password_instructions(cls, user):
"""Send email containing instructions to reset password."""
Expand Down Expand Up @@ -407,7 +405,7 @@ def post(self, **kwargs):
return self.success_response()


class SendConfirmationEmailView(MethodView):
class SendConfirmationEmailView(MethodView, UserViewMixin):
"""View function which sends confirmation instructions."""

decorators = [login_required]
Expand All @@ -418,10 +416,6 @@ class SendConfirmationEmailView(MethodView):
'email': fields.Email(required=True, validate=[user_exists]),
}

def get_user(self, email=None, **kwargs):
"""Retrieve a user by the provided arguments."""
return current_datastore.get_user(email)

def verify(self, user):
"""Verify that user is not confirmed."""
if user.confirmed_at is not None:
Expand Down Expand Up @@ -497,13 +491,13 @@ class SessionsListView(MethodView):

decorators = [login_required]

def get(self, sid_s=None, **kwargs):
def get(self, **kwargs):
"""Return user sessions info."""
sessions = SessionActivity.query.filter_by(
sessions = SessionActivity.query_by_user(
user_id=current_user.get_id())
results = [{
'created': s.created,
'current': s.is_current(s.sid_s),
'current': SessionActivity.is_current(s.sid_s),
'browser': s.browser,
'browser_version': s.browser_version,
'os': s.os,
Expand All @@ -520,8 +514,8 @@ class SessionsItemView(MethodView):

def delete(self, sid_s=None, **kwargs):
"""Revoke the given user session."""
if SessionActivity.query.filter_by(
user_id=current_user.get_id(), sid_s=sid_s).count() == 1:
if SessionActivity.query_by_user(current_user.get_id()) \
.filter_by(sid_s=sid_s).count() == 1:
delete_session(sid_s=sid_s)
db.session.commit()
message = 'Session {0} successfully removed. {1}.'
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@
'invenio-base>=1.2.2',
'invenio-i18n>=1.2.0',
'invenio-celery>=1.1.2',
'invenio-i18n>=1.0.0',
'invenio-rest>=1.1.0',
'invenio-rest>=1.1.3',
'maxminddb-geolite2>=2017.404',
'passlib>=1.7.1',
'pyjwt>=1.5.0',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_examples_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pytest


@pytest.yield_fixture
@pytest.fixture
def example_app():
"""Example app fixture."""
current_dir = os.getcwd()
Expand Down
Loading

0 comments on commit 9ce2323

Please sign in to comment.