Skip to content

Commit

Permalink
tests: flexible registration
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidalgarcia committed Mar 10, 2020
1 parent 9ce2323 commit 9e609b1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 0 additions & 1 deletion invenio_accounts/views/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ 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):
Expand Down
30 changes: 30 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,36 @@ def app_with_redis_url(request):
yield app


@pytest.fixture()
def app_with_flexible_registration(request):
"""Flask application fixture with Invenio Accounts."""
from webargs import fields
from invenio_accounts.views.rest import RegisterView, use_kwargs

class MyRegisterView(RegisterView):

post_args = {
**RegisterView.post_args,
'active': fields.Boolean(required=True)
}

@use_kwargs(post_args)
def post(self, **kwargs):
"""Register a user."""
return super(MyRegisterView, self).post(**kwargs)

api_app = _app_factory()
InvenioREST(api_app)
InvenioAccountsREST(api_app)

api_app.config['ACCOUNTS_REST_AUTH_VIEWS']['register'] = MyRegisterView

api_app.register_blueprint(create_blueprint(api_app))

_database_setup(api_app, request)
yield api_app


@pytest.fixture
def script_info(app):
"""Get ScriptInfo object for testing CLI."""
Expand Down
21 changes: 21 additions & 0 deletions tests/test_views_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ def test_registration_view(api):
assert res.status_code == 200


def test_custom_registration_view(app_with_flexible_registration):
app = app_with_flexible_registration
with app.app_context():
create_test_user(email='[email protected]')
db.session.commit()
with app.test_client() as client:
url = url_for('invenio_accounts_rest_auth.register')

# Missing custom field
res = client.post(url, data=dict(
email='[email protected]', password='123456'))
assert_error_resp(res, (
('active', 'required'),
))

# Successful registration
res = client.post(url, data=dict(
email='[email protected]', password='123456', active=True))
assert res.status_code == 200


def test_logout_view(api):
app = api
with app.app_context():
Expand Down

0 comments on commit 9e609b1

Please sign in to comment.