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

Add send email task and testing for the task using mailhog #111

Merged
merged 7 commits into from
Oct 9, 2023

Conversation

joseph-sentry
Copy link
Contributor

@joseph-sentry joseph-sentry commented Sep 21, 2023

Changes send_email task to send a single email to the email of a specific owner using the SMTP service. The contents of the email are generated using the template service. The send_email task takes in an ownerid, from addr, subject, email and kwargs to be substituted in the template as its args.

Fixes: codecov/engineering-team#157
Depends on: #110 #108 #107

@codecov-staging
Copy link

codecov-staging bot commented Sep 21, 2023

Codecov Report

❗ No coverage uploaded for pull request base (main@aba7972). Click here to learn what that means.
The diff coverage is 87.79%.

❗ Current head bb70d24 differs from pull request most recent head 1088ac0. Consider uploading reports for the commit 1088ac0 to get more accurate results

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #111   +/-   ##
=======================================
  Coverage        ?   93.41%           
=======================================
  Files           ?      346           
  Lines           ?    26959           
  Branches        ?        0           
=======================================
  Hits            ?    25183           
  Misses          ?     1776           
  Partials        ?        0           
Flag Coverage Δ
integration 93.41% <87.79%> (?)
latest-uploader-overall 93.41% <87.79%> (?)
unit 93.41% <87.79%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 94.23% <100.00%> (?)
OutsideTasks 96.81% <100.00%> (?)
Files Coverage Δ
conftest.py 88.63% <100.00%> (ø)
services/tests/test_smtp.py 100.00% <100.00%> (ø)
tasks/send_email.py 100.00% <100.00%> (ø)
tasks/tests/unit/test_send_email_task.py 100.00% <100.00%> (ø)
tasks/tests/integration/test_send_email_task.py 36.36% <36.36%> (ø)

@codecov-public-qa
Copy link

codecov-public-qa bot commented Sep 21, 2023

Codecov Report

Merging #111 (1088ac0) into main (aba7972) will increase coverage by 5.14%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #111      +/-   ##
==========================================
+ Coverage   93.30%   98.44%   +5.14%     
==========================================
  Files         347      347              
  Lines       27113    27196      +83     
