You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
4.5 KiB
194 lines
4.5 KiB
from collections import namedtuple |
|
|
|
import pytest |
|
from sqlalchemy.orm import clear_mappers |
|
|
|
from ordr3.views import RE_SIMPLE_URL |
|
|
|
ParsedMail = namedtuple("ParsedMail", ["body", "link"]) |
|
|
|
|
|
@pytest.fixture |
|
def _pyramid_app(): |
|
from ordr3 import main |
|
|
|
config = { |
|
"pyramid.includes": ["pyramid_debugtoolbar", "pyramid_mailer.testing"], |
|
"sqlalchemy.url": "sqlite:///:memory:", |
|
"retry.attempts": "3", |
|
"auth.secret": "change me for production", |
|
"session.secret": "change me for production", |
|
"session.auto_csrf": "true", |
|
"static_views.cache_max_age": "0", |
|
"mail.host": "localhost", |
|
"mail.port": "2525", |
|
"mail.default_sender": "ordr@example.com", |
|
"jinja2.filters ": [ |
|
"resource_url = pyramid_jinja2.filters:resource_url_filter", |
|
"as_date = ordr3.views:jinja_date", |
|
"as_time = ordr3.views:jinja_time", |
|
"as_datetime = ordr3.views:jinja_datetime", |
|
"view_comment = ordr3.views:jinja_view_comment", |
|
"extract_links = ordr3.views:jinja_extract_links", |
|
"nl2br = ordr3.views:jinja_nl2br", |
|
], |
|
} |
|
|
|
yield main({}, **config) |
|
clear_mappers() |
|
|
|
|
|
@pytest.fixture |
|
def _sqlite_repo(_pyramid_app): |
|
from pyramid.scripting import prepare |
|
from ordr3 import adapters |
|
|
|
with prepare() as env: |
|
with env["request"].tm: |
|
repo = env["root"].request.repo |
|
adapters.metadata.create_all(repo.session.get_bind()) |
|
yield repo |
|
|
|
|
|
@pytest.fixture |
|
def _example_data(_sqlite_repo): |
|
from ordr3 import models, security |
|
from datetime import datetime, timedelta |
|
|
|
today = datetime.utcnow() |
|
|
|
crypt_context = security.get_passlib_context() |
|
|
|
user = models.User( |
|
1, |
|
"TestUser", |
|
"Jon", |
|
"Smith", |
|
"jon@example.com", |
|
crypt_context.hash("jon"), |
|
models.UserRole.USER, |
|
) |
|
_sqlite_repo.add_user(user) |
|
|
|
admin = models.User( |
|
2, |
|
"TestAdmin", |
|
"Jane", |
|
"Doe", |
|
"jane@example.com", |
|
crypt_context.hash("jane"), |
|
models.UserRole.ADMIN, |
|
) |
|
_sqlite_repo.add_user(admin) |
|
|
|
inactive = models.User( |
|
3, |
|
"TestInactive", |
|
"Peter", |
|
"Peter", |
|
"peter@example.com", |
|
crypt_context.hash("peter"), |
|
models.UserRole.INACTIVE, |
|
) |
|
_sqlite_repo.add_user(inactive) |
|
|
|
order_1 = models.OrderItem( |
|
1, |
|
"Ethanol", |
|
"123", |
|
"VWR", |
|
models.OrderCategory.SOLVENT, |
|
"1 l", |
|
12.3, |
|
1, |
|
"EUR", |
|
"", |
|
"", |
|
) |
|
_sqlite_repo.add_order(order_1) |
|
log_entry = models.LogEntry( |
|
order_1.id, |
|
models.OrderStatus.COMPLETED, |
|
admin.username, |
|
today - timedelta(days=1), |
|
) |
|
order_1.add_to_log(log_entry) |
|
|
|
order_2 = models.OrderItem( |
|
2, |
|
"NaCl", |
|
"234", |
|
"Carl Roth", |
|
models.OrderCategory.CHEMICAL, |
|
"2 kg", |
|
23.4, |
|
2, |
|
"EUR", |
|
"Haushalt", |
|
"Kochsalz", |
|
) |
|
_sqlite_repo.add_order(order_2) |
|
log_entry = models.LogEntry( |
|
order_2.id, |
|
models.OrderStatus.APPROVAL, |
|
admin.username, |
|
today - timedelta(days=2), |
|
) |
|
order_2.add_to_log(log_entry) |
|
|
|
order_3 = models.OrderItem( |
|
3, |
|
"Eppis", |
|
"345", |
|
"VWR", |
|
models.OrderCategory.BIOLAB, |
|
"3 St", |
|
34.5, |
|
3, |
|
"USD", |
|
"Toto", |
|
"gefunden bei http://www.example.com/foo", |
|
) |
|
_sqlite_repo.add_order(order_3) |
|
log_entry = models.LogEntry( |
|
order_3.id, |
|
models.OrderStatus.COMPLETED, |
|
user.username, |
|
today - timedelta(days=3), |
|
) |
|
order_3.add_to_log(log_entry) |
|
|
|
|
|
@pytest.fixture |
|
def testapp(_pyramid_app, _example_data): |
|
from webtest import TestApp |
|
|
|
yield TestApp(_pyramid_app) |
|
|
|
|
|
@pytest.fixture |
|
def login_as(testapp): |
|
def _do_login(username, password): |
|
response = testapp.get("/login") |
|
form = response.form |
|
form["username"] = username |
|
form["password"] = password |
|
return form.submit("submit") |
|
|
|
yield _do_login |
|
|
|
|
|
@pytest.fixture |
|
def parse_latest_mail(testapp): |
|
from pyramid_mailer import get_mailer |
|
|
|
def _parse_mail(): |
|
registry = testapp.app.registry |
|
mailer = get_mailer(registry) |
|
last_mail = mailer.outbox[-1] |
|
body = last_mail.body |
|
links = RE_SIMPLE_URL.findall(body) |
|
link = links[0] if links else None |
|
return ParsedMail(body, link) |
|
|
|
yield _parse_mail
|
|
|