server/tests: use common fixtures in pw reset test

This commit is contained in:
rr- 2016-04-17 12:53:58 +02:00
parent cecab3caf0
commit 470ff70f91
2 changed files with 27 additions and 31 deletions

View File

@ -4,18 +4,6 @@ import pytest
from szurubooru import api, db, errors from szurubooru import api, db, errors
from szurubooru.util import auth, mailer 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 @pytest.fixture
def password_reset_api(config_injector): def password_reset_api(config_injector):
config_injector({ config_injector({
@ -29,15 +17,16 @@ def test_reset_non_existing(password_reset_api, context_factory):
with pytest.raises(errors.NotFoundError): with pytest.raises(errors.NotFoundError):
password_reset_api.get(context_factory(), 'u1') password_reset_api.get(context_factory(), 'u1')
def test_reset_without_email(password_reset_api, session, context_factory): def test_reset_without_email(
user = mock_user('u1', 'regular_user', None) password_reset_api, session, context_factory, user_factory):
session.add(user) session.add(user_factory(name='u1', rank='regular_user', email=None))
with pytest.raises(errors.ValidationError): with pytest.raises(errors.ValidationError):
password_reset_api.get(context_factory(), 'u1') password_reset_api.get(context_factory(), 'u1')
def test_reset_sending_email(password_reset_api, session, context_factory): def test_reset_sending_email(
user = mock_user('u1', 'regular_user', 'user@example.com') password_reset_api, session, context_factory, user_factory):
session.add(user) session.add(user_factory(
name='u1', rank='regular_user', email='user@example.com'))
for getter in ['u1', 'user@example.com']: for getter in ['u1', 'user@example.com']:
mailer.send_mail = mock.MagicMock() mailer.send_mail = mock.MagicMock()
assert password_reset_api.get(context_factory(), getter) == {} 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): with pytest.raises(errors.NotFoundError):
password_reset_api.post(context_factory(), 'u1') password_reset_api.post(context_factory(), 'u1')
def test_confirmation_no_token(password_reset_api, context_factory, session): def test_confirmation_no_token(
user = mock_user('u1', 'regular_user', 'user@example.com') password_reset_api, context_factory, session, user_factory):
session.add(user) session.add(user_factory(
name='u1', rank='regular_user', email='user@example.com'))
with pytest.raises(errors.ValidationError): with pytest.raises(errors.ValidationError):
password_reset_api.post(context_factory(input={}), 'u1') password_reset_api.post(context_factory(input={}), 'u1')
def test_confirmation_bad_token(password_reset_api, context_factory, session): def test_confirmation_bad_token(
user = mock_user('u1', 'regular_user', 'user@example.com') password_reset_api, context_factory, session, user_factory):
session.add(user) session.add(user_factory(
name='u1', rank='regular_user', email='user@example.com'))
with pytest.raises(errors.ValidationError): with pytest.raises(errors.ValidationError):
password_reset_api.post( password_reset_api.post(
context_factory(input={'token': 'bad'}), 'u1') context_factory(input={'token': 'bad'}), 'u1')
def test_confirmation_good_token(password_reset_api, context_factory, session): def test_confirmation_good_token(
user = mock_user('u1', 'regular_user', 'user@example.com') 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 old_hash = user.password_hash
session.add(user) session.add(user)
context = context_factory( context = context_factory(

View File

@ -1,9 +1,13 @@
import datetime import datetime
import uuid
import pytest import pytest
import sqlalchemy import sqlalchemy
from szurubooru import api, config, db from szurubooru import api, config, db
from szurubooru.util import misc from szurubooru.util import misc
def get_unique_name():
return str(uuid.uuid4())
@pytest.fixture @pytest.fixture
def fake_datetime(monkeypatch): def fake_datetime(monkeypatch):
def injector(now): def injector(now):
@ -46,13 +50,12 @@ def config_injector():
@pytest.fixture @pytest.fixture
def user_factory(): def user_factory():
def factory(name='dummy', rank='regular_user'): def factory(name=None, rank='regular_user', email='dummy'):
user = db.User() user = db.User()
user.name = name user.name = name or get_unique_name()
user.password = 'dummy'
user.password_salt = 'dummy' user.password_salt = 'dummy'
user.password_hash = 'dummy' user.password_hash = 'dummy'
user.email = 'dummy' user.email = email
user.rank = rank user.rank = rank
user.creation_time = datetime.datetime(1997, 1, 1) user.creation_time = datetime.datetime(1997, 1, 1)
user.avatar_style = db.User.AVATAR_GRAVATAR user.avatar_style = db.User.AVATAR_GRAVATAR
@ -63,7 +66,7 @@ def user_factory():
def tag_factory(): def tag_factory():
def factory(names=None, category='dummy'): def factory(names=None, category='dummy'):
tag = db.Tag() 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.category = category
tag.creation_time = datetime.datetime(1996, 1, 1) tag.creation_time = datetime.datetime(1996, 1, 1)
return tag return tag