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.py

169 lines
4.9 KiB
Python
Raw Normal View History

2018-11-08 19:45:21 +00:00
import pytest
import json
import os
from django.test import TestCase
from django.contrib.auth.models import Group
from django.conf import settings
from rest_framework.test import APIRequestFactory, force_authenticate, APIClient
from rest_framework.authtoken.models import Token
import peeringdb_server.models as models
import peeringdb_server.management.commands.pdb_api_test as api_test
from twentyc.rpc.client import RestClient
import django_namespace_perms as nsp
import peeringdb_server.inet as pdbinet
RdapLookup_get_asn = pdbinet.RdapLookup.get_asn
def setup_module(module):
# RDAP LOOKUP OVERRIDE
# Since we are working with fake ASNs throughout the api tests
# we need to make sure the RdapLookup client can fake results
# for us
# These ASNs will be seen as valid and a prepared json object
# will be returned for them (data/api/rdap_override.json)
#
# ALL ASNs outside of this range will raise a RdapNotFoundError
ASN_RANGE_OVERRIDE = list(range(9000000, 9000999))
2018-11-08 19:45:21 +00:00
with open(
2019-12-05 16:57:52 +00:00
os.path.join(os.path.dirname(__file__), "data", "api", "rdap_override.json"),
"r",
) as fh:
2018-11-08 19:45:21 +00:00
pdbinet.RdapLookup.override_result = json.load(fh)
def get_asn(self, asn):
if asn in ASN_RANGE_OVERRIDE:
return pdbinet.RdapAsn(self.override_result)
2019-02-07 08:00:22 +00:00
elif pdbinet.asn_is_bogon(asn):
return RdapLookup_get_asn(self, asn)
2018-11-08 19:45:21 +00:00
else:
raise pdbinet.RdapNotFoundError()
pdbinet.RdapLookup.get_asn = get_asn
def teardown_module(module):
pdbinet.RdapLookup.get_asn = RdapLookup_get_asn
class DummyResponse(object):
"""
Simulate requests response object
"""
def __init__(self, status_code, content, headers={}):
self.status_code = status_code
self.content = content
self.headers = headers
@property
def data(self):
return json.loads(self.content)
def read(self, *args, **kwargs):
return self.content
def getheader(self, name):
return self.headers.get(name)
def json(self):
return self.data
class DummyRestClient(RestClient):
"""
An extension of the twentyc.rpc RestClient that goes to the
django rest framework testing api instead
"""
def __init__(self, *args, **kwargs):
super(DummyRestClient, self).__init__(*args, **kwargs)
self.factory = APIRequestFactory()
self.api_client = APIClient()
self.useragent = kwargs.get("useragent")
2018-11-08 19:45:21 +00:00
if self.user:
self.user_inst = models.User.objects.get(username=self.user)
else:
self.user_inst = models.User.objects.get(username="guest")
self.api_client.force_authenticate(self.user_inst)
2019-12-05 16:57:52 +00:00
def _request(self, typ, id=0, method="GET", params=None, data=None, url=None):
2018-11-08 19:45:21 +00:00
if not url:
if id:
url = "/api/%s/%s" % (typ, id)
else:
2019-12-05 16:57:52 +00:00
url = "/api/%s" % (typ,)
2018-11-08 19:45:21 +00:00
fnc = getattr(self.api_client, method.lower())
if not data:
data = {}
if params:
data.update(**params)
res = fnc(url, data, format="json")
June updates (#751) * Add pointer from API docs to tutorial #650 * Sorting by clicking table headers should use local-compare #356 * Mark IXP peering LAN as bogon #352 * Add help text to "Add (Facility, Network, Exchange)" tab #669 * Add Looking Glass field to the IX object #672 * Add read-only Superuser #679 * Make spelling of traffic levels consistent #519 (#723) * Offer 2FA (#290) * Show "Last Updated" fields on fac, ix, org records (#526) * Enable sort and reverse sort of IP column in IX display (#72) * IRR validation not handling unexpected characters gracefully (#712) * Support alternative direction of writing, e.g. Arabic (#618) * Undeleting an ixlan with an emtpy IPv4 XOR IPv6 field throws a silly error (#644) * Changing org while adding net results in 500 #654 * missing delete button for organisations (#121) * When changing owner of an ix admin GUI borks because of "Ixlan for exchange already exists" #666 * Selection should only present undeleted objects (#664) * change default encoding of API calls to 'utf-8' #663 * Posting https://www.peeringdb.com onto social media doesn't select a good preview image #537 * Revert "Add Looking Glass field to the IX object #672" This reverts commit 4daf2520043c241fabe9a521757efa86a274e28a. Conflicts: peeringdb_server/migrations/0037_ix_looking_glass.py peeringdb_server/views.py * 500 Internal Error when creating IX where prefix already exists elsewhere #718 * Fix graceful restore of soft-deleted objects with translation active (#580) * Don't return any POC data with status=deleted #569 Hard delete soft-deleted pocs after grace period #566 * django-peeringdb from github@2.0.0.2-beta Co-authored-by: Stefan Pratter <stefan@20c.com>
2020-06-24 12:55:01 -05:00
assert res.charset == "utf-8"
2018-11-08 19:45:21 +00:00
return DummyResponse(res.status_code, res.content)
class APITests(TestCase, api_test.TestJSON, api_test.Command):
"""
API tests
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 = 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")
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)
nsp.models.GroupPermission.objects.create(
2019-12-05 16:57:52 +00:00
group=guest_group, namespace="peeringdb.organization", permissions=0x01
)
2018-11-08 19:45:21 +00:00
nsp.models.GroupPermission.objects.create(
2019-12-05 16:57:52 +00:00
group=user_group, namespace="peeringdb.organization", permissions=0x01
)
2018-11-08 19:45:21 +00:00
nsp.models.GroupPermission.objects.create(
2019-12-05 16:57:52 +00:00
group=user_group,
namespace="peeringdb.organization.{}".format(settings.SUGGEST_ENTITY_ORG),
permissions=0x04,
)
2018-11-08 19:45:21 +00:00
nsp.models.GroupPermission.objects.create(
group=user_group,
namespace="peeringdb.organization.*.network.*.poc_set.users",
2019-12-05 16:57:52 +00:00
permissions=0x01,
)
2018-11-08 19:45:21 +00:00
# prepare api test data
cls.prepare()