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

Twilio Integration and More update notification #1719

Merged
merged 13 commits into from
Aug 28, 2017

Conversation

valaparthvi
Copy link
Contributor

@valaparthvi valaparthvi commented Aug 15, 2017

Proposed changes in this pull request

Send update notification via sms with Twilio
[List all changes you want to add here. If you fixed an issue, please
add a reference to that issue as well.]

  1. Add gateways.py to accounts. There are 2 gateways, TwilioGateway and FakeGateway. TwilioGateway is used to send actual messages and in testing, FakeGateway is used for local development.
    The testing environment will use Test Credentials provided by Twilio, while production environment will Live Credentials provided by Twilio and a list of real phone numbers bought on Twilio.
    Add tests for the gateways.

  2. Add new messages to accounts/messages.py. This message will be sent via sms to user's phone.

  3. Make changes to ProfileForm. When user changes/deletes email, send an SMS update notification if verified email is linked to user account. When user changes/deletes phone, send an email update notification if a verified phone is linked to user account. Make corresponding changes to tests.

  4. Add new functions: send_phone_deleted_notification, send_email_deleted_notification, send_phone_changed_notification to accounts/utils.py.

  5. Add Gateway settings to the settings file.

  6. Make changes to AccountUser in accounts/views/api.py. Send update notification to the user's verified phone/email whenever the user changes/deletes phone/email. Add tests for the same.

When should this PR be merged

  • Currently, test setting uses Twilio Test credentials. These Test Credentials have limited access to resources. So there is no sure way to check if messages are actually sent during tests. This issue needs fixing.

  • Currently, there are no credentials, so tests using Twilio are failing. Once Twilio credentials are exported, and all test passes.

Risks

-None

Follow-up actions

  1. These variables must be exported to the environment in order to use Twilio.
    Live Credentials: TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN.

Checklist (for reviewing)

General

  • Is this PR explained thoroughly? All code changes must be accounted for in the PR description.
    • Review 1
    • Review 2
  • Is the PR labeled correctly? It should have the migration label if a new migration is added.
    • Review 1
    • Review 2
  • Is the risk level assessment sufficient? The risks section should contain all risks that might be introduced with the PR and which actions we need to take to mitigate these risks. Possible risks are database migrations, new libraries that need to be installed or changes to deployment scripts.
    • Review 1
    • Review 2

Functionality

  • Are all requirements met? Compare implemented functionality with the requirements specification.
    • Review 1
    • Review 2
  • Does the UI work as expected? There should be no Javascript errors in the console; all resources should load. There should be no unexpected errors. Deliberately try to break the feature to find out if there are corner cases that are not handled.
    • Review 1
    • Review 2

Code

  • Do you fully understand the introduced changes to the code? If not ask for clarification, it might uncover ways to solve a problem in a more elegant and efficient way.
    • Review 1
    • Review 2
  • Does the PR introduce any inefficient database requests? Use the debug server to check for duplicate requests.
    • Review 1
    • Review 2
  • Are all necessary strings marked for translation? All strings that are exposed to users via the UI must be marked for translation.
    • Review 1
    • Review 2
  • Is the code documented sufficiently? Large and complex classes, functions or methods must be annotated with comments following our code-style guidelines.
    • Review 1
    • Review 2
  • Has the scalability of this change been evaluated?
    • Review 1
    • Review 2
  • Is there a maintenance plan in place?
    • Review 1
    • Review 2

Tests

  • Are there sufficient test cases? Ensure that all components are tested individually; models, forms, and serializers should be tested in isolation even if a test for a view covers these components.
    • Review 1
    • Review 2
  • If this is a bug fix, are tests for the issue in place? There must be a test case for the bug to ensure the issue won’t regress. Make sure that the tests break without the new code to fix the issue.
    • Review 1
    • Review 2
  • If this is a new feature or a significant change to an existing feature? has the manual testing spreadsheet been updated with instructions for manual testing?
    • Review 1
    • Review 2

