Skip to content

Commit

Permalink
[qa] Updated to black 21.11
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Jan 19, 2022
1 parent bf16735 commit ba6499d
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 33 deletions.
6 changes: 4 additions & 2 deletions openwisp_controller/config/base/vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


def _peer_cache_key(vpn):
""" used to generate a unique cache key """
"""used to generate a unique cache key"""
return str(vpn.pk)


Expand Down Expand Up @@ -403,7 +403,9 @@ def auto_client(self, auto_cert=True, template_backend_class=None):
'vxlan_' if self._is_backend_type('vxlan') else ''
)
auto = getattr(template_backend_class, vpn_auto_client)(
host=vpn_host, server=self.config['wireguard'][0], **context_keys,
host=vpn_host,
server=self.config['wireguard'][0],
**context_keys,
)
else:
del context_keys['vpn_port']
Expand Down
3 changes: 2 additions & 1 deletion openwisp_controller/config/tests/test_vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,8 @@ def test_auto_peer_configuration(self):
self.assertIsInstance(peers, list)
self.assertEqual(len(peers), 1)
self.assertEqual(
peers[0], {'vni': vpnclient_qs.first().vni, 'remote': '10.0.0.2'},
peers[0],
{'vni': vpnclient_qs.first().vni, 'remote': '10.0.0.2'},
)

with self.subTest('cache update when a new peer is added'):
Expand Down
6 changes: 4 additions & 2 deletions openwisp_controller/subnet_division/rule_types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ def get_subnet_division_rules(cls, instance):
organization_id = cls.get_organization(instance)
subnet = cls.get_subnet(instance)
return subnet.subnetdivisionrule_set.filter(
organization_id__in=(organization_id, None), type=rule_type,
organization_id__in=(organization_id, None),
type=rule_type,
).iterator()

@classmethod
Expand Down Expand Up @@ -223,7 +224,8 @@ def create_ips(config, division_rule, generated_subnets, generated_indexes):
for subnet_obj in generated_subnets:
for ip_id in range(1, division_rule.number_of_ips + 1):
ip_obj = IpAddress(
subnet_id=subnet_obj.id, ip_address=str(subnet_obj.subnet[ip_id]),
subnet_id=subnet_obj.id,
ip_address=str(subnet_obj.subnet[ip_id]),
)
ip_obj.full_clean()
generated_ips.append(ip_obj)
Expand Down
4 changes: 3 additions & 1 deletion openwisp_controller/subnet_division/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@
)

HIDE_GENERATED_SUBNETS = getattr(
settings, 'OPENWISP_CONTROLLER_HIDE_AUTOMATICALLY_GENERATED_SUBNETS_AND_IPS', False,
settings,
'OPENWISP_CONTROLLER_HIDE_AUTOMATICALLY_GENERATED_SUBNETS_AND_IPS',
False,
)
12 changes: 9 additions & 3 deletions openwisp_controller/subnet_division/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def update_subnet_name_description(rule_id):
return

related_subnet_ids = division_rule.subnetdivisionindex_set.filter(
subnet_id__isnull=False, ip_id__isnull=True,
subnet_id__isnull=False,
ip_id__isnull=True,
).values_list('subnet_id')
subnet_queryset = Subnet.objects.filter(id__in=related_subnet_ids)

Expand Down Expand Up @@ -87,7 +88,9 @@ def _create_ipaddress_and_subnetdivision_index_objects(ips, indexes):
return

index_queryset = division_rule.subnetdivisionindex_set.filter(
subnet_id__isnull=False, config_id__isnull=False, ip_id__isnull=True,
subnet_id__isnull=False,
config_id__isnull=False,
ip_id__isnull=True,
).select_related('subnet')

starting_ip_id = old_number_of_ips + 1
Expand All @@ -96,7 +99,10 @@ def _create_ipaddress_and_subnetdivision_index_objects(ips, indexes):
for index in index_queryset:
subnet = index.subnet
for ip_id in range(starting_ip_id, ending_ip_id):
ip = IpAddress(subnet_id=subnet.id, ip_address=str(subnet.subnet[ip_id]),)
ip = IpAddress(
subnet_id=subnet.id,
ip_address=str(subnet.subnet[ip_id]),
)

