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 299016282e Gh 724 (#897)
* remove log file writing from migration

* run tests on mysql

* fix tests (pt.1)

* fix tests (pt.2)

* fix all user_id errors in tests

* Fix geocode typo

* More test changes for mysql id issues

* Add coverage config that defines coverage db should go inside test folder

* update docs

* fix mysql user

* fix tests cli

* add mysql collate settings

* docs

* fix sync

* fix sync

* docs

* remove debug output

* remove XXX

* interim commit to move to dev box

* mv db local, rm after run

* updates for 724

* note layer error message and work around

* fix travis

* chown tests

* more travis fixes

* travis: touch Ctl/dev/.env

* write coverage report to ./coverage

* clean up docs

* formatting

Co-authored-by: Stefan Pratter <stefan@20c.com>
Co-authored-by: Elliot Frank <elliot@20c.com>
2020-12-03 13:10:02 -06:00

114 lines
3.3 KiB
Python

import json
import os
from django.test import TestCase
from django.contrib.auth.models import Group, AnonymousUser
from django.conf import settings
import peeringdb_server.models as models
import django_namespace_perms as nsp
from peeringdb_server import settings as pdb_settings
class ClientCase(TestCase):
@classmethod
def setUpTestData(cls):
# create user and guest group
cls.guest_group = Group.objects.create(name="guest", id=settings.GUEST_GROUP_ID)
cls.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)
nsp.models.GroupPermission.objects.create(
group=cls.guest_group, namespace="peeringdb.organization", permissions=0x01
)
nsp.models.GroupPermission.objects.create(
group=cls.user_group, namespace="peeringdb.organization", permissions=0x01
)
nsp.models.GroupPermission.objects.create(
group=cls.user_group,
namespace="peeringdb.organization.*.network.*.poc_set.users",
permissions=0x01,
)
nsp.models.GroupPermission.objects.create(
group=cls.guest_group,
namespace="peeringdb.organization.*.network.*.poc_set.public",
permissions=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
"""
settings.USER_GROUP_ID = Group.objects.get(name="user").id
settings.GUEST_GROUP_ID = Group.objects.get(name="guest").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