Security

  • Confirm this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets.
    • Review 1
    • Review 2
  • Are all UI and API inputs run through forms or serializers?
    • Review 1
    • Review 2
  • Are all external inputs validated and sanitized appropriately?
    • Review 1
    • Review 2
  • Does all branching logic have a default case?
    • Review 1
    • Review 2
  • Does this solution handle outliers and edge cases gracefully?
    • Review 1
    • Review 2
  • Are all external communications secured and restricted to SSL?
    • Review 1
    • Review 2

Documentation

  • Are changes to the UI documented in the platform docs? If this PR introduces new platform site functionality or changes existing ones, the changes must be documented in the Cadasta Platform Documentation.
    • Review 1
    • Review 2
  • Are changes to the API documented in the API docs? If this PR introduces new API functionality or changes existing ones, the changes must be documented in the API docs.
    • Review 1
    • Review 2
  • Are reusable components documented? If this PR introduces components that are relevant to other developers (for instance a mixin for a view or a generic form) they should be documented in the Wiki.
    • Review 1
    • Review 2

@oliverroick
Copy link
Member

41 tests are failing for this PR. You need to fix them before we can proceed.

Copy link
Member

@oliverroick oliverroick left a comment

Choose a reason for hiding this comment

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

There are a few small things in the code. I also can't get it to run in the dev VM, the wrong SMS gateway is imported in utils.

SMS_GATEWAY = 'accounts.gateways.TwilioGateway'
TWILIO_ACCOUNT_SID = os.environ['TWILIO_ACCOUNT_SID']
TWILIO_AUTH_TOKEN = os.environ['TWILIO_AUTH_TOKEN']
TWILIO_PHONE_NUMBER_LIST = ['+15005550006']
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be hardcoded.

@@ -0,0 +1,65 @@
from django.test import TestCase
from django.conf import settings
# from accounts.gateways.twilio import TwilioGateway
Copy link
Member

Choose a reason for hiding this comment

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

Comments need to be removed

@@ -574,3 +574,8 @@

TOTP_TOKEN_VALIDITY = 3600
TOTP_DIGITS = 6

SMS_GATEWAY = 'accounts.gateways.TwilioGateway'
TWILIO_ACCOUNT_SID = os.environ['TWILIO_ACCOUNT_SID']
Copy link
Member

Choose a reason for hiding this comment

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

Since you're overwriting SMS_GATEWAY in the dev settings this breaks in the dev environment. We're not actually using Twilio for dev so there's no need to set this by default. Move these settings to the production settings.



def send_sms(to, body):
twilioobj = (import_string(settings.SMS_GATEWAY))()
Copy link
Member

Choose a reason for hiding this comment

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

In the dev VM accounts.gateways.FakeGateway should be imported but it isn't. Instead, accounts.gateways.TwilioGateway is imported and everything breaks.

Copy link
Contributor Author

@valaparthvi valaparthvi Aug 23, 2017

Choose a reason for hiding this comment

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

I did not know that dev settings were used for testing up until now. I think the system should work fine now, and there should be no breakage.

@valaparthvi
Copy link
Contributor Author

@oliverroick Updates from my conversation with @amplifi :

  1. Move settings used by all config files to default.py, this includes, TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN, SMS_GATEWAY. production.py will override SMS_GATEWAY.

  2. Remove TwilioGateway logger. Status of messages can always be checked from twilio console, so it's okay to remove that logger. Move FakeGateway logger to default.py since it is used by both dev VM and travis.

  3. An update notification must be sent to phone/email linked to the user's account regardless of phone/email('s) verification status.

@amplifi
Copy link
Contributor

amplifi commented Aug 23, 2017

👍 and the Ansible provisioning task is updated to populate the Twilio SID and auth token vars. Great work, @valaparthvi!

Copy link
Member

@oliverroick oliverroick left a comment

Choose a reason for hiding this comment

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

This looks good now, thanks for making the changes. Also, thanks @amplifi for helping out with this.

