mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2026-02-06 15:16:20 +01:00
Fix managed roles and reserved groups bugs in Discord Service and more
This commit is contained in:
35
allianceauth/utils/testing.py
Normal file
35
allianceauth/utils/testing.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import socket
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
class SocketAccessError(Exception):
|
||||
"""Error raised when a test script accesses the network"""
|
||||
|
||||
|
||||
class NoSocketsTestCase(TestCase):
|
||||
"""Variation of Django's TestCase class that prevents any network use.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
class TestMyStuff(NoSocketsTestCase):
|
||||
def test_should_do_what_i_need(self):
|
||||
...
|
||||
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.socket_original = socket.socket
|
||||
socket.socket = cls.guard
|
||||
return super().setUpClass()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
socket.socket = cls.socket_original
|
||||
return super().tearDownClass()
|
||||
|
||||
@staticmethod
|
||||
def guard(*args, **kwargs):
|
||||
raise SocketAccessError("Attempted to access network")
|
||||
9
allianceauth/utils/tests/test_testing.py
Normal file
9
allianceauth/utils/tests/test_testing.py
Normal file
@@ -0,0 +1,9 @@
|
||||
import requests
|
||||
from allianceauth.utils.testing import NoSocketsTestCase, SocketAccessError
|
||||
|
||||
|
||||
class TestNoSocketsTestCase(NoSocketsTestCase):
|
||||
def test_raises_exception_on_attempted_network_access(self):
|
||||
|
||||
with self.assertRaises(SocketAccessError):
|
||||
requests.get("https://www.google.com")
|
||||
Reference in New Issue
Block a user