-
Notifications
You must be signed in to change notification settings - Fork 185
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
for mpp-1813: add new_from_address flag #1913
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ SOCKETLABS_SECRET_KEY="dummy-value" | |
SOCKETLABS_API_KEY="dummy-value" | ||
SOCKETLABS_VALIDATION_KEY="dummy-value" | ||
RELAY_FROM_ADDRESS="[email protected]:8000" | ||
NEW_RELAY_FROM_ADDRESS="[email protected]:8000" | ||
SITE_ORIGIN="http://127.0.0.1:8000" | ||
MAX_NUM_FREE_ALIASES=5 | ||
TWILIO_ACCOUNT_SID= | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
from email.utils import parseaddr | ||
|
||
from django.conf import settings | ||
from django.contrib.auth.models import User | ||
from django.test import TestCase, override_settings | ||
from unittest.mock import patch | ||
from model_bakery import baker | ||
from waffle.testutils import override_sample | ||
from waffle.models import Flag | ||
|
||
from emails.models import get_domains_from_settings | ||
from emails.utils import ( | ||
NEW_FROM_ADDRESS_FLAG_NAME, | ||
generate_relay_From, | ||
get_email_domain_from_settings, | ||
remove_trackers, | ||
) | ||
|
||
from .models_tests import make_premium_test_user | ||
from .models_tests import make_free_test_user, make_premium_test_user | ||
|
||
|
||
class FormattingToolsTest(TestCase): | ||
|
@@ -91,6 +95,63 @@ def test_generate_relay_From_with_premium_user(self): | |
) | ||
assert formatted_from_address == expected_formatted_from | ||
|
||
@override_settings(RELAY_FROM_ADDRESS="[email protected]", | ||
NEW_RELAY_FROM_ADDRESS="[email protected]") | ||
def test_generate_relay_From_with_new_from_user(self): | ||
free_user = make_free_test_user() | ||
new_from_flag = Flag.objects.create(name=NEW_FROM_ADDRESS_FLAG_NAME) | ||
new_from_flag.users.add(free_user) | ||
original_from_address = '"foo bar" <[email protected]>' | ||
formatted_from_address = generate_relay_From( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest an Failing locally before setting |
||
original_from_address, free_user.profile_set.first() | ||
) | ||
expected_encoded_display_name = ( | ||
"=?utf-8?b?IiJmb28gYmFyIiA8Zm9vQGJhci5jb20+IFt2aWEgUmVsYXldIg==?=" | ||
) | ||
expected_formatted_from = "%s <%s>" % ( | ||
expected_encoded_display_name, | ||
"[email protected]", | ||
) | ||
assert formatted_from_address == expected_formatted_from | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Failing in CircleCI:
|
||
# WTF? TestCase tearDown doesn't clear out this waffle flag? | ||
new_from_flag.users.remove(free_user) | ||
|
||
@override_settings(RELAY_FROM_ADDRESS="[email protected]", | ||
NEW_RELAY_FROM_ADDRESS="[email protected]") | ||
def test_generate_relay_From_with_non_flagged_user(self): | ||
free_user = make_free_test_user() | ||
Flag.objects.create(name=NEW_FROM_ADDRESS_FLAG_NAME) | ||
original_from_address = '"foo bar" <[email protected]>' | ||
formatted_from_address = generate_relay_From( | ||
original_from_address, free_user.profile_set.first() | ||
) | ||
expected_encoded_display_name = ( | ||
"=?utf-8?b?IiJmb28gYmFyIiA8Zm9vQGJhci5jb20+IFt2aWEgUmVsYXldIg==?=" | ||
) | ||
expected_formatted_from = "%s <%s>" % ( | ||
expected_encoded_display_name, | ||
"[email protected]", | ||
) | ||
assert formatted_from_address == expected_formatted_from | ||
|
||
@override_settings(RELAY_FROM_ADDRESS="[email protected]", | ||
NEW_RELAY_FROM_ADDRESS="[email protected]") | ||
def test_generate_relay_From_with_no_user_profile_somehow(self): | ||
free_user = baker.make(User) | ||
Flag.objects.create(name=NEW_FROM_ADDRESS_FLAG_NAME) | ||
original_from_address = '"foo bar" <[email protected]>' | ||
formatted_from_address = generate_relay_From( | ||
original_from_address, free_user.profile_set.first() | ||
) | ||
expected_encoded_display_name = ( | ||
"=?utf-8?b?IiJmb28gYmFyIiA8Zm9vQGJhci5jb20+IFt2aWEgUmVsYXldIg==?=" | ||
) | ||
expected_formatted_from = "%s <%s>" % ( | ||
expected_encoded_display_name, | ||
"[email protected]", | ||
) | ||
assert formatted_from_address == expected_formatted_from | ||
|
||
@override_settings(ON_HEROKU=True, SITE_ORIGIN="https://test.com") | ||
def test_get_email_domain_from_settings_on_heroku(self): | ||
email_domain = get_email_domain_from_settings() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
import markus | ||
import logging | ||
from waffle import sample_is_active | ||
from waffle.models import Flag | ||
|
||
from django.apps import apps | ||
from django.conf import settings | ||
|
@@ -28,6 +29,8 @@ | |
from .models import DomainAddress, RelayAddress, Reply, get_domains_from_settings | ||
|
||
|
||
NEW_FROM_ADDRESS_FLAG_NAME = "new_from_address" | ||
|
||
logger = logging.getLogger("events") | ||
study_logger = logging.getLogger("studymetrics") | ||
metrics = markus.get_metrics("fx-private-relay") | ||
|
@@ -218,12 +221,17 @@ def get_post_data_from_request(request): | |
|
||
|
||
def generate_relay_From(original_from_address, user_profile=None): | ||
_, relay_from_address = parseaddr(settings.RELAY_FROM_ADDRESS) | ||
try: | ||
new_from_flag = Flag.objects.get(name=NEW_FROM_ADDRESS_FLAG_NAME) | ||
if user_profile and new_from_flag.is_active_for_user(user_profile.user): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: test for flag exists but not for that user. And maybe flag exists but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Finally got these tests working but I had to explicitly call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ug, that one sounds hard to debug! Looking at the docs, the supported method is the It may be worth investigating if 1) the database row in the many-to-many table is deleted during test cleanup, and 2) if there is a cache entry that is created and 3) if the cache entry is cleaned up. |
||
_, relay_from_address = parseaddr(settings.NEW_RELAY_FROM_ADDRESS) | ||
except Flag.DoesNotExist: | ||
pass | ||
if user_profile and user_profile.has_premium: | ||
relay_display_name, relay_from_address = parseaddr( | ||
_, relay_from_address = parseaddr( | ||
"replies@%s" % get_domains_from_settings().get("RELAY_FIREFOX_DOMAIN") | ||
) | ||
else: | ||
relay_display_name, relay_from_address = parseaddr(settings.RELAY_FROM_ADDRESS) | ||
# RFC 2822 (https://tools.ietf.org/html/rfc2822#section-2.1.1) | ||
# says email header lines must not be more than 998 chars long. | ||
# Encoding display names to longer than 998 chars will add wrap | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄 I love tests for new functionality! And bonus, the old functionality was already covered by tests!