2016-08-14 12:22:53 +00:00
|
|
|
# pylint: disable=redefined-outer-name
|
2016-04-20 09:48:34 +00:00
|
|
|
import contextlib
|
2016-04-30 21:17:08 +00:00
|
|
|
import os
|
2016-08-15 15:53:01 +00:00
|
|
|
import random
|
|
|
|
import string
|
2016-11-27 17:42:14 +00:00
|
|
|
from unittest.mock import patch
|
2016-08-15 15:53:01 +00:00
|
|
|
from datetime import datetime
|
2016-04-15 12:36:00 +00:00
|
|
|
import pytest
|
2016-04-18 17:42:24 +00:00
|
|
|
import freezegun
|
2017-02-04 00:08:12 +00:00
|
|
|
import sqlalchemy as sa
|
|
|
|
from szurubooru import config, db, model, rest
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-15 12:36:00 +00:00
|
|
|
|
2016-04-17 10:53:58 +00:00
|
|
|
def get_unique_name():
|
2016-08-15 15:53:01 +00:00
|
|
|
alphabet = string.ascii_letters + string.digits
|
|
|
|
return ''.join(random.choice(alphabet) for _ in range(8))
|
2016-04-17 10:53:58 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-16 08:57:42 +00:00
|
|
|
@pytest.fixture
|
2016-04-18 17:42:24 +00:00
|
|
|
def fake_datetime():
|
2016-04-20 09:48:34 +00:00
|
|
|
@contextlib.contextmanager
|
2016-04-16 08:57:42 +00:00
|
|
|
def injector(now):
|
2016-04-18 20:41:39 +00:00
|
|
|
freezer = freezegun.freeze_time(now)
|
|
|
|
freezer.start()
|
|
|
|
yield
|
|
|
|
freezer.stop()
|
2016-04-16 08:57:42 +00:00
|
|
|
return injector
|
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2019-09-28 03:18:28 +00:00
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def query_logger(pytestconfig):
|
|
|
|
if pytestconfig.option.verbose > 0:
|
2016-04-30 10:46:52 +00:00
|
|
|
import logging
|
2016-05-11 20:08:10 +00:00
|
|
|
import coloredlogs
|
2016-08-14 12:22:53 +00:00
|
|
|
coloredlogs.install(
|
|
|
|
fmt='[%(asctime)-15s] %(name)s %(message)s', isatty=True)
|
2016-04-30 10:46:52 +00:00
|
|
|
logging.basicConfig()
|
|
|
|
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
|
2016-04-20 09:48:34 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-20 09:48:34 +00:00
|
|
|
@pytest.yield_fixture(scope='function', autouse=True)
|
2020-03-06 23:14:16 +00:00
|
|
|
def session(query_logger, postgresql_db): # pylint: disable=unused-argument
|
|
|
|
db.session = postgresql_db.session
|
|
|
|
postgresql_db.create_table(*model.Base.metadata.sorted_tables)
|
2016-04-20 09:48:34 +00:00
|
|
|
try:
|
2020-03-06 23:14:16 +00:00
|
|
|
yield postgresql_db.session
|
2016-04-20 09:48:34 +00:00
|
|
|
finally:
|
2020-03-06 23:14:16 +00:00
|
|
|
postgresql_db.reset_db()
|
2016-04-15 12:36:00 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-15 12:36:00 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def context_factory(session):
|
2018-02-25 10:44:02 +00:00
|
|
|
def factory(params=None, files=None, user=None, headers=None):
|
2016-08-14 10:35:14 +00:00
|
|
|
ctx = rest.Context(
|
2018-07-08 08:04:09 +00:00
|
|
|
env={'HTTP_ORIGIN': 'http://example.com'},
|
2016-08-14 10:35:14 +00:00
|
|
|
method=None,
|
|
|
|
url=None,
|
2018-02-25 10:44:02 +00:00
|
|
|
headers=headers or {},
|
2016-08-14 10:35:14 +00:00
|
|
|
params=params or {},
|
|
|
|
files=files or {})
|
2016-04-15 15:54:21 +00:00
|
|
|
ctx.session = session
|
2017-02-04 00:08:12 +00:00
|
|
|
ctx.user = user or model.User()
|
2016-04-15 15:54:21 +00:00
|
|
|
return ctx
|
2016-04-15 12:36:00 +00:00
|
|
|
return factory
|
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-15 12:36:00 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def config_injector():
|
|
|
|
def injector(new_config_content):
|
|
|
|
config.config = new_config_content
|
|
|
|
return injector
|
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-15 12:36:00 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def user_factory():
|
2018-02-25 05:45:00 +00:00
|
|
|
def factory(
|
|
|
|
name=None,
|
|
|
|
rank=model.User.RANK_REGULAR,
|
|
|
|
email='dummy',
|
|
|
|
password_salt=None,
|
|
|
|
password_hash=None):
|
2017-02-04 00:08:12 +00:00
|
|
|
user = model.User()
|
2016-04-17 10:53:58 +00:00
|
|
|
user.name = name or get_unique_name()
|
2018-02-25 05:45:00 +00:00
|
|
|
user.password_salt = password_salt or 'dummy'
|
|
|
|
user.password_hash = password_hash or 'dummy'
|
2016-04-17 10:53:58 +00:00
|
|
|
user.email = email
|
2016-04-15 12:36:00 +00:00
|
|
|
user.rank = rank
|
2016-08-15 15:53:01 +00:00
|
|
|
user.creation_time = datetime(1997, 1, 1)
|
2017-02-04 00:08:12 +00:00
|
|
|
user.avatar_style = model.User.AVATAR_GRAVATAR
|
2016-04-15 12:36:00 +00:00
|
|
|
return user
|
|
|
|
return factory
|
2016-04-15 18:40:12 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2018-02-25 10:44:02 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def user_token_factory(user_factory):
|
|
|
|
def factory(
|
|
|
|
user=None,
|
|
|
|
token=None,
|
|
|
|
expiration_time=None,
|
|
|
|
enabled=None,
|
|
|
|
creation_time=None):
|
|
|
|
if user is None:
|
|
|
|
user = user_factory()
|
|
|
|
db.session.add(user)
|
|
|
|
user_token = model.UserToken()
|
|
|
|
user_token.user = user
|
|
|
|
user_token.token = token or 'dummy'
|
|
|
|
user_token.expiration_time = expiration_time
|
|
|
|
user_token.enabled = enabled if enabled is not None else True
|
|
|
|
user_token.creation_time = creation_time or datetime(1997, 1, 1)
|
|
|
|
return user_token
|
|
|
|
return factory
|
|
|
|
|
|
|
|
|
2016-04-19 09:56:09 +00:00
|
|
|
@pytest.fixture
|
2016-04-24 07:50:11 +00:00
|
|
|
def tag_category_factory():
|
2016-08-13 20:29:12 +00:00
|
|
|
def factory(name=None, color='dummy', default=False):
|
2017-02-04 00:08:12 +00:00
|
|
|
category = model.TagCategory()
|
2016-08-13 20:29:12 +00:00
|
|
|
category.name = name or get_unique_name()
|
2016-04-19 09:56:09 +00:00
|
|
|
category.color = color
|
2016-08-13 20:29:12 +00:00
|
|
|
category.default = default
|
2016-04-19 09:56:09 +00:00
|
|
|
return category
|
|
|
|
return factory
|
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-15 18:40:12 +00:00
|
|
|
@pytest.fixture
|
2016-04-24 07:50:11 +00:00
|
|
|
def tag_factory():
|
2016-08-13 20:29:12 +00:00
|
|
|
def factory(names=None, category=None):
|
2016-04-19 09:56:09 +00:00
|
|
|
if not category:
|
2017-02-04 00:08:12 +00:00
|
|
|
category = model.TagCategory(get_unique_name())
|
2016-04-24 07:50:11 +00:00
|
|
|
db.session.add(category)
|
2017-02-04 00:08:12 +00:00
|
|
|
tag = model.Tag()
|
2016-08-28 17:25:15 +00:00
|
|
|
tag.names = []
|
|
|
|
for i, name in enumerate(names or [get_unique_name()]):
|
2017-02-04 00:08:12 +00:00
|
|
|
tag.names.append(model.TagName(name, i))
|
2016-04-15 18:40:12 +00:00
|
|
|
tag.category = category
|
2016-08-15 15:53:01 +00:00
|
|
|
tag.creation_time = datetime(1996, 1, 1)
|
2016-04-15 18:40:12 +00:00
|
|
|
return tag
|
|
|
|
return factory
|
2016-04-17 10:55:07 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-17 10:55:07 +00:00
|
|
|
@pytest.fixture
|
2020-03-08 02:02:01 +00:00
|
|
|
def post_factory():
|
2016-08-14 12:22:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
2016-04-17 10:55:07 +00:00
|
|
|
def factory(
|
2016-04-22 18:58:04 +00:00
|
|
|
id=None,
|
2017-02-04 00:08:12 +00:00
|
|
|
safety=model.Post.SAFETY_SAFE,
|
|
|
|
type=model.Post.TYPE_IMAGE,
|
2016-04-17 10:55:07 +00:00
|
|
|
checksum='...'):
|
2017-02-04 00:08:12 +00:00
|
|
|
post = model.Post()
|
2016-04-22 18:58:04 +00:00
|
|
|
post.post_id = id
|
2016-04-17 10:55:07 +00:00
|
|
|
post.safety = safety
|
|
|
|
post.type = type
|
|
|
|
post.checksum = checksum
|
2016-04-22 18:58:04 +00:00
|
|
|
post.flags = []
|
2016-04-30 21:17:08 +00:00
|
|
|
post.mime_type = 'application/octet-stream'
|
2016-08-15 15:53:01 +00:00
|
|
|
post.creation_time = datetime(1996, 1, 1)
|
2016-04-17 10:55:07 +00:00
|
|
|
return post
|
|
|
|
return factory
|
2016-04-24 07:04:53 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-04-24 07:04:53 +00:00
|
|
|
@pytest.fixture
|
2016-04-24 07:50:11 +00:00
|
|
|
def comment_factory(user_factory, post_factory):
|
2016-08-15 15:53:01 +00:00
|
|
|
def factory(user=None, post=None, text='dummy', time=None):
|
2016-04-24 07:50:11 +00:00
|
|
|
if not user:
|
|
|
|
user = user_factory()
|
|
|
|
db.session.add(user)
|
|
|
|
if not post:
|
|
|
|
post = post_factory()
|
|
|
|
db.session.add(post)
|
2017-02-04 00:08:12 +00:00
|
|
|
comment = model.Comment()
|
2016-04-24 07:50:11 +00:00
|
|
|
comment.user = user
|
|
|
|
comment.post = post
|
2016-04-24 07:04:53 +00:00
|
|
|
comment.text = text
|
2016-08-15 15:53:01 +00:00
|
|
|
comment.creation_time = time or datetime(1996, 1, 1)
|
2016-04-24 07:04:53 +00:00
|
|
|
return comment
|
|
|
|
return factory
|
2016-04-30 21:17:08 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-10-21 19:48:08 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def post_score_factory(user_factory, post_factory):
|
|
|
|
def factory(post=None, user=None, score=1):
|
|
|
|
if user is None:
|
|
|
|
user = user_factory()
|
|
|
|
if post is None:
|
|
|
|
post = post_factory()
|
2017-02-04 00:08:12 +00:00
|
|
|
return model.PostScore(
|
2016-10-21 19:48:08 +00:00
|
|
|
post=post, user=user, score=score, time=datetime(1999, 1, 1))
|
|
|
|
return factory
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def post_favorite_factory(user_factory, post_factory):
|
|
|
|
def factory(post=None, user=None):
|
|
|
|
if user is None:
|
|
|
|
user = user_factory()
|
|
|
|
if post is None:
|
|
|
|
post = post_factory()
|
2017-02-04 00:08:12 +00:00
|
|
|
return model.PostFavorite(
|
2016-10-21 19:48:08 +00:00
|
|
|
post=post, user=user, time=datetime(1999, 1, 1))
|
|
|
|
return factory
|
|
|
|
|
|
|
|
|
2020-05-05 02:12:54 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def pool_category_factory():
|
|
|
|
def factory(name=None, color='dummy', default=False):
|
|
|
|
category = model.PoolCategory()
|
|
|
|
category.name = name or get_unique_name()
|
|
|
|
category.color = color
|
|
|
|
category.default = default
|
|
|
|
return category
|
|
|
|
return factory
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def pool_factory():
|
2020-06-03 15:55:50 +00:00
|
|
|
def factory(
|
|
|
|
id=None, names=None, description=None, category=None, time=None):
|
2020-05-05 02:12:54 +00:00
|
|
|
if not category:
|
|
|
|
category = model.PoolCategory(get_unique_name())
|
|
|
|
db.session.add(category)
|
|
|
|
pool = model.Pool()
|
|
|
|
pool.pool_id = id
|
|
|
|
pool.names = []
|
|
|
|
for i, name in enumerate(names or [get_unique_name()]):
|
|
|
|
pool.names.append(model.PoolName(name, i))
|
|
|
|
pool.description = description
|
|
|
|
pool.category = category
|
|
|
|
pool.creation_time = time or datetime(1996, 1, 1)
|
|
|
|
return pool
|
|
|
|
return factory
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def pool_post_factory(pool_factory, post_factory):
|
|
|
|
def factory(pool=None, post=None, order=None):
|
|
|
|
if not pool:
|
|
|
|
pool = pool_factory()
|
|
|
|
db.session.add(pool)
|
|
|
|
if not post:
|
|
|
|
post = post_factory()
|
|
|
|
db.session.add(post)
|
|
|
|
pool_post = model.PoolPost(post)
|
|
|
|
pool_post.pool = pool
|
|
|
|
pool_post.post = post
|
|
|
|
pool_post.order = order or 0
|
|
|
|
return pool_post
|
|
|
|
return factory
|
|
|
|
|
|
|
|
|
2016-04-30 21:17:08 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def read_asset():
|
|
|
|
def get(path):
|
|
|
|
path = os.path.join(os.path.dirname(__file__), 'assets', path)
|
|
|
|
with open(path, 'rb') as handle:
|
|
|
|
return handle.read()
|
|
|
|
return get
|