From 9e609b194c9a00e6c8a84d5dfc3f6872cbf62aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Vidal=20Garc=C3=ADa?= Date: Mon, 9 Mar 2020 16:50:48 +0100 Subject: [PATCH] tests: flexible registration --- invenio_accounts/views/rest.py | 1 - tests/conftest.py | 30 ++++++++++++++++++++++++++++++ tests/test_views_rest.py | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/invenio_accounts/views/rest.py b/invenio_accounts/views/rest.py index e026d55d..a61a540a 100644 --- a/invenio_accounts/views/rest.py +++ b/invenio_accounts/views/rest.py @@ -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): diff --git a/tests/conftest.py b/tests/conftest.py index a3237303..5cc69a99 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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.""" diff --git a/tests/test_views_rest.py b/tests/test_views_rest.py index 66efbb87..da3d0c37 100644 --- a/tests/test_views_rest.py +++ b/tests/test_views_rest.py @@ -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='old@test.com') + 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='new@test.com', password='123456')) + assert_error_resp(res, ( + ('active', 'required'), + )) + + # Successful registration + res = client.post(url, data=dict( + email='new@test.com', password='123456', active=True)) + assert res.status_code == 200 + + def test_logout_view(api): app = api with app.app_context():