Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Send mail action failed #21

Merged
merged 3 commits into from
Jan 8, 2025
Merged

fix: Send mail action failed #21

merged 3 commits into from
Jan 8, 2025

Conversation

fsbraun
Copy link
Member

@fsbraun fsbraun commented Jan 8, 2025

This PR fixes the send_mail_action and adds a test for it

Summary by Sourcery

Fix the send mail action to handle missing request headers and add tests for the action.

Bug Fixes:

  • Handle missing "User-Agent" and "Referer" headers in the request to prevent errors.

Tests:

  • Add tests for the send mail action, including cases with and without recipients.

Copy link

sourcery-ai bot commented Jan 8, 2025

Reviewer's Guide by Sourcery

This pull request fixes a bug in the send_mail action where emails were not being sent to the specified recipients. It also adds tests for the send_mail action.

Sequence diagram for the fixed send_mail action flow

sequenceDiagram
    participant Form
    participant SendMailAction
    participant DjangoMail

    Form->>SendMailAction: execute(form, request)
    SendMailAction->>SendMailAction: get_parameter(sendemail_recipients)
    SendMailAction->>SendMailAction: split recipients string
    SendMailAction->>SendMailAction: render templates
    alt no recipients
        SendMailAction->>DjangoMail: mail_admins(subject, message, html_message)
    else has recipients
        SendMailAction->>DjangoMail: send_mail(subject, message, from_mail, recipients, html_message)
    end
Loading

Class diagram for the updated SendMailAction class

classDiagram
    class SendMailAction {
        +execute(form, request)
        -get_parameter(form, param_name)
        -subject: str
        -from_mail: str
    }
    note for SendMailAction "Fixed recipients handling and template rendering"
Loading

File-Level Changes

Change Details Files
Fix the send_mail action to handle recipient email addresses correctly.
  • Change the way recipient email addresses are parsed from the action parameters.
  • Use the split() method to split the recipient string into a list of email addresses.
  • Handle cases where the recipient string is empty by sending the email to the site admins.
  • Add handling for missing User-Agent and Referer headers in the request.
djangocms_form_builder/actions.py
Add tests for the send_mail action.
  • Add a test case to verify that the send_mail action sends emails to the correct recipients.
  • Add a test case to verify that the email subject and body are rendered correctly.
  • Add a test case to verify that the action falls back to sending emails to the site admins when no recipients are specified.
tests/test_actions.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @fsbraun - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding email address validation before attempting to send. This would provide better error messages and fail faster than waiting for send_mail to raise an exception.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

djangocms_form_builder/actions.py Show resolved Hide resolved
tests/test_actions.py Outdated Show resolved Hide resolved
tests/test_actions.py Outdated Show resolved Hide resolved
@fsbraun fsbraun force-pushed the fix/send-mail-action branch from b36e989 to 816f3c1 Compare January 8, 2025 12:15
@fsbraun
Copy link
Member Author

fsbraun commented Jan 8, 2025

@sourcery-ai review

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @fsbraun - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

recipients = (self.get_parameter(form, "sendemail_recipients") or [])
from django.core.mail import mail_admins, send_mail

recipients = (self.get_parameter(form, "sendemail_recipients") or "").split()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Using whitespace splitting for email addresses is risky. Consider using a more robust delimiter like commas.

Email addresses might contain spaces (e.g., in quoted strings). This could lead to invalid recipient lists.

plugin.instance = plugin_instance

# Simulate form submission
with patch("django.core.mail.send_mail") as mock_send_mail:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Extract duplicate code into method (extract-duplicate-method)

@fsbraun fsbraun merged commit 743714b into main Jan 8, 2025
38 checks passed
@fsbraun fsbraun deleted the fix/send-mail-action branch January 8, 2025 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant