From a00a6b84975a05d5cf38d818af059084251e5f93 Mon Sep 17 00:00:00 2001 From: Thomas Lindner Date: Sat, 11 Jul 2020 18:23:27 +0200 Subject: [PATCH] [tests] Mock send_email --- kibicara/webapi/admin.py | 7 +++---- tests/conftest.py | 44 ++++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/kibicara/webapi/admin.py b/kibicara/webapi/admin.py index b7eded2..490fd34 100644 --- a/kibicara/webapi/admin.py +++ b/kibicara/webapi/admin.py @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index 51d9f56..b07c123 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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')