There's only one small thing with the settings now that still breaks in the dev VM.


SMS_GATEWAY = 'accounts.gateways.FakeGateway'

TWILIO_ACCOUNT_SID = os.environ['TWILIO_ACCOUNT_SID']
Copy link
Member

Choose a reason for hiding this comment

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

When I don't have the Twilio account settings exported in my dev VM this breaks. We have two options how we can deal with it:

  1. Add this and the next line to both production.py and travis.py and remove them from here.
  2. Write os.environ.get('TWILIO_ACCOUNT_SID') instead. In that case TWILIO_ACCOUNT_SID will be set to None and no exception is thrown.

Copy link
Contributor Author

@valaparthvi valaparthvi Aug 26, 2017

Choose a reason for hiding this comment

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

Ah, that is what you meant when you said dev VM breaks. I was under the assumption that we will always have credentials exported in the dev VM so I did not completely understand the problem earlier, but I like the second idea better. Although exceptions will be thrown in the dev VM when tests are run. These exceptions will be thrown by accounts/tests/test_gateways.py since it uses TwilioGateway.

Copy link
Member

@oliverroick oliverroick 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, thanks!

@valaparthvi valaparthvi merged commit 396a852 into additional-login-options Aug 28, 2017
valaparthvi added a commit that referenced this pull request Aug 28, 2017
* Twilio Gateway

* Enable Twilio test account for Travis

* Pass twilio settings into tox env

* Pass all the tests

* 100% test coverage

* Remove unnecessary lines of code

* Currently removed TWILIO_PHONE_NUMBER from settings, add that later once we have phone numbers, remove unnecessary settings from default settings

* Remove comments

* Make tests pass

* Make tests pass, remove TwilioGateway logger, move FakeGateway logger to settings/default.py

* Send email/sms regardless of phone/email('s) verification status

* Add changes addressed to PR

* Rebasing and changes
@valaparthvi valaparthvi mentioned this pull request Aug 31, 2017
46 tasks
valaparthvi added a commit that referenced this pull request Aug 31, 2017
* Twilio Gateway

* Enable Twilio test account for Travis

* Pass twilio settings into tox env

* Pass all the tests

* 100% test coverage

* Remove unnecessary lines of code

* Currently removed TWILIO_PHONE_NUMBER from settings, add that later once we have phone numbers, remove unnecessary settings from default settings

* Remove comments

* Make tests pass

* Make tests pass, remove TwilioGateway logger, move FakeGateway logger to settings/default.py

* Send email/sms regardless of phone/email('s) verification status

* Add changes addressed to PR

* Rebasing and changes
valaparthvi added a commit that referenced this pull request Sep 7, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Sep 8, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Sep 12, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Sep 21, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Sep 21, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
@valaparthvi valaparthvi deleted the phone/Twilio branch September 21, 2017 05:46
valaparthvi added a commit that referenced this pull request Sep 22, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Sep 26, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
clash99 pushed a commit that referenced this pull request Oct 3, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
oliverroick pushed a commit that referenced this pull request Oct 4, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
clash99 pushed a commit that referenced this pull request Oct 4, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Oct 5, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Oct 5, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Oct 5, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Oct 20, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Oct 20, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Oct 20, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Oct 26, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
valaparthvi added a commit that referenced this pull request Nov 8, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
oliverroick pushed a commit that referenced this pull request Nov 13, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
oliverroick pushed a commit that referenced this pull request Nov 30, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
oliverroick pushed a commit that referenced this pull request Dec 1, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
oliverroick pushed a commit that referenced this pull request Dec 6, 2017
* VerificationDevice model and Removal of 48hr email verification period (#1606)

* Registration with Phone number (#1662)

* Add phone to User Profile (#1698)

* Add Ansible provisioning for Twilio

* Allow user to login with phone and add Resend Token Page (#1708)

* Reset Password with Phone (#1717)

* Twilio Integration and More update notification (#1719)

* Add API endpoint for phone verification(2) (#1748)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants