[tests] Mock send_email
This commit is contained in:
parent
4b6a211c09
commit
a00a6b8497
|
@ -5,7 +5,7 @@
|
|||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
||||
from kibicara.email import send_email
|
||||
from kibicara import email
|
||||
from kibicara.model import Admin, AdminHoodRelation
|
||||
from logging import getLogger
|
||||
from nacl.encoding import URLSafeBase64Encoder
|
||||
|
@ -72,10 +72,9 @@ router = APIRouter()
|
|||
@router.post('/register/', status_code=status.HTTP_202_ACCEPTED)
|
||||
async def admin_register(values: BodyAdmin):
|
||||
register_token = to_token(**values.__dict__)
|
||||
# this logging output is captured and used by the register_token test fixture
|
||||
logger.info(register_token)
|
||||
logger.debug(f'register_token={register_token}')
|
||||
try:
|
||||
send_email(
|
||||
email.send_email(
|
||||
to=values.email,
|
||||
subject='Confirm Account',
|
||||
# XXX create real confirm link
|
||||
|
|
|
@ -4,9 +4,9 @@
|
|||
|
||||
from fastapi import FastAPI, status
|
||||
from fastapi.testclient import TestClient
|
||||
from kibicara import email
|
||||
from kibicara.model import Mapping
|
||||
from kibicara.webapi import router
|
||||
from logging import getLogger, Handler, INFO, WARNING
|
||||
from pytest import fixture
|
||||
|
||||
|
||||
|
@ -19,26 +19,36 @@ def client():
|
|||
return TestClient(app)
|
||||
|
||||
|
||||
class CaptureHandler(Handler):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.records = []
|
||||
@fixture(scope='module')
|
||||
def monkeymodule():
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
|
||||
def emit(self, record):
|
||||
self.records.append(record)
|
||||
mpatch = MonkeyPatch()
|
||||
yield mpatch
|
||||
mpatch.undo()
|
||||
|
||||
|
||||
@fixture(scope='module')
|
||||
def register_token(client):
|
||||
# can't use the caplog fixture, since it has only function scope
|
||||
logger = getLogger()
|
||||
capture = CaptureHandler()
|
||||
logger.setLevel(INFO)
|
||||
logger.addHandler(capture)
|
||||
client.post('/api/admin/register/', json={'email': 'user', 'password': 'pass'})
|
||||
logger.setLevel(WARNING)
|
||||
logger.removeHandler(capture)
|
||||
return capture.records[0].message
|
||||
def receive_email(monkeymodule):
|
||||
mailbox = []
|
||||
|
||||
def mock_send_email(to, subject, sender='kibicara', body=''):
|
||||
mailbox.append(dict(to=to, subject=subject, sender=sender, body=body))
|
||||
|
||||
def mock_receive_email():
|
||||
return mailbox.pop()
|
||||
|
||||
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')
|
||||
|
|
Loading…
Reference in a new issue