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

MPP-3352: Expand, refactor tests #4434

Merged
merged 17 commits into from
Feb 28, 2024
Merged

MPP-3352: Expand, refactor tests #4434

merged 17 commits into from
Feb 28, 2024

Conversation

jwhitlock
Copy link
Member

In preparation for adding Glean metrics in MPP-3352, this PR expands and refactors the tests. This will reduce the size and complexity of the near-future Glean PR.

There is one code change, to remove a redundant error log. The rest are test changes, so running the tests should be sufficient to validate the PR.

Each commit is roughly one refactor:

  • Add default data to version_json_path fixture
  • Add PATCH and DELETE tests for address APIs. This exposes an issue that the DomainAddress.address (the local part of a custom mask) is allowed to change with a PATCH. This field should be writable at creation and read-only later. Tracked in MPP-3753.
  • Add test for RandomAddress with generated_for. This pattern is used as the signal that the address was generated on a website, and will be a distinct interaction event in the future.
  • Add type annotations to log_record test utility
  • Ignore TYPE_CHECKING lines in coverage. This code is only evaluated by static type checkers, so it is impossible to get code coverage for them.
  • Refactor plan setting in Profile tests. Previously, the shared ProfileTestCase.upgrade_to_premium() could randomly select a VPN bundle plan. It now only selects a unlimited email protection plan. Addition helpers .upgrade_to_phone and .upgrade_to_vpn_bundle can upgrade the test user to those plan levels. This exposed some issues with the Django model cache around User and Profile. The make_free_test_user and make_premium_test_user functions now update the profile through user.profile, making the cache consistent.
  • Rename unlimited_subscription to premium_subscription, and only return unlimited email plans. Use in upgrade_test_user_to_premium, so that these users are only ones with the unlimited email plans, not the VPN bundle.
  • Move replies tests to new test case. Split SNSNotificationTest into three parts:
    • SNSNotificationTestBase - shared setup and helper functions
      - SNSNotificationIncomingTest - tests for emails to Relay users
    • SNSNotificationRepliesTest - test for email replies from Relay users
  • Convert test to assertLogs instead of mock logger
  • Add and expand email failure tests. Test more failure modes for incoming emails and Relay user replies. Check log messages and emitted metrics.
  • l10n changes have been submitted to the l10n repository, if any.
  • I've added a unit test to test for potential regressions of this bug.
  • I've added or updated relevant docs in the docs/ directory.
  • All UI revisions follow the coding standards, and use Protocol tokens where applicable (see /frontend/src/styles/tokens.scss).
  • Commits in this PR are minimal and have descriptive commit messages.

This exposes an issue that the DomainAddress.address (the local part of
a custom mask) is allowed to change with a PATCH. This field should be
writable at creation and read-only later. Tracked in MPP-3753.
This pattern is used as the signal that the address was generated on a
website, and will be a distinct interaction event in the future.
Previously, the shared ProfileTestCase.upgrade_to_premium() could
randomly select a VPN bundle plan. It now only selects a unlimited email
protection plan. Addition helpers .upgrade_to_phone and
.upgrade_to_vpn_bundle can upgrade the test user to those plan levels.

This exposed some issues with the Django model cache around User and
Profile. The make_free_test_user and make_premium_test_user functions
now update the profile through user.profile, making the cache
consistent.

make_premium_test_user can still select a VPN bundle for a user.
Rename unlimited_subscription to premium_subscription, and only return
unlimited email plans. Use in upgrade_test_user_to_premium, so that
these users are only ones with the unlimited email plans, not the VPN
bundle.
Split SNSNotificationTest into three parts:

- SNSNotificationTestBase - shared setup and helper functions
- SNSNotificationIncomingTest - tests for emails to Relay users
- SNSNotificationRepliesTest - test for email replies from Relay users
Test more failure modes for incoming emails and Relay user replies.
Check log messages and emitted metrics.
Comment on lines +443 to +447
("num_forwarded", -1),
("num_blocked", -1),
("num_spam", -1),
("num_level_one_trackers_blocked", -1),
("num_replied", -1),
Copy link
Member

Choose a reason for hiding this comment

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

