Skip to content

Commit 3a55ff0

Browse files
committed
[ci] Add mix of unit tests to raise tests coverage
1 parent c4667a6 commit 3a55ff0

File tree

9 files changed

+71
-39
lines changed

9 files changed

+71
-39
lines changed

backend/globaleaks/handlers/admin/network.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def db_update_network(session, tid, user_session, request):
2828
:param request: The request data
2929
:return: Return the serialized configuration for the specified tenant
3030
"""
31-
# Validate that IP addresses/ranges we're getting are good
31+
# Validate the IP addresses/ranges
3232
for k in ['admin', 'analyst', 'custodian', 'receiver']:
3333
if 'ip_filter_' + k in request and request['ip_filter_' + k + '_enable'] and request['ip_filter_' + k]:
3434
parse_csv_ip_ranges_to_ip_networks(request['ip_filter_' + k])

backend/globaleaks/tests/handlers/admin/test_network.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import copy
2+
13
from twisted.internet.defer import inlineCallbacks
24

35
from globaleaks.handlers.admin import network
@@ -7,6 +9,7 @@
79
class TestNetworkInstance(helpers.TestHandlerWithPopulatedDB):
810
_handler = network.NetworkInstance
911

12+
1013
@inlineCallbacks
1114
def test_get(self):
1215
handler = self.request(role='admin')
@@ -18,9 +21,12 @@ def test_get(self):
1821

1922
@inlineCallbacks
2023
def test_put(self):
21-
self.dummyNetwork['anonymize_outgoing_connection'] = True
24+
config = copy.deepcopy(self.dummyNetwork)
25+
config['ip_filter_custodian_enable'] = True
26+
config['ip_filter_custodian'] = '10.0.0.0/24'
27+
config['anonymize_outgoing_connection'] = True
2228

23-
handler = self.request(self.dummyNetwork, role='admin')
29+
handler = self.request(config, role='admin')
2430
response = yield handler.put()
2531

2632
self.assertTrue(isinstance(response, dict))

backend/globaleaks/tests/handlers/admin/test_operation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def _test_operation_handler(self, operation, args={}):
5858

5959
return handler.put()
6060

61-
def atest_admin_test_set_hostname(self):
61+
def test_admin_test_set_hostname(self):
6262
return self._test_operation_handler('set_hostname',
6363
{'value': 'www.nsa.gov'})
6464

backend/globaleaks/tests/handlers/admin/test_redirect.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
from globaleaks.orm import tw
66
from globaleaks.tests import helpers
77

8-
def get_dummy_redirect_desc():
9-
return {
10-
'path1': '/old',
11-
'path2': '/new'
12-
}
13-
14-
158
class TestRedirectCollection(helpers.TestHandlerWithPopulatedDB):
169
_handler = RedirectCollection
1710

@@ -29,15 +22,15 @@ def test_get(self):
2922

3023
@inlineCallbacks
3124
def test_post(self):
32-
handler = self.request(get_dummy_redirect_desc(), role='admin')
25+
handler = self.request(self.get_dummy_redirect(), role='admin')
3326
redirect = yield handler.post()
3427

3528
self.assertEqual(redirect['path1'], '/old')
3629
self.assertEqual(redirect['path2'], '/new')
3730

3831
@inlineCallbacks
3932
def _create_redirect(self):
40-
handler = self.request(get_dummy_redirect_desc(), role='admin')
33+
handler = self.request(self.get_dummy_redirect(), role='admin')
4134
return (yield handler.post())
4235

4336

backend/globaleaks/tests/helpers.py

+3-17
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,7 @@ def isSecure():
505505
request.isSecure = isSecure
506506

507507
def getResponseBody():
508-
# Ugh, hack. Twisted returns this all as bytes, and we want it as str
509-
if isinstance(request.written[0], bytes):
510-
return b''.join(request.written)
511-
else:
512-
return ''.join(request.written)
508+
return b''.join(request.written) if isinstance(request.written[0], bytes) else ''.join(request.written)
513509

514510
request.getResponseBody = getResponseBody
515511

@@ -715,8 +711,8 @@ def get_dummy_attachment(self, name=None, content=None):
715711

716712
def get_dummy_redirect(self, x=''):
717713
return {
718-
'path1': '/path1-' + x,
719-
'path2': '/path2-' + x
714+
'path1': '/old' + x,
715+
'path2': '/new' + x
720716
}
721717

722718
def emulate_file_upload(self, session, n):
@@ -939,10 +935,6 @@ def set_itips_near_to_expire(self, session):
939935
date = datetime_now() + timedelta(hours=self.state.tenants[1].cache.notification.tip_expiration_threshold - 1)
940936
session.query(models.InternalTip).update({'expiration_date': date})
941937

942-
@transact
943-
def set_contexts_timetolive(self, session, ttl):
944-
session.query(models.Context).update({'tip_timetolive': ttl})
945-
946938

947939
class TestHandler(TestGLWithPopulatedDB):
948940
"""
@@ -1038,12 +1030,6 @@ def request(self, body='', uri=b'https://www.globaleaks.org/',
10381030

10391031
return handler
10401032

1041-
def ss_serial_desc(self, safe_set, request_desc):
1042-
"""
1043-
Constructs a request_dec parser of a handler that uses a safe_set in its serialization
1044-
"""
1045-
return {k: v for k, v in request_desc.items() if k in safe_set}
1046-
10471033
def get_dummy_request(self):
10481034
return self._test_desc['model']().dict(u'en')
10491035

backend/globaleaks/tests/rest/test_decorators.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ def setUp(self):
3838
@defer.inlineCallbacks
3939
def test_decorator_rate_limit(self, mock_sleep):
4040
@decorator_rate_limit
41-
def test_func(self):
42-
return defer.succeed("Allowed")
41+
def test_func(self): return defer.succeed("Allowed")
4342

4443
self.session.role = "whistleblower"
4544

@@ -59,8 +58,7 @@ def test_func(self):
5958

6059
def test_decorator_require_session_or_token(self):
6160
@decorator_require_session_or_token
62-
def test_func(self):
63-
return "Authenticated"
61+
def test_func(self): return "Authenticated"
6462

6563
self.session = None
6664
self.request.token = None

backend/globaleaks/tests/utils/test_ip.py

+50-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from twisted.trial import unittest
2-
2+
import ipaddress
33
from globaleaks.utils import ip
44

55

66
class TestIPUtils(unittest.TestCase):
7-
def test_parse_csv_ip_ranges_to_ip_networks(self):
7+
def test_check_ip(self):
88
ip_str = "192.168.1.1,10.0.0.0/8,::1,2001:db8::/32"
99
self.assertTrue(ip.check_ip("192.168.1.1", ip_str))
1010

@@ -16,3 +16,51 @@ def test_parse_csv_ip_ranges_to_ip_networks(self):
1616

1717
ip_str = "192.168.1.2, 10.0.0.0/8, ::1,2001:db8::/32"
1818
self.assertTrue(ip.check_ip("2001:db8::2", ip_str))
19+
20+
def test_check_ip_invalid_ip(self):
21+
"""Test with an invalid IP address."""
22+
ip_str = "192.168.1.1,10.0.0.0/8"
23+
self.assertFalse(ip.check_ip("999.999.999.999", ip_str))
24+
25+
def test_check_ip_invalid_filter(self):
26+
"""Test with an invalid IP filter."""
27+
ip_str = "invalid_data"
28+
self.assertFalse(ip.check_ip("10.0.0.1", ip_str))
29+
30+
def test_check_ip_empty_filter(self):
31+
"""Test with an empty IP filter."""
32+
ip_str = ""
33+
self.assertFalse(ip.check_ip("10.0.0.1", ip_str))
34+
35+
def test_check_ip_bytes_input(self):
36+
"""Test with client IP as bytes."""
37+
ip_str = "10.0.0.0/8"
38+
self.assertTrue(ip.check_ip(b"10.0.0.1", ip_str))
39+
40+
def test_check_ip_ipv6(self):
41+
"""Test with IPv6 addresses."""
42+
ip_str = "::1,2001:db8::/32"
43+
self.assertTrue(ip.check_ip("2001:db8::2", ip_str))
44+
self.assertFalse(ip.check_ip("2001:dead::1", ip_str))
45+
46+
def test_parse_csv_ip_ranges_to_ip_networks(self):
47+
"""Test parsing valid IP and CIDR ranges."""
48+
ip_str = "192.168.1.1,10.0.0.0/8,::1,2001:db8::/32"
49+
expected = [
50+
ipaddress.ip_network("192.168.1.1/32"),
51+
ipaddress.ip_network("10.0.0.0/8"),
52+
ipaddress.ip_network("::1/128"),
53+
ipaddress.ip_network("2001:db8::/32")
54+
]
55+
56+
self.assertEqual(ip.parse_csv_ip_ranges_to_ip_networks(ip_str), expected)
57+
58+
def test_parse_csv_ip_ranges_invalid_entries(self):
59+
"""Test parsing invalid IPs and CIDR ranges."""
60+
invalid_ip_str = "invalid,300.300.300.300,10.0.0.1/33"
61+
with self.assertRaises(ValueError):
62+
ip.parse_csv_ip_ranges_to_ip_networks(invalid_ip_str)
63+
64+
def test_parse_csv_ip_ranges_empty(self):
65+
"""Test parsing an empty input."""
66+
self.assertEqual(ip.parse_csv_ip_ranges_to_ip_networks(""), [])

backend/globaleaks/utils/crypto.py

-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ def generateRandomPassword(N: int) -> str:
6464
Contains at least an uppercase ascii letter
6565
Contains at least a symbol in a selction of 10 common and accessible symbols
6666
"""
67-
if N < 10:
68-
N = 10
69-
7067
accessible_special_symbols = "!?@#+-/*="
7168
accessible_symbols_set = string.ascii_letters + string.digits + accessible_special_symbols
7269

backend/globaleaks/utils/ip.py

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ def parse_csv_ip_ranges_to_ip_networks(ip_str):
77
:param ip_str: the string of comma separated IP
88
:return: the list of parsed IPs
99
"""
10+
# Handle empty input
11+
if not ip_str:
12+
return []
13+
1014
# strip all white spaces
1115
ip_str = "".join(ip_str.split())
1216

0 commit comments

Comments
 (0)