2018-11-08 19:45:21 +00:00
import json
import uuid
2021-07-10 10:12:35 -05:00
import pytest
2021-03-09 13:30:30 -06:00
from django . conf import settings
2018-11-08 19:45:21 +00:00
from django . contrib . auth import get_user
2021-07-10 10:12:35 -05:00
from django . contrib . auth . models import AnonymousUser , Group
from django . test import Client , RequestFactory , TestCase
from django_grainy . models import GroupPermission , UserPermission
2020-12-03 19:10:02 +00:00
2018-11-08 19:45:21 +00:00
import peeringdb_server . models as models
2021-07-10 10:12:35 -05:00
import peeringdb_server . views as views
from . util import ClientCase
2018-11-08 19:45:21 +00:00
2020-12-03 19:10:02 +00:00
class ViewTestCase ( ClientCase ) :
2018-11-08 19:45:21 +00:00
entities = [ " ix " , " net " , " fac " ]
@classmethod
def setUpTestData ( cls ) :
2021-07-10 10:12:35 -05:00
super ( ) . setUpTestData ( )
2018-11-08 19:45:21 +00:00
# create test users
for name in [
2019-12-05 16:57:52 +00:00
" org_admin " ,
" user_a " ,
" user_b " ,
" user_c " ,
" user_d " ,
" user_e " ,
" user_f " ,
2018-11-08 19:45:21 +00:00
] :
2019-12-05 16:57:52 +00:00
setattr (
cls ,
name ,
models . User . objects . create_user ( name , " %s @localhost " % name , name ) ,
)
2018-11-08 19:45:21 +00:00
getattr ( cls , name ) . set_password ( name )
2020-12-03 19:10:02 +00:00
cls . user_group . user_set . add ( getattr ( cls , name ) )
2018-11-08 19:45:21 +00:00
# create test org
2019-12-05 16:57:52 +00:00
cls . org = models . Organization . objects . create ( name = " Test org " , status = " ok " )
2018-11-08 19:45:21 +00:00
cls . org_other = models . Organization . objects . create (
2019-12-05 16:57:52 +00:00
name = " Test org other " , status = " ok "
)
2018-11-08 19:45:21 +00:00
# create test entities
for tag in cls . entities :
kwargs = { " name " : " Test %s " % tag , " status " : " ok " , " org " : cls . org }
if tag == " net " :
kwargs . update ( asn = 1 )
setattr ( cls , tag , models . REFTAG_MAP [ tag ] . objects . create ( * * kwargs ) )
# add org_admin user to org as admin
cls . org . admin_usergroup . user_set . add ( cls . org_admin )
# add user_a user to org as member
cls . org . usergroup . user_set . add ( cls . user_a )
cls . org_other . usergroup . user_set . add ( cls . user_b )
def setUp ( self ) :
self . factory = RequestFactory ( )
def run_view_test ( self , reftag ) :
id = getattr ( self , reftag ) . id
# test #1 - not logged in
c = Client ( )
resp = c . get ( " / %s / %d " % ( reftag , id ) , follow = True )
self . assertEqual ( resp . status_code , 200 )
# test #2 - guest logged in (not affiliated to any org)
c = Client ( )
c . login ( username = " guest " , password = " guest " )
resp = c . get ( " / %s / %d " % ( reftag , id ) , follow = True )
self . assertEqual ( resp . status_code , 200 )
# test #3 - user logged in
c = Client ( )
c . login ( username = " user_a " , password = " user_a " )
resp = c . get ( " / %s / %d " % ( reftag , id ) , follow = True )
self . assertEqual ( resp . status_code , 200 )
class TestExchangeView ( ViewTestCase ) :
def test_view ( self ) :
self . run_view_test ( " ix " )
class TestFacilityView ( ViewTestCase ) :
def test_view ( self ) :
self . run_view_test ( " fac " )
class TestOrgView ( ViewTestCase ) :
def test_view ( self ) :
self . run_view_test ( " org " )
class TestNetworkView ( ViewTestCase ) :
@classmethod
def setUpTestData ( cls ) :
ViewTestCase . setUpTestData ( )
# Create PoCs
models . NetworkContact . objects . create (
2019-12-05 16:57:52 +00:00
network = cls . net ,
visible = " Users " ,
name = " Contact Users " ,
phone = " 12345 " ,
email = " a@a.a " ,
status = " ok " ,
)
2018-11-08 19:45:21 +00:00
models . NetworkContact . objects . create (
2019-12-05 16:57:52 +00:00
network = cls . net ,
visible = " Public " ,
name = " Contact Public " ,
phone = " 12345 " ,
email = " a@a.a " ,
status = " ok " ,
)
2018-11-08 19:45:21 +00:00
models . NetworkContact . objects . create (
2019-12-05 16:57:52 +00:00
network = cls . net ,
visible = " Private " ,
name = " Contact Private " ,
phone = " 12345 " ,
email = " a@a.a " ,
status = " ok " ,
)
2018-11-08 19:45:21 +00:00
def test_view ( self ) :
self . run_view_test ( " net " )
def test_poc_notify ( self ) :
"""
Test that viewers are notified if PoCs are hidden from them
"""
TEXT_NOT_LOGGED_IN = " Some of this network ' s contacts are hidden because they are only visible to authenticated users and you are currently not logged in. "
TEXT_NOT_VERIFIED = " Some of this network ' s contacts are hidden because your user account is not affiliated with any organization. "
self . assertEqual ( models . NetworkContact . objects . all ( ) . count ( ) , 3 )
# test #1 - not logged in
c = Client ( )
resp = c . get ( " /net/ %d " % self . net . id , follow = True )
2021-02-05 13:41:37 +00:00
content = resp . content . decode ( " utf-8 " )
2018-11-08 19:45:21 +00:00
self . assertEqual ( resp . status_code , 200 )
2020-01-08 13:29:58 -06:00
assert resp . status_code == 200
2021-02-05 13:41:37 +00:00
assert TEXT_NOT_LOGGED_IN in content
assert " Contact Public " in content
assert " Contact Private " not in content
assert " Contact Users " not in content
2018-11-08 19:45:21 +00:00
# test #2 - guest logged in (not affiliated to any org)
c = Client ( )
c . login ( username = " guest " , password = " guest " )
resp = c . get ( " /net/ %d " % self . net . id )
2021-02-05 13:41:37 +00:00
content = resp . content . decode ( " utf-8 " )
2020-01-08 13:29:58 -06:00
assert resp . status_code == 200
2021-02-05 13:41:37 +00:00
assert TEXT_NOT_VERIFIED in content
assert " Contact Public " in content
assert " Contact Private " not in content
assert " Contact Users " not in content
2018-11-08 19:45:21 +00:00
# test #3 - user logged in
c = Client ( )
c . login ( username = " user_a " , password = " user_a " )
resp = c . get ( " /net/ %d " % self . net . id )
2021-02-05 13:41:37 +00:00
content = resp . content . decode ( " utf-8 " )
2020-01-08 13:29:58 -06:00
assert resp . status_code == 200
2021-02-05 13:41:37 +00:00
assert TEXT_NOT_LOGGED_IN not in content
assert TEXT_NOT_VERIFIED not in content
assert " Contact Public " in content
assert " Contact Private " in content
assert " Contact Users " in content
2018-11-08 19:45:21 +00:00
def test_search_asn_redirect ( self ) :
"""
2020-01-08 13:29:58 -06:00
When the user types AS * * * or ASN * * * and hits enter , if
2018-11-08 19:45:21 +00:00
a result is found it should redirect directly to the result
"""
c = Client ( )
for q in [ " as1 " , " asn1 " , " AS1 " , " ASN1 " ] :
2020-07-15 02:07:01 -05:00
resp = c . get ( f " /search?q= { q } " , follow = True )
2018-11-08 19:45:21 +00:00
self . assertEqual ( resp . status_code , 200 )
2021-07-10 10:12:35 -05:00
self . assertEqual ( resp . redirect_chain , [ ( f " /net/ { self . net . id } " , 302 ) ] )