generated_ips.append(ip)
generated_indexes.append(
Expand Down
12 changes: 8 additions & 4 deletions openwisp_controller/subnet_division/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ def test_related_links(self):
reverse(f'admin:{self.ipam_label}_subnet_changelist')
)
self.assertContains(
response, f'<a href="{url}">{self.config.device.name}</a>',
response,
f'<a href="{url}">{self.config.device.name}</a>',
)

with self.subTest('Test change view'):
response = self.client.get(
reverse(f'admin:{self.ipam_label}_subnet_change', args=[subnet.pk])
)
self.assertContains(
response, f'<a href="{url}">{self.config.device.name}</a>',
response,
f'<a href="{url}">{self.config.device.name}</a>',
)

def test_device_filter(self):
Expand All @@ -62,7 +64,8 @@ def test_device_filter(self):
url = f'{subnet_changelist}?device={self.config.device.name}'
response = self.client.get(url)
self.assertContains(
response, self.config.device.name,
response,
self.config.device.name,
)
self.assertNotContains(response, config2.device.name)

Expand Down Expand Up @@ -189,7 +192,8 @@ def test_subnet_filter(self):
)
response = self.client.get(url)
self.assertContains(
response, self.config.device.name,
response,
self.config.device.name,
)
self.assertNotContains(response, device2.name)

Expand Down
51 changes: 34 additions & 17 deletions openwisp_controller/subnet_division/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@


class TestSubnetDivisionRule(
SubnetDivisionTestMixin, TransactionTestCase,
SubnetDivisionTestMixin,
TransactionTestCase,
):
def setUp(self):
self.org = self._get_org()
self.master_subnet = self._get_master_subnet()
self.vpn_server = self._create_wireguard_vpn(
subnet=self.master_subnet, organization=self.org,
subnet=self.master_subnet,
organization=self.org,
)
self.template = self._create_template(
name='vpn-test', type='vpn', vpn=self.vpn_server, organization=self.org
Expand Down Expand Up @@ -192,7 +194,8 @@ def test_provisioned_subnets(self):
self.config.templates.add(self.template)

self.assertEqual(
subnet_query.count(), rule.number_of_subnets,
subnet_query.count(),
rule.number_of_subnets,
)
self.assertEqual(
self.ip_query.count(), (rule.number_of_subnets * rule.number_of_ips)
Expand Down Expand Up @@ -240,7 +243,8 @@ def test_rule_label_updated(self):

# Assert name and description of Subnet are updated
new_subnet_count = subnet_queryset.filter(
name__startswith=new_rule_label, description__contains=new_rule_label,
name__startswith=new_rule_label,
description__contains=new_rule_label,
).count()
self.assertEqual(subnet_count, new_subnet_count)

Expand Down Expand Up @@ -301,7 +305,8 @@ def test_vpnclient_deleted(self):
self.config.templates.add(self.template)

self.assertEqual(
subnet_query.count(), rule.number_of_subnets,
subnet_query.count(),
rule.number_of_subnets,
)
# 1 IP is automatically assigned to VPN server and client each,
# hence add two in below assertion
Expand All @@ -311,7 +316,8 @@ def test_vpnclient_deleted(self):

self.config.templates.remove(self.template)
self.assertEqual(
subnet_query.count(), 0,
subnet_query.count(),
0,
)
self.assertEqual(self.ip_query.count(), 0)

Expand All @@ -321,7 +327,8 @@ def test_subnets_exhausted(self, mocked_logger):
'10.0.0.0/28', master_subnet=self.master_subnet
)
self._get_vpn_subdivision_rule(
master_subnet=subnet, size=29,
master_subnet=subnet,
size=29,
)
self.vpn_server.subnet = subnet
self.vpn_server.save()
Expand Down Expand Up @@ -355,12 +362,14 @@ def test_subnet_already_provisioned(self):
)
self.config.templates.add(self.template)
self.assertEqual(
self.config.subnetdivisionindex_set.count(), index_count,
self.config.subnetdivisionindex_set.count(),
index_count,
)
vpn_client = VpnClient.objects.first()
vpn_client.save()
self.assertEqual(
self.config.subnetdivisionindex_set.count(), index_count,
self.config.subnetdivisionindex_set.count(),
index_count,
)