==========================================
+ Hits        25298    26774    +1476     
+ Misses       1815      422    -1393     
Flag Coverage Δ
integration 98.44% <100.00%> (+5.14%) ⬆️
latest-uploader-overall 98.44% <100.00%> (+5.14%) ⬆️
unit 98.44% <100.00%> (+5.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 97.02% <100.00%> (+3.00%) ⬆️
OutsideTasks 98.23% <100.00%> (+1.49%) ⬆️
Files Coverage Δ
conftest.py 94.69% <100.00%> (+6.60%) ⬆️
services/tests/test_smtp.py 100.00% <100.00%> (ø)
tasks/send_email.py 100.00% <100.00%> (+52.17%) ⬆️
tasks/tests/integration/test_send_email_task.py 100.00% <100.00%> (ø)
tasks/tests/unit/test_send_email_task.py 100.00% <100.00%> (ø)

... and 43 files with indirect coverage changes

@codecov
Copy link

codecov bot commented Sep 21, 2023

Codecov Report

Merging #111 (1088ac0) into main (aba7972) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##             main     #111    +/-   ##
========================================
  Coverage   98.39%   98.40%            
========================================
  Files         371      373     +2     
  Lines       27522    27692   +170     
========================================
+ Hits        27081    27251   +170     
  Misses        441      441            
Flag Coverage Δ
integration 98.44% <100.00%> (+5.14%) ⬆️
latest-uploader-overall 98.44% <100.00%> (+5.14%) ⬆️
unit 98.44% <100.00%> (+5.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 96.91% <100.00%> (+<0.01%) ⬆️
OutsideTasks 98.23% <100.00%> (+1.49%) ⬆️
Files Coverage Δ
conftest.py 94.69% <100.00%> (+0.25%) ⬆️
services/tests/test_smtp.py 100.00% <100.00%> (ø)
tasks/send_email.py 100.00% <100.00%> (ø)
tasks/tests/integration/test_send_email_task.py 100.00% <100.00%> (ø)
tasks/tests/unit/test_send_email_task.py 100.00% <100.00%> (ø)

This change has been scanned for critical changes. Learn more

@joseph-sentry joseph-sentry marked this pull request as ready for review September 27, 2023 14:22
@joseph-sentry joseph-sentry force-pushed the joseph/send-email branch 3 times, most recently from a6c9200 to 42874da Compare October 4, 2023 16:27
@codecov-qa
Copy link

codecov-qa bot commented Oct 4, 2023

Codecov Report

Merging #111 (1088ac0) into main (aba7972) will increase coverage by 5.14%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #111      +/-   ##
==========================================
+ Coverage   93.30%   98.44%   +5.14%     
==========================================
  Files         347      347              
  Lines       27113    27196      +83     
==========================================
+ Hits        25298    26774    +1476     
+ Misses       1815      422    -1393     
Flag Coverage Δ
integration 98.44% <100.00%> (+5.14%) ⬆️
latest-uploader-overall 98.44% <100.00%> (+5.14%) ⬆️
unit 98.44% <100.00%> (+5.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 97.02% <100.00%> (+3.00%) ⬆️
OutsideTasks 98.23% <100.00%> (+1.49%) ⬆️
Files Coverage Δ
conftest.py 94.69% <100.00%> (+6.60%) ⬆️
services/tests/test_smtp.py 100.00% <100.00%> (ø)
tasks/send_email.py 100.00% <100.00%> (+52.17%) ⬆️
tasks/tests/integration/test_send_email_task.py 100.00% <100.00%> (ø)
tasks/tests/unit/test_send_email_task.py 100.00% <100.00%> (ø)

... and 43 files with indirect coverage changes

Copy link
Contributor

@giovanni-guidini giovanni-guidini left a comment

Choose a reason for hiding this comment

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

Looks good. Good job testing the many different error cases in the unit tests.

There seems to be something wrong with the integration tests tho. The annotations would indicate that it didn't run. Maybe it's a CI issue (that we are not running the integration tests in CI), but I think it is worth investigating.

extra=log_extra_dict,
)
return None
to_addr = owner.email
Copy link
Contributor

Choose a reason for hiding this comment

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

I think some Owners might not have an email address... Maybe I'm confusing with orgs (which are also owners)

But I think it'd be inexpensive to check for that here, before trying to send the email

email_helper = Sendgrid(actual_type)
return email_helper.send_email(owner)

owners = db_session.query(Owner).filter_by(ownerid=ownerid)
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] I think it's fine to use the one-liner owners = db_session.query(Owner).filter_by(ownerid=ownerid).first() . It's all through the code.

There should ever only be 1 owner with any given ownerid after all

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah the original code did this as well, I see.... Still don't see much advantage to that over the one-liner

return None
template_service = TemplateService()

with metrics.timer("worker.tasks.send_email.render_templates"):
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice idea having this metric here, I like that

html_template = template_service.get_template(f"{template_name}.html")
html = html_template.render(**kwargs)

email_wrapper = Email(to_addr, from_addr, subject, text, html)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just so I understand this better, the txt template is a fallback to the html template in case the owner's email service can't handle html emails?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sort of, I guess if the email client of the person viewing the email does not support displaying HTML, then it will show the text

username="test_username",
)

assert metrics.data["worker.tasks.send_email.attempt"] == 1
Copy link
Contributor

Choose a reason for hiding this comment

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

This is nice. But you could have an assert for the result as well

* Changes send_email task to send a single email
  to the email of a specific owner using the SMTP
  service. The contents of the email are generated
  using the template service. The send_email task
  takes in an ownerid, from addr, subject, email and
  kwargs to be substituted in the template as its args.

* Adds testing for the send_email task using mailhog.

Signed-off-by: joseph-sentry <[email protected]>
Signed-off-by: joseph-sentry <[email protected]>
Signed-off-by: joseph-sentry <[email protected]>
Signed-off-by: joseph-sentry <[email protected]>
Signed-off-by: joseph-sentry <[email protected]>
Signed-off-by: joseph-sentry <[email protected]>
@joseph-sentry joseph-sentry merged commit 41cf406 into main Oct 9, 2023
25 of 26 checks passed
@joseph-sentry joseph-sentry deleted the joseph/send-email branch October 9, 2023 12:53
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.

Worker can send emails
2 participants