From 93f93d4ac4e35237987ab6639dd5afce03a4892b Mon Sep 17 00:00:00 2001 From: Charlie Taylor <56566188+charliettaylor@users.noreply.github.com> Date: Sat, 31 Aug 2024 22:02:20 -0700 Subject: [PATCH 1/2] update format command --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4b8eb83..f2f21dd 100644 --- a/README.md +++ b/README.md @@ -45,5 +45,5 @@ fastapi run main.py Format code before new changes ```bash -black . +black . && isort . ``` From 581620ec9dd24099cc71b0427fb13241137954fe Mon Sep 17 00:00:00 2001 From: Charlie Taylor Date: Sat, 31 Aug 2024 23:03:22 -0700 Subject: [PATCH 2/2] init changes --- Client.py | 3 +- SmsClient.py | 2 +- TestClient.py => TextTestClient.py | 9 ++-- config.py | 18 +++---- test_suite.py | 78 ++++++++++++++++++++++++++---- 5 files changed, 84 insertions(+), 26 deletions(-) rename TestClient.py => TextTestClient.py (80%) diff --git a/Client.py b/Client.py index c9a5540..13f7ea2 100644 --- a/Client.py +++ b/Client.py @@ -116,7 +116,8 @@ def handle_image(self, from_: str, url: str) -> None: self.send_message(from_, FAILED_PIC_SAVE) else: self.send_message( - from_, VIEW_SUBMISSIONS.format(self.generate_view_url(user.hash, prompt.id)) + from_, + VIEW_SUBMISSIONS.format(self.generate_view_url(user.hash, prompt.id)), ) def handle_admin_message(self, text: str) -> None: diff --git a/SmsClient.py b/SmsClient.py index 0742788..094a879 100644 --- a/SmsClient.py +++ b/SmsClient.py @@ -7,7 +7,7 @@ from Client import Client from config import Settings, settings -from constants import PROD_ENV, IGNORE_MESSAGE +from constants import IGNORE_MESSAGE, PROD_ENV from database import Database logger = logging.getLogger(__name__) diff --git a/TestClient.py b/TextTestClient.py similarity index 80% rename from TestClient.py rename to TextTestClient.py index f7e315d..98da56b 100644 --- a/TestClient.py +++ b/TextTestClient.py @@ -1,13 +1,11 @@ from typing import override -from sqlalchemy.orm import Session - from Client import Client from config import Settings, settings from database import Database -class TestClient(Client): +class TextTestClient(Client): def __init__(self, settings: Settings, db: Database | None = None): super().__init__(settings, db) @@ -22,12 +20,11 @@ def receive_message(self, from_: str, text: str): @override def handle_beta_message(self, from_: str, text: str): - prompt_text = " ".join(text.split(" ")[1:]) - return prompt_text + return text if __name__ == "__main__": - client = TextClient() + client = TextTestClient() while True: text = input() diff --git a/config.py b/config.py index 9c2239a..7844a65 100644 --- a/config.py +++ b/config.py @@ -4,15 +4,15 @@ class Settings(BaseSettings): model_config = SettingsConfigDict(env_file=".env") - environment: str - twilio_account_sid: str - twilio_auth_token: str - twilio_phone_number: str - db_name: str - hash_secret: str - admin_pass: str - beta_code: str - beta_allowlist: str + environment: str = "DEV" + twilio_account_sid: str = "" + twilio_auth_token: str = "" + twilio_phone_number: str = "" + db_name: str = "snapshot" + hash_secret: str = "test" + admin_pass: str = "definitely_not_admin" + beta_code: str = "test" + beta_allowlist: str = "" settings = Settings() diff --git a/test_suite.py b/test_suite.py index c81bff6..cb1e926 100644 --- a/test_suite.py +++ b/test_suite.py @@ -1,29 +1,89 @@ -import re from unittest.mock import MagicMock from config import settings from models import Registration -from TestClient import TestClient +from TextTestClient import TextTestClient settings.twilio_account_sid = "please_dont_work" settings.twilio_auth_token = "secret" settings.twilio_phone_number = "8675309" +USER = "Jenny" +NUM = "8675309" + + def test_handle_message_no_reg(): # Arrange db = MagicMock() db.get_reg = MagicMock(return_value=None) - # db.create_reg = MagicMock( - # return_value=Registration(username="Jenny", phone="8675309", state=0) - # ) + db.create_reg = MagicMock( + return_value=Registration(username=USER, phone=NUM, state=0) + ) + db.get_user_by_phone = MagicMock(return_value=None) + + tc = TextTestClient(settings, db) + # Act + tc.handle_message(NUM, "HI") + + # Assert + db.get_reg.assert_called_once_with(NUM) + db.create_reg.assert_called_once_with(NUM) + + +def test_handle_message_reg_state_0(): + # Arrange + db = MagicMock() + + db.get_reg = MagicMock(return_value=Registration(username=USER, phone=NUM, state=0)) + db.create_reg = MagicMock(return_value=None) + db.get_user_by_phone = MagicMock(return_value=None) + + tc = TextTestClient(settings, db) + # Act + tc.handle_message(NUM, "HI") + + # Assert + db.get_reg.assert_called_once_with(NUM) + db.create_reg.assert_not_called() + + +def test_handle_message_reg_state_1(): + # Arrange + db = MagicMock() + + db.get_reg = MagicMock(return_value=Registration(username=USER, phone=NUM, state=1)) + db.create_reg = MagicMock(return_value=None) + db.get_user_by_phone = MagicMock(return_value=None) + db.update_reg = MagicMock(return_value=None) + + tc = TextTestClient(settings, db) + # Act + tc.handle_message(NUM, USER) + + # Assert + db.get_reg.assert_called_once_with(NUM) + # db.update_reg.assert_called_once() + + +def test_handle_message_reg_state_2(): + # Arrange + db = MagicMock() + + db.get_reg = MagicMock(return_value=Registration(username=USER, phone=NUM, state=2)) + db.create_reg = MagicMock(return_value=None) db.get_user_by_phone = MagicMock(return_value=None) + db.update_reg = MagicMock(return_value=None) + db.create_user = MagicMock(return_value=None) + db.get_current_prompt = MagicMock(return_value=None) - tc = TestClient(settings, db) + tc = TextTestClient(settings, db) # Act - tc.receive_message("8675309", "HI") + tc.handle_message(NUM, "yes") # Assert - db.get_reg.assert_called_once_with("8675309") - db.create_reg.assert_called_once_with("8675309") + db.get_reg.assert_called_once_with(NUM) + db.create_user.assert_called_once_with(NUM, USER) + db.update_reg.assert_called_once_with(NUM, 3, USER) + db.get_current_prompt.assert_called_once()