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-3897: Add managers to RealPhone to handle multiple phones per user #5044

Merged
merged 11 commits into from
Sep 17, 2024

Conversation

jwhitlock
Copy link
Member

This PR adds managers with helper methods to RealPhone, and updates the code to use them. The managers and methods:

  • RealPhone.objects - The original, can still be used in tests or when working with all RealPhone objects.
  • RealPhone.verified_objects - RealPhone objects that the user has verified via a texted verification code.
    • .get_for_user(user) - Return the verified RealPhone object for the user, or raise DoesNotExist. Used to get the RealPhone for a requesting user.
    • .exists_for_number(number) - Returns True if there is a verified RealPhone object for the phone number. Used to determine if a number is already claimed.
    • .country_code_for_user(user) - Returns the country code of the user's verified RealPhone. Used to limit the suggested Relay numbers to the user's country code.
  • RealPhone.recent_objects - RealPhone objects where a verification code was recently sent
    • .get_for_user_number_and_verification_code(user, number, code) - Used to check that a verification code is the one sent by the authorized user to their phone number, and that it hasn't been too long since it was sent. To keep double submissions from being errors, the RealPhone can already be verified.
  • RealPhone.pending_objects - Similar to RealPhone.recent_objects, except only the unverified RealPhone objects.
    • exists_for_number(number) - Check if a pending verification code is currently active for this number, to prevent having two pending codes at the same time and a potential abuse vector.
  • RealPhone.expired_objects - The opposite of RealPhone.recent_objects - RealPhone objects where the verification code was sent a long time ago and is no longer valid.
    • delete_for_number(number) - Delete any expired records for a particular number. Used in the RealPhone save method for ... reasons.

This PR fixes a few bugs around RealPhone queries when a user has multiple RealPhone records, like a verified RealPhone and some unverified RealPhone for other numbers they tried:

  • /api/v1/profiles/ returns a 500 error, trying to determine when they subscribed to The code now only queries for the verified RealPhone, and there should be one per user (enforced by code, not the database)
  • Calls to send_welcome_message, to send them their Relay number and contact info, fail. The codenow only queries for the verified RealPhone.
  • Using ./manage.py delete_phone_data will fail. Now it deletes all the user's RealPhone records, and reports one per line.

How to test: Unit tests should be sufficient. If you want to try live tests, ensure you add an unverified RealPhone before a verified RealPhone. The code will not allow you to register a new RealPhone if you have a verified one.

Copy link

sentry-io bot commented Sep 16, 2024

🔍 Existing Issues For Review

Your pull request is modifying functions with the following pre-existing issues:

📄 File: phones/models.py

Function Unhandled Issue
relaynumber_post_save RealPhone.MultipleObjectsReturned: get() returned more than one RealPhone -- it returned 2! ...
Event Count: 1
📄 File: privaterelay/models.py (Click to Expand)
Function Unhandled Issue
date_phone_registered RealPhone.MultipleObjectsReturned: get() returned more than one RealPhone -- it returned 2! ...
Event Count: 5
---

Did you find this useful? React with a 👍 or 👎

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.

(priase) Nice changes! I had about 3-4 comments lined up to submit, but as I went thru the code each question or suggestion I had was already addressed.

@groovecoder groovecoder added this pull request to the merge queue Sep 17, 2024
Merged via the queue into main with commit 9a418bd Sep 17, 2024
29 checks passed
@groovecoder groovecoder deleted the two-phones-mpp-3897 branch September 17, 2024 13:18
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