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

feat(phone_verify): Add Kavenegar backend #34

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5f3d7f6
ignore vscode files to commit
SepehrHasanabadi Dec 12, 2019
d9468a1
chore: using otp method of kavenegar
SepehrHasanabadi Dec 23, 2019
a061379
chore: using otp method of kavenegar
SepehrHasanabadi Dec 23, 2019
9c4b316
kavenegar api iranian OTP
SepehrHasanabadi Dec 29, 2019
5596a2e
feat(docs): Add Iranian sending message platform (Kavenegar)
SepehrHasanabadi Jan 19, 2020
7dcdc99
chore(*): Fix test case errors.
SepehrHasanabadi Jan 19, 2020
6659037
refactor(*): Change indentions form tab to space.
SepehrHasanabadi Jan 20, 2020
ccbb2e9
chore(): Upgrade requirements
SepehrHasanabadi Jan 25, 2020
ca42409
Merge branch 'upgrade' into kavenegar-api
SepehrHasanabadi Jan 25, 2020
6381fdc
refactor(): Phone verification in settings consists the active backend
SepehrHasanabadi Jan 25, 2020
de56ea4
refactor(*): Test coverage, code improvements.
SepehrHasanabadi Jan 26, 2020
5884cde
refactor(README): Add Kavenegar Docs to README.rst
SepehrHasanabadi Jan 26, 2020
53c2e27
refactor(*): Phone registration test have DRY approach.
SepehrHasanabadi Jan 27, 2020
87281a7
refactor(*): Test services for available backends, Alter exception ha…
SepehrHasanabadi Jan 30, 2020
ca4faad
refactor(init): Resolve flake8 error in test file __init__.
SepehrHasanabadi Jan 30, 2020
92681ff
Merge branch 'master' into kavenegar-api
CuriousLearner Feb 18, 2020
7f2e976
Update to Py3 syntax
CuriousLearner Feb 21, 2020
1847bac
Merge branch 'master' of github.com:CuriousLearner/django-phone-verif…
CuriousLearner Feb 21, 2020
e4532ba
Merge branch 'master' of github.com:CuriousLearner/django-phone-verif…
CuriousLearner Feb 21, 2020
c59e927
Merge branch 'master' of github.com:CuriousLearner/django-phone-verif…
gutsytechster Mar 10, 2020
391e505
fix: Modify sending sms parameters
SepehrHasanabadi Mar 28, 2020
d3aec32
Merge branch 'kavenegar-api' of github.com:SepehrHasanabadi/django-ph…
SepehrHasanabadi Mar 28, 2020
b1af51b
Merge branch 'master' into SepehrHasanabadi-kavenegar-api
CuriousLearner Apr 30, 2020
987cbca
refactor(gitignore): add idea folder to gitignore
SepehrHasanabadi May 17, 2020
facc894
fix(test): Modified kavenegar test_data
SepehrHasanabadi May 17, 2020
b10b1da
Merge branch 'kavenegar-api' of github.com:SepehrHasanabadi/django-ph…
SepehrHasanabadi May 17, 2020
0c45d81
fix(test): Modified tests, Create KavenegarException class.
SepehrHasanabadi May 18, 2020
18707fd
fix: modified test kavenegar, Add Kavenegar sandbox
SepehrHasanabadi May 19, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ venv.bak/

# mypy
.mypy_cache/

# vscode configuration
.vscode
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Configuration
"VERIFY_SECURITY_CODE_ONLY_ONCE": False, # If False, then a security code can be used multiple times for verification
}

- In case of using Kavenegar as your backend service, you have to replace ``BACKEND`` with ``phone_verify.backends.kavenegar.KavenegarBackend`` and locate your ``API-KEY`` in ``SECRET`` and ``SENDER`` in ``FROM``, extra fields could be omitted.
Usage
-----

Expand Down
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from tests import test_settings

backends = {"twilio.TwilioBackend", "nexmo.NexmoBackend"}
backends = {"twilio.TwilioBackend", "nexmo.NexmoBackend", "kavenegar.KavenegarBackend"}
sandbox_backends = {"twilio.TwilioSandboxBackend", "nexmo.NexmoSandboxBackend"}
all_backends = list(backends) + list(sandbox_backends)

