Spaces:
Sleeping
Sleeping
integrate new settings and add mailing tests
Browse files- blossomtune_gradio/mail.py +2 -1
- blossomtune_gradio/settings/blossomtune.schema.json +3 -3
- blossomtune_gradio/settings/blossomtune.yaml +1 -1
- blossomtune_gradio/ui/callbacks.py +20 -16
- blossomtune_gradio/ui/components.py +9 -3
- pyproject.toml +1 -0
- tests/__init__.py +0 -0
- tests/test_mail.py +149 -0
- uv.lock +17 -1
blossomtune_gradio/mail.py
CHANGED
|
@@ -74,7 +74,8 @@ class MailjetSender(EmailSender):
|
|
| 74 |
"""
|
| 75 |
Sends an email using the Mailjet transactional API v3.1.
|
| 76 |
"""
|
| 77 |
-
|
|
|
|
| 78 |
error_msg = "Mailjet API keys are not configured."
|
| 79 |
log(f"[Email] {error_msg}")
|
| 80 |
return False, error_msg
|
|
|
|
| 74 |
"""
|
| 75 |
Sends an email using the Mailjet transactional API v3.1.
|
| 76 |
"""
|
| 77 |
+
# Check for truthy values, not just attribute existence.
|
| 78 |
+
if not (cfg.SMTP_USER and cfg.SMTP_PASSWORD):
|
| 79 |
error_msg = "Mailjet API keys are not configured."
|
| 80 |
log(f"[Email] {error_msg}")
|
| 81 |
return False, error_msg
|
blossomtune_gradio/settings/blossomtune.schema.json
CHANGED
|
@@ -60,11 +60,11 @@
|
|
| 60 |
"type": "string",
|
| 61 |
"description": "Message for a denied participant. JINJA2: {{ participant_id }}"
|
| 62 |
},
|
| 63 |
-
"
|
| 64 |
"type": "string",
|
| 65 |
"description": "Admin warning when trying to approve a non-activated user."
|
| 66 |
},
|
| 67 |
-
"
|
| 68 |
"type": "string",
|
| 69 |
"description": "Admin warning for an already assigned partition ID. JINJA2: {{ partition_id }}"
|
| 70 |
},
|
|
@@ -99,7 +99,7 @@
|
|
| 99 |
"status_approved_md",
|
| 100 |
"status_pending_md",
|
| 101 |
"status_denied_md",
|
| 102 |
-
"
|
| 103 |
"partition_in_use_warning",
|
| 104 |
"auth_status_logged_in_owner_md",
|
| 105 |
"auth_status_logged_in_user_md",
|
|
|
|
| 60 |
"type": "string",
|
| 61 |
"description": "Message for a denied participant. JINJA2: {{ participant_id }}"
|
| 62 |
},
|
| 63 |
+
"participant_not_activated_warning_md": {
|
| 64 |
"type": "string",
|
| 65 |
"description": "Admin warning when trying to approve a non-activated user."
|
| 66 |
},
|
| 67 |
+
"partition_in_use_warning_md": {
|
| 68 |
"type": "string",
|
| 69 |
"description": "Admin warning for an already assigned partition ID. JINJA2: {{ partition_id }}"
|
| 70 |
},
|
|
|
|
| 99 |
"status_approved_md",
|
| 100 |
"status_pending_md",
|
| 101 |
"status_denied_md",
|
| 102 |
+
"participant_not_activated_warning_md",
|
| 103 |
"partition_in_use_warning",
|
| 104 |
"auth_status_logged_in_owner_md",
|
| 105 |
"auth_status_logged_in_user_md",
|
blossomtune_gradio/settings/blossomtune.yaml
CHANGED
|
@@ -62,7 +62,7 @@ ui:
|
|
| 62 |
Your request for ID `{{ participant_id }}` has been denied.
|
| 63 |
|
| 64 |
# --- Admin Panel ---
|
| 65 |
-
|
| 66 |
This participant has not activated their email yet. Approval is not allowed.
|
| 67 |
|
| 68 |
partition_in_use_warning: |
|
|
|
|
| 62 |
Your request for ID `{{ participant_id }}` has been denied.
|
| 63 |
|
| 64 |
# --- Admin Panel ---
|
| 65 |
+
participant_not_activated_warning_md: |
|
| 66 |
This participant has not activated their email yet. Approval is not allowed.
|
| 67 |
|
| 68 |
partition_in_use_warning: |
|
blossomtune_gradio/ui/callbacks.py
CHANGED
|
@@ -7,6 +7,7 @@ from blossomtune_gradio import config as cfg
|
|
| 7 |
from blossomtune_gradio.logs import log
|
| 8 |
from blossomtune_gradio import federation as fed
|
| 9 |
from blossomtune_gradio import processing
|
|
|
|
| 10 |
|
| 11 |
from . import components
|
| 12 |
from . import auth
|
|
@@ -23,23 +24,26 @@ def get_full_status_update(
|
|
| 23 |
profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None
|
| 24 |
):
|
| 25 |
owner = auth.is_space_owner(profile, oauth_token)
|
| 26 |
-
auth_status = "Authenticating..."
|
| 27 |
is_on_space = cfg.SPACE_OWNER is not None
|
| 28 |
hf_handle_val = ""
|
| 29 |
hf_handle_interactive = not is_on_space
|
| 30 |
|
| 31 |
if is_on_space:
|
| 32 |
if profile:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
hf_handle_val = profile.username
|
| 39 |
else:
|
| 40 |
-
auth_status =
|
| 41 |
else:
|
| 42 |
-
auth_status =
|
| 43 |
|
| 44 |
with sqlite3.connect(cfg.DB_PATH) as conn:
|
| 45 |
pending_rows = conn.execute(
|
|
@@ -58,11 +62,11 @@ def get_full_status_update(
|
|
| 58 |
and processing.process_store["runner"].poll() is None
|
| 59 |
)
|
| 60 |
|
|
|
|
| 61 |
superlink_status = "🟢 Running" if superlink_is_running else "🔴 Not Running"
|
| 62 |
runner_status = "🟢 Running" if runner_is_running else "🔴 Not Running"
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
# Use gr.update() to modify existing components instead of creating new ones.
|
| 66 |
if superlink_is_running:
|
| 67 |
superlink_btn_update = gr.update(value="🛑 Stop Superlink", variant="stop")
|
| 68 |
else:
|
|
@@ -78,7 +82,6 @@ def get_full_status_update(
|
|
| 78 |
return {
|
| 79 |
components.admin_panel: gr.update(visible=owner),
|
| 80 |
components.auth_status_md: gr.update(value=auth_status),
|
| 81 |
-
# components.log_output: gr.update(value=log.output),
|
| 82 |
components.superlink_status_public_txt: gr.update(value=superlink_status),
|
| 83 |
components.superlink_status_admin_txt: gr.update(value=superlink_status),
|
| 84 |
components.runner_status_txt: gr.update(value=runner_status),
|
|
@@ -107,6 +110,7 @@ def toggle_superlink(
|
|
| 107 |
):
|
| 108 |
"""Toggles the Superlink process on or off."""
|
| 109 |
if not auth.is_space_owner(profile, oauth_token):
|
|
|
|
| 110 |
gr.Warning("You are not authorized to perform this operation.")
|
| 111 |
return
|
| 112 |
if (
|
|
@@ -127,6 +131,7 @@ def toggle_runner(
|
|
| 127 |
):
|
| 128 |
"""Toggles the Runner process on or off."""
|
| 129 |
if not auth.is_space_owner(profile, oauth_token):
|
|
|
|
| 130 |
gr.Warning("You are not authorized to perform this operation.")
|
| 131 |
return
|
| 132 |
if (
|
|
@@ -147,17 +152,14 @@ def on_select_pending(pending_data: list, evt: gr.SelectData):
|
|
| 147 |
if not evt.index:
|
| 148 |
return "", ""
|
| 149 |
|
| 150 |
-
# Ensure pending_data is a Pandas DataFrame
|
| 151 |
pending_df = pd.DataFrame(
|
| 152 |
pending_data, columns=["Participant ID", "HF Handle", "Email"]
|
| 153 |
)
|
| 154 |
|
| 155 |
row_index = evt.index[0]
|
| 156 |
-
# Use pending_df.empty to check for truthiness
|
| 157 |
if pending_df.empty or row_index >= len(pending_df):
|
| 158 |
return "", ""
|
| 159 |
|
| 160 |
-
# Use .iloc to safely access the row by index
|
| 161 |
participant_id = pending_df.iloc[row_index, 0]
|
| 162 |
return participant_id, str(fed.get_next_partion_id())
|
| 163 |
|
|
@@ -169,7 +171,7 @@ def on_check_participant_status(
|
|
| 169 |
if is_on_space and not profile:
|
| 170 |
return {
|
| 171 |
components.request_status_md: gr.update(
|
| 172 |
-
"
|
| 173 |
)
|
| 174 |
}
|
| 175 |
|
|
@@ -177,13 +179,14 @@ def on_check_participant_status(
|
|
| 177 |
if not user_hf_handle or not user_hf_handle.strip():
|
| 178 |
return {
|
| 179 |
components.request_status_md: gr.update(
|
| 180 |
-
value=
|
| 181 |
)
|
| 182 |
}
|
| 183 |
|
| 184 |
pid_to_check = user_hf_handle.strip()
|
| 185 |
email_to_add = email.strip()
|
| 186 |
activation_code_to_check = activation_code.strip()
|
|
|
|
| 187 |
_, message = fed.check_participant_status(
|
| 188 |
pid_to_check, email_to_add, activation_code_to_check
|
| 189 |
)
|
|
@@ -191,6 +194,7 @@ def on_check_participant_status(
|
|
| 191 |
|
| 192 |
|
| 193 |
def on_manage_fed_request(participant_id: str, partition_id: str, action: str):
|
|
|
|
| 194 |
result, message = fed.manage_request(participant_id, partition_id, action)
|
| 195 |
if result:
|
| 196 |
gr.Info(message)
|
|
|
|
| 7 |
from blossomtune_gradio.logs import log
|
| 8 |
from blossomtune_gradio import federation as fed
|
| 9 |
from blossomtune_gradio import processing
|
| 10 |
+
from blossomtune_gradio.settings import settings
|
| 11 |
|
| 12 |
from . import components
|
| 13 |
from . import auth
|
|
|
|
| 24 |
profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None
|
| 25 |
):
|
| 26 |
owner = auth.is_space_owner(profile, oauth_token)
|
| 27 |
+
auth_status = "Authenticating..." # Initial state, not in schema
|
| 28 |
is_on_space = cfg.SPACE_OWNER is not None
|
| 29 |
hf_handle_val = ""
|
| 30 |
hf_handle_interactive = not is_on_space
|
| 31 |
|
| 32 |
if is_on_space:
|
| 33 |
if profile:
|
| 34 |
+
if owner:
|
| 35 |
+
auth_status = settings.get_text(
|
| 36 |
+
"auth_status_logged_in_owner_md", profile=profile
|
| 37 |
+
)
|
| 38 |
+
else:
|
| 39 |
+
auth_status = settings.get_text(
|
| 40 |
+
"auth_status_logged_in_user_md", profile=profile
|
| 41 |
+
)
|
| 42 |
hf_handle_val = profile.username
|
| 43 |
else:
|
| 44 |
+
auth_status = settings.get_text("auth_status_not_logged_in_md")
|
| 45 |
else:
|
| 46 |
+
auth_status = settings.get_text("auth_status_local_mode_md")
|
| 47 |
|
| 48 |
with sqlite3.connect(cfg.DB_PATH) as conn:
|
| 49 |
pending_rows = conn.execute(
|
|
|
|
| 62 |
and processing.process_store["runner"].poll() is None
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# Hardcode status text as it's not in the schema
|
| 66 |
superlink_status = "🟢 Running" if superlink_is_running else "🔴 Not Running"
|
| 67 |
runner_status = "🟢 Running" if runner_is_running else "🔴 Not Running"
|
| 68 |
|
| 69 |
+
# Hardcode button text as it's not in the schema
|
|
|
|
| 70 |
if superlink_is_running:
|
| 71 |
superlink_btn_update = gr.update(value="🛑 Stop Superlink", variant="stop")
|
| 72 |
else:
|
|
|
|
| 82 |
return {
|
| 83 |
components.admin_panel: gr.update(visible=owner),
|
| 84 |
components.auth_status_md: gr.update(value=auth_status),
|
|
|
|
| 85 |
components.superlink_status_public_txt: gr.update(value=superlink_status),
|
| 86 |
components.superlink_status_admin_txt: gr.update(value=superlink_status),
|
| 87 |
components.runner_status_txt: gr.update(value=runner_status),
|
|
|
|
| 110 |
):
|
| 111 |
"""Toggles the Superlink process on or off."""
|
| 112 |
if not auth.is_space_owner(profile, oauth_token):
|
| 113 |
+
# Hardcode warning text as it's not in the schema
|
| 114 |
gr.Warning("You are not authorized to perform this operation.")
|
| 115 |
return
|
| 116 |
if (
|
|
|
|
| 131 |
):
|
| 132 |
"""Toggles the Runner process on or off."""
|
| 133 |
if not auth.is_space_owner(profile, oauth_token):
|
| 134 |
+
# Hardcode warning text as it's not in the schema
|
| 135 |
gr.Warning("You are not authorized to perform this operation.")
|
| 136 |
return
|
| 137 |
if (
|
|
|
|
| 152 |
if not evt.index:
|
| 153 |
return "", ""
|
| 154 |
|
|
|
|
| 155 |
pending_df = pd.DataFrame(
|
| 156 |
pending_data, columns=["Participant ID", "HF Handle", "Email"]
|
| 157 |
)
|
| 158 |
|
| 159 |
row_index = evt.index[0]
|
|
|
|
| 160 |
if pending_df.empty or row_index >= len(pending_df):
|
| 161 |
return "", ""
|
| 162 |
|
|
|
|
| 163 |
participant_id = pending_df.iloc[row_index, 0]
|
| 164 |
return participant_id, str(fed.get_next_partion_id())
|
| 165 |
|
|
|
|
| 171 |
if is_on_space and not profile:
|
| 172 |
return {
|
| 173 |
components.request_status_md: gr.update(
|
| 174 |
+
value=settings.get_text("auth_required_md")
|
| 175 |
)
|
| 176 |
}
|
| 177 |
|
|
|
|
| 179 |
if not user_hf_handle or not user_hf_handle.strip():
|
| 180 |
return {
|
| 181 |
components.request_status_md: gr.update(
|
| 182 |
+
value=settings.get_text("hf_handle_empty_md")
|
| 183 |
)
|
| 184 |
}
|
| 185 |
|
| 186 |
pid_to_check = user_hf_handle.strip()
|
| 187 |
email_to_add = email.strip()
|
| 188 |
activation_code_to_check = activation_code.strip()
|
| 189 |
+
# The federation module is responsible for getting the correct text from settings
|
| 190 |
_, message = fed.check_participant_status(
|
| 191 |
pid_to_check, email_to_add, activation_code_to_check
|
| 192 |
)
|
|
|
|
| 194 |
|
| 195 |
|
| 196 |
def on_manage_fed_request(participant_id: str, partition_id: str, action: str):
|
| 197 |
+
# The federation module is responsible for getting the correct text from settings
|
| 198 |
result, message = fed.manage_request(participant_id, partition_id, action)
|
| 199 |
if result:
|
| 200 |
gr.Info(message)
|
blossomtune_gradio/ui/components.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
|
|
|
|
|
|
|
| 4 |
auth_status_md = gr.Markdown("Authenticating...", render=False)
|
|
|
|
| 5 |
superlink_status_public_txt = gr.Textbox(
|
| 6 |
"🔴 Not Running", label="Superlink Status", interactive=False, render=False
|
| 7 |
)
|
|
@@ -13,11 +16,13 @@ log_output = gr.Textbox(
|
|
| 13 |
show_copy_button=True,
|
| 14 |
render=False,
|
| 15 |
)
|
|
|
|
| 16 |
superlink_toggle_btn = gr.Button("🚀 Start Superlink", variant="secondary")
|
|
|
|
| 17 |
hf_handle_tb = gr.Textbox(
|
| 18 |
label="Your Hugging Face Handle",
|
| 19 |
placeholder="Enter for local testing...",
|
| 20 |
-
interactive=True, #
|
| 21 |
render=False,
|
| 22 |
)
|
| 23 |
email_tb = gr.Textbox(
|
|
@@ -37,7 +42,6 @@ superlink_status_admin_txt = gr.Textbox(
|
|
| 37 |
interactive=False,
|
| 38 |
render=False,
|
| 39 |
)
|
| 40 |
-
superlink_toggle_btn = gr.Button("🚀 Start Superlink", variant="secondary")
|
| 41 |
runner_status_txt = gr.Textbox(
|
| 42 |
"🔴 Not Running", label="Runner Status", interactive=False, render=False
|
| 43 |
)
|
|
@@ -52,7 +56,9 @@ run_id_tb = gr.Textbox(label="Run ID", placeholder="e.g., run_123")
|
|
| 52 |
num_partitions_tb = gr.Textbox(
|
| 53 |
label="Total Partitions", value="10", placeholder="e.g., 10", render=False
|
| 54 |
)
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
pending_requests_df = gr.DataFrame(
|
| 57 |
headers=["Participant ID", "HF Handle", "Email"],
|
| 58 |
label="Pending Requests (click a row to select)",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
|
| 4 |
+
# This component's value is updated dynamically by callbacks.
|
| 5 |
+
# We'll use a simple initial value here as the schema doesn't define an "authenticating" state.
|
| 6 |
auth_status_md = gr.Markdown("Authenticating...", render=False)
|
| 7 |
+
|
| 8 |
superlink_status_public_txt = gr.Textbox(
|
| 9 |
"🔴 Not Running", label="Superlink Status", interactive=False, render=False
|
| 10 |
)
|
|
|
|
| 16 |
show_copy_button=True,
|
| 17 |
render=False,
|
| 18 |
)
|
| 19 |
+
# The button's text/variant is updated dynamically by callbacks.
|
| 20 |
superlink_toggle_btn = gr.Button("🚀 Start Superlink", variant="secondary")
|
| 21 |
+
|
| 22 |
hf_handle_tb = gr.Textbox(
|
| 23 |
label="Your Hugging Face Handle",
|
| 24 |
placeholder="Enter for local testing...",
|
| 25 |
+
interactive=True, # This is controlled by callbacks
|
| 26 |
render=False,
|
| 27 |
)
|
| 28 |
email_tb = gr.Textbox(
|
|
|
|
| 42 |
interactive=False,
|
| 43 |
render=False,
|
| 44 |
)
|
|
|
|
| 45 |
runner_status_txt = gr.Textbox(
|
| 46 |
"🔴 Not Running", label="Runner Status", interactive=False, render=False
|
| 47 |
)
|
|
|
|
| 56 |
num_partitions_tb = gr.Textbox(
|
| 57 |
label="Total Partitions", value="10", placeholder="e.g., 10", render=False
|
| 58 |
)
|
| 59 |
+
# This component is populated by callbacks using the settings file.
|
| 60 |
+
request_status_md = gr.Markdown(render=False)
|
| 61 |
+
|
| 62 |
pending_requests_df = gr.DataFrame(
|
| 63 |
headers=["Participant ID", "HF Handle", "Email"],
|
| 64 |
label="Pending Requests (click a row to select)",
|
pyproject.toml
CHANGED
|
@@ -69,6 +69,7 @@ convention = "google" # Accepts: "google", "numpy", or "pep257".
|
|
| 69 |
[dependency-groups]
|
| 70 |
dev = [
|
| 71 |
"pytest>=8.4.1",
|
|
|
|
| 72 |
]
|
| 73 |
|
| 74 |
[tool.pytest.ini_options]
|
|
|
|
| 69 |
[dependency-groups]
|
| 70 |
dev = [
|
| 71 |
"pytest>=8.4.1",
|
| 72 |
+
"pytest-mock>=3.15.1",
|
| 73 |
]
|
| 74 |
|
| 75 |
[tool.pytest.ini_options]
|
tests/__init__.py
ADDED
|
File without changes
|
tests/test_mail.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import pytest
|
| 3 |
+
from unittest.mock import MagicMock, patch
|
| 4 |
+
|
| 5 |
+
from blossomtune_gradio import mail
|
| 6 |
+
from blossomtune_gradio import config as cfg
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class TestSMTPMailSender:
|
| 10 |
+
"""Tests for the SMTPMailSender class."""
|
| 11 |
+
|
| 12 |
+
@pytest.fixture
|
| 13 |
+
def sender(self):
|
| 14 |
+
return mail.SMTPMailSender()
|
| 15 |
+
|
| 16 |
+
def test_send_email_success(self, sender, mocker):
|
| 17 |
+
"""Verify that a successful email send works as expected."""
|
| 18 |
+
mock_smtp = mocker.patch("smtplib.SMTP")
|
| 19 |
+
recipient = "test@example.com"
|
| 20 |
+
subject = "Test Subject"
|
| 21 |
+
body = "Test Body"
|
| 22 |
+
|
| 23 |
+
success, message = sender.send_email(recipient, subject, body)
|
| 24 |
+
|
| 25 |
+
assert success is True
|
| 26 |
+
assert message == ""
|
| 27 |
+
mock_smtp.assert_called_once_with(cfg.SMTP_SERVER, cfg.SMTP_PORT)
|
| 28 |
+
# Check that send_message was called on the context manager's result
|
| 29 |
+
mock_smtp.return_value.__enter__.return_value.send_message.assert_called_once()
|
| 30 |
+
|
| 31 |
+
def test_send_email_failure(self, sender, mocker):
|
| 32 |
+
"""Verify that an SMTP error is handled correctly."""
|
| 33 |
+
mocker.patch("smtplib.SMTP", side_effect=Exception("SMTP Connection Error"))
|
| 34 |
+
recipient = "test@example.com"
|
| 35 |
+
subject = "Test Subject"
|
| 36 |
+
body = "Test Body"
|
| 37 |
+
|
| 38 |
+
success, message = sender.send_email(recipient, subject, body)
|
| 39 |
+
|
| 40 |
+
assert success is False
|
| 41 |
+
assert "SMTP Connection Error" in message
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class TestMailjetSender:
|
| 45 |
+
"""Tests for the MailjetSender class."""
|
| 46 |
+
|
| 47 |
+
@pytest.fixture
|
| 48 |
+
def sender(self):
|
| 49 |
+
return mail.MailjetSender()
|
| 50 |
+
|
| 51 |
+
def test_send_email_success(self, sender, mocker):
|
| 52 |
+
"""Verify a successful send via the Mailjet API."""
|
| 53 |
+
mock_post = mocker.patch("requests.post")
|
| 54 |
+
mock_response = MagicMock()
|
| 55 |
+
mock_response.status_code = 200
|
| 56 |
+
mock_response.raise_for_status.return_value = None
|
| 57 |
+
mock_post.return_value = mock_response
|
| 58 |
+
|
| 59 |
+
# Mock config attributes
|
| 60 |
+
mocker.patch.object(cfg, "SMTP_USER", "test_api_key")
|
| 61 |
+
mocker.patch.object(cfg, "SMTP_PASSWORD", "test_api_secret")
|
| 62 |
+
|
| 63 |
+
success, message = sender.send_email("test@example.com", "Subject", "Body")
|
| 64 |
+
|
| 65 |
+
assert success is True
|
| 66 |
+
assert message == ""
|
| 67 |
+
mock_post.assert_called_once()
|
| 68 |
+
|
| 69 |
+
def test_send_email_api_failure(self, sender, mocker):
|
| 70 |
+
"""Verify that a Mailjet API error is handled correctly."""
|
| 71 |
+
mock_post = mocker.patch(
|
| 72 |
+
"requests.post",
|
| 73 |
+
side_effect=requests.exceptions.RequestException("API Error"),
|
| 74 |
+
)
|
| 75 |
+
# Mock the response attribute on the exception
|
| 76 |
+
mock_post.side_effect.response = MagicMock(text="Bad Request")
|
| 77 |
+
|
| 78 |
+
mocker.patch.object(cfg, "SMTP_USER", "test_api_key")
|
| 79 |
+
mocker.patch.object(cfg, "SMTP_PASSWORD", "test_api_secret")
|
| 80 |
+
|
| 81 |
+
success, message = sender.send_email("test@example.com", "Subject", "Body")
|
| 82 |
+
|
| 83 |
+
assert success is False
|
| 84 |
+
assert "API Error" in message
|
| 85 |
+
|
| 86 |
+
def test_send_email_no_credentials(self, sender, mocker):
|
| 87 |
+
"""Verify failure when Mailjet credentials are not configured."""
|
| 88 |
+
# Ensure the attributes don't exist
|
| 89 |
+
if hasattr(cfg, "SMTP_USER"):
|
| 90 |
+
mocker.stopall() # Stop any previous mocks if needed
|
| 91 |
+
mocker.patch.object(cfg, "SMTP_USER", None)
|
| 92 |
+
|
| 93 |
+
success, message = sender.send_email("test@example.com", "Subject", "Body")
|
| 94 |
+
assert success is False
|
| 95 |
+
assert "Mailjet API keys are not configured" in message
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class TestEmailFactory:
|
| 99 |
+
"""Tests for the get_email_sender factory function."""
|
| 100 |
+
|
| 101 |
+
def test_get_smtp_sender_by_default(self, mocker):
|
| 102 |
+
"""Verify it returns SMTP sender if provider is not set."""
|
| 103 |
+
# Ensure EMAIL_PROVIDER is not set on cfg
|
| 104 |
+
if hasattr(cfg, "EMAIL_PROVIDER"):
|
| 105 |
+
mocker.patch.object(cfg, "EMAIL_PROVIDER", None)
|
| 106 |
+
sender = mail.get_email_sender()
|
| 107 |
+
assert isinstance(sender, mail.SMTPMailSender)
|
| 108 |
+
|
| 109 |
+
def test_get_smtp_sender_explicitly(self, mocker):
|
| 110 |
+
"""Verify it returns SMTP sender when configured."""
|
| 111 |
+
mocker.patch.object(cfg, "EMAIL_PROVIDER", "smtp")
|
| 112 |
+
sender = mail.get_email_sender()
|
| 113 |
+
assert isinstance(sender, mail.SMTPMailSender)
|
| 114 |
+
|
| 115 |
+
def test_get_mailjet_sender(self, mocker):
|
| 116 |
+
"""Verify it returns Mailjet sender when configured."""
|
| 117 |
+
mocker.patch.object(cfg, "EMAIL_PROVIDER", "mailjet")
|
| 118 |
+
sender = mail.get_email_sender()
|
| 119 |
+
assert isinstance(sender, mail.MailjetSender)
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
@patch("blossomtune_gradio.mail.get_email_sender")
|
| 123 |
+
def test_send_activation_email_success(mock_get_sender):
|
| 124 |
+
"""Test successful activation email dispatch."""
|
| 125 |
+
mock_sender_instance = MagicMock()
|
| 126 |
+
mock_sender_instance.send_email.return_value = (True, "")
|
| 127 |
+
mock_get_sender.return_value = mock_sender_instance
|
| 128 |
+
|
| 129 |
+
success, message = mail.send_activation_email("test@example.com", "12345")
|
| 130 |
+
|
| 131 |
+
assert success is True
|
| 132 |
+
assert message == ""
|
| 133 |
+
mock_sender_instance.send_email.assert_called_once()
|
| 134 |
+
# Check that the subject contains the right text
|
| 135 |
+
call_args, _ = mock_sender_instance.send_email.call_args
|
| 136 |
+
assert "Your BlossomTune Activation Code" in call_args[1]
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
@patch("blossomtune_gradio.mail.get_email_sender")
|
| 140 |
+
def test_send_activation_email_failure(mock_get_sender):
|
| 141 |
+
"""Test failed activation email dispatch."""
|
| 142 |
+
mock_sender_instance = MagicMock()
|
| 143 |
+
mock_sender_instance.send_email.return_value = (False, "Provider Error")
|
| 144 |
+
mock_get_sender.return_value = mock_sender_instance
|
| 145 |
+
|
| 146 |
+
success, message = mail.send_activation_email("test@example.com", "12345")
|
| 147 |
+
|
| 148 |
+
assert success is False
|
| 149 |
+
assert "Provider Error" in message
|
uv.lock
CHANGED
|
@@ -242,6 +242,7 @@ dependencies = [
|
|
| 242 |
[package.dev-dependencies]
|
| 243 |
dev = [
|
| 244 |
{ name = "pytest" },
|
|
|
|
| 245 |
]
|
| 246 |
|
| 247 |
[package.metadata]
|
|
@@ -259,7 +260,10 @@ requires-dist = [
|
|
| 259 |
]
|
| 260 |
|
| 261 |
[package.metadata.requires-dev]
|
| 262 |
-
dev = [
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
[[package]]
|
| 265 |
name = "brotli"
|
|
@@ -2081,6 +2085,18 @@ wheels = [
|
|
| 2081 |
{ url = "https://files.pythonhosted.org/packages/a8/a4/20da314d277121d6534b3a980b29035dcd51e6744bd79075a6ce8fa4eb8d/pytest-8.4.2-py3-none-any.whl", hash = "sha256:872f880de3fc3a5bdc88a11b39c9710c3497a547cfa9320bc3c5e62fbf272e79", size = 365750, upload-time = "2025-09-04T14:34:20.226Z" },
|
| 2082 |
]
|
| 2083 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2084 |
[[package]]
|
| 2085 |
name = "python-dateutil"
|
| 2086 |
version = "2.9.0.post0"
|
|
|
|
| 242 |
[package.dev-dependencies]
|
| 243 |
dev = [
|
| 244 |
{ name = "pytest" },
|
| 245 |
+
{ name = "pytest-mock" },
|
| 246 |
]
|
| 247 |
|
| 248 |
[package.metadata]
|
|
|
|
| 260 |
]
|
| 261 |
|
| 262 |
[package.metadata.requires-dev]
|
| 263 |
+
dev = [
|
| 264 |
+
{ name = "pytest", specifier = ">=8.4.1" },
|
| 265 |
+
{ name = "pytest-mock", specifier = ">=3.15.1" },
|
| 266 |
+
]
|
| 267 |
|
| 268 |
[[package]]
|
| 269 |
name = "brotli"
|
|
|
|
| 2085 |
{ url = "https://files.pythonhosted.org/packages/a8/a4/20da314d277121d6534b3a980b29035dcd51e6744bd79075a6ce8fa4eb8d/pytest-8.4.2-py3-none-any.whl", hash = "sha256:872f880de3fc3a5bdc88a11b39c9710c3497a547cfa9320bc3c5e62fbf272e79", size = 365750, upload-time = "2025-09-04T14:34:20.226Z" },
|
| 2086 |
]
|
| 2087 |
|
| 2088 |
+
[[package]]
|
| 2089 |
+
name = "pytest-mock"
|
| 2090 |
+
version = "3.15.1"
|
| 2091 |
+
source = { registry = "https://pypi.org/simple" }
|
| 2092 |
+
dependencies = [
|
| 2093 |
+
{ name = "pytest" },
|
| 2094 |
+
]
|
| 2095 |
+
sdist = { url = "https://files.pythonhosted.org/packages/68/14/eb014d26be205d38ad5ad20d9a80f7d201472e08167f0bb4361e251084a9/pytest_mock-3.15.1.tar.gz", hash = "sha256:1849a238f6f396da19762269de72cb1814ab44416fa73a8686deac10b0d87a0f", size = 34036, upload-time = "2025-09-16T16:37:27.081Z" }
|
| 2096 |
+
wheels = [
|
| 2097 |
+
{ url = "https://files.pythonhosted.org/packages/5a/cc/06253936f4a7fa2e0f48dfe6d851d9c56df896a9ab09ac019d70b760619c/pytest_mock-3.15.1-py3-none-any.whl", hash = "sha256:0a25e2eb88fe5168d535041d09a4529a188176ae608a6d249ee65abc0949630d", size = 10095, upload-time = "2025-09-16T16:37:25.734Z" },
|
| 2098 |
+
]
|
| 2099 |
+
|
| 2100 |
[[package]]
|
| 2101 |
name = "python-dateutil"
|
| 2102 |
version = "2.9.0.post0"
|