Skip to content

Commit

Permalink
[feature] Emit signal in freeradius accounting view #341
Browse files Browse the repository at this point in the history
Closes #341

Co-authored-by: sankalp <sankalp123427@gmail.com>
  • Loading branch information
devkapilbansal and codesankalp authored Nov 29, 2021
1 parent db2fbbe commit 8124ff4
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 9 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ jobs:
run: |
pip install -U "pip==20.2.4" wheel setuptools
pip install ${{ matrix.django-version }}
pip install swapper@git+git://github.com/openwisp/django-swappable-models.git@master
pip install -U -r requirements-test.txt
- name: Install npm dependencies
Expand Down
20 changes: 20 additions & 0 deletions docs/source/developer/signals.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=======
Signals
=======

``radius_accounting_success``
-----------------------------

**Path**: ``openwisp_radius.signals.radius_accounting_success``

**Arguments**:

- ``sender`` : ``AccountingView``
- ``accounting_data`` (``dict``): accounting information
- ``view``: instance of ``AccountingView``

This signal is emitted every time the accounting REST API endpoint
completes successfully, just before the response is returned.

The ``view`` argument can also be used to access the ``request``
object i.e. ``view.request``.
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ openwisp-radius
/user/social_login
/user/saml
/user/api
/developer/signals
/developer/how_to_extend
/developer/captive_portal_mock.rst
/general/support
Expand Down
10 changes: 10 additions & 0 deletions openwisp_radius/api/freeradius_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .. import settings as app_settings
from ..counters.base import BaseCounter
from ..counters.exceptions import MaxQuotaReached, SkipCheck
from ..signals import radius_accounting_success
from ..utils import load_model
from .serializers import (
AuthorizeSerializer,
Expand Down Expand Up @@ -431,12 +432,14 @@ def post(self, request, *args, **kwargs):
acct_data = self._data_to_acct_model(serializer.validated_data.copy())
serializer.create(acct_data)
headers = self.get_success_headers(serializer.data)
self.send_radius_accounting_signal(serializer.validated_data)
return Response(None, status=201, headers=headers)
else:
serializer = self.get_serializer(instance, data=data, partial=False)
serializer.is_valid(raise_exception=True)
acct_data = self._data_to_acct_model(serializer.validated_data.copy())
serializer.update(instance, acct_data)
self.send_radius_accounting_signal(serializer.validated_data)
return Response(None)

def _data_to_acct_model(self, valid_data):
Expand All @@ -445,6 +448,13 @@ def _data_to_acct_model(self, valid_data):
valid_data['organization'] = acct_org
return valid_data

def send_radius_accounting_signal(self, accounting_data):
radius_accounting_success.send(
sender=self.__class__,
accounting_data=accounting_data,
view=self,
)


accounting = AccountingView.as_view()

Expand Down
3 changes: 3 additions & 0 deletions openwisp_radius/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.dispatch import Signal

radius_accounting_success = Signal() # providing_args=['accounting_data', 'view']
21 changes: 14 additions & 7 deletions openwisp_radius/tests/test_api/test_freeradius_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
from django.utils.timezone import now
from freezegun import freeze_time

from openwisp_utils.tests import capture_any_output
from openwisp_utils.tests import capture_any_output, catch_signal

from ... import registration
from ... import settings as app_settings
from ...api.freeradius_views import logger as freeradius_api_logger
from ...counters.exceptions import MaxQuotaReached, SkipCheck
from ...signals import radius_accounting_success
from ...utils import load_model
from ..mixins import ApiTokenMixin, BaseTestCase

Expand Down Expand Up @@ -681,7 +682,11 @@ def test_accounting_start_200(self):
self.assertEqual(RadiusAccounting.objects.count(), 0)
ra = self._create_radius_accounting(**self._acct_initial_data)
data = self._prep_start_acct_data()
response = self.post_json(data)
with catch_signal(radius_accounting_success) as handler:
response = self.post_json(data)
handler.assert_called_once()
view = handler.mock_calls[0].kwargs.get('view')
self.assertTrue(hasattr(view, 'request'))
self.assertEqual(response.status_code, 200)
self.assertIsNone(response.data)
self.assertEqual(RadiusAccounting.objects.count(), 1)
Expand All @@ -694,11 +699,13 @@ def test_accounting_start_radius_token_201(self):
data = self._prep_start_acct_data()
data.update(username='tester')
self.assertEqual(RadiusAccounting.objects.count(), 0)
response = self.client.post(
self._acct_url,
data=json.dumps(data),
content_type='application/json',
)
with catch_signal(radius_accounting_success) as handler:
response = self.client.post(
self._acct_url,
data=json.dumps(data),
content_type='application/json',
)
handler.assert_called_once()
self.assertEqual(response.status_code, 201)
self.assertIsNone(response.data)
self.assertEqual(RadiusAccounting.objects.count(), 1)
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
zip_safe=False,
install_requires=[
'django>=3.0,<3.2',
'swapper @ git+git://github.com/openwisp/django-swappable-models.git@master',
# Needed for the new authentication backend in openwisp-users
# TODO: remove when the new version of openwisp-users is released
'openwisp-users @ https://github.com/openwisp/openwisp-users/tarball/master',
Expand Down

0 comments on commit 8124ff4

Please sign in to comment.