Skip to content

Commit

Permalink
Merge branch 'master' of github.com:CuriousLearner/django-phone-verif…
Browse files Browse the repository at this point in the history
…y into SepehrHasanabadi-kavenegar-api

* 'master' of github.com:CuriousLearner/django-phone-verify:
  fix(phone_verify): Return same number of arguments in the overridden method (#38)
  • Loading branch information
CuriousLearner committed Feb 21, 2020
2 parents 1847bac + 24052cf commit e4532ba
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
5 changes: 3 additions & 2 deletions docs/how_to_write_custom_backend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ The above steps will remain same if you wish to create a sandbox utility for you
# Third Party Stuff
import nexmo
from phone_verify.backends.base import BaseBackend
from phone_verify.models import SMSVerification
class NexmoSandboxBackend(BaseBackend):
Expand Down Expand Up @@ -152,9 +153,9 @@ The above steps will remain same if you wish to create a sandbox utility for you
"""
Always validate security code for testing purposes
"""
return self.SECURITY_CODE_VALID
return SMSVerification.objects.none(), self.SECURITY_CODE_VALID
We have also overriden the ``generate_security_code`` and ``validate_security_code`` methods of ``BaseBackend`` class. The ``validate_security_code`` method must have ``security_code``, ``phone_number`` and ``session_token`` as its positional parameters.
We have also overriden the ``generate_security_code`` and ``validate_security_code`` methods of ``BaseBackend`` class. The ``validate_security_code`` method must have ``security_code``, ``phone_number`` and ``session_token`` as its positional parameters. We returned an empty SMSVerification object to keep the return arguments uniform with the acutal base class method.

In order to use this new custom backend class, it should be replaced in the ``BACKEND`` key under ``PHONE_VERIFICATION`` settings as shown below.

Expand Down
3 changes: 2 additions & 1 deletion phone_verify/backends/nexmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# Local
from .base import BaseBackend
from phone_verify.models import SMSVerification


class NexmoBackend(BaseBackend):
Expand Down Expand Up @@ -63,4 +64,4 @@ def generate_security_code(self):
return self._token

def validate_security_code(self, security_code, phone_number, session_token):
return self.SECURITY_CODE_VALID
return SMSVerification.objects.none(), self.SECURITY_CODE_VALID
3 changes: 2 additions & 1 deletion phone_verify/backends/twilio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Local
from .base import BaseBackend
from phone_verify.models import SMSVerification


class TwilioBackend(BaseBackend):
Expand Down Expand Up @@ -56,4 +57,4 @@ def generate_security_code(self):
return self._token

def validate_security_code(self, security_code, phone_number, session_token):
return self.SECURITY_CODE_VALID
return SMSVerification.objects.none(), self.SECURITY_CODE_VALID
12 changes: 12 additions & 0 deletions tests/test_nexmo_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,17 @@ def test_nexmo_sandbox_backend(client, mocker):
}
mock_send_message.assert_called_once_with(test_data)

url = reverse("phone-verify")
data = {
"phone_number": phone_number,
"session_token": SESSION_TOKEN,
"security_code": SECURITY_CODE
}

response = client.post(url, data)
assert response.status_code == 200
response_data = {"message": "Security code is valid."}
assert response.data == response_data

# Assign back the Backend as defined in settings
test_settings.DJANGO_SETTINGS["PHONE_VERIFICATION"]["BACKEND"] = "phone_verify.backends.twilio.TwilioBackend"
12 changes: 12 additions & 0 deletions tests/test_twilio_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,17 @@ def test_twilio_sandbox_backend(client, mocker):

mock_twilio_messages.create.assert_called_once_with(to=phone_number, body=message, from_=from_number)

url = reverse("phone-verify")
data = {
"phone_number": phone_number,
"session_token": SESSION_TOKEN,
"security_code": SECURITY_CODE
}

response = client.post(url, data)
assert response.status_code == 200
response_data = {"message": "Security code is valid."}
assert response.data == response_data

# Assign back the Backend as defined in settings
test_settings.DJANGO_SETTINGS["PHONE_VERIFICATION"]["BACKEND"] = "phone_verify.backends.twilio.TwilioBackend"

0 comments on commit e4532ba

Please sign in to comment.