question (non-blocking): Why all these negative (invalid?) values? Could this be accidentally testing the validation of these fields and not testing the read-only aspect of them?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't want to accidentally use the original value, so I picked a value it definitely could not be. I thought the 200 response was sufficient for saying it was not a validation error.

I could first do a GET and then write the next value for integers, if the invalid value worries you.

Copy link
Member

Choose a reason for hiding this comment

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

suggestion (non-blocking): if it's not too much work, let's try to GET and then write the next value.

api/tests/views_tests.py Show resolved Hide resolved
Comment on lines +632 to +636
("num_forwarded", -1),
("num_blocked", -1),
("num_spam", -1),
("num_level_one_trackers_blocked", -1),
("num_replied", -1),
Copy link
Member

Choose a reason for hiding this comment

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

question (non-blocking): same question as above: should we use -1 values here?

api/tests/views_tests.py Show resolved Hide resolved
emails/tests/models_tests.py Show resolved Hide resolved
emails/tests/models_tests.py Outdated Show resolved Hide resolved
emails/tests/models_tests.py Outdated Show resolved Hide resolved
emails/tests/models_tests.py Outdated Show resolved Hide resolved
emails/tests/views_tests.py Outdated Show resolved Hide resolved
emails/tests/views_tests.py Show resolved Hide resolved
Copy link
Member Author

@jwhitlock jwhitlock left a comment

Choose a reason for hiding this comment

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

I'm going to remove some duplicate code around premium subscriptions.

There is a potential bigger change around assert_email_equals that could be it's own PR. Let me know if you think this should be done in this PR.

I've answered the other questions, and I think there are no code changes for those.

Comment on lines +443 to +447
("num_forwarded", -1),
("num_blocked", -1),
("num_spam", -1),
("num_level_one_trackers_blocked", -1),
("num_replied", -1),
Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't want to accidentally use the original value, so I picked a value it definitely could not be. I thought the 200 response was sufficient for saying it was not a validation error.

I could first do a GET and then write the next value for integers, if the invalid value worries you.

api/tests/views_tests.py Show resolved Hide resolved
emails/tests/models_tests.py Outdated Show resolved Hide resolved
emails/tests/models_tests.py Outdated Show resolved Hide resolved
emails/tests/views_tests.py Outdated Show resolved Hide resolved
emails/tests/views_tests.py Show resolved Hide resolved
@jwhitlock
Copy link
Member Author

jwhitlock commented Feb 27, 2024

I've got some time, I'm planning to change assert_email_equals and get_details_from_mock_send_raw_email()

Replace
SNSNotificationTestBase.get_details_from_mock_send_raw_email()
with
SNSNotificationTestBase.assert_email_equals()
which extracts the sent email from mock_send_raw_email and calls the
helper assert_email_equals(). This implicitly checks headers and
content, so remove redundant checks.

Optionally check the source and destination values passed to
send_raw_email() as well.
@jwhitlock
Copy link
Member Author

The further changes:

  • Move phone_subscription and vpn_subscription out as well
  • Remove redundant upgrade_to_phone_premium() and tests
  • Add some -> None: annotations to new tests that missed them
  • Replace SNSNotificationTestBase.get_details_from_mock_send_raw_email() with SNSNotificationTestBase.assert_email_equals, which leans on the _expected.email fixtures to check headers and content. This removes some explicit assertions.

Copy link
Member

@groovecoder groovecoder left a comment

Choose a reason for hiding this comment

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

1 non-blocking suggestion.

emails/tests/models_tests.py Show resolved Hide resolved
emails/tests/views_tests.py Outdated Show resolved Hide resolved
emails/tests/views_tests.py Outdated Show resolved Hide resolved
Rename the top-level function to assert_email_equals_fixture, and the
test case method to check_sent_email_matches_fixture. Rearrange and
rename the parameters for consistency.
@jwhitlock jwhitlock added this pull request to the merge queue Feb 28, 2024
Merged via the queue into main with commit 41052de Feb 28, 2024
27 checks passed
@jwhitlock jwhitlock deleted the expand-tests-mpp-3352 branch February 28, 2024 17:05
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.

2 participants