From 470ff70f9155dd9f3cfcee3769b18c29a0445a61 Mon Sep 17 00:00:00 2001 From: rr- Date: Sun, 17 Apr 2016 12:53:58 +0200 Subject: [PATCH] server/tests: use common fixtures in pw reset test --- .../tests/api/test_password_reset.py | 45 ++++++++----------- server/szurubooru/tests/conftest.py | 13 +++--- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/server/szurubooru/tests/api/test_password_reset.py b/server/szurubooru/tests/api/test_password_reset.py index 9d48563..395db15 100644 --- a/server/szurubooru/tests/api/test_password_reset.py +++ b/server/szurubooru/tests/api/test_password_reset.py @@ -4,18 +4,6 @@ import pytest from szurubooru import api, db, errors from szurubooru.util import auth, mailer -def mock_user(name, rank, email): - user = db.User() - user.name = name - user.password = 'dummy' - user.password_salt = 'dummy' - user.password_hash = 'dummy' - user.email = email - user.rank = rank - user.creation_time = datetime(1997, 1, 1) - user.avatar_style = db.User.AVATAR_GRAVATAR - return user - @pytest.fixture def password_reset_api(config_injector): config_injector({ @@ -29,15 +17,16 @@ def test_reset_non_existing(password_reset_api, context_factory): with pytest.raises(errors.NotFoundError): password_reset_api.get(context_factory(), 'u1') -def test_reset_without_email(password_reset_api, session, context_factory): - user = mock_user('u1', 'regular_user', None) - session.add(user) +def test_reset_without_email( + password_reset_api, session, context_factory, user_factory): + session.add(user_factory(name='u1', rank='regular_user', email=None)) with pytest.raises(errors.ValidationError): password_reset_api.get(context_factory(), 'u1') -def test_reset_sending_email(password_reset_api, session, context_factory): - user = mock_user('u1', 'regular_user', 'user@example.com') - session.add(user) +def test_reset_sending_email( + password_reset_api, session, context_factory, user_factory): + session.add(user_factory( + name='u1', rank='regular_user', email='user@example.com')) for getter in ['u1', 'user@example.com']: mailer.send_mail = mock.MagicMock() assert password_reset_api.get(context_factory(), getter) == {} @@ -54,21 +43,25 @@ def test_confirmation_non_existing(password_reset_api, context_factory): with pytest.raises(errors.NotFoundError): password_reset_api.post(context_factory(), 'u1') -def test_confirmation_no_token(password_reset_api, context_factory, session): - user = mock_user('u1', 'regular_user', 'user@example.com') - session.add(user) +def test_confirmation_no_token( + password_reset_api, context_factory, session, user_factory): + session.add(user_factory( + name='u1', rank='regular_user', email='user@example.com')) with pytest.raises(errors.ValidationError): password_reset_api.post(context_factory(input={}), 'u1') -def test_confirmation_bad_token(password_reset_api, context_factory, session): - user = mock_user('u1', 'regular_user', 'user@example.com') - session.add(user) +def test_confirmation_bad_token( + password_reset_api, context_factory, session, user_factory): + session.add(user_factory( + name='u1', rank='regular_user', email='user@example.com')) with pytest.raises(errors.ValidationError): password_reset_api.post( context_factory(input={'token': 'bad'}), 'u1') -def test_confirmation_good_token(password_reset_api, context_factory, session): - user = mock_user('u1', 'regular_user', 'user@example.com') +def test_confirmation_good_token( + password_reset_api, context_factory, session, user_factory): + user = user_factory( + name='u1', rank='regular_user', email='user@example.com') old_hash = user.password_hash session.add(user) context = context_factory( diff --git a/server/szurubooru/tests/conftest.py b/server/szurubooru/tests/conftest.py index 04ddf90..e0d6785 100644 --- a/server/szurubooru/tests/conftest.py +++ b/server/szurubooru/tests/conftest.py @@ -1,9 +1,13 @@ import datetime +import uuid import pytest import sqlalchemy from szurubooru import api, config, db from szurubooru.util import misc +def get_unique_name(): + return str(uuid.uuid4()) + @pytest.fixture def fake_datetime(monkeypatch): def injector(now): @@ -46,13 +50,12 @@ def config_injector(): @pytest.fixture def user_factory(): - def factory(name='dummy', rank='regular_user'): + def factory(name=None, rank='regular_user', email='dummy'): user = db.User() - user.name = name - user.password = 'dummy' + user.name = name or get_unique_name() user.password_salt = 'dummy' user.password_hash = 'dummy' - user.email = 'dummy' + user.email = email user.rank = rank user.creation_time = datetime.datetime(1997, 1, 1) user.avatar_style = db.User.AVATAR_GRAVATAR @@ -63,7 +66,7 @@ def user_factory(): def tag_factory(): def factory(names=None, category='dummy'): tag = db.Tag() - tag.names = [db.TagName(name) for name in (names or ['dummy'])] + tag.names = [db.TagName(name) for name in (names or [get_unique_name()])] tag.category = category tag.creation_time = datetime.datetime(1996, 1, 1) return tag