Files

171 lines
5.5 KiB
Python
Raw Permalink Normal View History

2021-07-10 10:12:35 -05:00
import datetime
2018-11-08 19:45:21 +00:00
import json
import os
import re
2021-07-10 10:12:35 -05:00
import tempfile
2018-11-08 19:45:21 +00:00
2021-07-10 10:12:35 -05:00
import pytest
from django.conf import settings
2018-11-08 19:45:21 +00:00
from django.contrib.auth.models import Group
from django.core.management import call_command
2021-07-10 10:12:35 -05:00
from django.test import TestCase
from django_grainy.models import GroupPermission, UserPermission
2021-01-13 20:35:07 +00:00
2018-11-08 19:45:21 +00:00
import peeringdb_server.management.commands.pdb_api_test as api_test
2021-07-10 10:12:35 -05:00
import peeringdb_server.models as models
2018-11-08 19:45:21 +00:00
2020-01-08 13:29:58 -06:00
from . import test_api as api_tests
2021-07-10 10:12:35 -05:00
from .util import reset_group_ids
2018-11-08 19:45:21 +00:00
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")
2021-03-09 13:30:30 -06:00
reset_group_ids()
2018-11-08 19:45:21 +00:00
guest_user = models.User.objects.create_user(
2019-12-05 16:57:52 +00:00
"guest", "guest@localhost", "guest"
)
2018-11-08 19:45:21 +00:00
guest_group.user_set.add(guest_user)
2021-01-13 20:35:07 +00:00
GroupPermission.objects.create(
group=guest_group, namespace="peeringdb.organization", permission=0x01
2019-12-05 16:57:52 +00:00
)
2018-11-08 19:45:21 +00:00
2021-01-13 20:35:07 +00:00
GroupPermission.objects.create(
2020-07-15 02:07:01 -05:00
group=guest_group,
namespace="peeringdb.organization.*.internetexchange.*.ixf_ixp_member_list_url.public",
2021-01-13 20:35:07 +00:00
permission=0x01,
2020-07-15 02:07:01 -05:00
)
2021-01-13 20:35:07 +00:00
GroupPermission.objects.create(
group=user_group, namespace="peeringdb.organization", permission=0x01
2019-12-05 16:57:52 +00:00
)
2018-11-08 19:45:21 +00:00
2021-01-13 20:35:07 +00:00
GroupPermission.objects.create(
2019-12-05 16:57:52 +00:00
group=user_group,
2020-07-15 02:07:01 -05:00
namespace=f"peeringdb.organization.{settings.SUGGEST_ENTITY_ORG}",
2021-01-13 20:35:07 +00:00
permission=0x04,
2019-12-05 16:57:52 +00:00
)
2018-11-08 19:45:21 +00:00
2021-01-13 20:35:07 +00:00
GroupPermission.objects.create(
2018-11-08 19:45:21 +00:00
group=user_group,
namespace="peeringdb.organization.*.network.*.poc_set.users",
2021-01-13 20:35:07 +00:00
permission=0x01,
2019-12-05 16:57:52 +00:00
)
2018-11-08 19:45:21 +00:00
2021-01-13 20:35:07 +00:00
GroupPermission.objects.create(
2020-07-15 02:07:01 -05:00
group=user_group,
namespace="peeringdb.organization.*.internetexchange.*.ixf_ixp_member_list_url.public",
2021-01-13 20:35:07 +00:00
permission=0x01,
2020-07-15 02:07:01 -05:00
)
2021-01-13 20:35:07 +00:00
GroupPermission.objects.create(
2020-07-15 02:07:01 -05:00
group=user_group,
namespace="peeringdb.organization.*.internetexchange.*.ixf_ixp_member_list_url.users",
2021-01-13 20:35:07 +00:00
permission=0x01,
2020-07-15 02:07:01 -05:00
)
2018-11-08 19:45:21 +00:00
# prepare api test data
cls.prepare()
settings.API_CACHE_ROOT = tempfile.mkdtemp()
2019-12-05 16:57:52 +00:00
settings.API_CACHE_LOG = os.path.join(settings.API_CACHE_ROOT, "log.log")
2018-11-08 19:45:21 +00:00
super_user = models.User.objects.create_user(
2019-12-05 16:57:52 +00:00
"admin", "admin@localhost", "admin"
)
2018-11-08 19:45:21 +00:00
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"))
2021-01-13 20:35:07 +00:00
settings.GENERATING_API_CACHE = False
2018-11-08 19:45:21 +00:00
def setUp(self):
settings.API_CACHE_ALL_LIMITS = True
settings.API_CACHE_ENABLED = True
2020-07-15 02:07:01 -05:00
super().setUp()
2018-11-08 19:45:21 +00:00
def tearDown(self):
settings.API_CACHE_ALL_LIMITS = False
settings.API_CACHE_ENABLED = False
2020-07-15 02:07:01 -05:00
super().tearDown()
2022-04-19 12:45:02 -04:00
@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(
2022-11-08 19:25:32 +02:00
setting="API_THROTTLE_REPEATED_REQUEST_ENABLED_IP", value_bool=True
2022-04-19 12:45:02 -04:00
)
models.EnvironmentSetting.objects.create(
2022-11-08 19:25:32 +02:00
setting="API_THROTTLE_REPEATED_REQUEST_THRESHOLD_IP", value_int=1
2022-04-19 12:45:02 -04:00
)
models.EnvironmentSetting.objects.create(
2022-11-08 19:25:32 +02:00
setting="API_THROTTLE_REPEATED_REQUEST_RATE_IP", value_str="1/minute"
2022-04-19 12:45:02 -04:00
)
models.EnvironmentSetting.objects.create(
2022-11-08 19:25:32 +02:00
setting="API_THROTTLE_REPEATED_REQUEST_ENABLED_CIDR", value_bool=True
2022-04-19 12:45:02 -04:00
)
models.EnvironmentSetting.objects.create(
2022-11-08 19:25:32 +02:00
setting="API_THROTTLE_REPEATED_REQUEST_THRESHOLD_CIDR", value_int=1
2022-04-19 12:45:02 -04:00
)
models.EnvironmentSetting.objects.create(
2022-11-08 19:25:32 +02:00
setting="API_THROTTLE_REPEATED_REQUEST_RATE_CIDR", value_str="1/minute"
2022-04-19 12:45:02 -04:00
)
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
2023-02-15 09:55:01 +02:00
for dirpath, dirnames, filenames in os.walk(settings.API_CACHE_ROOT):
2022-04-19 12:45:02 -04:00
for f in filenames:
if f in ["log.log"]:
continue
path = os.path.join(settings.API_CACHE_ROOT, f)
2022-07-15 21:47:59 +03:00
with open(path) as fh:
2022-04-19 12:45:02 -04:00
data_raw = fh.read()
data = json.loads(data_raw)
assert not data.get("message")