1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/tests/test_api_cache.py
Matt Griswold e85330c8a0 Support 202203 fixes (#1148)
* remove survey notifications

* fixing old reference of IXF_IMPORTER_DAYS_UNTIL_TICKET through EnvironmentSettings, this setting is no longer controlled through that and should come straight from settings

* fix session auth not setting x-auth-id header (#1120)
fix basic auth not setting x-auth-id header on success (#1120)
fix api key auth only setting prefix in x-auth-id header (#1120)
fix x-auth-id header not being cleared between requests (#1120)

* fix issue with rest throttling breaking api-cache generation (#1146)

* add caching for get_permission_holder_from_request - fixes perfomance issues in #1147

* fix intermediate issue with api_cache rest throttle tests

* sanitize cache key names for state normalization (#1079)
each state normalization lookup moved into its own transaction so errors dont cause us to lose already obtained data (#1079)
write cache regardess of --commit on or off (#1079)
add a sanity check for running non-committal mode without --limit (#1079)

* fix issue with ip block rate limiting if x-forwarded-for is set (#1126)

* better handling of melissa timeouts through retrying (#1079)
fix state normalization cache timeout to have no expiry (#1079)
normalization command will display validation errors at the end and exit with a return code if there are any (#1079)

* automatically apply address field normalization for `state` (#1079)

* additional tests

* only do a sanity check for --limit if no specific object is targeted

* linting

Co-authored-by: Stefan Pratter <stefan@20c.com>
2022-04-19 11:45:02 -05:00

171 lines
5.5 KiB
Python

import datetime
import json
import os
import re
import tempfile
import pytest
from django.conf import settings
from django.contrib.auth.models import Group
from django.core.management import call_command
from django.test import TestCase
from django_grainy.models import GroupPermission, UserPermission
import peeringdb_server.management.commands.pdb_api_test as api_test
import peeringdb_server.models as models
from . import test_api as api_tests
from .util import reset_group_ids
def setup_module(module):
api_tests.setup_module(module)
def teardown_module(module):
api_tests.teardown_module(module)
class APICacheTests(TestCase, api_test.TestJSON, api_test.Command):
"""
Runs the api test after generating cache files and enabling
api cache
You can find the logic / definition of those tests in
peeringdb_server.manangement.commands.pdb_api_test
This simply extends the command and testcase defined for it
but uses a special RestClient that sends requests to the
rest_framework testing api instead of a live server.
"""
# we want to use this rest-client for our requests
rest_client = api_tests.DummyRestClient
# The db will be empty and at least one of the tests
# requires there to be >100 organizations in the database
# this tells the test to create them
create_extra_orgs = 110
@classmethod
def setUpTestData(cls):
# create user and guest group
guest_group = Group.objects.create(name="guest")
user_group = Group.objects.create(name="user")
reset_group_ids()
guest_user = models.User.objects.create_user(
"guest", "guest@localhost", "guest"
)
guest_group.user_set.add(guest_user)
GroupPermission.objects.create(
group=guest_group, namespace="peeringdb.organization", permission=0x01
)
GroupPermission.objects.create(
group=guest_group,
namespace="peeringdb.organization.*.internetexchange.*.ixf_ixp_member_list_url.public",
permission=0x01,
)
GroupPermission.objects.create(
group=user_group, namespace="peeringdb.organization", permission=0x01
)
GroupPermission.objects.create(
group=user_group,
namespace=f"peeringdb.organization.{settings.SUGGEST_ENTITY_ORG}",
permission=0x04,
)
GroupPermission.objects.create(
group=user_group,
namespace="peeringdb.organization.*.network.*.poc_set.users",
permission=0x01,
)
GroupPermission.objects.create(
group=user_group,
namespace="peeringdb.organization.*.internetexchange.*.ixf_ixp_member_list_url.public",
permission=0x01,
)
GroupPermission.objects.create(
group=user_group,
namespace="peeringdb.organization.*.internetexchange.*.ixf_ixp_member_list_url.users",
permission=0x01,
)
# prepare api test data
cls.prepare()
settings.API_CACHE_ROOT = tempfile.mkdtemp()
settings.API_CACHE_LOG = os.path.join(settings.API_CACHE_ROOT, "log.log")
super_user = models.User.objects.create_user(
"admin", "admin@localhost", "admin"
)
super_user.is_superuser = True
super_user.is_staff = True
super_user.save()
# generate cache files
now = datetime.datetime.now() + datetime.timedelta(days=1)
call_command("pdb_api_cache", date=now.strftime("%Y%m%d"))
settings.GENERATING_API_CACHE = False
def setUp(self):
settings.API_CACHE_ALL_LIMITS = True
settings.API_CACHE_ENABLED = True
super().setUp()
def tearDown(self):
settings.API_CACHE_ALL_LIMITS = False
settings.API_CACHE_ENABLED = False
super().tearDown()
@pytest.mark.django_db
def test_no_api_throttle():
guest_group = Group.objects.create(name="guest")
user_group = Group.objects.create(name="user")
reset_group_ids()
models.EnvironmentSetting.objects.create(
setting="API_THROTTLE_RESPONSE_SIZE_ENABLED_IP", value_bool=True
)
models.EnvironmentSetting.objects.create(
setting="API_THROTTLE_RESPONSE_SIZE_THRESHOLD_IP", value_int=1
)
models.EnvironmentSetting.objects.create(
setting="API_THROTTLE_RESPONSE_SIZE_RATE_IP", value_str="1/minute"
)
models.EnvironmentSetting.objects.create(
setting="API_THROTTLE_RESPONSE_SIZE_ENABLED_CIDR", value_bool=True
)
models.EnvironmentSetting.objects.create(
setting="API_THROTTLE_RESPONSE_SIZE_THRESHOLD_CIDR", value_int=1
)
models.EnvironmentSetting.objects.create(
setting="API_THROTTLE_RESPONSE_SIZE_RATE_CIDR", value_str="1/minute"
)
models.EnvironmentSetting.objects.create(
setting="API_THROTTLE_RATE_ANON", value_str="1/minute"
)
call_command("pdb_generate_test_data", limit=2, commit=True)
now = datetime.datetime.now() + datetime.timedelta(days=1)
call_command("pdb_api_cache", date=now.strftime("%Y%m%d"))
settings.GENERATING_API_CACHE = False
for (dirpath, dirnames, filenames) in os.walk(settings.API_CACHE_ROOT):
for f in filenames:
if f in ["log.log"]:
continue
path = os.path.join(settings.API_CACHE_ROOT, f)
with open(path, "r") as fh:
data_raw = fh.read()
data = json.loads(data_raw)
assert not data.get("message")