Expand Down
2 changes: 1 addition & 1 deletion phone_verify/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, **settings):
self.exception_class = None

@abstractmethod
def send_sms(self, numbers, message):
def send_sms(self, number, message):
raise NotImplementedError()

@abstractmethod
Expand Down
28 changes: 28 additions & 0 deletions phone_verify/backends/kavenegar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

# Third Party Stuff
from kavenegar import KavenegarAPI, APIException, HTTPException

# Local
from .base import BaseBackend


class KavenegarBackend(BaseBackend):
def __init__(self, **options):
super().__init__(**options)
# Lower case it just to be sure
options = {key.lower(): value for key, value in options.items()}
self.api_key = options.get("secret", None)
self.sender = options.get("from", None)

self.client = KavenegarAPI(self.api_key)
self.exception_class = APIException, HTTPException

def send_sms(self, number, message):
params = {'sender': self.sender, 'receptor': number, 'message': message, }
self.client.sms_send(params)

def send_bulk_sms(self, numbers, message):
params = {'sender': self.sender, 'receptor': numbers, 'message': message, }
Copy link
Owner

Choose a reason for hiding this comment

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

The API here states that sender should be a list of strings and you're providing a single sender here 🤔

https://github.com/kavenegar/kavenegar-python

Copy link
Owner

Choose a reason for hiding this comment

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

Hi @SepehrHasanabadi

Can you please have a look at this when you get the time?

Copy link
Owner

Choose a reason for hiding this comment

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

Ping @SepehrHasanabadi Can you please have a look at this conversation? Have you tested these changes?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, @SepehrHasanabadi, have a look at this comment as well.

self.client.sms_sendarray(params)
1 change: 1 addition & 0 deletions requirements/common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ python-dotenv==0.10.0
phonenumbers==8.10.2
django-phonenumber-field==2.1.0
twilio==6.21.0
kavenegar==1.1.2
nexmo==2.4.0
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"phonenumbers>=8.10.2",
"django-phonenumber-field>=2.1.0",
"twilio>=6.21.0",
"kavenegar>=1.1.2",
"nexmo>=2.4.0",
],
classifiers=[
Expand Down
4 changes: 2 additions & 2 deletions tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@


def create_verification(**kwargs):
SMSVerification = apps.get_model("phone_verify", "SMSVerification")
verification = G(SMSVerification, **kwargs)
sms_verification = apps.get_model("phone_verify", "SMSVerification")
verification = G(sms_verification, **kwargs)
return verification
6 changes: 3 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def test_phone_registration_sends_message(client, mocker, backend):
url = reverse("phone-register")
phone_number = PHONE_NUMBER
data = {"phone_number": phone_number}
twilio_api = mocker.patch(

mock_api = mocker.patch(
"phone_verify.services.PhoneVerificationService.send_verification"
)

response = client.post(url, data)

assert response.status_code == 200
assert twilio_api.called
assert mock_api.assert_called_once
assert "session_token" in response.data
SMSVerification = apps.get_model("phone_verify", "SMSVerification")
assert SMSVerification.objects.get(
Expand Down
13 changes: 13 additions & 0 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ def test_backends(client, mocker, backend):
"phone_verify.backends.twilio.TwilioRestClient.messages"
)
mock_twilio_send_message.create = mocker.MagicMock()
elif backend_cls == "kavenegar.KavenegarBackend":
# Mock the Kavenegar client
mock_kavenegar_send_message = mocker.patch(
"phone_verify.backends.kavenegar.KavenegarAPI.sms_send"
)
test_data = {
"receptor": phone_number,
"template": "",
"token": message,
"type": "sms",
}

response = client.post(url, data)
assert response.status_code == 200
Expand All @@ -67,6 +78,8 @@ def test_backends(client, mocker, backend):
mock_twilio_send_message.create.assert_called_once_with(
to=phone_number, body=message, from_=from_number
)
elif backend_cls == "kavenegar.KavenegarBackend":
mock_kavenegar_send_message.assert_called_once_with(test_data)

# Get the last part of the backend and check if that is a Sandbox Backend
if backend_cls in sandbox_backends:
Expand Down