From 38576742442555d956bb03e6820b9c67e0e919bf Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Tue, 27 Sep 2022 02:47:38 +0000 Subject: [PATCH] Put fallbacks in their own file. --- undercover/db.py | 20 ++------------------ undercover/email.py | 10 ++++------ undercover/fallback.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 undercover/fallback.py diff --git a/undercover/db.py b/undercover/db.py index b0b85f5..d77d9f9 100644 --- a/undercover/db.py +++ b/undercover/db.py @@ -1,7 +1,6 @@ import os import sys import threading -import types from dataclasses import dataclass from datetime import datetime, timedelta from typing import Optional @@ -10,6 +9,8 @@ from uuid import uuid4, UUID import bcrypt import psycopg +from undercover.fallback import MockConnection + @dataclass class Letter: @@ -53,23 +54,6 @@ else: def connect(): return MockConnection() - class MockConnection: - mock_cursor = types.SimpleNamespace() - mock_cursor.execute = lambda *a: () - mock_cursor.fetchone = lambda *a: None - mock_cursor.fetchall = lambda *a: [] - - def __enter__(self, *a): - return self - - def __exit__(self, *a): - pass - - def cursor(self): - return self.mock_cursor - - def commit(self, *a): - pass def login(user_email: str, password: str): diff --git a/undercover/email.py b/undercover/email.py index f599391..1ca857f 100644 --- a/undercover/email.py +++ b/undercover/email.py @@ -3,6 +3,7 @@ import os import sys from mailjet_rest import Client +from undercover.fallback import mock_sender api_key = os.environ.get('MAILJET_API_KEY') api_secret = os.environ.get('MAILJET_SECRET_KEY') @@ -14,6 +15,7 @@ if not mailjet: sys.stderr.write('Mailjet keys not configured: email access is disabled.\n') sys.stderr.write(' Emails will be printed to the console.\n') sys.stderr.write(' To enable, ensure MAILJET_API_KEY and MAILJET_SECRET_KEY are set\n') + mailjet = mock_sender def send_password_reset(to_email: str, reset_link: str): @@ -39,12 +41,8 @@ Or copy and paste this link into your browser: {reset_link} ] } - if mailjet: - result = mailjet.send.create(data=data) - return 200 <= result.status_code <= 299 - - print(json.JSONEncoder(indent=2).encode(data)) - return True + result = mailjet.send.create(data=data) + return 200 <= result.status_code <= 299 if __name__ == "__main__": diff --git a/undercover/fallback.py b/undercover/fallback.py new file mode 100644 index 0000000..3bb4aa7 --- /dev/null +++ b/undercover/fallback.py @@ -0,0 +1,33 @@ +import types +import json + +class MockConnection: + mock_cursor = types.SimpleNamespace() + mock_cursor.execute = lambda *a: () + mock_cursor.fetchone = lambda *a: None + mock_cursor.fetchall = lambda *a: [] + + def __enter__(self, *a): + return self + + def __exit__(self, *a): + pass + + def cursor(self): + return self.mock_cursor + + def commit(self, *a): + pass + + +class MockCreator: + def create(self, data=None, *a): + print(json.JSONEncoder(indent=2).encode(data)) + result = types.SimpleNamespace() + result.status_code = 200 + return result + + +mock_sender = types.SimpleNamespace() +mock_sender.send = lambda self: MockCreator() +