-
Notifications
You must be signed in to change notification settings - Fork 168
/
test_sqlite_counters.py
124 lines (107 loc) · 5.33 KB
/
test_sqlite_counters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from openwisp_utils.tests import capture_any_output
from ...counters.base import BaseCounter
from ...counters.exceptions import MaxQuotaReached, SkipCheck
from ...counters.sqlite.daily_counter import DailyCounter
from ...counters.sqlite.daily_traffic_counter import DailyTrafficCounter
from ...counters.sqlite.monthly_traffic_counter import MonthlyTrafficCounter
from ...utils import load_model
from ..mixins import BaseTestCase
from .utils import TestCounterMixin, _acct_data
RadiusAccounting = load_model('RadiusAccounting')
RadiusGroup = load_model('RadiusGroup')
RadiusGroupCheck = load_model('RadiusGroupCheck')
class TestSqliteCounters(TestCounterMixin, BaseTestCase):
def test_time_counter_repr(self):
opts = self._get_kwargs('Max-Daily-Session')
counter = DailyCounter(**opts)
expected = (
'sqlite.DailyCounter(user=tester, '
'group=test-org-users, '
f'organization_id={counter.organization_id})'
)
self.assertEqual(repr(counter), expected)
def test_time_counter_no_sessions(self):
opts = self._get_kwargs('Max-Daily-Session')
counter = DailyCounter(**opts)
self.assertEqual(counter.check(), int(opts['group_check'].value))
def test_time_counter_with_sessions(self):
opts = self._get_kwargs('Max-Daily-Session')
counter = DailyCounter(**opts)
self._create_radius_accounting(**_acct_data)
expected = int(opts['group_check'].value) - int(_acct_data['session_time'])
self.assertEqual(counter.check(), expected)
_acct_data2 = _acct_data.copy()
_acct_data2.update({'session_id': '2', 'unique_id': '2', 'session_time': '500'})
self._create_radius_accounting(**_acct_data2)
session_time = int(_acct_data['session_time']) + int(
_acct_data2['session_time']
)
expected = int(opts['group_check'].value) - session_time
self.assertEqual(counter.check(), expected)
@capture_any_output()
def test_counter_skip_exceptions(self):
options = self._get_kwargs('Max-Daily-Session')
with self.subTest('missing group'):
opts = options.copy()
opts['group'] = None
with self.assertRaises(AssertionError):
counter = DailyCounter(**opts)
with self.subTest('missing group check'):
opts = options.copy()
opts['group_check'] = None
counter = DailyCounter(**opts)
with self.assertRaises(SkipCheck) as ctx:
counter.check()
self.assertEqual(ctx.exception.level, 'debug')
group_check_value = options['group_check'].value
with self.subTest('missing group check'):
opts = options.copy()
opts['group_check'].value = 'broken'
counter = DailyCounter(**opts)
with self.assertRaises(SkipCheck) as ctx:
counter.check()
self.assertEqual(ctx.exception.level, 'info')
self.assertIn('cannot be converted to integer', ctx.exception.message)
options['group_check'].value = group_check_value
with self.subTest('MaxQuotaReached'):
acct_data = _acct_data.copy()
acct_data['session_time'] = '200000'
self._create_radius_accounting(**acct_data)
with self.assertRaises(MaxQuotaReached) as ctx:
counter.check()
self.assertEqual(ctx.exception.level, 'info')
self.assertEqual(ctx.exception.reply_message, BaseCounter.reply_message)
def test_traffic_counter_no_sessions(self):
opts = self._get_kwargs('Max-Daily-Session-Traffic')
counter = DailyTrafficCounter(**opts)
self.assertEqual(counter.check(), int(opts['group_check'].value))
def test_traffic_counter_with_sessions(self):
opts = self._get_kwargs('Max-Daily-Session-Traffic')
counter = DailyTrafficCounter(**opts)
acct = _acct_data.copy()
acct.update({'input_octets': '50000', 'output_octets': '60000'})
self._create_radius_accounting(**acct)
traffic = int(acct['input_octets']) + int(acct['output_octets'])
expected = int(opts['group_check'].value) - traffic
self.assertEqual(counter.check(), expected)
def test_traffic_counter_reply_and_check_name(self):
opts = self._get_kwargs('Max-Daily-Session-Traffic')
counter = DailyTrafficCounter(**opts)
self.assertEqual(counter.check_name, 'Max-Daily-Session-Traffic')
self.assertEqual(counter.reply_name, 'CoovaChilli-Max-Total-Octets')
def test_monthly_traffic_counter_with_sessions(self):
rg = RadiusGroup.objects.filter(name='test-org-users').first()
rgc = RadiusGroupCheck(
group=rg, attribute='Max-Monthly-Session-Traffic', value='3000000000'
)
rgc.full_clean()
rgc.save()
RadiusGroupCheck.objects.filter(attribute='Max-Daily-Session-Traffic').delete()
opts = self._get_kwargs('Max-Monthly-Session-Traffic')
counter = MonthlyTrafficCounter(**opts)
acct = _acct_data.copy()
acct.update({'input_octets': '50000', 'output_octets': '60000'})
self._create_radius_accounting(**acct)
traffic = int(acct['input_octets']) + int(acct['output_octets'])
expected = int(opts['group_check'].value) - traffic
self.assertEqual(counter.check(), expected)