|
1 | 1 | import socket
|
2 | 2 |
|
3 |
| -from django.test import TestCase |
| 3 | +from django.test import RequestFactory, TestCase |
| 4 | +from django.test.utils import override_settings |
4 | 5 |
|
5 |
| -from gargoyle.builtins import HostConditionSet |
| 6 | +from gargoyle.builtins import HostConditionSet, IPAddressConditionSet |
6 | 7 | from gargoyle.manager import SwitchManager
|
7 | 8 | from gargoyle.models import SELECTIVE, Switch
|
8 | 9 |
|
9 | 10 |
|
| 11 | +class IPAddressConditionSetTests(TestCase): |
| 12 | + |
| 13 | + condition_set = 'gargoyle.builtins.IPAddressConditionSet' |
| 14 | + |
| 15 | + def setUp(self): |
| 16 | + super(IPAddressConditionSetTests, self).setUp() |
| 17 | + self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, auto_create=True) |
| 18 | + self.gargoyle.register(IPAddressConditionSet()) |
| 19 | + self.request_factory = RequestFactory() |
| 20 | + |
| 21 | + Switch.objects.create(key='test', status=SELECTIVE) |
| 22 | + self.switch = self.gargoyle['test'] |
| 23 | + assert not self.gargoyle.is_active('test') |
| 24 | + |
| 25 | + def test_percent(self): |
| 26 | + self.switch.add_condition( |
| 27 | + condition_set=self.condition_set, |
| 28 | + field_name='percent', |
| 29 | + condition='0-100', |
| 30 | + ) |
| 31 | + |
| 32 | + request = self.request_factory.get('/', REMOTE_ADDR='1.0.0.0') |
| 33 | + assert self.gargoyle.is_active('test', request) |
| 34 | + |
| 35 | + def test_0_percent(self): |
| 36 | + self.switch.add_condition( |
| 37 | + condition_set=self.condition_set, |
| 38 | + field_name='percent', |
| 39 | + condition='0-0', |
| 40 | + ) |
| 41 | + |
| 42 | + request = self.request_factory.get('/', REMOTE_ADDR='1.0.0.0') |
| 43 | + assert not self.gargoyle.is_active('test', request) |
| 44 | + |
| 45 | + def test_specific_address(self): |
| 46 | + self.switch.add_condition( |
| 47 | + condition_set=self.condition_set, |
| 48 | + field_name='ip_address', |
| 49 | + condition='1.1.1.1', |
| 50 | + ) |
| 51 | + |
| 52 | + request = self.request_factory.get('/', REMOTE_ADDR='1.0.0.0') |
| 53 | + assert not self.gargoyle.is_active('test', request) |
| 54 | + |
| 55 | + request = self.request_factory.get('/', REMOTE_ADDR='1.1.1.1') |
| 56 | + assert self.gargoyle.is_active('test', request) |
| 57 | + |
| 58 | + @override_settings(INTERNAL_IPS=['1.0.0.0']) |
| 59 | + def test_internal_ip(self): |
| 60 | + self.switch.add_condition( |
| 61 | + condition_set=self.condition_set, |
| 62 | + field_name='internal_ip', |
| 63 | + condition='', |
| 64 | + ) |
| 65 | + |
| 66 | + request = self.request_factory.get('/', REMOTE_ADDR='1.0.0.0') |
| 67 | + assert self.gargoyle.is_active('test', request) |
| 68 | + |
| 69 | + request = self.request_factory.get('/', REMOTE_ADDR='1.1.1.1') |
| 70 | + assert not self.gargoyle.is_active('test', request) |
| 71 | + |
| 72 | + @override_settings(INTERNAL_IPS=['1.0.0.0']) |
| 73 | + def test_not_internal_ip(self): |
| 74 | + self.switch.add_condition( |
| 75 | + condition_set=self.condition_set, |
| 76 | + field_name='internal_ip', |
| 77 | + condition='', |
| 78 | + exclude=True, |
| 79 | + ) |
| 80 | + |
| 81 | + request = self.request_factory.get('/', REMOTE_ADDR='1.0.0.0') |
| 82 | + assert not self.gargoyle.is_active('test', request) |
| 83 | + |
| 84 | + request = self.request_factory.get('/', REMOTE_ADDR='1.1.1.1') |
| 85 | + assert self.gargoyle.is_active('test', request) |
| 86 | + |
| 87 | + |
10 | 88 | class HostConditionSetTest(TestCase):
|
11 | 89 | def setUp(self):
|
12 | 90 | self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, auto_create=True)
|
|
0 commit comments