[tests] Mock send_email

This commit is contained in:
Thomas Lindner 2020-07-11 18:23:27 +02:00
parent 4b6a211c09
commit a00a6b8497
2 changed files with 30 additions and 21 deletions

View file

@ -5,7 +5,7 @@
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from kibicara.email import send_email from kibicara import email
from kibicara.model import Admin, AdminHoodRelation from kibicara.model import Admin, AdminHoodRelation
from logging import getLogger from logging import getLogger
from nacl.encoding import URLSafeBase64Encoder from nacl.encoding import URLSafeBase64Encoder
@ -72,10 +72,9 @@ router = APIRouter()
@router.post('/register/', status_code=status.HTTP_202_ACCEPTED) @router.post('/register/', status_code=status.HTTP_202_ACCEPTED)
async def admin_register(values: BodyAdmin): async def admin_register(values: BodyAdmin):
register_token = to_token(**values.__dict__) register_token = to_token(**values.__dict__)
# this logging output is captured and used by the register_token test fixture logger.debug(f'register_token={register_token}')
logger.info(register_token)
try: try:
send_email( email.send_email(
to=values.email, to=values.email,
subject='Confirm Account', subject='Confirm Account',
# XXX create real confirm link # XXX create real confirm link

View file

@ -4,9 +4,9 @@
from fastapi import FastAPI, status from fastapi import FastAPI, status
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from kibicara import email
from kibicara.model import Mapping from kibicara.model import Mapping
from kibicara.webapi import router from kibicara.webapi import router
from logging import getLogger, Handler, INFO, WARNING
from pytest import fixture from pytest import fixture
@ -19,26 +19,36 @@ def client():
return TestClient(app) return TestClient(app)
class CaptureHandler(Handler): @fixture(scope='module')
def __init__(self): def monkeymodule():
super().__init__() from _pytest.monkeypatch import MonkeyPatch
self.records = []
def emit(self, record): mpatch = MonkeyPatch()
self.records.append(record) yield mpatch
mpatch.undo()
@fixture(scope='module') @fixture(scope='module')
def register_token(client): def receive_email(monkeymodule):
# can't use the caplog fixture, since it has only function scope mailbox = []
logger = getLogger()
capture = CaptureHandler() def mock_send_email(to, subject, sender='kibicara', body=''):
logger.setLevel(INFO) mailbox.append(dict(to=to, subject=subject, sender=sender, body=body))
logger.addHandler(capture)
client.post('/api/admin/register/', json={'email': 'user', 'password': 'pass'}) def mock_receive_email():
logger.setLevel(WARNING) return mailbox.pop()
logger.removeHandler(capture)
return capture.records[0].message monkeymodule.setattr(email, 'send_email', mock_send_email)
return mock_receive_email
@fixture(scope='module')
def register_token(client, receive_email):
response = client.post(
'/api/admin/register/', json={'email': 'user', 'password': 'pass'}
)
assert response.status_code == status.HTTP_202_ACCEPTED
return receive_email()['body']
@fixture(scope='module') @fixture(scope='module')