2018-11-08 19:45:21 +00:00
#!/bin/env python
2019-05-02 15:20:20 +00:00
# -*- coding: utf-8 -*-
2018-11-08 19:45:21 +00:00
"""
series of integration / unit tests for the pdb api
"""
2020-01-08 13:29:58 -06:00
import pytest
2018-11-08 19:45:21 +00:00
import copy
import unittest
import uuid
import random
import re
import time
import datetime
2020-02-05 21:26:21 -06:00
import json
2018-11-08 19:45:21 +00:00
2019-12-05 16:57:52 +00:00
from twentyc . rpc import (
RestClient ,
PermissionDeniedException ,
InvalidRequestException ,
NotFoundException ,
)
from django_namespace_perms . constants import (
PERM_READ ,
PERM_UPDATE ,
PERM_CREATE ,
PERM_DELETE ,
)
2018-11-08 19:45:21 +00:00
from django . core . management . base import BaseCommand
from django . contrib . auth . models import Group
from django . conf import settings
from django . db . utils import IntegrityError
from rest_framework import serializers
2020-02-05 21:26:21 -06:00
from rest_framework . test import APIRequestFactory
2018-11-08 19:45:21 +00:00
from peeringdb_server . models import (
2019-12-05 16:57:52 +00:00
REFTAG_MAP ,
QUEUE_ENABLED ,
User ,
Organization ,
Network ,
InternetExchange ,
Facility ,
NetworkContact ,
NetworkIXLan ,
NetworkFacility ,
IXLan ,
IXLanPrefix ,
InternetExchangeFacility ,
2020-07-15 02:07:01 -05:00
DeskProTicket ,
2019-12-05 16:57:52 +00:00
)
2018-11-08 19:45:21 +00:00
from peeringdb_server . serializers import REFTAG_MAP as REFTAG_MAP_SLZ
2019-02-07 08:00:22 +00:00
from peeringdb_server import inet , settings as pdb_settings
2020-02-05 21:26:21 -06:00
from peeringdb_server . rest import NetworkViewSet
2018-11-08 19:45:21 +00:00
START_TIMESTAMP = time . time ( )
SHARED = { }
NUMERIC_TESTS = {
" lt " : " Less " ,
" lte " : " LessEqual " ,
" gt " : " Greater " ,
" gte " : " GreaterEqual " ,
2019-12-05 16:57:52 +00:00
" " : " Equal " ,
2018-11-08 19:45:21 +00:00
}
DATETIME = datetime . datetime . now ( )
DATE = DATETIME . date ( )
DATE_YDAY = DATE - datetime . timedelta ( days = 1 )
DATE_TMRW = DATE - datetime . timedelta ( days = - 1 )
DATES = {
" today " : ( DATE , DATE . strftime ( " % Y- % m- %d " ) ) ,
" yesterday " : ( DATE_YDAY , DATE_YDAY . strftime ( " % Y- % m- %d " ) ) ,
2019-12-05 16:57:52 +00:00
" tomorrow " : ( DATE_TMRW , DATE_TMRW . strftime ( " % Y- % m- %d " ) ) ,
2018-11-08 19:45:21 +00:00
}
# entity names
ORG_RW = " API Test Organization RW "
ORG_RW_PENDING = " %s :Pending " % ORG_RW
ORG_R = " API Test Organization R "
NET_R = " %s :Network " % ORG_R
NET_R_PENDING = " %s :Pending " % NET_R
NET_R_DELETED = " %s :Deleted " % NET_R
IX_R = " %s :Exchange " % ORG_R
FAC_R = " %s :Facility " % ORG_R
# user specs
USER = { " user " : " api_test " , " password " : " 89c8ec05-b897 " }
USER_ORG_ADMIN = { " user " : " api_test_org_admin " , " password " : " 89c8ec05-b897 " }
USER_ORG_MEMBER = { " user " : " api_test_org_member " , " password " : " 89c8ec05-b897 " }
USER_CRUD = {
2019-12-05 16:57:52 +00:00
" delete " : { " user " : " api_test_crud_delete " , " password " : " 89c8ec05-b897 " } ,
" update " : { " user " : " api_test_crud_update " , " password " : " 89c8ec05-b897 " } ,
" create " : { " user " : " api_test_crud_create " , " password " : " 89c8ec05-b897 " } ,
2018-11-08 19:45:21 +00:00
}
# server location
URL = settings . API_URL
# common
CITY = " Chicago "
COUNTRY = " US "
CONTINENT = " North America "
2020-01-08 13:29:58 -06:00
PHONE = " +12065550199 "
2018-11-08 19:45:21 +00:00
WEBSITE = " http://www.test.apitest "
STATE = " IL "
ZIPCODE = " 1-2345 "
NOTE = " This is a test entry made by a script to test out the API "
EMAIL = " test@20c.com "
VERBOSE = False
2019-05-02 18:16:44 +00:00
PREFIXES_V4 = [
2020-01-08 13:29:58 -06:00
" 206.223.114.0/24 " ,
" 206.223.115.0/24 " ,
" 206.223.116.0/24 " ,
" 206.223.117.0/24 " ,
" 206.223.118.0/24 " ,
" 206.223.119.0/24 " ,
" 206.223.120.0/24 " ,
" 206.223.121.0/24 " ,
" 206.223.122.0/24 " ,
2019-05-02 18:16:44 +00:00
]
PREFIXES_V6 = [
2020-01-08 13:29:58 -06:00
" 2001:504:0:1::/64 " ,
" 2001:504:0:2::/64 " ,
" 2001:504:0:3::/64 " ,
" 2001:504:0:4::/64 " ,
" 2001:504:0:5::/64 " ,
" 2001:504:0:6::/64 " ,
" 2001:504:0:7::/64 " ,
" 2001:504:0:8::/64 " ,
" 2001:504:0:9::/64 " ,
2019-05-02 18:16:44 +00:00
]
2018-11-08 19:45:21 +00:00
class TestJSON ( unittest . TestCase ) :
rest_client = RestClient
PREFIX_COUNT = 110
IP4_COUNT = 1
IP6_COUNT = 1
@classmethod
2019-05-02 18:16:44 +00:00
def get_ip6 ( cls , ixlan ) :
hosts = [ ]
2019-12-05 16:57:52 +00:00
for host in (
ixlan . ixpfx_set . filter ( status = ixlan . status , protocol = 6 )
. first ( )
. prefix . hosts ( )
) :
2019-05-02 18:16:44 +00:00
if len ( hosts ) < 100 :
hosts . append ( host )
else :
break
2020-01-08 13:29:58 -06:00
r = " {} " . format ( hosts [ cls . IP6_COUNT ] )
2018-11-08 19:45:21 +00:00
cls . IP6_COUNT + = 1
return r
@classmethod
2019-05-02 18:16:44 +00:00
def get_ip4 ( cls , ixlan ) :
hosts = [ ]
2019-12-05 16:57:52 +00:00
for host in (
ixlan . ixpfx_set . filter ( status = ixlan . status , protocol = 4 )
. first ( )
. prefix . hosts ( )
) :
2019-05-02 18:16:44 +00:00
if len ( hosts ) < 100 :
hosts . append ( host )
else :
break
2020-01-08 13:29:58 -06:00
r = " {} " . format ( hosts [ cls . IP4_COUNT ] )
2018-11-08 19:45:21 +00:00
cls . IP4_COUNT + = 1
return r
@classmethod
def get_prefix4 ( cls ) :
2020-01-08 13:29:58 -06:00
r = " 206.41. {} .0/24 " . format ( cls . PREFIX_COUNT )
2018-11-08 19:45:21 +00:00
cls . PREFIX_COUNT + = 1
return r
2018-12-04 09:43:11 +00:00
@classmethod
def get_prefix6 ( cls ) :
2020-01-08 13:29:58 -06:00
r = " 2001:504:41: {} ::/64 " . format ( cls . PREFIX_COUNT )
2018-12-04 09:43:11 +00:00
cls . PREFIX_COUNT + = 1
return r
2018-11-08 19:45:21 +00:00
def setUp ( self ) :
self . db_guest = self . rest_client ( URL , verbose = VERBOSE )
self . db_user = self . rest_client ( URL , verbose = VERBOSE , * * USER )
2019-12-05 16:57:52 +00:00
self . db_org_member = self . rest_client ( URL , verbose = VERBOSE , * * USER_ORG_MEMBER )
self . db_org_admin = self . rest_client ( URL , verbose = VERBOSE , * * USER_ORG_ADMIN )
2020-06-24 12:55:01 -05:00
self . user_org_admin = User . objects . get ( username = " api_test_org_admin " )
2020-01-08 13:29:58 -06:00
for p , specs in list ( USER_CRUD . items ( ) ) :
2019-12-05 16:57:52 +00:00
setattr (
self , " db_crud_ %s " % p , self . rest_client ( URL , verbose = VERBOSE , * * specs )
)
2018-11-08 19:45:21 +00:00
def all_dbs ( self , exclude = [ ] ) :
return [
db
for db in [
2019-12-05 16:57:52 +00:00
self . db_guest ,
self . db_org_member ,
self . db_user ,
self . db_org_admin ,
self . db_crud_create ,
self . db_crud_delete ,
self . db_crud_update ,
]
if db not in exclude
2018-11-08 19:45:21 +00:00
]
def readonly_dbs ( self , exclude = [ ] ) :
return [
2019-12-05 16:57:52 +00:00
db
for db in [ self . db_guest , self . db_org_member , self . db_user ]
2018-11-08 19:45:21 +00:00
if db not in exclude
]
##########################################################################
@classmethod
def make_data_org ( self , * * kwargs ) :
data = {
" name " : self . make_name ( " Test " ) ,
" website " : WEBSITE ,
" notes " : NOTE ,
" address1 " : " address " ,
" address2 " : " address " ,
" city " : CITY ,
" country " : COUNTRY ,
" state " : " state " ,
2019-12-05 16:57:52 +00:00
" zipcode " : " 12345 " ,
2018-11-08 19:45:21 +00:00
}
data . update ( * * kwargs )
return data
##########################################################################
@classmethod
def make_data_ix ( self , * * kwargs ) :
data = {
" name " : self . make_name ( " Test " ) ,
" org_id " : SHARED [ " org_rw_ok " ] . id ,
" name_long " : self . make_name ( " Test Long Name " ) ,
" city " : CITY ,
" country " : COUNTRY ,
" region_continent " : CONTINENT ,
" media " : " Ethernet " ,
" notes " : NOTE ,
" proto_unicast " : True ,
" proto_multicast " : False ,
" proto_ipv6 " : True ,
" website " : WEBSITE ,
" url_stats " : " %s /stats " % WEBSITE ,
" tech_email " : EMAIL ,
" tech_phone " : PHONE ,
" policy_email " : EMAIL ,
2019-12-05 16:57:52 +00:00
" policy_phone " : PHONE ,
2018-11-08 19:45:21 +00:00
}
data . update ( * * kwargs )
return data
##########################################################################
@classmethod
def make_data_fac ( self , * * kwargs ) :
data = {
" name " : self . make_name ( " Test " ) ,
" org_id " : SHARED [ " org_rw_ok " ] . id ,
" website " : WEBSITE ,
2020-03-06 16:26:37 +00:00
" city " : CITY ,
" zipcode " : ZIPCODE ,
" address1 " : " Some street " ,
2018-11-08 19:45:21 +00:00
" clli " : str ( uuid . uuid4 ( ) ) [ : 6 ] . upper ( ) ,
2020-07-15 02:07:01 -05:00
" rencode " : " " ,
2018-11-08 19:45:21 +00:00
" npanxx " : " 000-111 " ,
" latitude " : None ,
" longitude " : None ,
2019-12-05 16:57:52 +00:00
" notes " : NOTE ,
2020-04-04 10:11:13 +00:00
" country " : COUNTRY ,
" tech_email " : EMAIL ,
" tech_phone " : PHONE ,
" sales_email " : EMAIL ,
" sales_phone " : PHONE ,
2018-11-08 19:45:21 +00:00
}
data . update ( * * kwargs )
return data
##########################################################################
@classmethod
def make_data_net ( self , * * kwargs ) :
try :
asn = Network . objects . order_by ( " -asn " ) . first ( ) . asn
except AttributeError :
asn = 90000000
if asn < 90000000 :
asn = 90000000
else :
asn = asn + 1
data = {
" name " : self . make_name ( " Test " ) ,
" org_id " : SHARED [ " org_rw_ok " ] . id ,
" aka " : self . make_name ( " Also known as " ) ,
" asn " : asn ,
" website " : WEBSITE ,
2020-03-31 11:04:26 +00:00
" irr_as_set " : " AS-ZZ-ZZZZZZ@RIPE " ,
2018-11-08 19:45:21 +00:00
" info_type " : " NSP " ,
" info_prefixes4 " : 11000 ,
" info_prefixes6 " : 12000 ,
2020-05-21 03:53:49 -05:00
" info_traffic " : " 1-5Tbps " ,
2018-11-08 19:45:21 +00:00
" info_ratio " : " Mostly Outbound " ,
" info_scope " : " Global " ,
" info_unicast " : True ,
" info_multicast " : False ,
" info_ipv6 " : True ,
2020-01-08 13:29:58 -06:00
" info_never_via_route_servers " : True ,
2018-11-08 19:45:21 +00:00
" notes " : NOTE ,
" policy_url " : " %s /policy " % WEBSITE ,
" policy_general " : " Restrictive " ,
" policy_locations " : " Required - International " ,
" policy_ratio " : True ,
2019-12-05 16:57:52 +00:00
" policy_contracts " : " Required " ,
2020-07-15 02:07:01 -05:00
" allow_ixp_update " : True ,
2018-11-08 19:45:21 +00:00
}
data . update ( * * kwargs )
return data
##########################################################################
@classmethod
def make_data_poc ( self , * * kwargs ) :
data = {
" net_id " : 1 ,
" role " : " Technical " ,
" visible " : " Private " ,
" name " : " NOC " ,
" phone " : PHONE ,
" email " : EMAIL ,
2019-12-05 16:57:52 +00:00
" url " : WEBSITE ,
2018-11-08 19:45:21 +00:00
}
data . update ( * * kwargs )
return data
##########################################################################
@classmethod
def make_data_ixlan ( self , * * kwargs ) :
data = {
" ix_id " : 1 ,
2020-02-05 21:25:25 -06:00
" id " : 1 ,
2018-11-08 19:45:21 +00:00
" name " : self . make_name ( " Test " ) ,
" descr " : NOTE ,
" mtu " : 12345 ,
" dot1q_support " : False ,
2020-07-15 02:07:01 -05:00
" ixf_ixp_member_list_url_visible " : " Private " ,
2018-11-08 19:45:21 +00:00
" rs_asn " : 12345 ,
2019-12-05 16:57:52 +00:00
" arp_sponge " : None ,
2018-11-08 19:45:21 +00:00
}
2020-02-05 21:25:25 -06:00
if " ix_id " in kwargs :
data [ " id " ] = kwargs . get ( " ix_id " )
2018-11-08 19:45:21 +00:00
data . update ( * * kwargs )
return data
##########################################################################
@classmethod
def make_data_ixpfx ( self , * * kwargs ) :
data = {
" ixlan_id " : SHARED [ " ixlan_r_ok " ] . id ,
" protocol " : " IPv4 " ,
2019-12-05 16:57:52 +00:00
" prefix " : " 10. %d .10.0/23 " % ( self . PREFIX_COUNT + 1 ) ,
2020-08-18 09:28:18 -05:00
" in_dfz " : True ,
2018-11-08 19:45:21 +00:00
}
if " prefix " not in kwargs :
self . PREFIX_COUNT + = 1
data . update ( * * kwargs )
return data
##########################################################################
@classmethod
def make_data_netixlan ( self , rename = { } , * * kwargs ) :
data = {
" net_id " : SHARED [ " net_r_ok " ] . id ,
" ixlan_id " : SHARED [ " ixlan_r_ok " ] . id ,
" notes " : NOTE ,
" speed " : 30000 ,
" asn " : 12345 ,
}
data . update ( * * kwargs )
2020-01-08 13:29:58 -06:00
for k , v in list ( rename . items ( ) ) :
2018-11-08 19:45:21 +00:00
data [ v ] = data [ k ]
del data [ k ]
2019-05-02 18:16:44 +00:00
data . update (
ipaddr4 = self . get_ip4 ( IXLan . objects . get ( id = data [ " ixlan_id " ] ) ) ,
ipaddr6 = self . get_ip6 ( IXLan . objects . get ( id = data [ " ixlan_id " ] ) ) ,
)
2018-11-08 19:45:21 +00:00
return data
##########################################################################
@classmethod
def make_name ( self , name ) :
return " api-test: %s : %s " % ( name , uuid . uuid4 ( ) )
##########################################################################
@classmethod
def serializer_related_fields ( cls , serializer_class ) :
"""
Returns declared relation fields on the provided serializer class
Returned value will be a tuple in which the first item is a list of
field names for primary key related fields and the second item is a list
of fields names for related sets
"""
pk_rel = [ ]
nested_rel = [ ]
2020-01-08 13:29:58 -06:00
for name , fld in list ( serializer_class . _declared_fields . items ( ) ) :
2018-11-08 19:45:21 +00:00
if type ( fld ) == serializers . PrimaryKeyRelatedField :
pk_rel . append ( name [ : - 3 ] )
elif isinstance ( fld , serializers . ListSerializer ) :
nested_rel . append ( ( name , fld . child ) )
return ( pk_rel , nested_rel )
##########################################################################
def assert_handleref_integrity ( self , data ) :
"""
here we assert the integrity of a handleref ( which is
the base of all the models exposed on the api )
we do this by making sure all of the handleref fields
exist in the data
"""
self . assertIn ( " status " , data )
# self.assertIn("version", data)
self . assertIn ( " id " , data )
self . assertIn ( " created " , data )
self . assertIn ( " updated " , data )
self . assertNotEqual ( " created " , None )
##########################################################################
def assert_data_integrity ( self , data , typ , ignore = [ ] ) :
if hasattr ( self , " make_data_ %s " % typ ) :
msg = " data integrity failed on key ' %s ' "
func = getattr ( self , " make_data_ %s " % typ )
2020-01-08 13:29:58 -06:00
for k , v in list ( func ( ) . items ( ) ) :
2018-11-08 19:45:21 +00:00
if k in ignore :
continue
2020-01-08 13:29:58 -06:00
if type ( v ) in [ str , str ] :
self . assertIn ( type ( data . get ( k ) ) , [ str , str ] , msg = msg % k )
elif type ( v ) in [ int , int ] :
self . assertIn ( type ( data . get ( k ) ) , [ int , int ] , msg = msg % k )
2018-11-08 19:45:21 +00:00
else :
self . assertEqual ( type ( v ) , type ( data . get ( k ) ) , msg = msg % k )
##########################################################################
def assert_get_single ( self , data ) :
self . assertEqual ( len ( data ) , 1 )
return data [ 0 ]
##########################################################################
def assert_get_forbidden ( self , db , typ , id ) :
2020-01-08 13:29:58 -06:00
with pytest . raises ( PermissionDeniedException ) :
2018-11-08 19:45:21 +00:00
db . get ( typ , id )
##########################################################################
def assert_get_handleref ( self , db , typ , id ) :
data = self . assert_get_single ( db . get ( typ , id ) )
self . assert_handleref_integrity ( data )
self . assert_data_integrity ( data , typ )
return data
##########################################################################
def assert_existing_fields ( self , a , b , ignore = { } ) :
2020-01-08 13:29:58 -06:00
for k , v in list ( a . items ( ) ) :
2018-11-08 19:45:21 +00:00
if ignore and k in ignore :
continue
if k in [ " suggest " ] :
continue
self . assertEqual ( v , b . get ( k ) )
##########################################################################
2020-07-15 02:07:01 -05:00
def assert_delete (
self , db , typ , test_success = None , test_failure = None , test_protected = None
) :
2018-11-08 19:45:21 +00:00
if test_success :
db . rm ( typ , test_success )
2020-01-08 13:29:58 -06:00
with pytest . raises ( NotFoundException ) :
2018-11-08 19:45:21 +00:00
self . assert_get_handleref ( db , typ , test_success )
if test_failure :
2020-01-08 13:29:58 -06:00
with pytest . raises ( PermissionDeniedException ) :
2018-11-08 19:45:21 +00:00
db . rm ( typ , test_failure )
try :
self . assert_get_handleref ( db , typ , test_failure )
except PermissionDeniedException :
pass
2020-07-15 02:07:01 -05:00
if test_protected :
with pytest . raises ( PermissionDeniedException ) :
db . rm ( typ , test_protected )
assert DeskProTicket . objects . filter (
subject__icontains = f " { typ } - { test_protected } "
) . exists ( )
2018-11-08 19:45:21 +00:00
##########################################################################
2019-12-05 16:57:52 +00:00
def assert_create (
self , db , typ , data , test_failures = None , test_success = True , * * kwargs
) :
2018-11-08 19:45:21 +00:00
if test_success :
r_data = self . assert_get_single (
2019-12-05 16:57:52 +00:00
db . create ( typ , data , return_response = True ) . get ( " data " )
)
self . assert_existing_fields ( data , r_data , ignore = kwargs . get ( " ignore " ) )
2018-11-08 19:45:21 +00:00
self . assertGreater ( r_data . get ( " id " ) , 0 )
status_checked = False
for model in QUEUE_ENABLED :
if hasattr ( model , " handleref " ) and model . handleref . tag == typ :
self . assertEqual ( r_data . get ( " status " ) , " pending " )
status_checked = True
if not status_checked :
self . assertEqual ( r_data . get ( " status " ) , " ok " )
else :
r_data = { }
# if test_failures is set we want to test fail conditions
if test_failures :
# we test fail because of invalid data
if " invalid " in test_failures :
2020-08-18 09:28:18 -05:00
tests = test_failures [ " invalid " ]
if not isinstance ( tests , list ) :
tests = [ tests ]
for test in tests :
data_invalid = copy . copy ( data )
for k , v in list ( test . items ( ) ) :
data_invalid [ k ] = v
with pytest . raises ( InvalidRequestException ) as excinfo :
r = db . create ( typ , data_invalid , return_response = True )
assert " 400 Bad Request " in str ( excinfo . value )
2018-11-08 19:45:21 +00:00
# we test fail because of parent entity status
if " status " in test_failures :
data_status = copy . copy ( data )
2020-01-08 13:29:58 -06:00
for k , v in list ( test_failures [ " status " ] . items ( ) ) :
2018-11-08 19:45:21 +00:00
data_status [ k ] = v
2020-01-08 13:29:58 -06:00
with pytest . raises ( InvalidRequestException ) as excinfo :
2018-11-08 19:45:21 +00:00
r = db . create ( typ , data_status , return_response = True )
2020-01-08 13:29:58 -06:00
assert " not yet been approved " in str ( excinfo . value )
2018-11-08 19:45:21 +00:00
# we test fail because of permissions
if " perms " in test_failures :
data_perms = copy . copy ( data )
2020-01-08 13:29:58 -06:00
for k , v in list ( test_failures [ " perms " ] . items ( ) ) :
2018-11-08 19:45:21 +00:00
data_perms [ k ] = v
2020-01-08 13:29:58 -06:00
with pytest . raises ( PermissionDeniedException ) :
2018-11-08 19:45:21 +00:00
db . create ( typ , data_perms , return_response = True )
return r_data
##########################################################################
def assert_create_status_failure ( self , db , typ , data ) :
"""
Wrapper for assert_create for assertion of permission failure
"""
2019-12-05 16:57:52 +00:00
self . assert_create (
db , typ , data , test_failures = { " status " : { } } , test_success = False
)
2018-11-08 19:45:21 +00:00
##########################################################################
2019-12-05 16:57:52 +00:00
def assert_update ( self , db , typ , id , data , test_failures = False , test_success = True ) :
2018-11-08 19:45:21 +00:00
if test_success :
orig = self . assert_get_handleref ( db , typ , id )
orig . update ( * * data )
else :
orig = { " id " : id }
orig . update ( * * data )
2020-01-08 13:29:58 -06:00
for k , v in list ( orig . items ( ) ) :
2018-11-08 19:45:21 +00:00
if k [ - 3 : ] == " _id " and k [ : - 3 ] in orig :
del orig [ k [ : - 3 ] ]
if test_success :
db . update ( typ , * * orig )
u_data = self . assert_get_handleref ( db , typ , id )
if type ( test_success ) == list :
for test in test_success :
if test and callable ( test ) :
test ( data , u_data )
else :
# self.assertGreater(u_data["version"], orig["version"])
2020-01-08 13:29:58 -06:00
for k , v in list ( data . items ( ) ) :
2018-11-08 19:45:21 +00:00
self . assertEqual ( u_data . get ( k ) , v )
# if test_failures is set we want to test fail conditions
if test_failures :
# we test fail because of invalid data
if " invalid " in test_failures :
2020-08-18 09:28:18 -05:00
tests = test_failures [ " invalid " ]
# `invalid` test_failures can be a list to
# test multiple instances of invalid values
# however we also support passing a single
# dict of fields, in which case we wrap
# it in a list here.
if not isinstance ( tests , list ) :
tests = [ tests ]
for test in tests :
data_invalid = copy . copy ( orig )
for k , v in list ( test . items ( ) ) :
data_invalid [ k ] = v
with pytest . raises ( InvalidRequestException ) as excinfo :
db . update ( typ , * * data_invalid )
assert " 400 Bad Request " in str ( excinfo . value )
2018-11-08 19:45:21 +00:00
# we test fail because of permissions
if " perms " in test_failures :
data_perms = copy . copy ( orig )
2020-01-08 13:29:58 -06:00
for k , v in list ( test_failures [ " perms " ] . items ( ) ) :
2018-11-08 19:45:21 +00:00
data_perms [ k ] = v
2020-02-05 21:26:21 -06:00
# if data is empty set something so we dont
# trigger the empty data error
data_perms [ " _dummy_ " ] = 1
2020-01-08 13:29:58 -06:00
with pytest . raises ( PermissionDeniedException ) :
2018-11-08 19:45:21 +00:00
db . update ( typ , * * data_perms )
# we test failure to update readonly fields
if " readonly " in test_failures :
data_ro = copy . copy ( orig )
b_data = self . assert_get_handleref ( db , typ , id )
data_ro . update ( * * test_failures [ " readonly " ] )
db . update ( typ , * * data_ro )
u_data = self . assert_get_handleref ( db , typ , id )
2020-01-08 13:29:58 -06:00
for k , v in list ( test_failures [ " readonly " ] . items ( ) ) :
2018-11-08 19:45:21 +00:00
self . assertEqual ( u_data . get ( k ) , b_data . get ( k ) )
##########################################################################
2019-12-05 16:57:52 +00:00
def assert_list_filter_related (
self , target , rel , fld = " id " , valid = None , valid_m = None
) :
# if not valid:
2018-11-08 19:45:21 +00:00
# valid = [o.id for k, o in SHARED.items() if type(
# o) != int and k.find("%s_" % target) == 0]
if fld != " id " :
qfld = " _ %s " % fld
else :
qfld = fld
ids = [
getattr ( SHARED [ " %s _r_ok " % rel ] , fld ) ,
2019-12-05 16:57:52 +00:00
getattr ( SHARED [ " %s _rw_ok " % rel ] , fld ) ,
2018-11-08 19:45:21 +00:00
]
2019-12-05 16:57:52 +00:00
kwargs_s = { " %s _ %s " % ( rel , qfld ) : getattr ( SHARED [ " %s _r_ok " % rel ] , fld ) }
kwargs_m = { " %s _ %s __in " % ( rel , qfld ) : " , " . join ( [ str ( id ) for id in ids ] ) }
2018-11-08 19:45:21 +00:00
2020-02-05 21:25:25 -06:00
attr = getattr ( REFTAG_MAP [ target ] , rel , None )
if attr and not isinstance ( attr , property ) :
2018-11-08 19:45:21 +00:00
valid_s = [
r . id
2019-12-05 16:57:52 +00:00
for r in REFTAG_MAP [ target ]
. objects . filter ( * * kwargs_s )
2018-11-08 19:45:21 +00:00
. filter ( status = " ok " )
]
valid_m = [
r . id
for r in REFTAG_MAP [ target ]
2019-12-05 16:57:52 +00:00
. objects . filter ( * * { " %s _ %s __in " % ( rel , qfld ) : ids } )
. filter ( status = " ok " )
2018-11-08 19:45:21 +00:00
]
elif target == " poc " :
valid_s = [ SHARED [ " %s _r_ok_public " % target ] . id ]
valid_m = [
SHARED [ " %s _r_ok_public " % target ] . id ,
2019-12-05 16:57:52 +00:00
SHARED [ " %s _rw_ok_public " % target ] . id ,
2018-11-08 19:45:21 +00:00
]
2019-05-02 18:16:44 +00:00
elif target == " ixpfx " :
valid_s = [
SHARED [ " %s _r_ok " % target ] . id ,
SHARED [ " %s _r_v6_ok " % target ] . id ,
]
valid_m = [
SHARED [ " %s _r_ok " % target ] . id ,
SHARED [ " %s _rw_ok " % target ] . id ,
SHARED [ " %s _r_v6_ok " % target ] . id ,
SHARED [ " %s _rw_v6_ok " % target ] . id ,
]
2018-11-08 19:45:21 +00:00
else :
valid_s = [ SHARED [ " %s _r_ok " % target ] . id ]
2019-12-05 16:57:52 +00:00
valid_m = [ SHARED [ " %s _r_ok " % target ] . id , SHARED [ " %s _rw_ok " % target ] . id ]
2018-11-08 19:45:21 +00:00
2019-05-02 18:16:44 +00:00
# exact
2018-11-08 19:45:21 +00:00
data = self . db_guest . all ( target , * * kwargs_s )
self . assertGreater ( len ( data ) , 0 )
for row in data :
self . assert_data_integrity ( row , target )
self . assertIn ( row [ " id " ] , valid_s )
# in
data = self . db_guest . all ( target , * * kwargs_m )
self . assertGreater ( len ( data ) , 0 )
for row in data :
self . assert_data_integrity ( row , target )
self . assertIn ( row [ " id " ] , valid_m )
##########################################################################
2019-12-05 16:57:52 +00:00
def assert_related_depth (
self ,
obj ,
serializer_class ,
r_depth ,
t_depth ,
note_tag ,
typ = " listing " ,
list_exclude = [ ] ,
) :
2018-11-08 19:45:21 +00:00
"""
Assert the data indegrity of structures within a result that have
been expanded via the depth parameter
"""
# get all the realtion ship properties declared in the serializer
pk_flds , n_flds = self . serializer_related_fields ( serializer_class )
# some tag so we can track where the assertions fail since this will
# be doing nested checks
note_tag = " %s ( %d / %d ) " % ( note_tag , r_depth , t_depth )
# first check that the provided object is not None, as this should
# never be the case
2020-01-08 13:29:58 -06:00
self . assertNotEqual ( obj , None , msg = note_tag )
2018-11-08 19:45:21 +00:00
# single primary key relation fields
for pk_fld in pk_flds :
# serializer has marked field as to be excluded from serialized data
# dont check for it
if pk_fld in list_exclude :
continue
if typ == " listing " :
# in listing mode, depth should never expand pk relations
self . assertEqual (
2019-12-05 16:57:52 +00:00
obj . get ( pk_fld ) , None , msg = " PK Relation %s %s " % ( note_tag , pk_fld )
)
2018-11-08 19:45:21 +00:00
else :
# in single get mode, expand everything as long as we are at
# a relative depth greater than 1
if r_depth > = 1 :
self . assert_related_depth (
2019-12-05 16:57:52 +00:00
obj . get ( pk_fld ) ,
REFTAG_MAP_SLZ . get ( pk_fld ) ,
r_depth - 1 ,
t_depth ,
" %s . %s " % ( note_tag , pk_fld ) ,
typ = typ ,
)
2018-11-08 19:45:21 +00:00
else :
self . assertIn (
type ( obj . get ( pk_fld ) ) ,
2020-01-08 13:29:58 -06:00
[ int , type ( None ) ] ,
2019-12-05 16:57:52 +00:00
msg = " PK Relation %s %s " % ( note_tag , pk_fld ) ,
)
2018-11-08 19:45:21 +00:00
# nested set relations
for n_fld , n_fld_cls in n_flds :
if r_depth > 1 :
# sets should be expanded to objects
2019-12-05 16:57:52 +00:00
self . assertIn (
n_fld , obj , msg = " Nested set existing (dN) %s %s " % ( note_tag , n_fld )
)
2018-11-08 19:45:21 +00:00
# make sure set exists and is of the correct type
self . assertEqual (
2019-12-05 16:57:52 +00:00
type ( obj [ n_fld ] ) ,
list ,
msg = " Nested set list type (dN) %s %s " % ( note_tag , n_fld ) ,
)
2018-11-08 19:45:21 +00:00
# assert further depth expansions on all expanded objects in
# the set
for row in obj [ n_fld ] :
self . assert_related_depth (
2019-12-05 16:57:52 +00:00
row ,
n_fld_cls ,
r_depth - 2 ,
t_depth ,
" %s . %s " % ( note_tag , n_fld ) ,
typ = typ ,
list_exclude = getattr ( n_fld_cls . Meta , " list_exclude " , [ ] ) ,
)
2018-11-08 19:45:21 +00:00
elif r_depth == 1 :
# sets should be expanded to ids
2019-12-05 16:57:52 +00:00
self . assertIn (
n_fld , obj , msg = " Nested set existing (d1) %s %s " % ( note_tag , n_fld )
)
2018-11-08 19:45:21 +00:00
# make sure set exists and is of the correct type
self . assertEqual (
2019-12-05 16:57:52 +00:00
type ( obj [ n_fld ] ) ,
list ,
msg = " Nested set list type (d1) %s %s " % ( note_tag , n_fld ) ,
)
2018-11-08 19:45:21 +00:00
# make all values in the set are of type int or long
for row in obj [ n_fld ] :
self . assertIn (
type ( row ) ,
2020-01-08 13:29:58 -06:00
[ int , int ] ,
2019-12-05 16:57:52 +00:00
msg = " Nested set containing ids (d1) %s %s " % ( note_tag , n_fld ) ,
)
2018-11-08 19:45:21 +00:00
else :
# sets should not exist
2019-12-05 16:57:52 +00:00
self . assertNotIn (
n_fld ,
obj ,
msg = " Netsted set not existing (d0) %s %s " % ( note_tag , n_fld ) ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
# TESTS WITH USER THAT IS NOT A MEMBER OF AN ORGANIZATION
##########################################################################
def test_user_001_GET_org ( self ) :
self . assert_get_handleref ( self . db_user , " org " , SHARED [ " org_r_ok " ] . id )
##########################################################################
def test_user_001_GET_net ( self ) :
2019-12-05 16:57:52 +00:00
data = self . assert_get_handleref ( self . db_user , " net " , SHARED [ " net_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
self . assertNotEqual ( len ( data . get ( " poc_set " ) ) , 0 )
##########################################################################
def test_user_001_GET_ix ( self ) :
self . assert_get_handleref ( self . db_user , " ix " , SHARED [ " ix_r_ok " ] . id )
##########################################################################
2019-05-02 18:03:21 +00:00
def test_user_001_GET_ix_net_count ( self ) :
2019-12-05 16:57:52 +00:00
data = self . assert_get_handleref ( self . db_user , " ix " , SHARED [ " ix_r_ok " ] . id )
2019-05-02 18:03:21 +00:00
self . assertEqual ( data . get ( " net_count " ) , 1 )
##########################################################################
2018-11-08 19:45:21 +00:00
def test_user_001_GET_fac ( self ) :
self . assert_get_handleref ( self . db_user , " fac " , SHARED [ " fac_r_ok " ] . id )
##########################################################################
def test_user_001_GET_fac_netcount ( self ) :
2019-12-05 16:57:52 +00:00
data = self . assert_get_handleref ( self . db_user , " fac " , SHARED [ " fac_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
self . assertEqual ( data . get ( " net_count " ) , 1 )
##########################################################################
def test_user_001_GET_poc_public ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_user , " poc " , SHARED [ " poc_r_ok_public " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_user_001_GET_poc_users ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_user , " poc " , SHARED [ " poc_r_ok_users " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_user_001_GET_poc_private ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_forbidden ( self . db_user , " poc " , SHARED [ " poc_r_ok_private " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_user_001_GET_nefac ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_user , " netfac " , SHARED [ " netfac_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_user_001_GET_netixlan ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_user , " netixlan " , SHARED [ " netixlan_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_user_001_GET_ixfac ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_user , " ixfac " , SHARED [ " ixfac_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_user_001_GET_ixlan ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_user , " ixlan " , SHARED [ " ixlan_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
2020-07-15 02:07:01 -05:00
def test_user_001_GET_ixlan_ixf_ixp_member_list_url ( self ) :
for ixlan in self . db_user . all (
" ixlan " , ixf_ixp_member_list_url__startswith = " http "
) :
if ixlan [ " ixf_ixp_member_list_url_visible " ] in [ " Public " , " Users " ] :
assert ixlan [ " ixf_ixp_member_list_url " ] == " http://localhost "
else :
assert " ixf_ixp_member_list_url " not in ixlan
##########################################################################
2018-11-08 19:45:21 +00:00
def test_user_001_GET_ixpfx ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_user , " ixpfx " , SHARED [ " ixpfx_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_user_005_list_poc ( self ) :
data = self . db_guest . all ( " poc " , limit = 1000 )
for row in data :
self . assertIn ( row . get ( " visible " ) , [ " Users " , " Public " ] )
data = self . db_guest . all ( " poc " , visible = " Private " , limit = 100 )
self . assertEqual ( 0 , len ( data ) )
2019-05-02 18:13:03 +00:00
##########################################################################
def test_user_001_GET_as_set ( self ) :
data = self . db_guest . all ( " as_set " )
networks = Network . objects . filter ( status = " ok " )
2020-01-08 13:29:58 -06:00
print ( data )
2019-05-02 18:13:03 +00:00
for net in networks :
2020-01-08 13:29:58 -06:00
self . assertEqual ( data [ 0 ] . get ( " {} " . format ( net . asn ) ) , net . irr_as_set )
2019-05-02 18:13:03 +00:00
2018-11-08 19:45:21 +00:00
##########################################################################
# TESTS WITH USER THAT IS ORGANIZATION MEMBER
##########################################################################
def test_org_member_001_GET_poc_public ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref (
self . db_org_member , " poc " , SHARED [ " poc_r_ok_public " ] . id
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_member_001_GET_poc_users ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref (
self . db_org_member , " poc " , SHARED [ " poc_r_ok_users " ] . id
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_member_001_GET_poc_private ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref (
self . db_org_member , " poc " , SHARED [ " poc_r_ok_private " ] . id
)
2018-11-08 19:45:21 +00:00
2020-07-15 02:07:01 -05:00
#########################################################################
def test_org_member_001_GET_ixlan_ixf_ixp_member_list_url ( self ) :
for ixlan in self . db_org_member . all (
" ixlan " , ixf_ixp_member_list_url__startswith = " http "
) :
if ixlan [ " ixf_ixp_member_list_url_visible " ] in [ " Public " , " Users " ] :
assert ixlan [ " ixf_ixp_member_list_url " ] == " http://localhost "
else :
if ixlan [ " id " ] == SHARED [ " ixlan_r3_ok " ] . id :
assert ixlan [ " ixf_ixp_member_list_url " ] == " http://localhost "
else :
assert " ixf_ixp_member_list_url " not in ixlan
2018-11-08 19:45:21 +00:00
##########################################################################
# TESTS WITH USER THAT IS ORGANIZATION ADMINISTRATOR
##########################################################################
##########################################################################
def test_org_admin_001_GET_poc_public ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref (
self . db_org_admin , " poc " , SHARED [ " poc_r_ok_public " ] . id
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_admin_001_GET_poc_users ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_org_admin , " poc " , SHARED [ " poc_r_ok_users " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_admin_001_GET_poc_private ( self ) :
# org admin is admin of rw org, so trying to access the private poc of the r org
# should still be forbidden
2019-12-05 16:57:52 +00:00
self . assert_get_forbidden (
self . db_org_admin , " poc " , SHARED [ " poc_r_ok_private " ] . id
)
2018-11-08 19:45:21 +00:00
2020-07-15 02:07:01 -05:00
#########################################################################
def test_org_admin_001_GET_ixlan_ixf_ixp_member_list_url ( self ) :
for ixlan in self . db_org_admin . all (
" ixlan " , ixf_ixp_member_list_url__startswith = " http "
) :
if ixlan [ " ixf_ixp_member_list_url_visible " ] in [ " Public " , " Users " ] :
assert ixlan [ " ixf_ixp_member_list_url " ] == " http://localhost "
else :
if ixlan [ " id " ] == SHARED [ " ixlan_rw3_ok " ] . id :
assert ixlan [ " ixf_ixp_member_list_url " ] == " http://localhost "
else :
assert " ixf_ixp_member_list_url " not in ixlan
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_admin_002_POST_PUT_DELETE_ix ( self ) :
data = self . make_data_ix ( prefix = self . get_prefix4 ( ) )
r_data = self . assert_create (
self . db_org_admin ,
" ix " ,
data ,
ignore = [ " prefix " ] ,
test_failures = {
2019-12-05 16:57:52 +00:00
" invalid " : { " prefix " : self . get_prefix4 ( ) , " name " : " " } ,
2018-11-08 19:45:21 +00:00
" perms " : {
" prefix " : self . get_prefix4 ( ) ,
# need to set name again so it doesnt fail unique validation
" name " : self . make_name ( " Test " ) ,
# set org to an organization the user doesnt have perms to
2019-12-05 16:57:52 +00:00
" org_id " : SHARED [ " org_r_ok " ] . id ,
2018-11-08 19:45:21 +00:00
} ,
" status " : {
# need to set name again so it doesnt fail unique validation
" prefix " : self . get_prefix4 ( ) ,
" name " : self . make_name ( " Test " ) ,
2019-12-05 16:57:52 +00:00
" org_id " : SHARED [ " org_rwp " ] . id ,
} ,
} ,
)
2018-11-08 19:45:21 +00:00
2020-04-03 11:34:17 +00:00
# test that ixlan id and prefix id were return in the POST
# response (see #609)
assert r_data . get ( " ixlan_id " ) > 0
assert r_data . get ( " ixpfx_id " ) > 0
2018-11-08 19:45:21 +00:00
SHARED [ " ix_id " ] = r_data . get ( " id " )
2020-02-05 21:25:25 -06:00
# make sure ixlan was created and has matching id
ix = InternetExchange . objects . get ( id = SHARED [ " ix_id " ] )
assert ix . ixlan
assert ix . ixlan . id == ix . id
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
" ix " ,
SHARED [ " ix_id " ] ,
{ " name " : self . make_name ( " Test " ) } ,
test_failures = {
" invalid " : { " name " : " " } ,
" perms " : { " id " : SHARED [ " ix_r_ok " ] . id } ,
2020-07-15 02:07:01 -05:00
" readonly " : { " ixf_net_count " : 50 , " ixf_last_import " : " not even valid " } ,
2019-12-05 16:57:52 +00:00
} ,
)
2018-11-08 19:45:21 +00:00
2019-12-05 16:57:52 +00:00
self . assert_delete (
self . db_org_admin ,
" ix " ,
test_success = SHARED [ " ix_id " ] ,
test_failure = SHARED [ " ix_r_ok " ] . id ,
)
2018-11-08 19:45:21 +00:00
self . assert_create (
2019-12-05 16:57:52 +00:00
self . db_org_admin ,
" ix " ,
data ,
test_success = False ,
test_failures = {
2020-03-06 16:26:37 +00:00
" invalid " : { " prefix " : self . get_prefix4 ( ) , " tech_email " : " " , } ,
2019-12-05 16:57:52 +00:00
} ,
)
2018-11-08 19:45:21 +00:00
2020-02-05 21:25:57 -06:00
self . assert_create (
self . db_org_admin ,
" ix " ,
data ,
test_success = False ,
2020-03-06 16:26:37 +00:00
test_failures = { " invalid " : { " prefix " : self . get_prefix4 ( ) , " website " : " " , } , } ,
2020-02-05 21:25:57 -06:00
)
2019-12-05 16:57:52 +00:00
self . assert_create (
self . db_org_admin ,
" ix " ,
data ,
test_success = False ,
test_failures = { " invalid " : { " prefix " : " " } , } ,
)
2018-11-08 19:45:21 +00:00
2018-12-04 09:43:11 +00:00
# test ix creation with a ipv6 prefix
data = self . make_data_ix ( prefix = self . get_prefix6 ( ) )
self . assert_create ( self . db_org_admin , " ix " , data , ignore = [ " prefix " ] )
2020-07-15 02:07:01 -05:00
# check protected ix validation
self . assert_delete (
self . db_org_admin , " ix " , test_protected = SHARED [ " ix_rw_ok " ] . id ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_admin_002_POST_PUT_DELETE_fac ( self ) :
data = self . make_data_fac ( )
r_data = self . assert_create (
self . db_org_admin ,
" fac " ,
data ,
test_failures = {
2020-07-15 02:07:01 -05:00
" invalid " : { " name " : " " , } ,
2018-11-08 19:45:21 +00:00
" perms " : {
# need to set name again so it doesnt fail unique validation
" name " : self . make_name ( " Test " ) ,
# set org to an organization the user doesnt have perms to
2019-12-05 16:57:52 +00:00
" org_id " : SHARED [ " org_r_ok " ] . id ,
2018-11-08 19:45:21 +00:00
} ,
" status " : {
" name " : self . make_name ( " Test " ) ,
2019-12-05 16:57:52 +00:00
" org_id " : SHARED [ " org_rwp " ] . id ,
} ,
} ,
)
2018-11-08 19:45:21 +00:00
SHARED [ " fac_id " ] = r_data . get ( " id " )
self . assert_update (
self . db_org_admin ,
" fac " ,
SHARED [ " fac_id " ] ,
{ " name " : self . make_name ( " Test " ) } ,
test_failures = {
2019-12-05 16:57:52 +00:00
" invalid " : { " name " : " " } ,
" perms " : { " id " : SHARED [ " fac_r_ok " ] . id } ,
2018-11-08 19:45:21 +00:00
" readonly " : {
2019-12-05 16:57:52 +00:00
" latitude " : 1 , # this should not take as it is read only
" longitude " : 1 , # this should not take as it is read only
2020-07-15 02:07:01 -05:00
" rencode " : str ( uuid . uuid4 ( ) ) [
: 6
] . upper ( ) , # this should not take as it is read only
2019-12-05 16:57:52 +00:00
} ,
2018-11-08 19:45:21 +00:00
} ,
)
2019-12-05 16:57:52 +00:00
self . assert_delete (
self . db_org_admin ,
" fac " ,
test_success = SHARED [ " fac_id " ] ,
test_failure = SHARED [ " fac_r_ok " ] . id ,
)
2018-11-08 19:45:21 +00:00
2020-07-15 02:07:01 -05:00
# check protected ix validation
self . assert_delete (
self . db_org_admin , " fac " , test_protected = SHARED [ " fac_rw_ok " ] . id ,
)
# Create new data with a non-null rencode
data_new = self . make_data_fac ( )
obsolete_rencode = str ( uuid . uuid4 ( ) ) [ : 6 ] . upper ( )
data_new [ " rencode " ] = obsolete_rencode
# Data should be successfully created
r_data_new = self . assert_get_single (
self . db_org_admin . create ( " fac " , data_new , return_response = True ) . get ( " data " )
)
# But rencode should be null
assert r_data_new [ " rencode " ] == " "
2020-04-20 09:45:48 -05:00
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_admin_002_POST_PUT_DELETE_net ( self ) :
data = self . make_data_net ( asn = 9000900 )
r_data = self . assert_create (
self . db_org_admin ,
" net " ,
data ,
test_failures = {
2019-12-05 16:57:52 +00:00
" invalid " : { " name " : " " } ,
2018-11-08 19:45:21 +00:00
" perms " : {
# need to set name again so it doesnt fail unique validation
" name " : self . make_name ( " Test " ) ,
" asn " : data [ " asn " ] + 1 ,
# set org to an organization the user doesnt have perms to
2019-12-05 16:57:52 +00:00
" org_id " : SHARED [ " org_r_ok " ] . id ,
2018-11-08 19:45:21 +00:00
} ,
" status " : {
" org_id " : SHARED [ " org_rwp " ] . id ,
" asn " : data [ " asn " ] + 1 ,
2019-12-05 16:57:52 +00:00
" name " : self . make_name ( " Test " ) ,
} ,
} ,
)
2018-11-08 19:45:21 +00:00
SHARED [ " net_id " ] = r_data . get ( " id " )
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
" net " ,
SHARED [ " net_id " ] ,
{ " name " : self . make_name ( " Test " ) } ,
test_failures = {
" invalid " : { " name " : " " } ,
" perms " : { " id " : SHARED [ " net_r_ok " ] . id } ,
} ,
)
2018-11-08 19:45:21 +00:00
2020-07-15 02:07:01 -05:00
# Test ASN cannot update
self . assert_update (
self . db_org_admin ,
" net " ,
SHARED [ " net_id " ] ,
data ,
test_failures = { " invalid " : { " asn " : data [ " asn " ] + 1 } , } ,
)
2019-12-05 16:57:52 +00:00
self . assert_delete (
self . db_org_admin ,
" net " ,
test_success = SHARED [ " net_id " ] ,
test_failure = SHARED [ " net_r_ok " ] . id ,
)
2018-11-08 19:45:21 +00:00
# Test RiR not found failure
r_data = self . assert_create (
2019-12-05 16:57:52 +00:00
self . db_org_admin ,
" net " ,
data ,
test_failures = { " invalid " : { " asn " : 9999999 } } ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
2020-04-04 09:11:59 +00:00
##########################################################################
def test_org_admin_002_POST_net_looking_glass_url ( self ) :
2020-07-15 02:07:01 -05:00
for scheme in [ " http " , " https " , " ssh " , " telnet " ] :
2020-04-04 09:11:59 +00:00
r_data = self . assert_create (
self . db_org_admin ,
" net " ,
2020-07-15 02:07:01 -05:00
self . make_data_net (
asn = 9000900 , looking_glass = " {} ://foo.bar " . format ( scheme )
) ,
test_failures = { " invalid " : { " looking_glass " : " foo://www.bar.com " } } ,
2020-04-04 09:11:59 +00:00
)
Network . objects . get ( id = r_data [ " id " ] ) . delete ( hard = True )
##########################################################################
def test_org_admin_002_POST_net_route_server_url ( self ) :
2020-07-15 02:07:01 -05:00
for scheme in [ " http " , " https " , " ssh " , " telnet " ] :
2020-04-04 09:11:59 +00:00
r_data = self . assert_create (
self . db_org_admin ,
" net " ,
2020-07-15 02:07:01 -05:00
self . make_data_net (
asn = 9000900 , route_server = " {} ://foo.bar " . format ( scheme )
) ,
test_failures = { " invalid " : { " route_server " : " foo://www.bar.com " } } ,
2020-04-04 09:11:59 +00:00
)
Network . objects . get ( id = r_data [ " id " ] ) . delete ( hard = True )
2019-11-16 21:30:34 -06:00
##########################################################################
2020-04-04 10:11:13 +00:00
2019-11-16 21:30:34 -06:00
def test_org_admin_002_POST_net_deleted ( self ) :
data = self . make_data_net ( asn = SHARED [ " net_rw_dupe_deleted " ] . asn )
2020-01-08 13:29:58 -06:00
with pytest . raises ( InvalidRequestException ) as excinfo :
2019-11-16 21:30:34 -06:00
r_data = self . db_org_admin . create ( " net " , data , return_response = True )
2020-01-08 13:29:58 -06:00
# check exception vs value
assert " Network has been deleted. Please contact " in excinfo . value . extra [ " asn " ]
2019-11-16 21:30:34 -06:00
2018-11-08 19:45:21 +00:00
##########################################################################
2019-05-02 18:13:03 +00:00
def test_org_admin_002_POST_PUT_DELETE_as_set ( self ) :
"""
The as - set endpoint is readonly , so all of these should
fail
"""
data = self . make_data_net ( asn = 9000900 )
2020-01-08 13:29:58 -06:00
with pytest . raises ( PermissionDeniedException ) as excinfo :
2019-12-05 16:57:52 +00:00
r_data = self . assert_create ( self . db_org_admin , " as_set " , data )
2020-01-08 13:29:58 -06:00
assert " You do not have permission " in str ( excinfo . value )
2019-05-02 18:13:03 +00:00
2020-01-08 13:29:58 -06:00
with pytest . raises ( PermissionDeniedException ) as excinfo :
self . db_org_admin . update ( " as_set " , { " 9000900 " : " AS-ZZZ " } )
assert " You do not have permission " in str ( excinfo . value )
2019-05-02 18:13:03 +00:00
2020-01-08 13:29:58 -06:00
with pytest . raises ( PermissionDeniedException ) as excinfo :
2019-05-02 18:13:03 +00:00
self . db_org_admin . rm ( " as_set " , SHARED [ " net_rw_ok " ] . asn )
2020-01-08 13:29:58 -06:00
assert " You do not have permission " in str ( excinfo . value )
2019-05-02 18:13:03 +00:00
##########################################################################
2019-02-07 08:00:22 +00:00
def test_org_admin_002_POST_net_bogon_asn ( self ) :
# Test bogon asn failure
data = self . make_data_net ( )
for bogon_asn in inet . BOGON_ASN_RANGES :
r_data = self . assert_create (
2019-12-05 16:57:52 +00:00
self . db_org_admin ,
" net " ,
data ,
test_failures = { " invalid " : { " asn " : bogon_asn [ 0 ] } } ,
test_success = False ,
)
2019-02-07 08:00:22 +00:00
# server running in tutorial mode should be allowed
# to create networks with bogon asns, so we test that
# as well
pdb_settings . TUTORIAL_MODE = True
2019-02-15 17:39:38 +00:00
for bogon_asn in inet . TUTORIAL_ASN_RANGES :
2019-02-07 08:00:22 +00:00
data = self . make_data_net ( asn = bogon_asn [ 0 ] )
r_data = self . assert_create ( self . db_org_admin , " net " , data )
pdb_settings . TUTORIAL_MODE = False
##########################################################################
2018-11-08 19:45:21 +00:00
def test_org_admin_002_POST_PUT_DELETE_netfac ( self ) :
data = {
" net_id " : SHARED [ " net_rw_ok " ] . id ,
" fac_id " : SHARED [ " fac_rw_ok " ] . id ,
}
r_data = self . assert_create (
self . db_org_admin ,
" netfac " ,
data ,
test_failures = {
2019-12-05 16:57:52 +00:00
" invalid " : { " net_id " : " " } ,
2018-11-08 19:45:21 +00:00
" perms " : {
# set network to one the user doesnt have perms to
" net_id " : SHARED [ " net_r_ok " ] . id
} ,
" status " : {
" net_id " : SHARED [ " net_rw_pending " ] . id ,
" fac_id " : SHARED [ " fac_rw_pending " ] . id ,
2019-12-05 16:57:52 +00:00
} ,
} ,
)
2018-11-08 19:45:21 +00:00
SHARED [ " netfac_id " ] = r_data . get ( " id " )
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
" netfac " ,
SHARED [ " netfac_id " ] ,
2020-01-08 13:29:58 -06:00
data ,
test_success = False ,
2019-12-05 16:57:52 +00:00
test_failures = {
" invalid " : { " fac_id " : " " } ,
" perms " : { " net_id " : SHARED [ " net_r_ok " ] . id } ,
} ,
)
self . assert_delete (
self . db_org_admin ,
" netfac " ,
test_success = SHARED [ " netfac_id " ] ,
test_failure = SHARED [ " netfac_r_ok " ] . id ,
)
2018-11-08 19:45:21 +00:00
# re-create deleted netfac
r_data = self . assert_create ( self . db_org_admin , " netfac " , data )
# re-delete
2019-12-05 16:57:52 +00:00
self . assert_delete (
self . db_org_admin , " netfac " , test_success = SHARED [ " netfac_id " ]
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_admin_002_POST_PUT_DELETE_poc ( self ) :
data = self . make_data_poc ( net_id = SHARED [ " net_rw_ok " ] . id )
r_data = self . assert_create (
self . db_org_admin ,
" poc " ,
data ,
test_failures = {
2019-12-05 16:57:52 +00:00
" invalid " : { " net_id " : " " } ,
2018-11-08 19:45:21 +00:00
" perms " : {
# set network to one the user doesnt have perms to
" net_id " : SHARED [ " net_r_ok " ] . id
} ,
2019-12-05 16:57:52 +00:00
" status " : { " net_id " : SHARED [ " net_rw_pending " ] . id } ,
} ,
)
2018-11-08 19:45:21 +00:00
SHARED [ " poc_id " ] = r_data . get ( " id " )
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
" poc " ,
SHARED [ " poc_id " ] ,
{ " role " : " Sales " } ,
test_failures = {
" invalid " : { " role " : " NOPE " } ,
" perms " : { " net_id " : SHARED [ " net_r_ok " ] . id } ,
} ,
)
2018-11-08 19:45:21 +00:00
2019-12-05 16:57:52 +00:00
self . assert_delete (
self . db_org_admin ,
" poc " ,
test_success = SHARED [ " poc_id " ] ,
test_failure = SHARED [ " poc_r_ok_users " ] . id ,
)
2018-11-08 19:45:21 +00:00
2020-06-24 12:55:01 -05:00
# soft-deleted pocs should return blank
# values for sensitive fields (#569)
poc = self . db_org_admin . all ( " poc " , id = SHARED [ " poc_id " ] , since = 1 ) [ 0 ]
assert poc [ " name " ] == " "
assert poc [ " phone " ] == " "
assert poc [ " email " ] == " "
assert poc [ " url " ] == " "
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_admin_002_POST_PUT_DELETE_ixlan ( self ) :
data = self . make_data_ixlan ( ix_id = SHARED [ " ix_rw_ok " ] . id )
2020-02-05 21:25:25 -06:00
with self . assertRaises ( Exception ) as exc :
r_data = self . assert_create (
self . db_org_admin ,
" ixlan " ,
data ,
test_failures = {
" invalid " : { " ix_id " : " " } ,
" perms " : { " ix_id " : SHARED [ " ix_r_ok " ] . id } ,
" status " : { " ix_id " : SHARED [ " ix_rw_pending " ] . id } ,
} ,
)
self . assertIn ( ' Method " POST " not allowed ' , str ( exc . exception ) )
2018-11-08 19:45:21 +00:00
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
" ixlan " ,
2020-02-05 21:25:25 -06:00
SHARED [ " ixlan_rw_ok " ] . id ,
2019-12-05 16:57:52 +00:00
{ " name " : self . make_name ( " Test " ) } ,
test_failures = {
" invalid " : { " mtu " : " NEEDS TO BE INT " } ,
" perms " : { " ix_id " : SHARED [ " ix_r_ok " ] . id } ,
} ,
)
2018-11-08 19:45:21 +00:00
2020-02-05 21:25:25 -06:00
with self . assertRaises ( Exception ) as exc :
self . assert_delete (
self . db_org_admin ,
" ixlan " ,
test_success = SHARED [ " ixlan_rw_ok " ] . id ,
test_failure = SHARED [ " ixlan_r_ok " ] . id ,
)
self . assertIn ( ' Method " DELETE " not allowed ' , str ( exc . exception ) )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_admin_002_POST_PUT_DELETE_ixpfx ( self ) :
2019-12-05 16:57:52 +00:00
data = self . make_data_ixpfx (
ixlan_id = SHARED [ " ixlan_rw_ok " ] . id , prefix = " 206.126.236.0/25 "
)
2018-11-08 19:45:21 +00:00
r_data = self . assert_create (
2019-12-05 16:57:52 +00:00
self . db_org_admin ,
" ixpfx " ,
data ,
test_failures = {
2020-08-18 09:28:18 -05:00
" invalid " : [
{ " prefix " : " 127.0.0.0/8 " } ,
{ " in_dfz " : False }
] ,
2018-11-08 19:45:21 +00:00
" perms " : {
" prefix " : " 205.127.237.0/24 " ,
2019-12-05 16:57:52 +00:00
" ixlan_id " : SHARED [ " ixlan_r_ok " ] . id ,
2018-11-08 19:45:21 +00:00
} ,
" status " : {
" prefix " : " 205.127.237.0/24 " ,
2019-12-05 16:57:52 +00:00
" ixlan_id " : SHARED [ " ixlan_rw_pending " ] . id ,
} ,
} ,
)
2018-11-08 19:45:21 +00:00
SHARED [ " ixpfx_id " ] = r_data [ " id " ]
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
" ixpfx " ,
SHARED [ " ixpfx_id " ] ,
{ " prefix " : " 206.127.236.0/26 " } ,
test_failures = {
2020-08-18 09:28:18 -05:00
" invalid " : [
{ " prefix " : " NEEDS TO BE VALID PREFIX " } ,
{ " in_dfz " : False }
] ,
2019-12-05 16:57:52 +00:00
" perms " : { " ixlan_id " : SHARED [ " ixlan_r_ok " ] . id } ,
} ,
)
self . assert_delete (
self . db_org_admin ,
" ixpfx " ,
test_success = SHARED [ " ixpfx_id " ] ,
test_failure = SHARED [ " ixpfx_r_ok " ] . id ,
)
2018-11-08 19:45:21 +00:00
# re-create deleted ixpfx
r_data = self . assert_create ( self . db_org_admin , " ixpfx " , data )
# re-delete
2019-12-05 16:57:52 +00:00
self . assert_delete ( self . db_org_admin , " ixpfx " , test_success = SHARED [ " ixpfx_id " ] )
2018-11-08 19:45:21 +00:00
2020-07-15 02:07:01 -05:00
# re-creating a deleted ixpfx that is under another exchange
# that we dont have write perms too
2019-12-05 16:57:52 +00:00
pfx = IXLanPrefix . objects . create (
2020-01-08 13:29:58 -06:00
ixlan = SHARED [ " ixlan_r_ok " ] , prefix = " 205.127.237.0/24 " , protocol = " IPv4 "
2019-12-05 16:57:52 +00:00
)
2018-11-08 19:45:21 +00:00
pfx . delete ( )
data . update ( prefix = " 205.127.237.0/24 " )
2020-07-15 02:07:01 -05:00
r_data = self . assert_create ( self . db_org_admin , " ixpfx " , data , )
2018-11-08 19:45:21 +00:00
# make sure protocols are validated
2019-12-05 16:57:52 +00:00
r_data = self . assert_create (
self . db_org_admin ,
" ixpfx " ,
data ,
test_failures = {
" invalid " : { " prefix " : " 207.128.238.0/24 " , " protocol " : " IPv6 " } ,
} ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
2020-07-15 02:07:01 -05:00
# test protected ixpfx cant be deleted
prefix = IXLanPrefix . objects . get ( id = SHARED [ " ixpfx_id " ] )
NetworkIXLan . objects . create (
network = SHARED [ " net_rw_ok " ] ,
asn = SHARED [ " net_rw_ok " ] . asn ,
ixlan = SHARED [ " ixlan_rw_ok " ] ,
ipaddr4 = prefix . prefix [ 0 ] ,
status = " ok " ,
speed = 1000 ,
)
self . assert_delete (
self . db_org_admin , " ixpfx " , test_protected = SHARED [ " ixpfx_id " ]
)
2020-08-18 09:28:18 -05:00
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_admin_002_POST_PUT_DELETE_netixlan ( self ) :
2019-12-05 16:57:52 +00:00
data = self . make_data_netixlan (
2020-01-08 13:29:58 -06:00
net_id = SHARED [ " net_rw_ok " ] . id ,
ixlan_id = SHARED [ " ixlan_rw_ok " ] . id ,
asn = SHARED [ " net_rw_ok " ] . asn ,
2019-12-05 16:57:52 +00:00
)
2018-11-08 19:45:21 +00:00
r_data = self . assert_create (
self . db_org_admin ,
" netixlan " ,
data ,
test_failures = {
2020-01-08 13:29:58 -06:00
" invalid " : { " ipaddr4 " : " a b c " } ,
2018-11-08 19:45:21 +00:00
" perms " : {
# set network to one the user doesnt have perms to
2019-05-02 18:16:44 +00:00
" ipaddr4 " : self . get_ip4 ( SHARED [ " ixlan_rw_ok " ] ) ,
" ipaddr6 " : self . get_ip6 ( SHARED [ " ixlan_rw_ok " ] ) ,
2019-12-05 16:57:52 +00:00
" net_id " : SHARED [ " net_r_ok " ] . id ,
} ,
} ,
)
2019-05-02 18:16:44 +00:00
2020-04-10 09:03:12 +00:00
assert r_data [ " operational " ]
2018-11-08 19:45:21 +00:00
SHARED [ " netixlan_id " ] = r_data . get ( " id " )
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
" netixlan " ,
SHARED [ " netixlan_id " ] ,
{ " speed " : 2000 } ,
test_failures = {
" invalid " : { " ipaddr4 " : " NEEDS TO BE VALID IP " } ,
" perms " : { " net_id " : SHARED [ " net_r_ok " ] . id } ,
} ,
)
2018-11-08 19:45:21 +00:00
2019-12-05 16:57:52 +00:00
self . assert_delete (
self . db_org_admin ,
" netixlan " ,
test_success = SHARED [ " netixlan_id " ] ,
test_failure = SHARED [ " netixlan_r_ok " ] . id ,
)
2019-05-02 18:16:44 +00:00
##########################################################################
def test_org_admin_002_POST_PUT_netixlan_validation ( self ) :
2019-12-05 16:57:52 +00:00
data = self . make_data_netixlan (
net_id = SHARED [ " net_rw_ok " ] . id , ixlan_id = SHARED [ " ixlan_rw_ok " ] . id
)
2019-05-02 18:16:44 +00:00
test_failures = [
# test failure if ip4 not in prefix
2019-12-05 16:57:52 +00:00
{ " invalid " : { " ipaddr4 " : self . get_ip4 ( SHARED [ " ixlan_r_ok " ] ) } } ,
2019-05-02 18:16:44 +00:00
# test failure if ip6 not in prefix
2019-12-05 16:57:52 +00:00
{ " invalid " : { " ipaddr6 " : self . get_ip6 ( SHARED [ " ixlan_r_ok " ] ) } } ,
2019-05-02 18:16:44 +00:00
]
for test_failure in test_failures :
2019-12-05 16:57:52 +00:00
self . assert_create (
self . db_org_admin ,
" netixlan " ,
data ,
test_failures = test_failure ,
test_success = False ,
)
2019-05-02 18:16:44 +00:00
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_admin_002_POST_PUT_DELETE_ixfac ( self ) :
2019-12-05 16:57:52 +00:00
data = { " fac_id " : SHARED [ " fac_rw2_ok " ] . id , " ix_id " : SHARED [ " ix_rw2_ok " ] . id }
2018-11-08 19:45:21 +00:00
r_data = self . assert_create (
self . db_org_admin ,
" ixfac " ,
data ,
test_failures = {
2019-12-05 16:57:52 +00:00
" invalid " : { " ix_id " : " " } ,
2018-11-08 19:45:21 +00:00
" perms " : {
# set network to one the user doesnt have perms to
" ix_id " : SHARED [ " ix_r_ok " ] . id
} ,
" status " : {
" fac_id " : SHARED [ " fac_rw2_pending " ] . id ,
2019-12-05 16:57:52 +00:00
" ix_id " : SHARED [ " ix_rw2_pending " ] . id ,
} ,
} ,
)
2018-11-08 19:45:21 +00:00
SHARED [ " ixfac_id " ] = r_data . get ( " id " )
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
" ixfac " ,
SHARED [ " ixfac_id " ] ,
{ " fac_id " : SHARED [ " fac_r2_ok " ] . id } ,
test_failures = {
" invalid " : { " fac_id " : " " } ,
" perms " : { " ix_id " : SHARED [ " ix_r_ok " ] . id } ,
} ,
)
2018-11-08 19:45:21 +00:00
2019-12-05 16:57:52 +00:00
self . assert_delete (
self . db_org_admin ,
" ixfac " ,
test_success = SHARED [ " ixfac_id " ] ,
test_failure = SHARED [ " ixfac_r_ok " ] . id ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_org_admin_003_PUT_org ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
" org " ,
SHARED [ " org_rw_ok " ] . id ,
{ " name " : self . make_name ( " Test " ) } ,
test_failures = {
" invalid " : { " name " : " " } ,
" perms " : { " id " : SHARED [ " org_r_ok " ] . id } ,
} ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_zz_org_admin_004_DELETE_org ( self ) :
2020-06-24 12:55:01 -05:00
org = Organization . objects . create ( name = " Deletable org " , status = " ok " )
org . admin_usergroup . user_set . add ( self . user_org_admin )
2019-12-05 16:57:52 +00:00
self . assert_delete (
self . db_org_admin ,
" org " ,
2020-06-24 12:55:01 -05:00
# can delete the org we just made
test_success = org . id ,
# cant delete the org we dont have write perms to
2019-12-05 16:57:52 +00:00
test_failure = SHARED [ " org_r_ok " ] . id ,
)
2020-06-24 12:55:01 -05:00
self . assert_delete (
self . db_org_admin ,
" org " ,
# cant delete the org that we have write perms to
# but is not empty
test_failure = SHARED [ " org_rw_ok " ] . id ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
# GUEST TESTS
##########################################################################
def test_guest_001_GET_org ( self ) :
self . assert_get_handleref ( self . db_guest , " org " , SHARED [ " org_r_ok " ] . id )
##########################################################################
def test_guest_001_GET_net ( self ) :
2019-12-05 16:57:52 +00:00
data = self . assert_get_handleref ( self . db_guest , " net " , SHARED [ " net_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
for poc in data . get ( " poc_set " ) :
self . assertEqual ( poc [ " visible " ] , " Public " )
##########################################################################
def test_guest_001_GET_ix ( self ) :
self . assert_get_handleref ( self . db_guest , " ix " , SHARED [ " ix_r_ok " ] . id )
##########################################################################
def test_guest_001_GET_fac ( self ) :
self . assert_get_handleref ( self . db_guest , " fac " , SHARED [ " fac_r_ok " ] . id )
##########################################################################
def test_guest_001_GET_poc_private ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_forbidden ( self . db_guest , " poc " , SHARED [ " poc_r_ok_private " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_001_GET_poc_users ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_forbidden ( self . db_guest , " poc " , SHARED [ " poc_r_ok_users " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_001_GET_poc_public ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_guest , " poc " , SHARED [ " poc_r_ok_public " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_001_GET_nefac ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_guest , " netfac " , SHARED [ " netfac_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_001_GET_netixlan ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_guest , " netixlan " , SHARED [ " netixlan_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_001_GET_ixfac ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_guest , " ixfac " , SHARED [ " ixfac_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_001_GET_ixlan ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_guest , " ixlan " , SHARED [ " ixlan_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
2020-07-15 02:07:01 -05:00
def test_guest_001_GET_ixlan_ixf_ixp_member_list_url ( self ) :
for ixlan in self . db_guest . all (
" ixlan " , ixf_ixp_member_list_url__startswith = " http "
) :
if ixlan [ " ixf_ixp_member_list_url_visible " ] == " Public " :
assert ixlan [ " ixf_ixp_member_list_url " ] == " http://localhost "
else :
assert " ixf_ixp_member_list_url " not in ixlan
##########################################################################
2018-11-08 19:45:21 +00:00
def test_guest_001_GET_ixpfx ( self ) :
2019-12-05 16:57:52 +00:00
self . assert_get_handleref ( self . db_guest , " ixpfx " , SHARED [ " ixpfx_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_001_GET_list_404 ( self ) :
for tag in REFTAG_MAP :
2020-01-08 13:29:58 -06:00
with pytest . raises ( NotFoundException ) :
2018-11-08 19:45:21 +00:00
data = self . db_guest . all ( tag , limit = 1 , id = 99999999 )
if tag == " net " :
2020-01-08 13:29:58 -06:00
with pytest . raises ( NotFoundException ) :
2018-11-08 19:45:21 +00:00
data = self . db_guest . all ( tag , limit = 1 , asn = 99999999999 )
for tag in REFTAG_MAP :
if tag == " poc " :
data = self . db_guest . all ( tag , id = SHARED [ " poc_r_ok_public " ] . id )
else :
data = self . db_guest . all ( tag , id = SHARED [ " %s _r_ok " % tag ] . id )
self . assertEqual ( len ( data ) , 1 )
self . assert_handleref_integrity ( data [ 0 ] )
##########################################################################
def test_guest_005_list_all ( self ) :
data = self . db_guest . all ( " org " )
self . assertGreater ( len ( data ) , 1 )
self . assert_handleref_integrity ( data [ 0 ] )
self . assert_data_integrity ( data [ 0 ] , " org " )
##########################################################################
def test_guest_005_list_all_tags ( self ) :
for tag in REFTAG_MAP :
if tag == " poc " :
continue
data = self . db_guest . all ( tag , limit = 10 )
self . assertLess ( len ( data ) , 11 )
self . assert_handleref_integrity ( data [ 0 ] )
data = self . db_guest . all ( " poc " , limit = 10 , visible = " Public " )
self . assertLess ( len ( data ) , 11 )
self . assert_handleref_integrity ( data [ 0 ] )
##########################################################################
def test_org_admin_005_list ( self ) :
for tag in REFTAG_MAP :
data = self . db_org_admin . all ( tag , limit = 10 )
self . assertLess ( len ( data ) , 11 )
self . assert_handleref_integrity ( data [ 0 ] )
for row in data :
self . assertEqual ( row [ " status " ] , " ok " )
##########################################################################
def test_guest_005_fields_filter ( self ) :
2019-12-05 16:57:52 +00:00
data = self . db_guest . all ( " org " , limit = 10 , fields = " , " . join ( [ " name " , " status " ] ) )
2018-11-08 19:45:21 +00:00
self . assertGreater ( len ( data ) , 0 )
for row in data :
2020-01-08 13:29:58 -06:00
self . assertEqual ( sorted ( row . keys ( ) ) , sorted ( [ " name " , " status " ] ) )
2018-11-08 19:45:21 +00:00
data = self . db_guest . get ( " org " , 1 , fields = " , " . join ( [ " name " , " status " ] ) )
self . assertGreater ( len ( data ) , 0 )
2020-01-08 13:29:58 -06:00
self . assertEqual ( sorted ( data [ 0 ] . keys ( ) ) , sorted ( [ " name " , " status " ] ) )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_005_list_limit ( self ) :
data = self . db_guest . all ( " org " , limit = 10 )
self . assertEqual ( len ( data ) , 10 )
self . assert_handleref_integrity ( data [ 0 ] )
self . assert_data_integrity ( data [ 0 ] , " org " )
##########################################################################
def test_guest_005_list_pagination ( self ) :
n = 1
for i in range ( 0 , 10 ) :
data = self . db_guest . all ( " org " , skip = i * 10 , limit = 10 )
for row in data :
self . assertEqual ( row . get ( " id " ) , n )
n + = 1
##########################################################################
def test_guest_005_list_since ( self ) :
2019-12-05 16:57:52 +00:00
data = self . db_guest . all (
" net " , since = int ( START_TIMESTAMP ) - 10 , status = " deleted "
)
2018-11-08 19:45:21 +00:00
self . assertEqual ( len ( data ) , 2 )
self . assert_handleref_integrity ( data [ 0 ] )
self . assert_data_integrity ( data [ 0 ] , " net " )
##########################################################################
def test_guest_005_get_depth_all ( self ) :
"""
Test all end points single object GET with all valid depths
This also asserts data structure integrity for objects expanded
by the depth parameter
"""
for depth in [ 0 , 1 , 2 , 3 , 4 ] :
2020-01-08 13:29:58 -06:00
for tag , slz in list ( REFTAG_MAP_SLZ . items ( ) ) :
2018-11-08 19:45:21 +00:00
note_tag = " ( %s %s ) " % ( tag , depth )
if tag == " poc " :
o = SHARED [ " %s _r_ok_public " % tag ]
else :
o = SHARED [ " %s _r_ok " % tag ]
data = self . db_guest . get ( tag , o . id , depth = depth )
self . assertEqual ( len ( data ) , 1 , msg = " Data length %s " % note_tag )
pk_flds , n_flds = self . serializer_related_fields ( slz )
obj = data [ 0 ]
2019-12-05 16:57:52 +00:00
self . assert_related_depth (
obj , slz , depth , depth , note_tag , typ = " single "
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_005_list_depth_all ( self ) :
"""
Tests all end points multiple object GET with all valid depths
This also asserts data structure integrity for objects expanded
by the depth parameter
"""
for depth in [ 0 , 1 , 2 , 3 ] :
2020-01-08 13:29:58 -06:00
for tag , slz in list ( REFTAG_MAP_SLZ . items ( ) ) :
2018-11-08 19:45:21 +00:00
note_tag = " ( %s %s ) " % ( tag , depth )
if tag == " poc " :
o = SHARED [ " %s _r_ok_public " % tag ]
else :
o = SHARED [ " %s _r_ok " % tag ]
data = self . db_guest . all ( tag , id = o . id , depth = depth )
self . assertEqual ( len ( data ) , 1 , msg = " Data length %s " % note_tag )
pk_flds , n_flds = self . serializer_related_fields ( slz )
obj = data [ 0 ]
2019-12-05 16:57:52 +00:00
self . assert_related_depth (
obj , slz , depth , depth , note_tag , typ = " listing "
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_005_list_depth_not_set ( self ) :
data = self . db_guest . all ( " org " , id = SHARED [ " org_r_ok " ] . id )
self . assertEqual ( data [ 0 ] . get ( " net_set " ) , None )
##########################################################################
def test_guest_005_list_depth_0 ( self ) :
data = self . db_guest . all ( " org " , id = SHARED [ " org_r_ok " ] . id , depth = 0 )
self . assertEqual ( data [ 0 ] . get ( " net_set " ) , None )
##########################################################################
def test_guest_005_list_depth_1 ( self ) :
data = self . db_guest . all ( " org " , id = SHARED [ " org_r_ok " ] . id , depth = 1 )
self . assertEqual ( len ( data [ 0 ] . get ( " net_set " ) ) , 3 )
self . assertEqual ( data [ 0 ] . get ( " net_set " ) [ 0 ] , SHARED [ " net_r_ok " ] . id )
self . assertEqual ( data [ 0 ] . get ( " net_set " ) [ 1 ] , SHARED [ " net_r2_ok " ] . id )
self . assertEqual ( data [ 0 ] . get ( " net_set " ) [ 2 ] , SHARED [ " net_r3_ok " ] . id )
#############################################################################
def test_guest_005_list_depth_2 ( self ) :
data = self . db_guest . all ( " org " , id = SHARED [ " org_r_ok " ] . id , depth = 2 )
self . assertEqual ( len ( data [ 0 ] . get ( " net_set " ) ) , 3 )
obj = data [ 0 ] . get ( " net_set " ) [ 0 ]
self . assertEqual ( obj . get ( " id " ) , SHARED [ " net_r_ok " ] . id )
self . assert_data_integrity ( obj , " net " , ignore = [ " org_id " ] )
#############################################################################
def test_guest_005_list_depth_3 ( self ) :
data = self . db_guest . all ( " org " , id = SHARED [ " org_r_ok " ] . id , depth = 3 )
self . assertEqual ( len ( data [ 0 ] . get ( " net_set " ) ) , 3 )
obj = data [ 0 ] . get ( " net_set " ) [ 0 ]
self . assertEqual ( obj . get ( " id " ) , SHARED [ " net_r_ok " ] . id )
self . assert_data_integrity ( obj , " net " , ignore = [ " org_id " ] )
obj = obj . get ( " netfac_set " )
self . assertEqual ( len ( obj ) , 1 )
self . assertEqual ( obj [ 0 ] , SHARED [ " netfac_r_ok " ] . id )
##########################################################################
def test_guest_005_list_filter_dates_numeric ( self ) :
2020-01-08 13:29:58 -06:00
for flt , ass in list ( NUMERIC_TESTS . items ( ) ) :
2018-11-08 19:45:21 +00:00
for fld in [ " created " , " updated " ] :
if flt in [ " gt " , " gte " ] :
DATE = DATES [ " yesterday " ]
elif flt in [ " lt " ] :
DATE = DATES [ " tomorrow " ]
else :
DATE = DATES [ " today " ]
if flt :
kwargs = { " %s __ %s " % ( fld , flt ) : DATE [ 1 ] }
else :
kwargs = { fld : DATE [ 1 ] }
data = self . db_guest . all ( " fac " , limit = 10 , * * kwargs )
self . assertGreater (
2019-12-05 16:57:52 +00:00
len ( data ) , 0 , msg = " %s _ %s - data length assertion " % ( fld , flt )
)
2018-11-08 19:45:21 +00:00
for row in data :
self . assert_data_integrity ( row , " fac " )
try :
dt = datetime . datetime . strptime (
2019-12-05 16:57:52 +00:00
row [ fld ] , " % Y- % m- %d T % H: % M: % SZ "
) . date ( )
2018-11-08 19:45:21 +00:00
except ValueError :
dt = datetime . datetime . strptime (
2019-12-05 16:57:52 +00:00
row [ fld ] , " % Y- % m- %d T % H: % M: % S. %f Z "
) . date ( )
2018-11-08 19:45:21 +00:00
fnc = getattr ( self , " assert %s " % ass )
2019-12-05 16:57:52 +00:00
fnc (
dt ,
DATE [ 0 ] ,
msg = " %s __ %s : %s , %s " % ( fld , flt , row [ fld ] , DATE [ 1 ] ) ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_005_list_filter_numeric ( self ) :
data = self . db_guest . all ( " net " , asn = SHARED [ " net_r_ok " ] . asn )
self . assertEqual ( len ( data ) , 1 )
self . assert_data_integrity ( data [ 0 ] , " net " )
self . assertEqual ( data [ 0 ] [ " asn " ] , SHARED [ " net_r_ok " ] . asn )
##########################################################################
def test_guest_005_list_filter_numeric_lte ( self ) :
data = self . db_guest . all ( " fac " , id__lte = SHARED [ " fac_rw_ok " ] . id )
self . assertGreater ( len ( data ) , 0 )
self . assert_data_integrity ( data [ 0 ] , " fac " )
for fac in data :
2020-01-08 13:29:58 -06:00
self . assertLessEqual ( int ( fac [ " id " ] ) , SHARED [ " fac_rw_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_005_list_filter_numeric_lt ( self ) :
data = self . db_guest . all ( " fac " , id__lt = SHARED [ " fac_rw_ok " ] . id )
self . assertGreater ( len ( data ) , 0 )
self . assert_data_integrity ( data [ 0 ] , " fac " )
for fac in data :
2020-01-08 13:29:58 -06:00
self . assertLess ( int ( fac [ " id " ] ) , SHARED [ " fac_rw_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_005_list_filter_numeric_gte ( self ) :
data = self . db_guest . all ( " fac " , id__gte = SHARED [ " fac_r_ok " ] . id )
self . assertGreater ( len ( data ) , 0 )
self . assert_data_integrity ( data [ 0 ] , " fac " )
for fac in data :
2020-01-08 13:29:58 -06:00
self . assertGreaterEqual ( int ( fac [ " id " ] ) , SHARED [ " fac_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_005_list_filter_numeric_gt ( self ) :
data = self . db_guest . all ( " fac " , id__gt = SHARED [ " fac_r_ok " ] . id )
self . assertGreater ( len ( data ) , 0 )
self . assert_data_integrity ( data [ 0 ] , " fac " )
for fac in data :
2020-01-08 13:29:58 -06:00
self . assertGreater ( int ( fac [ " id " ] ) , SHARED [ " fac_r_ok " ] . id )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_005_list_filter_numeric_in ( self ) :
ids = [ SHARED [ " fac_r_ok " ] . id , SHARED [ " fac_rw_ok " ] . id ]
data = self . db_guest . all ( " fac " , id__in = " %s , %s " % tuple ( ids ) )
self . assertEqual ( len ( data ) , len ( ids ) )
self . assert_data_integrity ( data [ 0 ] , " fac " )
for fac in data :
2020-01-08 13:29:58 -06:00
self . assertIn ( int ( fac [ " id " ] ) , ids )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_005_list_filter_string ( self ) :
data = self . db_guest . all ( " ix " , name = SHARED [ " ix_r_ok " ] . name )
self . assertEqual ( len ( data ) , 1 )
self . assert_data_integrity ( data [ 0 ] , " ix " )
self . assertEqual ( data [ 0 ] [ " name " ] , SHARED [ " ix_r_ok " ] . name )
##########################################################################
def test_guest_005_list_filter_string_contains ( self ) :
token = SHARED [ " ix_r_ok " ] . name [ 3 : 5 ]
data = self . db_guest . all ( " ix " , name__contains = token . lower ( ) )
self . assertGreater ( len ( data ) , 0 )
self . assert_data_integrity ( data [ 0 ] , " ix " )
for ix in data :
self . assertIn ( token , ix [ " name " ] )
##########################################################################
def test_guest_005_list_filter_string_startswith ( self ) :
token = SHARED [ " ix_r_ok " ] . name [ 0 : 5 ]
data = self . db_guest . all ( " ix " , name__startswith = token . lower ( ) )
self . assertGreater ( len ( data ) , 0 )
self . assert_data_integrity ( data [ 0 ] , " ix " )
for ix in data :
self . assertEqual ( ix [ " name " ] [ : 5 ] , token )
##########################################################################
def test_guest_005_list_filter_string_in ( self ) :
cities = [ " API Test:IX:RW:ok " , " API Test:IX:R:ok " ]
data = self . db_guest . all ( " ix " , name__in = " %s , %s " % tuple ( cities ) )
self . assertGreater ( len ( data ) , 0 )
self . assert_data_integrity ( data [ 0 ] , " ix " )
for ix in data :
self . assertIn ( ix [ " name " ] , cities )
##########################################################################
def test_guest_005_list_filter_relation_basic ( self ) :
data = self . db_guest . all ( " ix " , org_id = SHARED [ " ix_r_ok " ] . org_id )
self . assertEqual ( len ( data ) , 3 )
self . assert_data_integrity ( data [ 0 ] , " ix " )
self . assertEqual ( data [ 0 ] [ " org_id " ] , SHARED [ " ix_r_ok " ] . org_id )
##########################################################################
def test_guest_005_list_filter_relation_basic_2 ( self ) :
data = self . db_guest . all ( " ix " , org = SHARED [ " ix_r_ok " ] . org_id )
self . assertEqual ( len ( data ) , 3 )
self . assert_data_integrity ( data [ 0 ] , " ix " )
self . assertEqual ( data [ 0 ] [ " org_id " ] , SHARED [ " ix_r_ok " ] . org_id )
##########################################################################
def test_guest_005_list_filter_relation_fld_xl ( self ) :
data = self . db_guest . all ( " netixlan " , net_id__lt = 4 )
for row in data :
self . assertLess ( row [ " net_id " ] , 4 )
##########################################################################
def test_guest_005_list_filter_relation_nested ( self ) :
data = self . db_user . all ( " poc " , net__asn = SHARED [ " net_r_ok " ] . asn )
self . assertEqual ( len ( data ) , 2 )
for row in data :
self . assertEqual ( row . get ( " net_id " ) , SHARED [ " net_r_ok " ] . id )
##########################################################################
def test_guest_005_list_poc ( self ) :
data = self . db_guest . all ( " poc " , limit = 100 )
for row in data :
self . assertEqual ( row . get ( " visible " ) , " Public " )
data = self . db_guest . all ( " poc " , visible__in = " Private,Users " , limit = 100 )
self . assertEqual ( 0 , len ( data ) )
##########################################################################
def test_guest_005_list_filter_net_related ( self ) :
self . assert_list_filter_related ( " net " , " ix " )
self . assert_list_filter_related ( " net " , " ixlan " )
self . assert_list_filter_related ( " net " , " netixlan " )
self . assert_list_filter_related ( " net " , " netfac " )
self . assert_list_filter_related ( " net " , " fac " )
self . assert_list_filter_related ( " net " , " org " )
##########################################################################
def test_guest_005_list_filter_net_not_ix ( self ) :
ix = SHARED [ " ix_r_ok " ]
data_a = self . db_guest . all ( " net " , ix = ix . id )
data_b = self . db_guest . all ( " net " , not_ix = ix . id )
self . assertGreater ( len ( data_a ) , 0 )
self . assertGreater ( len ( data_b ) , 0 )
for row_b in data_b :
for row_a in data_a :
self . assertNotEqual ( row_a [ " id " ] , row_b [ " id " ] )
##########################################################################
def test_guest_005_list_filter_net_not_fac ( self ) :
fac = SHARED [ " fac_r_ok " ]
data_a = self . db_guest . all ( " net " , fac = fac . id )
data_b = self . db_guest . all ( " net " , not_fac = fac . id )
self . assertGreater ( len ( data_a ) , 0 )
self . assertGreater ( len ( data_b ) , 0 )
for row_b in data_b :
for row_a in data_a :
self . assertNotEqual ( row_a [ " id " ] , row_b [ " id " ] )
##########################################################################
def test_guest_005_list_filter_ixpfx_related ( self ) :
self . assert_list_filter_related ( " ixpfx " , " ix " )
self . assert_list_filter_related ( " ixpfx " , " ixlan " )
##########################################################################
2020-02-05 21:26:21 -06:00
def test_guest_005_list_filter_ixpfx_whereis ( self ) :
ixpfx = SHARED [ " ixpfx_r_ok " ]
ipaddr = " {} " . format ( ixpfx . prefix [ 0 ] )
data = self . db_guest . all ( " ixpfx " , whereis = ipaddr )
assert len ( data ) == 1
assert data [ 0 ] [ " id " ] == ixpfx . id
##########################################################################
2018-11-08 19:45:21 +00:00
def test_guest_005_list_filter_ix_related ( self ) :
self . assert_list_filter_related ( " ix " , " ixlan " )
self . assert_list_filter_related ( " ix " , " ixfac " )
self . assert_list_filter_related ( " ix " , " fac " )
self . assert_list_filter_related ( " ix " , " net " )
self . assert_list_filter_related ( " ix " , " net " , " asn " )
self . assert_list_filter_related ( " ix " , " org " )
##########################################################################
def test_guest_005_list_filter_ix_ipblock ( self ) :
prefix = str ( SHARED [ " ixpfx_r_ok " ] . prefix ) [ : - 3 ]
data = self . db_guest . all ( " ix " , ipblock = prefix )
self . assertGreater ( len ( data ) , 0 )
for row in data :
self . assertEqual ( row [ " id " ] , SHARED [ " ix_r_ok " ] . id )
##########################################################################
def test_guest_005_list_filter_ix_name_search ( self ) :
data = self . db_guest . all ( " ix " , name_search = SHARED [ " ix_r_ok " ] . name )
self . assertEqual ( len ( data ) , 1 )
for row in data :
self . assertEqual ( row [ " id " ] , SHARED [ " ix_r_ok " ] . id )
data = self . db_guest . all ( " ix " , name_search = SHARED [ " ix_r_ok " ] . name_long )
self . assertEqual ( len ( data ) , 1 )
for row in data :
self . assertEqual ( row [ " id " ] , SHARED [ " ix_r_ok " ] . id )
##########################################################################
def test_guest_005_list_filter_ix_asn_overlap ( self ) :
# create three test networks
networks = [
Network . objects . create ( status = " ok " , * * self . make_data_net ( ) )
for i in range ( 0 , 3 )
]
# create two test exchanges
exchanges = [
InternetExchange . objects . create ( status = " ok " , * * self . make_data_ix ( ) )
for i in range ( 0 , 2 )
]
2020-02-05 21:25:25 -06:00
# collect ixlans
ixlans = [ ix . ixlan for ix in exchanges ]
2018-11-08 19:45:21 +00:00
# all three networks peer at first exchange
for net in networks :
2019-12-05 16:57:52 +00:00
NetworkIXLan . objects . create (
network = net , ixlan = ixlans [ 0 ] , status = " ok " , asn = net . asn , speed = 0
)
2018-11-08 19:45:21 +00:00
# only the first two networks peer at second exchange
for net in networks [ : 2 ] :
2019-12-05 16:57:52 +00:00
NetworkIXLan . objects . create (
network = net , ixlan = ixlans [ 1 ] , status = " ok " , asn = net . asn , speed = 0
)
2018-11-08 19:45:21 +00:00
# do test queries
# query #1 - test overlapping exchanges for all 3 asns - should return first ix
2019-12-05 16:57:52 +00:00
data = self . db_guest . all (
" ix " , asn_overlap = " , " . join ( [ str ( net . asn ) for net in networks ] )
)
2018-11-08 19:45:21 +00:00
self . assertEqual ( len ( data ) , 1 )
self . assertEqual ( data [ 0 ] [ " id " ] , exchanges [ 0 ] . id )
# query #2 - test overlapping exchanges for first 2 asns - should return both ixs
2019-12-05 16:57:52 +00:00
data = self . db_guest . all (
" ix " , asn_overlap = " , " . join ( [ str ( net . asn ) for net in networks [ : 2 ] ] )
)
2018-11-08 19:45:21 +00:00
self . assertEqual ( len ( data ) , 2 )
for row in data :
self . assertIn ( row [ " id " ] , [ ix . id for ix in exchanges ] )
# query #3 - should error when only passing one asn
2020-01-08 13:29:58 -06:00
with pytest . raises ( InvalidRequestException ) :
2018-11-08 19:45:21 +00:00
self . db_guest . all ( " ix " , asn_overlap = networks [ 0 ] . asn )
# query #4 - should error when passing too many asns
2020-01-08 13:29:58 -06:00
with pytest . raises ( InvalidRequestException ) :
2019-12-05 16:57:52 +00:00
self . db_guest . all (
" ix " , asn_overlap = " , " . join ( [ str ( i ) for i in range ( 0 , 30 ) ] )
)
2018-11-08 19:45:21 +00:00
# clean up data
for net in networks :
net . delete ( hard = True )
for ix in exchanges :
ix . delete ( hard = True )
##########################################################################
def test_guest_005_list_filter_fac_related ( self ) :
self . assert_list_filter_related ( " fac " , " ix " )
self . assert_list_filter_related ( " fac " , " net " )
##########################################################################
def test_guest_005_list_filter_fac_org_name ( self ) :
data = self . db_guest . all ( " fac " , org_name = SHARED [ " org_r_ok " ] . name [ 2 : 10 ] )
for row in data :
self . assertEqual ( data [ 0 ] [ " org_id " ] , SHARED [ " org_r_ok " ] . id )
self . assert_data_integrity ( data [ 0 ] , " fac " )
##########################################################################
def test_guest_005_list_filter_fac_net_count ( self ) :
data = self . db_guest . all ( " fac " , net_count = 1 )
for row in data :
self . assert_data_integrity ( row , " fac " )
self . assertEqual ( row [ " net_count " ] , 1 )
data = self . db_guest . all ( " fac " , net_count = 0 )
for row in data :
self . assert_data_integrity ( row , " fac " )
self . assertEqual ( row [ " net_count " ] , 0 )
data = self . db_guest . all ( " fac " , net_count__lt = 1 )
for row in data :
self . assert_data_integrity ( row , " fac " )
self . assertEqual ( row [ " net_count " ] , 0 )
data = self . db_guest . all ( " fac " , net_count__gt = 0 )
for row in data :
self . assert_data_integrity ( row , " fac " )
self . assertGreater ( row [ " net_count " ] , 0 )
2019-05-02 18:03:21 +00:00
##########################################################################
def test_guest_005_list_filter_ix_net_count ( self ) :
data = self . db_guest . all ( " ix " , net_count = 1 )
for row in data :
self . assert_data_integrity ( row , " ix " )
self . assertEqual ( row [ " net_count " ] , 1 )
data = self . db_guest . all ( " ix " , net_count = 0 )
for row in data :
self . assert_data_integrity ( row , " ix " )
self . assertEqual ( row [ " net_count " ] , 0 )
data = self . db_guest . all ( " ix " , net_count__lt = 1 )
for row in data :
self . assert_data_integrity ( row , " ix " )
self . assertEqual ( row [ " net_count " ] , 0 )
data = self . db_guest . all ( " ix " , net_count__gt = 0 )
for row in data :
self . assert_data_integrity ( row , " ix " )
self . assertGreater ( row [ " net_count " ] , 0 )
data = self . db_guest . all ( " ix " , net_count__lte = 2 )
for row in data :
self . assert_data_integrity ( row , " ix " )
self . assertLessEqual ( row [ " net_count " ] , 2 )
data = self . db_guest . all ( " ix " , net_count__gte = 1 )
for row in data :
self . assert_data_integrity ( row , " ix " )
self . assertGreaterEqual ( row [ " net_count " ] , 1 )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_guest_005_list_filter_fac_asn_overlap ( self ) :
# create three test networks
networks = [
Network . objects . create ( status = " ok " , * * self . make_data_net ( ) )
for i in range ( 0 , 3 )
]
# create two test facilities
facilities = [
Facility . objects . create ( status = " ok " , * * self . make_data_fac ( ) )
for i in range ( 0 , 2 )
]
# all three networks peer at first facility
for net in networks :
2019-12-05 16:57:52 +00:00
NetworkFacility . objects . create (
network = net , facility = facilities [ 0 ] , status = " ok "
)
2018-11-08 19:45:21 +00:00
# only the first two networks peer at second facility
for net in networks [ : 2 ] :
2019-12-05 16:57:52 +00:00
NetworkFacility . objects . create (
network = net , facility = facilities [ 1 ] , status = " ok "
)
2018-11-08 19:45:21 +00:00
# do test queries
# query #1 - test overlapping facilities for all 3 asns - should return first facility
2019-12-05 16:57:52 +00:00
data = self . db_guest . all (
" fac " , asn_overlap = " , " . join ( [ str ( net . asn ) for net in networks ] )
)
2018-11-08 19:45:21 +00:00
self . assertEqual ( len ( data ) , 1 )
self . assertEqual ( data [ 0 ] [ " id " ] , facilities [ 0 ] . id )
# query #2 - test overlapping facilities for first 2 asns - should return both facs
2019-12-05 16:57:52 +00:00
data = self . db_guest . all (
" fac " , asn_overlap = " , " . join ( [ str ( net . asn ) for net in networks [ : 2 ] ] )
)
2018-11-08 19:45:21 +00:00
self . assertEqual ( len ( data ) , 2 )
for row in data :
self . assertIn ( row [ " id " ] , [ ix . id for ix in facilities ] )
# query #3 - should error when only passing one asn
2020-01-08 13:29:58 -06:00
with pytest . raises ( InvalidRequestException ) :
2018-11-08 19:45:21 +00:00
self . db_guest . all ( " fac " , asn_overlap = networks [ 0 ] . asn )
# query #4 - should error when passing too many asns
2020-01-08 13:29:58 -06:00
with pytest . raises ( InvalidRequestException ) :
2019-12-05 16:57:52 +00:00
self . db_guest . all (
" fac " , asn_overlap = " , " . join ( [ str ( i ) for i in range ( 0 , 30 ) ] )
)
2018-11-08 19:45:21 +00:00
# clean up data
for net in networks :
net . delete ( hard = True )
for fac in facilities :
fac . delete ( hard = True )
##########################################################################
2020-01-08 13:29:58 -06:00
def test_guest_005_list_filter_org_asn ( self ) :
data = self . db_guest . all ( " org " , asn = SHARED [ " net_r_ok " ] . asn )
self . assertEqual ( len ( data ) , 1 )
for row in data :
self . assertEqual ( row [ " id " ] , SHARED [ " org_r_ok " ] . id )
##########################################################################
2018-11-08 19:45:21 +00:00
def test_guest_005_list_filter_netixlan_related ( self ) :
self . assert_list_filter_related ( " netixlan " , " net " )
self . assert_list_filter_related ( " netixlan " , " ixlan " )
self . assert_list_filter_related ( " netixlan " , " ix " )
##########################################################################
2020-04-10 09:03:12 +00:00
def test_guest_005_list_filter_netixlan_operational ( self ) :
# all netixlans are operational at this point,
# filtering by operational=False should return empty list
data = self . db_guest . all ( " netixlan " , operational = 0 )
assert len ( data ) == 0
# set one netixlan to not operational
netixlan = NetworkIXLan . objects . first ( )
2020-07-15 02:07:01 -05:00
netixlan . operational = False
2020-04-10 09:03:12 +00:00
netixlan . save ( )
# assert that it is now returned in the operational=False
# result
data = self . db_guest . all ( " netixlan " , operational = 0 )
assert len ( data ) == 1
assert data [ 0 ] [ " id " ] == netixlan . id
##########################################################################
2018-11-08 19:45:21 +00:00
def test_guest_005_list_filter_netixlan_related_name ( self ) :
data = self . db_guest . all ( " netixlan " , name = SHARED [ " ix_rw_ok " ] . name )
self . assertEqual ( len ( data ) , 1 )
self . assert_data_integrity ( data [ 0 ] , " netixlan " )
##########################################################################
def test_guest_005_list_filter_netfac_related ( self ) :
self . assert_list_filter_related ( " netfac " , " net " )
self . assert_list_filter_related ( " netfac " , " fac " )
##########################################################################
def test_guest_005_list_filter_netfac_related_name ( self ) :
data = self . db_guest . all ( " netfac " , name = SHARED [ " fac_rw_ok " ] . name )
self . assertEqual ( len ( data ) , 1 )
self . assert_data_integrity ( data [ 0 ] , " netfac " )
##########################################################################
def test_guest_005_list_filter_netfac_related_city ( self ) :
data = self . db_guest . all ( " netfac " , city = SHARED [ " fac_rw_ok " ] . city )
self . assertEqual ( len ( data ) , 2 )
self . assert_data_integrity ( data [ 0 ] , " netfac " )
##########################################################################
def test_guest_005_list_filter_netfac_related_country ( self ) :
2020-01-08 13:29:58 -06:00
data = self . db_guest . all (
2020-07-15 02:07:01 -05:00
" netfac " , country = " {} " . format ( SHARED [ " fac_rw_ok " ] . country )
2020-01-08 13:29:58 -06:00
)
2018-11-08 19:45:21 +00:00
self . assertEqual ( len ( data ) , 2 )
self . assert_data_integrity ( data [ 0 ] , " netfac " )
##########################################################################
def test_guest_005_list_filter_ixlan_related ( self ) :
self . assert_list_filter_related ( " ixlan " , " ix " )
##########################################################################
def test_guest_005_list_filter_ixfac_related ( self ) :
self . assert_list_filter_related ( " ixfac " , " fac " )
self . assert_list_filter_related ( " ixfac " , " ix " )
##########################################################################
def test_guest_005_list_filter_poc_related ( self ) :
self . assert_list_filter_related ( " poc " , " net " )
return
data = self . db_guest . all ( " poc " , net_id = SHARED [ " net_r_ok " ] . id )
self . assertGreater ( len ( data ) , 0 )
for row in data :
self . assert_data_integrity ( row , " poc " )
self . assertEqual ( row [ " net_id " ] , SHARED [ " net_r_ok " ] . id )
##########################################################################
def test_guest_005_list_skip ( self ) :
data = self . db_guest . all ( " org " , skip = 0 , limit = 20 )
self . assertEqual ( len ( data ) , 20 )
target = data [ 10 ]
data = self . db_guest . all ( " org " , skip = 10 , limit = 20 )
self . assertEqual ( len ( data ) , 20 )
comp = data [ 0 ]
self . assertEqual ( target , comp )
2019-05-02 15:20:20 +00:00
##########################################################################
def test_guest_005_list_filter_accented ( self ) :
"""
test filtering with accented search terms
"""
2019-12-05 16:57:52 +00:00
# TODO: sqlite3 is being used as the testing backend, and django 1.11
# seems to be unable to set a collation on it, so we can't properly test
# the other way atm, for now this test at least confirms that the term is
# unaccented correctly.
2019-05-02 15:20:20 +00:00
#
2019-12-05 16:57:52 +00:00
# on production we run mysql with flattened accents so both ways should work
# there regardless.
2019-05-02 15:20:20 +00:00
org = Organization . objects . create ( name = " org unaccented " , status = " ok " )
2019-12-05 16:57:52 +00:00
net = Network . objects . create (
2020-01-08 13:29:58 -06:00
asn = 12345 , name = " net unaccented " , status = " ok " , org = org
2019-12-05 16:57:52 +00:00
)
2020-01-08 13:29:58 -06:00
ix = InternetExchange . objects . create ( org = org , name = " ix unaccented " , status = " ok " )
fac = Facility . objects . create ( org = org , name = " fac unaccented " , status = " ok " )
2019-05-02 15:20:20 +00:00
2019-12-05 16:57:52 +00:00
for tag in [ " org " , " net " , " ix " , " fac " ] :
2020-01-08 13:29:58 -06:00
data = self . db_guest . all ( tag , name = " {} unãccented " . format ( tag ) )
2019-05-02 15:20:20 +00:00
self . assertEqual ( len ( data ) , 1 )
2018-11-08 19:45:21 +00:00
##########################################################################
# READONLY PERMISSION TESTS
# These tests assert that the readonly users cannot write anything
##########################################################################
##########################################################################
def test_readonly_users_003_PUT_org ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_update (
db ,
" org " ,
SHARED [ " org_r_ok " ] . id ,
{ } ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_002_POST_ix ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_create (
db ,
" ix " ,
self . make_data_ix ( prefix = self . get_prefix4 ( ) ) ,
test_failures = { " perms " : { } } ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_003_PUT_ix ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_update (
db ,
" ix " ,
SHARED [ " ix_r_ok " ] . id ,
{ } ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_004_DELETE_ix ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_delete (
db , " ix " , test_success = False , test_failure = SHARED [ " ix_r_ok " ] . id
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_002_POST_fac ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_create (
db ,
" fac " ,
self . make_data_fac ( ) ,
test_failures = { " perms " : { } } ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_003_PUT_fac ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_update (
db ,
" fac " ,
SHARED [ " fac_r_ok " ] . id ,
{ } ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_004_DELETE_fac ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_delete (
db , " fac " , test_success = False , test_failure = SHARED [ " fac_r_ok " ] . id
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_002_POST_netfac ( self ) :
for db in self . readonly_dbs ( ) :
self . assert_create (
2019-12-05 16:57:52 +00:00
db ,
" netfac " ,
2020-01-08 13:29:58 -06:00
{ " net_id " : SHARED [ " net_r_ok " ] . id , " fac_id " : SHARED [ " fac_r2_ok " ] . id , } ,
2019-12-05 16:57:52 +00:00
test_failures = { " perms " : { } } ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_003_PUT_netfac ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_update (
db ,
" netfac " ,
SHARED [ " netfac_r_ok " ] . id ,
{ } ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_004_DELETE_netfac ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_delete (
db , " netfac " , test_success = False , test_failure = SHARED [ " netfac_r_ok " ] . id
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_002_POST_ixfac ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_create (
db ,
" ixfac " ,
{ " ix_id " : SHARED [ " ix_r_ok " ] . id , " fac_id " : SHARED [ " fac_r2_ok " ] . id } ,
test_failures = { " perms " : { } } ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_003_PUT_ixfac ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_update (
db ,
" ixfac " ,
SHARED [ " ixfac_r_ok " ] . id ,
{ } ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_004_DELETE_ixfac ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_delete (
db , " ixfac " , test_success = False , test_failure = SHARED [ " ixfac_r_ok " ] . id
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_002_POST_poc ( self ) :
for db in self . readonly_dbs ( ) :
self . assert_create (
2019-12-05 16:57:52 +00:00
db ,
" poc " ,
self . make_data_poc ( net_id = SHARED [ " net_rw_ok " ] . id ) ,
test_failures = { " perms " : { } } ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_003_PUT_poc ( self ) :
for db in self . readonly_dbs ( exclude = [ self . db_user ] ) :
2019-12-05 16:57:52 +00:00
self . assert_update (
db ,
" poc " ,
SHARED [ " poc_r_ok_public " ] . id ,
{ } ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
self . assert_update (
db ,
" poc " ,
SHARED [ " poc_r_ok_private " ] . id ,
{ } ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
self . assert_update (
db ,
" poc " ,
SHARED [ " poc_r_ok_users " ] . id ,
{ } ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_004_DELETE_poc ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_delete (
db , " poc " , test_success = False , test_failure = SHARED [ " poc_r_ok_public " ] . id
)
self . assert_delete (
db ,
" poc " ,
test_success = False ,
test_failure = SHARED [ " poc_r_ok_private " ] . id ,
)
self . assert_delete (
db , " poc " , test_success = False , test_failure = SHARED [ " poc_r_ok_users " ] . id
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_002_POST_ixlan ( self ) :
for db in self . readonly_dbs ( ) :
2020-02-05 21:25:25 -06:00
with self . assertRaises ( Exception ) as exc :
self . assert_create (
db ,
" ixlan " ,
self . make_data_ixlan ( ) ,
test_failures = { " perms " : { } } ,
test_success = False ,
)
self . assertIn ( ' Method " POST " not allowed ' , str ( exc . exception ) )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_003_PUT_ixlan ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_update (
db ,
" ixlan " ,
SHARED [ " ixlan_r_ok " ] . id ,
{ } ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_004_DELETE_ixlan ( self ) :
for db in self . readonly_dbs ( ) :
2020-02-05 21:25:25 -06:00
with self . assertRaises ( Exception ) as exc :
self . assert_delete (
db ,
" ixlan " ,
test_success = False ,
test_failure = SHARED [ " ixlan_r_ok " ] . id ,
)
self . assertIn ( ' Method " DELETE " not allowed ' , str ( exc . exception ) )
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_002_POST_ixpfx ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_create (
db ,
" ixpfx " ,
self . make_data_ixpfx ( prefix = " 200.100.200.0/22 " ) ,
test_failures = { " perms " : { } } ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_003_PUT_ixpfx ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_update (
db ,
" ixpfx " ,
SHARED [ " ixpfx_r_ok " ] . id ,
{ } ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_004_DELETE_ixpfx ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_delete (
db , " ixpfx " , test_success = False , test_failure = SHARED [ " ixpfx_r_ok " ] . id
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_002_POST_netixlan ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_create (
db ,
" netixlan " ,
self . make_data_netixlan ( ) ,
test_failures = { " perms " : { } } ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_003_PUT_netixlan ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_update (
db ,
" netixlan " ,
SHARED [ " netixlan_r_ok " ] . id ,
{ } ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_004_DELETE_netixlan ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_delete (
db ,
" netixlan " ,
test_success = False ,
test_failure = SHARED [ " netixlan_r_ok " ] . id ,
)
2018-11-08 19:45:21 +00:00
##########################################################################
def test_readonly_users_004_DELETE_org ( self ) :
for db in self . readonly_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_delete (
db , " org " , test_success = False , test_failure = SHARED [ " org_r_ok " ] . id
)
2018-11-08 19:45:21 +00:00
##########################################################################
# CRUD PERMISSION TESTS
##########################################################################
def test_z_crud_002_create ( self ) :
# user with create perms should be allowed to create a new poc under net_rw3_ok
# but not under net_rw2_ok
2019-12-05 16:57:52 +00:00
self . assert_create (
self . db_crud_create ,
" poc " ,
self . make_data_poc ( net_id = SHARED [ " net_rw3_ok " ] . id ) ,
test_failures = { " perms " : { " net_id " : SHARED [ " net_rw2_ok " ] . id } } ,
)
2018-11-08 19:45:21 +00:00
# other crud test users should not be able to create a new poc under
# net_rw3_ok
for p in [ " delete " , " update " ] :
self . assert_create (
2019-12-05 16:57:52 +00:00
getattr ( self , " db_crud_ %s " % p ) ,
" poc " ,
2018-11-08 19:45:21 +00:00
self . make_data_poc ( net_id = SHARED [ " net_rw3_ok " ] . id ) ,
2019-12-05 16:57:52 +00:00
test_failures = { " perms " : { } } ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
def test_z_crud_003_update ( self ) :
# user with update perms should be allowed to update net_rw3_ok
# but not net_rw2_ok
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_crud_update ,
" net " ,
SHARED [ " net_rw3_ok " ] . id ,
{ " name " : self . make_name ( " Test " ) } ,
test_failures = { " perms " : { " id " : SHARED [ " net_rw2_ok " ] . id } } ,
)
2018-11-08 19:45:21 +00:00
# user with update perms should not be allowed to update ix_rw3_ok
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_crud_update ,
" ix " ,
SHARED [ " ix_rw3_ok " ] . id ,
{ " name " : self . make_name ( " Test " ) } ,
test_failures = { " perms " : { } } ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
# other crud test users should not be able to update net_rw3_ok
for p in [ " delete " , " create " ] :
self . assert_update (
2019-12-05 16:57:52 +00:00
getattr ( self , " db_crud_ %s " % p ) ,
" net " ,
SHARED [ " net_rw3_ok " ] . id ,
{ " name " : self . make_name ( " Test " ) } ,
test_failures = { " perms " : { } } ,
test_success = False ,
)
2018-11-08 19:45:21 +00:00
def test_z_crud_004_delete ( self ) :
# other crud test users should not be able to delete net_rw3_ok
for p in [ " update " , " create " ] :
self . assert_delete (
getattr ( self , " db_crud_ %s " % p ) ,
" net " ,
test_success = False ,
test_failure = SHARED [ " net_rw3_ok " ] . id ,
)
# user with delete perms should be allowed to update net_rw3_ok
# but not net_rw2_ok
2019-12-05 16:57:52 +00:00
self . assert_delete (
self . db_crud_delete ,
" net " ,
SHARED [ " net_rw3_ok " ] . id ,
test_failure = SHARED [ " net_rw2_ok " ] . id ,
)
2018-11-08 19:45:21 +00:00
# user with delete perms should not be allowed to delete ix_rw3_ok
self . assert_delete (
self . db_crud_delete ,
" ix " ,
test_success = False ,
test_failure = SHARED [ " ix_rw3_ok " ] . id ,
)
##########################################################################
# MISC TESTS
##########################################################################
2020-07-15 02:07:01 -05:00
def _test_GET_ixf_ixp_member_list_url ( self , db , tests = [ ] , suffix = " r " ) :
ixlan = SHARED [ f " ixlan_ { suffix } _ok " ]
ixlan . ixf_ixp_member_list_url = " http://localhost "
ixlan . save ( )
for visible , expected in tests :
ixlan . ixf_ixp_member_list_url_visible = visible
ixlan . full_clean ( )
ixlan . save ( )
data = db . get ( " ixlan " , id = ixlan . id ) [ 0 ]
assert data [ " ixf_ixp_member_list_url_visible " ] == visible
if expected :
assert data [ " ixf_ixp_member_list_url " ] == ixlan . ixf_ixp_member_list_url
else :
assert " ixf_ixp_member_list_url " not in data
def test_z_misc_GET_ixf_ixp_member_list_url ( self ) :
"""
Test the visibility of ixlan . ixf_ixp_member_list_url for
Guest , User , Org member and org admin
"""
self . _test_GET_ixf_ixp_member_list_url (
self . db_user , [ ( " Private " , False ) , ( " Users " , True ) , ( " Public " , True ) ]
)
self . _test_GET_ixf_ixp_member_list_url (
self . db_guest , [ ( " Private " , False ) , ( " Users " , False ) , ( " Public " , True ) ]
)
self . _test_GET_ixf_ixp_member_list_url (
self . db_org_member , [ ( " Private " , True ) , ( " Users " , True ) , ( " Public " , True ) ]
)
self . _test_GET_ixf_ixp_member_list_url (
self . db_org_admin ,
[ ( " Private " , True ) , ( " Users " , True ) , ( " Public " , True ) ] ,
suffix = " rw " ,
)
2020-04-20 09:45:48 -05:00
def test_z_misc_POST_ix_fac_missing_phone_fields ( self ) :
"""
Test that omitting the * _phone fields during fac
and ix object creation doesnt error 500
TODO : a test that drops all the non - required fields
and tests for every reftag model
"""
data = self . make_data_fac ( )
db = self . db_org_admin
del data [ " tech_phone " ]
r = db . create ( " fac " , data , return_response = True ) . get ( " data " )
data = self . make_data_fac ( )
del data [ " sales_phone " ]
r = db . create ( " fac " , data , return_response = True ) . get ( " data " )
data = self . make_data_ix ( prefix = self . get_prefix4 ( ) )
del data [ " tech_phone " ]
r = db . create ( " ix " , data , return_response = True ) . get ( " data " )
data = self . make_data_ix ( prefix = self . get_prefix4 ( ) )
del data [ " policy_phone " ]
r = db . create ( " ix " , data , return_response = True ) . get ( " data " )
2018-11-08 19:45:21 +00:00
def test_z_misc_002_dupe_netixlan_ip ( self ) :
# test that addint duplicate netixlan ips is impossible
A = SHARED [ " netixlan_rw_ok " ]
2019-12-05 16:57:52 +00:00
self . assert_create (
self . db_org_admin ,
" netixlan " ,
self . make_data_netixlan ( ixlan_id = A . ixlan_id , net_id = A . network_id ) ,
test_success = False ,
2020-01-08 13:29:58 -06:00
test_failures = { " invalid " : { " ipaddr4 " : str ( A . ipaddr4 ) } } ,
2019-12-05 16:57:52 +00:00
)
self . assert_create (
self . db_org_admin ,
" netixlan " ,
self . make_data_netixlan ( ixlan_id = A . ixlan_id , net_id = A . network_id , ) ,
test_success = False ,
2020-01-08 13:29:58 -06:00
test_failures = { " invalid " : { " ipaddr6 " : str ( A . ipaddr6 ) } } ,
2019-12-05 16:57:52 +00:00
)
2018-11-08 19:45:21 +00:00
2020-01-08 13:29:58 -06:00
def test_z_misc_002_local_asn ( self ) :
# test that local_asn gets enforced (#186)
net = SHARED [ " net_rw_ok " ]
fac = SHARED [ " fac_rw_ok " ]
ixlan = SHARED [ " ixlan_rw_ok " ]
# test netfac create without asn sent (should auto set)
data = { " net_id " : net . id , " fac_id " : fac . id }
r_data = self . db_org_admin . create ( " netfac " , data , return_response = True ) . get (
" data "
) [ 0 ]
assert r_data [ " local_asn " ] == net . asn
NetworkFacility . objects . get ( id = r_data [ " id " ] ) . delete ( )
# test nefac create with local_asn sent (should ignore and auto set)
data = { " net_id " : net . id , " fac_id " : fac . id , " local_asn " : 12345 }
r_data = self . db_org_admin . create ( " netfac " , data , return_response = True ) . get (
" data "
) [ 0 ]
assert r_data [ " local_asn " ] == net . asn
NetworkFacility . objects . get ( id = r_data [ " id " ] ) . delete ( )
# test netixlan create without asn sent (should auto set)
data = self . make_data_netixlan ( ixlan_id = ixlan . id , net_id = net . id )
del data [ " asn " ]
r_data = self . db_org_admin . create ( " netixlan " , data , return_response = True ) . get (
" data "
) [ 0 ]
assert r_data [ " asn " ] == net . asn
NetworkIXLan . objects . get ( id = r_data [ " id " ] ) . delete ( )
# test neixlan create with asn sent (should ignore and auto set)
data = self . make_data_netixlan ( ixlan_id = ixlan . id , net_id = net . id , asn = 12345 )
r_data = self . db_org_admin . create ( " netixlan " , data , return_response = True ) . get (
" data "
) [ 0 ]
assert r_data [ " asn " ] == net . asn
NetworkIXLan . objects . get ( id = r_data [ " id " ] ) . delete ( )
2018-11-08 19:45:21 +00:00
def test_z_misc_002_dupe_name_update ( self ) :
# test that changing the name of entity A (status=ok)
# to name of entity B (status=deleted) does raise the approporiate
# unique key error and does not undelete entity B
A = SHARED [ " fac_rw_dupe_ok " ]
B = SHARED [ " fac_rw_dupe_deleted " ]
self . assertEqual ( A . status , " ok " )
self . assertEqual ( B . status , " deleted " )
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
" fac " ,
A . id ,
{ } ,
test_failures = { " invalid " : { " name " : B . name } } ,
)
2018-11-08 19:45:21 +00:00
B . refresh_from_db ( )
self . assertEqual ( B . status , " deleted " )
2020-01-08 13:29:58 -06:00
def test_z_misc_001_ix_phone_number_validation ( self ) :
data = self . make_data_ix ( org_id = SHARED [ " org_rw_ok " ] . id )
# test that valid number comes back properly formatted
data . update (
prefix = PREFIXES_V4 [ - 1 ] ,
tech_phone = " +1 206 555 0199 " ,
policy_phone = " +1 206 555 0199 " ,
)
r_data = self . db_org_admin . create ( " ix " , data , return_response = True ) . get ( " data " ) [
0
]
assert r_data [ " tech_phone " ] == " +12065550199 "
assert r_data [ " policy_phone " ] == " +12065550199 "
# test that invalid numbers raise validation errors
self . assert_update (
self . db_org_admin ,
" ix " ,
r_data [ " id " ] ,
{ } ,
test_failures = { " invalid " : { " tech_phone " : " invalid number " } } ,
)
self . assert_update (
self . db_org_admin ,
" ix " ,
r_data [ " id " ] ,
{ } ,
test_failures = { " invalid " : { " policy_phone " : " invalid number " } } ,
)
def test_z_misc_001_poc_phone_number_validation ( self ) :
data = self . make_data_poc ( net_id = SHARED [ " net_rw_ok " ] . id )
# test that valid number comes back properly formatted
data . update ( phone = " +1 206 555 0199 " )
r_data = self . db_org_admin . create ( " poc " , data , return_response = True ) . get (
" data "
) [ 0 ]
assert r_data [ " phone " ] == " +12065550199 "
# test that invalid numbers raise validation errors
self . assert_update (
self . db_org_admin ,
" poc " ,
r_data [ " id " ] ,
{ } ,
test_failures = { " invalid " : { " phone " : " invalid number " } } ,
)
2018-11-08 19:45:21 +00:00
def test_z_misc_001_org_create ( self ) :
# no one should be allowed to create an org via the api
# at this point in time
for db in self . all_dbs ( ) :
2019-12-05 16:57:52 +00:00
self . assert_create (
db ,
" org " ,
self . make_data_org ( name = self . make_name ( " Test " ) ) ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
2018-11-08 19:45:21 +00:00
def test_z_misc_001_suggest_net ( self ) :
# test network suggestions
data = self . make_data_net (
2019-12-05 16:57:52 +00:00
asn = 9000901 , org_id = settings . SUGGEST_ENTITY_ORG , suggest = True
)
2018-11-08 19:45:21 +00:00
r_data = self . assert_create ( self . db_user , " net " , data )
self . assertEqual ( r_data [ " org_id " ] , settings . SUGGEST_ENTITY_ORG )
self . assertEqual ( r_data [ " status " ] , " pending " )
net = Network . objects . get ( id = r_data [ " id " ] )
self . assertEqual ( net . org_id , settings . SUGGEST_ENTITY_ORG )
data = self . make_data_net (
2019-12-05 16:57:52 +00:00
asn = 9000902 , org_id = settings . SUGGEST_ENTITY_ORG , suggest = True
)
2018-11-08 19:45:21 +00:00
2019-12-05 16:57:52 +00:00
r_data = self . assert_create (
self . db_guest , " net " , data , test_success = False , test_failures = { " perms " : { } }
)
2018-11-08 19:45:21 +00:00
def test_z_misc_001_suggest_fac ( self ) :
# test facility suggestions
2019-12-05 16:57:52 +00:00
data = self . make_data_fac ( org_id = settings . SUGGEST_ENTITY_ORG , suggest = True )
2018-11-08 19:45:21 +00:00
r_data = self . assert_create ( self . db_user , " fac " , data )
self . assertEqual ( r_data [ " org_id " ] , settings . SUGGEST_ENTITY_ORG )
self . assertEqual ( r_data [ " status " ] , " pending " )
fac = Facility . objects . get ( id = r_data [ " id " ] )
self . assertEqual ( fac . org_id , settings . SUGGEST_ENTITY_ORG )
2019-12-05 16:57:52 +00:00
data = self . make_data_fac ( org_id = settings . SUGGEST_ENTITY_ORG , suggest = True )
2018-11-08 19:45:21 +00:00
2019-12-05 16:57:52 +00:00
r_data = self . assert_create (
self . db_guest , " fac " , data , test_success = False , test_failures = { " perms " : { } }
)
2018-11-08 19:45:21 +00:00
def test_z_misc_001_suggest_ix ( self ) :
# test exchange suggestions
2019-12-05 16:57:52 +00:00
data = self . make_data_ix (
org_id = settings . SUGGEST_ENTITY_ORG , suggest = True , prefix = self . get_prefix4 ( )
)
2018-11-08 19:45:21 +00:00
2019-12-05 16:57:52 +00:00
r_data = self . assert_create (
self . db_user , " ix " , data , ignore = [ " prefix " , " suggest " ]
)
2018-11-08 19:45:21 +00:00
self . assertEqual ( r_data [ " org_id " ] , settings . SUGGEST_ENTITY_ORG )
self . assertEqual ( r_data [ " status " ] , " pending " )
ix = InternetExchange . objects . get ( id = r_data [ " id " ] )
self . assertEqual ( ix . org_id , settings . SUGGEST_ENTITY_ORG )
2019-12-05 16:57:52 +00:00
data = self . make_data_ix (
org_id = settings . SUGGEST_ENTITY_ORG , suggest = True , prefix = self . get_prefix4 ( )
)
2018-11-08 19:45:21 +00:00
2019-12-05 16:57:52 +00:00
r_data = self . assert_create (
self . db_guest ,
" ix " ,
data ,
ignore = [ " prefix " , " suggest " ] ,
test_success = False ,
test_failures = { " perms " : { } } ,
)
2018-11-08 19:45:21 +00:00
def test_z_misc_001_suggest_outside_of_post ( self ) :
# The `suggest` keyword should only be allowed for
# `POST` events
for reftag in [ " ix " , " fac " , " net " ] :
ent = SHARED [ " {} _rw_ok " . format ( reftag ) ]
org_id = ent . org_id
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
reftag ,
ent . id ,
{ " notes " : " bla " } ,
test_failures = { " invalid " : { " suggest " : True } } ,
)
2018-11-08 19:45:21 +00:00
ent . refresh_from_db ( )
self . assertEqual ( ent . org_id , org_id )
def test_z_misc_001_fac_address_geocode ( self ) :
# test that facility gets marked for geocode sync after address field
# change
fac = SHARED [ " fac_rw_ok " ]
fac . geocode_status = True
fac . save ( )
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin , " fac " , fac . id , { " address1 " : " This is a test " }
)
2018-11-08 19:45:21 +00:00
fac . refresh_from_db ( )
self . assertEqual ( fac . geocode_status , False )
# reset geocode status
fac . geocode_status = True
fac . save ( )
# test that facility does NOT get marked for geocode sync after non relevant
# fields are changed
2019-12-05 16:57:52 +00:00
self . assert_update (
self . db_org_admin ,
" fac " ,
fac . id ,
{ " website " : " http://example.com " , " name " : fac . name + " Geocode Test " } ,
)
2018-11-08 19:45:21 +00:00
fac . refresh_from_db ( )
self . assertEqual ( fac . geocode_status , True )
2020-02-05 21:26:21 -06:00
def test_z_misc_001_api_errors ( self ) :
"""
Test empty POST , PUT data error response
Test parse error POST , PUT data error response
"""
for reftag in list ( REFTAG_MAP . keys ( ) ) :
self . _test_z_misc_001_api_errors ( reftag , " post " , " create " )
self . _test_z_misc_001_api_errors ( reftag , " put " , " update " )
def _test_z_misc_001_api_errors ( self , reftag , method , action ) :
factory = APIRequestFactory ( )
url = " / {} / " . format ( reftag )
view_action = { method : action }
view = NetworkViewSet . as_view ( view_action )
fn = getattr ( factory , method )
ERR_PARSE = " Data supplied with the {} request could not be parsed: JSON parse error - Expecting value: line 1 column 1 (char 0) " . format (
method . upper ( )
)
ERR_MISSING = " No data was supplied with the {} request " . format ( method . upper ( ) )
# test posting invalid json error
request = fn ( url , " in { valid json " , content_type = " application/json " )
response = view ( request )
response . render ( )
assert json . loads ( response . content ) [ " meta " ] [ " error " ] == ERR_PARSE
# test posting empty json error
request = fn ( " /net/ " , " {} " , content_type = " application/json " )
response = view ( request )
response . render ( )
assert json . loads ( response . content ) [ " meta " ] [ " error " ] == ERR_MISSING
# test posting empty json error
request = fn ( " /net/ " , " " , content_type = " application/json " )
response = view ( request )
response . render ( )
assert json . loads ( response . content ) [ " meta " ] [ " error " ] == ERR_MISSING
2018-11-08 19:45:21 +00:00
class Command ( BaseCommand ) :
help = " This runs the api test harness. All write ops are performed under an organization specifically made for testing, so running to against a prod environment should be fine in theory. "
def add_arguments ( self , parser ) :
parser . add_argument ( " --only " , help = " only run this test " , dest = " only " )
2019-12-05 16:57:52 +00:00
parser . add_argument (
" --setup " ,
help = " runs api test setup (user, org create) only " ,
dest = " setup " ,
action = " store_true " ,
)
2018-11-08 19:45:21 +00:00
@classmethod
def log ( cls , msg ) :
2020-01-08 13:29:58 -06:00
print ( msg )
2018-11-08 19:45:21 +00:00
@classmethod
2019-12-05 16:57:52 +00:00
def create_entity (
cls , model , prefix = " rw " , unset = [ ] , key_suffix = None , name_suffix = None , * * kwargs
) :
2018-11-08 19:45:21 +00:00
tag = model . handleref . tag
status = kwargs . get ( " status " , " ok " )
name = " API Test: %s : %s : %s " % ( tag . upper ( ) , prefix . upper ( ) , status )
if name_suffix :
name = " %s %s " % ( name , name_suffix )
data = { " status " : status }
if tag in [ " ix " , " net " , " fac " , " org " ] :
data [ " name " ] = name
2019-05-02 18:16:44 +00:00
if tag == " ixpfx " :
if kwargs . get ( " protocol " , 4 ) == 4 :
data [ " prefix " ] = PREFIXES_V4 [ model . objects . all ( ) . count ( ) ]
elif kwargs . get ( " protocol " ) == 6 :
data [ " prefix " ] = PREFIXES_V6 [ model . objects . all ( ) . count ( ) ]
2018-11-08 19:45:21 +00:00
data . update ( * * kwargs )
try :
obj = model . objects . get ( * * data )
cls . log (
" %s with status ' %s ' for %s testing already exists, skipping! "
2019-12-05 16:57:52 +00:00
% ( tag . upper ( ) , status , prefix . upper ( ) )
)
2018-11-08 19:45:21 +00:00
except model . DoesNotExist :
fn = getattr ( TestJSON , " make_data_ %s " % tag , None )
if fn :
data = fn ( * * data )
for k in unset :
if k in data :
del data [ k ]
2020-02-05 21:25:25 -06:00
obj = model ( * * data )
obj . save ( )
2019-12-05 16:57:52 +00:00
cls . log (
" %s with status ' %s ' for %s testing created! ( %s ) "
% ( tag . upper ( ) , status , prefix . upper ( ) , obj . updated )
)
2018-11-08 19:45:21 +00:00
id = " %s _ %s _ %s " % ( tag , prefix , status )
if key_suffix :
id = " %s _ %s " % ( id , key_suffix )
SHARED [ id ] = obj
return obj
@classmethod
def create_user ( cls , USER ) :
try :
user = User . objects . get ( username = USER . get ( " user " ) )
cls . log ( " USER ' %s ' already exists, skipping! " % USER . get ( " user " ) )
user . groups . clear ( )
user . userpermission_set . all ( ) . delete ( )
except User . DoesNotExist :
user = User . objects . create ( username = USER . get ( " user " ) )
user . set_password ( USER . get ( " password " ) )
user . save ( )
cls . log ( " USER ' %s ' created! " % USER . get ( " user " ) )
return user
@classmethod
def prepare ( cls , * args , * * options ) :
cls . log ( " Running setup for API testing... " )
memberGroup = Group . objects . get ( name = " user " )
# create API test user
user = cls . create_user ( USER )
memberGroup . user_set . add ( user )
# create API test user org member
user_org_member = cls . create_user ( USER_ORG_MEMBER )
memberGroup . user_set . add ( user_org_member )
# create API test user org member
user_org_admin = cls . create_user ( USER_ORG_ADMIN )
memberGroup . user_set . add ( user_org_admin )
# create API test user for crud testing
crud_users = { }
2020-01-08 13:29:58 -06:00
for p , specs in list ( USER_CRUD . items ( ) ) :
2018-11-08 19:45:21 +00:00
crud_user = cls . create_user ( specs )
crud_users [ p ] = crud_user
memberGroup . user_set . add ( crud_user )
# see if we need to create extra organizations (to fill up the
# database)
extra_orgs = getattr ( cls , " create_extra_orgs " , 0 )
i = 0
while i < extra_orgs :
cls . create_entity ( Organization , prefix = " r_ %d " % i , status = " ok " )
i + = 1
# create API test organization (read & write)
try :
org_rw = Organization . objects . get ( name = ORG_RW )
cls . log ( " ORG for WRITE testing already exists, skipping! " )
except Organization . DoesNotExist :
org_rw = Organization . objects . create ( status = " ok " , name = ORG_RW )
cls . log ( " ORG for WRITE testing created! " )
org_rw . admin_usergroup . user_set . add ( user_org_admin )
2020-01-08 13:29:58 -06:00
for crud_user in list ( crud_users . values ( ) ) :
2018-11-08 19:45:21 +00:00
org_rw . usergroup . user_set . add ( crud_user )
SHARED [ " org_id " ] = org_rw . id
SHARED [ " org_rw " ] = SHARED [ " org_rw_ok " ] = org_rw
# create API test organization (read & write) - status pending
try :
org_rwp = Organization . objects . get ( name = ORG_RW_PENDING )
cls . log (
" ORG for WRITE testing (with status pending) already exists, skipping! "
)
except Organization . DoesNotExist :
2019-12-05 16:57:52 +00:00
org_rwp = Organization . objects . create ( status = " pending " , name = ORG_RW_PENDING )
2018-11-08 19:45:21 +00:00
cls . log ( " ORG for WRITE testing (with status pending) created! " )
org_rwp . admin_usergroup . user_set . add ( user_org_admin )
SHARED [ " org_rwp " ] = SHARED [ " org_rw_pending " ] = org_rwp
# create API test organization (read only)
try :
org_r = Organization . objects . get ( name = ORG_R )
cls . log ( " ORG for READONLY testing already exists, skipping! " )
except Organization . DoesNotExist :
org_r = Organization . objects . create ( name = ORG_R , status = " ok " )
cls . log ( " ORG for READONLY testing created! " )
org_r . usergroup . user_set . add ( user_org_member )
SHARED [ " org_r " ] = SHARED [ " org_r_ok " ] = org_r
cls . create_entity ( Organization , prefix = " r " , status = " pending " )
# create API test network (for status "deleted" tests)
try :
net_rd = Network . objects . get ( name = NET_R_DELETED , org_id = org_r . id )
2019-12-05 16:57:52 +00:00
cls . log ( " NET for status ' deleted ' testing already exists, skipping! " )
2018-11-08 19:45:21 +00:00
except Network . DoesNotExist :
2019-12-05 16:57:52 +00:00
net_rd = Network . objects . create (
* * TestJSON . make_data_net ( name = NET_R_DELETED , org_id = org_r . id )
)
2018-11-08 19:45:21 +00:00
cls . log ( " NET for status ' deleted ' testing created! " )
net_rd . delete ( )
SHARED [ " net_rd " ] = net_rd
# create various entities for rw testing
for model in [ Network , Facility , InternetExchange ] :
for status in [ " ok " , " pending " ] :
for prefix in [ " r " , " rw " ] :
cls . create_entity (
2019-12-05 16:57:52 +00:00
model ,
status = status ,
prefix = prefix ,
org_id = SHARED [ " org_ %s _ %s " % ( prefix , status ) ] . id ,
)
2018-11-08 19:45:21 +00:00
cls . create_entity (
2019-12-05 16:57:52 +00:00
model ,
status = status ,
prefix = " %s 2 " % prefix ,
org_id = SHARED [ " org_ %s _ %s " % ( prefix , status ) ] . id ,
)
cls . create_entity (
model ,
status = status ,
prefix = " %s 3 " % prefix ,
org_id = SHARED [ " org_ %s _ %s " % ( prefix , status ) ] . id ,
)
2018-11-08 19:45:21 +00:00
# create entities for duplicate validation testing
for model in [ Network , Facility , InternetExchange ] :
2019-12-05 16:57:52 +00:00
cls . create_entity (
model ,
status = " deleted " ,
prefix = " rw_dupe " ,
name_suffix = " DUPE " ,
org_id = SHARED [ " org_rw_ok " ] . id ,
)
cls . create_entity (
model ,
status = " ok " ,
prefix = " rw_dupe " ,
name_suffix = " DUPE ! " ,
org_id = SHARED [ " org_rw_ok " ] . id ,
)
2018-11-08 19:45:21 +00:00
2020-07-15 02:07:01 -05:00
visibility = {
" rw " : " Public " ,
" rw2 " : " Users " ,
" rw3 " : " Private " ,
" r " : " Public " ,
" r2 " : " Users " ,
" r3 " : " Private " ,
}
2018-11-08 19:45:21 +00:00
for status in [ " ok " , " pending " ] :
2020-07-15 02:07:01 -05:00
for prefix in [ " r " , " r2 " , " r3 " , " rw " , " rw2 " , " rw3 " ] :
ixlan = SHARED [ " ixlan_ {} _ {} " . format ( prefix , status ) ] = SHARED [
2020-02-05 21:25:25 -06:00
" ix_ {} _ {} " . format ( prefix , status )
] . ixlan
2020-07-15 02:07:01 -05:00
if prefix in visibility :
visible = visibility [ prefix ]
ixlan . ixf_ixp_member_list_url_visible = visible
ixlan . ixf_ixp_member_list_url = " http://localhost "
ixlan . save ( )
2020-02-05 21:25:25 -06:00
for status in [ " ok " , " pending " ] :
for prefix in [ " r " , " rw " ] :
2018-11-08 19:45:21 +00:00
cls . create_entity (
IXLanPrefix ,
status = status ,
prefix = prefix ,
2019-05-02 18:16:44 +00:00
protocol = 4 ,
ixlan_id = SHARED [ " ixlan_ %s _ %s " % ( prefix , status ) ] . id ,
)
cls . create_entity (
IXLanPrefix ,
status = status ,
prefix = " {} _v6 " . format ( prefix ) ,
protocol = 6 ,
2018-11-08 19:45:21 +00:00
ixlan_id = SHARED [ " ixlan_ %s _ %s " % ( prefix , status ) ] . id ,
)
cls . create_entity (
2019-12-05 16:57:52 +00:00
InternetExchangeFacility ,
status = status ,
prefix = prefix ,
2018-11-08 19:45:21 +00:00
facility_id = SHARED [ " fac_ %s _ %s " % ( prefix , status ) ] . id ,
2019-12-05 16:57:52 +00:00
ix_id = SHARED [ " ix_ %s _ %s " % ( prefix , status ) ] . id ,
)
2018-11-08 19:45:21 +00:00
cls . create_entity (
2019-12-05 16:57:52 +00:00
NetworkFacility ,
status = status ,
prefix = prefix ,
unset = [ " net_id " ] ,
facility_id = SHARED [ " fac_ %s _ %s " % ( prefix , status ) ] . id ,
network_id = SHARED [ " net_ %s _ %s " % ( prefix , status ) ] . id ,
)
2018-11-08 19:45:21 +00:00
cls . create_entity (
2019-12-05 16:57:52 +00:00
NetworkIXLan ,
status = status ,
prefix = prefix ,
unset = [ " net_id " ] ,
ixlan_id = SHARED [ " ixlan_ %s _ %s " % ( prefix , status ) ] . id ,
network_id = SHARED [ " net_ %s _ %s " % ( prefix , status ) ] . id ,
)
2018-11-08 19:45:21 +00:00
for v in [ " Private " , " Users " , " Public " ] :
2019-12-05 16:57:52 +00:00
cls . create_entity (
NetworkContact ,
status = status ,
prefix = prefix ,
visible = v ,
network_id = SHARED [ " net_ %s _ %s " % ( prefix , status ) ] . id ,
unset = [ " net_id " ] ,
key_suffix = v . lower ( ) ,
)
2018-11-08 19:45:21 +00:00
# set up permissions for crud permission tests
crud_users [ " delete " ] . userpermission_set . create (
namespace = SHARED [ " net_rw3_ok " ] . nsp_namespace ,
2019-12-05 16:57:52 +00:00
permissions = PERM_READ | PERM_DELETE ,
)
2018-11-08 19:45:21 +00:00
crud_users [ " create " ] . userpermission_set . create (
namespace = SHARED [ " net_rw3_ok " ] . nsp_namespace ,
2019-12-05 16:57:52 +00:00
permissions = PERM_READ | PERM_CREATE ,
)
2018-11-08 19:45:21 +00:00
crud_users [ " update " ] . userpermission_set . create (
namespace = SHARED [ " net_rw3_ok " ] . nsp_namespace ,
2019-12-05 16:57:52 +00:00
permissions = PERM_READ | PERM_UPDATE ,
)
2018-11-08 19:45:21 +00:00
# undelete in case they got flagged as deleted
2020-01-08 13:29:58 -06:00
for name , obj in list ( SHARED . items ( ) ) :
2019-12-05 16:57:52 +00:00
if (
hasattr ( obj , " status " )
and obj . status == " deleted "
and obj != net_rd
and getattr ( obj , " name " , " " ) . find ( " DUPE " ) == - 1
) :
2018-11-08 19:45:21 +00:00
obj . status = " ok "
obj . save ( )
2019-12-05 16:57:52 +00:00
Organization . objects . create (
name = " Suggested Entitites " , status = " ok " , id = settings . SUGGEST_ENTITY_ORG
)
2018-11-08 19:45:21 +00:00
cls . log ( " Setup for API testing completed! " )
@classmethod
def cleanup ( cls , * args , * * options ) :
cls . log ( " Cleaning up... " )
deleted = 0
2020-01-08 13:29:58 -06:00
for k , obj in list ( SHARED . items ( ) ) :
2018-11-08 19:45:21 +00:00
if hasattr ( obj , " delete " ) :
# print "HARD deleting ", obj
try :
obj . delete ( hard = True )
deleted + = 1
except AssertionError :
pass
elif k [ - 3 : ] == " _id " :
reftag = re . match ( " ^(.+)_id$ " , k ) . group ( 1 )
cls = REFTAG_MAP . get ( reftag )
if cls :
try :
inst = cls . objects . get ( id = obj )
# print "HARD deleting ",inst
deleted + = 1
inst . delete ( )
except cls . DoesNotExist :
pass
2020-01-08 13:29:58 -06:00
print ( " Deleted " , deleted , " objects " )
2018-11-08 19:45:21 +00:00
def handle ( self , * args , * * options ) :
try :
self . prepare ( )
2020-01-08 13:29:58 -06:00
except IntegrityError as inst :
print ( inst )
2018-11-08 19:45:21 +00:00
self . cleanup ( )
2020-01-08 13:29:58 -06:00
print ( " Cleaned up after inegrity error, please try again .. " )
2018-11-08 19:45:21 +00:00
return
2019-12-05 16:57:52 +00:00
if options [ " setup " ] :
2018-11-08 19:45:21 +00:00
return
2019-12-05 16:57:52 +00:00
if not options [ " only " ] :
2018-11-08 19:45:21 +00:00
suite = unittest . TestLoader ( ) . loadTestsFromTestCase ( TestJSON )
else :
only = options [ " only " ] . split ( " , " )
funcs = [ ]
2020-01-08 13:29:58 -06:00
for key in list ( vars ( TestJSON ) . keys ( ) ) :
2018-11-08 19:45:21 +00:00
for o in only :
if key [ : 5 ] == " test_ " and key . find ( o ) > - 1 :
funcs . append (
" peeringdb_server.management.commands.pdb_api_test.TestJSON. %s "
2019-12-05 16:57:52 +00:00
% key
)
2018-11-08 19:45:21 +00:00
funcs = sorted ( funcs )
suite = unittest . TestLoader ( ) . loadTestsFromNames ( funcs )
unittest . TextTestRunner ( verbosity = 2 ) . run ( suite )
self . cleanup ( )