1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/tests/util.py
Matt Griswold 8ef76dc5ab Support 202110 (#1085)
* Block registering private ASN ranges

* Add a continental region field for facilities #1007

* Incorrect order of search results #232

* Allow users to upload a small logo to their record #346

* Sponsor badge CSS and text translation issues #453

* IXP and Facility summary #18

* Add sales email and phone contact to ix object #949

* Increase timeout timer for IX-F JSON importer to 30s #962

* IX-F manually triggered import bugs #1054

* remove survey notifications

* IX-F importer: manual add followed by IX-F prompted add can result in "The server rejected your data" #807

* performance fix for net view

* api documentation generate broken #956

* fix poc set missing from net view

* Auto focus cursor on search field on main website #695

* PeeringDB website has a poor choice of line-breaks for IPv6 addresses. #748

* Delete childless org objects #838

* linting

* poetry lock

* re-add mistakingly dropped translation to email template

* fix template variables

* regen docs

* regen api docs

* fix merge cruft

* django-peeringdb to 2.11 and poetry relock

* linting

* comment out django-peeringdb volume mount

* add missing declaration

Co-authored-by: Stefan Pratter <stefan@20c.com>
Co-authored-by: David Poarch <dpoarch@20c.com>
2021-11-09 09:47:38 -06:00

128 lines
3.5 KiB
Python

import json
import os
from django.conf import settings
from django.contrib.auth.models import AnonymousUser, Group
from django.test import TestCase
from django_grainy.models import GroupPermission, UserPermission
import peeringdb_server.models as models
from peeringdb_server import settings as pdb_settings
class ClientCase(TestCase):
@classmethod
def setUpTestData(cls):
# create user and guest group
cls.guest_group = guest_group = Group.objects.create(
name="guest", id=settings.GUEST_GROUP_ID
)
cls.user_group = user_group = Group.objects.create(
name="user", id=settings.USER_GROUP_ID
)
settings.USER_GROUP_ID = cls.user_group.id
settings.GUEST_GROUP_ID = cls.guest_group.id
cls.guest_user = models.User.objects.create_user(
"guest", "guest@localhost", "guest"
)
cls.guest_group.user_set.add(cls.guest_user)
GroupPermission.objects.create(
group=guest_group, namespace="peeringdb.organization", permission=0x01
)
GroupPermission.objects.create(
group=user_group, namespace="peeringdb.organization", permission=0x01
)
GroupPermission.objects.create(
group=user_group,
namespace="peeringdb.organization.*.network.*.poc_set.users",
permission=0x01,
)
GroupPermission.objects.create(
group=guest_group,
namespace="peeringdb.organization.*.network.*.poc_set.public",
permission=0x01,
)
class SettingsCase(ClientCase):
"""
Since we read settings from peeringdb_server.settings
we can't use the `settings` fixture from pytest-django
This class instead does something similar for peeringdb_server.settings,
where it will override settings specified and then reset after test case
is finished
"""
settings = {}
@classmethod
def setUp(cls):
cls._restore = {}
for k, v in list(cls.settings.items()):
cls._restore[k] = getattr(pdb_settings, k, getattr(settings, k, None))
setattr(pdb_settings, k, v)
setattr(settings, k, v)
@classmethod
def tearDown(cls):
for k, v in list(cls._restore.items()):
setattr(pdb_settings, k, v)
setattr(settings, k, v)
def reset_group_ids():
"""
Guest and user groups will get recreated for each tests,
however mysql sequential ids wont be reset between tests.
Tests that require USER_GROUP_ID and GUEST_GROUP_ID to
point to to correct groups should call this function
to make sure the settings are updated
"""
try:
settings.USER_GROUP_ID = Group.objects.get(name="user").id
except Group.DoesNotExist:
Group.objects.create(name="user", id=settings.USER_GROUP_ID)
try:
settings.GUEST_GROUP_ID = Group.objects.get(name="guest").id
except Group.DoesNotExist:
Group.objects.create(name="guest", id=settings.GUEST_GROUP_ID)
def override_group_id():
from django.test import override_settings
return override_settings(
USER_GROUP_ID=Group.objects.get(name="user").id,
GUEST_GROUP_ID=Group.objects.get(name="guest").id,
)
# For IXF member tests
def setup_test_data(filename):
json_data = {}
entities = {}
with open(
os.path.join(
os.path.dirname(__file__),
"data",
"json_members_list",
f"{filename}.json",
),
) as fh:
json_data = json.load(fh)
return json_data