def test_shareable_vpn_vpnclient_subnet(self):
Expand Down Expand Up @@ -426,12 +435,14 @@ def test_device_deleted(self):
)
self.config.templates.add(self.template)
self.assertEqual(
subnet_query.count(), rule.number_of_subnets,
subnet_query.count(),
rule.number_of_subnets,
)

self.config.device.delete()
self.assertEqual(
subnet_query.count(), 0,
subnet_query.count(),
0,
)

def test_reserved_subnet(self):
Expand Down Expand Up @@ -476,7 +487,8 @@ def test_device_subnet_division_rule(self):
self.assertEqual(lines[0], 'registration-result: success')

self.assertEqual(
subnet_query.count(), rule.number_of_subnets,
subnet_query.count(),
rule.number_of_subnets,
)
self.assertEqual(
self.ip_query.count(), (rule.number_of_subnets * rule.number_of_ips)
Expand All @@ -494,7 +506,8 @@ def test_device_subnet_division_rule(self):
# Verify working of delete handler
device.delete()
self.assertEqual(
subnet_query.count(), 0,
subnet_query.count(),
0,
)
self.assertEqual(self.ip_query.count(), 0)

Expand All @@ -505,7 +518,8 @@ def test_device_subnet_division_rule_existing_devices(self):
self.assertEqual(subnet_query.count(), 0)
rule = self._get_device_subdivision_rule()
self.assertEqual(
subnet_query.count(), rule.number_of_subnets,
subnet_query.count(),
rule.number_of_subnets,
)
self.assertEqual(
self.ip_query.count(), (rule.number_of_subnets * rule.number_of_ips)
Expand All @@ -519,7 +533,8 @@ def test_vpn_subnet_division_rule_existing_devices(self):
self.assertEqual(subnet_query.count(), 0)
rule = self._get_vpn_subdivision_rule()
self.assertEqual(
subnet_query.count(), rule.number_of_subnets,
subnet_query.count(),
rule.number_of_subnets,
)
self.assertEqual(
self.ip_query.count(), (rule.number_of_subnets * rule.number_of_ips)
Expand Down Expand Up @@ -569,7 +584,8 @@ def test_subnet_ips_provisioned_signal(self):
id=self.master_subnet.id
)
self.assertEqual(
subnet_query.count(), rule.number_of_subnets,
subnet_query.count(),
rule.number_of_subnets,
)

def test_vpn_rule_assigns_vpnclient_ip(self):
Expand All @@ -584,7 +600,8 @@ def test_vpn_rule_assigns_vpnclient_ip(self):
vpn_client = self.config.vpnclient_set.first()
self.assertEqual(vpn_client.ip, expected_assigned_ip)
self.assertEqual(
subnet_query.count(), rule.number_of_subnets,
subnet_query.count(),
rule.number_of_subnets,
)

def test_subnet_division_index_validation(self):
Expand Down
10 changes: 8 additions & 2 deletions tests/openwisp2/sample_config/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,14 @@ class Migration(migrations.Migration):
),
),
('auto_cert', models.BooleanField(default=False)),
('public_key', models.CharField(blank=True, max_length=44),),
('private_key', models.CharField(blank=True, max_length=44),),
(
'public_key',
models.CharField(blank=True, max_length=44),
),
(
'private_key',
models.CharField(blank=True, max_length=44),
),
(
'vni',
models.PositiveIntegerField(
Expand Down
4 changes: 3 additions & 1 deletion tests/openwisp2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379/6',
'OPTIONS': {'CLIENT_CLASS': 'django_redis.client.DefaultClient',},
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
},
}
}

Expand Down

0 comments on commit ba6499d

Please sign in to comment.