Put fallbacks in their own file.

This commit is contained in:
Sage Vaillancourt 2022-09-27 02:47:38 +00:00
parent d358c55017
commit 3857674244
3 changed files with 39 additions and 24 deletions

View File

@ -1,7 +1,6 @@
import os import os
import sys import sys
import threading import threading
import types
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Optional from typing import Optional
@ -10,6 +9,8 @@ from uuid import uuid4, UUID
import bcrypt import bcrypt
import psycopg import psycopg
from undercover.fallback import MockConnection
@dataclass @dataclass
class Letter: class Letter:
@ -53,23 +54,6 @@ else:
def connect(): def connect():
return MockConnection() 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): def login(user_email: str, password: str):

View File

@ -3,6 +3,7 @@ import os
import sys import sys
from mailjet_rest import Client from mailjet_rest import Client
from undercover.fallback import mock_sender
api_key = os.environ.get('MAILJET_API_KEY') api_key = os.environ.get('MAILJET_API_KEY')
api_secret = os.environ.get('MAILJET_SECRET_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('Mailjet keys not configured: email access is disabled.\n')
sys.stderr.write(' Emails will be printed to the console.\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') 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): def send_password_reset(to_email: str, reset_link: str):
@ -39,13 +41,9 @@ Or copy and paste this link into your browser: {reset_link}
] ]
} }
if mailjet:
result = mailjet.send.create(data=data) result = mailjet.send.create(data=data)
return 200 <= result.status_code <= 299 return 200 <= result.status_code <= 299
print(json.JSONEncoder(indent=2).encode(data))
return True
if __name__ == "__main__": if __name__ == "__main__":
send_password_reset('sage@sagev.space', 'https://sagev.space/') send_password_reset('sage@sagev.space', 'https://sagev.space/')

33
undercover/fallback.py Normal file
View File

@ -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()