Skip to content

Commit e4532ba

Browse files
Merge branch 'master' of github.com:CuriousLearner/django-phone-verify 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)
2 parents 1847bac + 24052cf commit e4532ba

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

docs/how_to_write_custom_backend.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ The above steps will remain same if you wish to create a sandbox utility for you
113113
# Third Party Stuff
114114
import nexmo
115115
from phone_verify.backends.base import BaseBackend
116+
from phone_verify.models import SMSVerification
116117
117118
118119
class NexmoSandboxBackend(BaseBackend):
@@ -152,9 +153,9 @@ The above steps will remain same if you wish to create a sandbox utility for you
152153
"""
153154
Always validate security code for testing purposes
154155
"""
155-
return self.SECURITY_CODE_VALID
156+
return SMSVerification.objects.none(), self.SECURITY_CODE_VALID
156157
157-
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.
158+
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.
158159

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

phone_verify/backends/nexmo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
# Local
88
from .base import BaseBackend
9+
from phone_verify.models import SMSVerification
910

1011

1112
class NexmoBackend(BaseBackend):
@@ -63,4 +64,4 @@ def generate_security_code(self):
6364
return self._token
6465

6566
def validate_security_code(self, security_code, phone_number, session_token):
66-
return self.SECURITY_CODE_VALID
67+
return SMSVerification.objects.none(), self.SECURITY_CODE_VALID

phone_verify/backends/twilio.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
# Local
99
from .base import BaseBackend
10+
from phone_verify.models import SMSVerification
1011

1112

1213
class TwilioBackend(BaseBackend):
@@ -56,4 +57,4 @@ def generate_security_code(self):
5657
return self._token
5758

5859
def validate_security_code(self, security_code, phone_number, session_token):
59-
return self.SECURITY_CODE_VALID
60+
return SMSVerification.objects.none(), self.SECURITY_CODE_VALID

tests/test_nexmo_backend.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,17 @@ def test_nexmo_sandbox_backend(client, mocker):
6060
}
6161
mock_send_message.assert_called_once_with(test_data)
6262

63+
url = reverse("phone-verify")
64+
data = {
65+
"phone_number": phone_number,
66+
"session_token": SESSION_TOKEN,
67+
"security_code": SECURITY_CODE
68+
}
69+
70+
response = client.post(url, data)
71+
assert response.status_code == 200
72+
response_data = {"message": "Security code is valid."}
73+
assert response.data == response_data
74+
6375
# Assign back the Backend as defined in settings
6476
test_settings.DJANGO_SETTINGS["PHONE_VERIFICATION"]["BACKEND"] = "phone_verify.backends.twilio.TwilioBackend"

tests/test_twilio_backend.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,17 @@ def test_twilio_sandbox_backend(client, mocker):
4747

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

50+
url = reverse("phone-verify")
51+
data = {
52+
"phone_number": phone_number,
53+
"session_token": SESSION_TOKEN,
54+
"security_code": SECURITY_CODE
55+
}
56+
57+
response = client.post(url, data)
58+
assert response.status_code == 200
59+
response_data = {"message": "Security code is valid."}
60+
assert response.data == response_data
61+
5062
# Assign back the Backend as defined in settings
5163
test_settings.DJANGO_SETTINGS["PHONE_VERIFICATION"]["BACKEND"] = "phone_verify.backends.twilio.TwilioBackend"

0 commit comments

Comments
 (0)