mirror of
https://github.com/peeringdb/peeringdb.git
synced 2024-05-11 05:55:09 +00:00
* use new peeringdb client (1.0.0) for pdb_load_data sync (#599) * drop django-mobi for lack of py3/dj2 support (#492) remove django-forms-bootstrap for lack of py3/dj2 support (#492) * black formatted * django2.2 and py3 upgrade (#492) * drop ixlans (#21) ui and api changes * drop local_asn (#168) * org search (#193) * phone number validation (#50) * implement help text tooltips (#228) * Mark own ASN as transit-free (#394) * py3 fix for `pdb_migrate_ixlans` command when writing migration report * pdb_migrate_ixlans: properly handle py3 Runtime error if ixlan dict changes during iteration * set rest DEFAULT_SCHEMA_CLASS to coreapi to fix swagger apidocs fix migration 0027 missing from facsimile manifest * fix swagger doc strings * fix tests that were broken from api doc fixes * fix UniqueFieldValidator for netixlan ipaddress validation that broke during django/drf upgrade * fix org merge tool layout issues * travis config * update pipfile and lock * black formatting * update travis dist * beta mode banner (#411) * add beta banner template (#411) * automatically scheduled sync may not always be on, add a flag that lets us reflect that state in the beta banner message clean up beta banner implementation (#411) * add tests for beta banner (#411)
204 lines
6.4 KiB
Python
204 lines
6.4 KiB
Python
import pytest
|
|
import json
|
|
import uuid
|
|
|
|
from django.test import Client, TestCase, RequestFactory
|
|
from django.contrib.auth.models import Group, AnonymousUser
|
|
from django.contrib.auth import get_user
|
|
import django_namespace_perms as nsp
|
|
|
|
import peeringdb_server.views as views
|
|
import peeringdb_server.models as models
|
|
|
|
|
|
class ViewTestCase(TestCase):
|
|
|
|
entities = ["ix", "net", "fac"]
|
|
|
|
@classmethod
|
|
def setUpTestData(cls):
|
|
# create user and guest group
|
|
|
|
guest_group = Group.objects.create(name="guest")
|
|
user_group = Group.objects.create(name="user")
|
|
|
|
cls.guest_user = models.User.objects.create_user(
|
|
"guest", "guest@localhost", "guest"
|
|
)
|
|
cls.guest_user.set_password("guest")
|
|
guest_group.user_set.add(cls.guest_user)
|
|
|
|
nsp.models.GroupPermission.objects.create(
|
|
group=guest_group, namespace="peeringdb.organization", permissions=0x01
|
|
)
|
|
|
|
nsp.models.GroupPermission.objects.create(
|
|
group=user_group, namespace="peeringdb.organization", permissions=0x01
|
|
)
|
|
|
|
nsp.models.GroupPermission.objects.create(
|
|
group=user_group,
|
|
namespace="peeringdb.organization.*.network.*.poc_set.users",
|
|
permissions=0x01,
|
|
)
|
|
|
|
nsp.models.GroupPermission.objects.create(
|
|
group=guest_group,
|
|
namespace="peeringdb.organization.*.network.*.poc_set.public",
|
|
permissions=0x01,
|
|
)
|
|
|
|
# create test users
|
|
for name in [
|
|
"org_admin",
|
|
"user_a",
|
|
"user_b",
|
|
"user_c",
|
|
"user_d",
|
|
"user_e",
|
|
"user_f",
|
|
]:
|
|
setattr(
|
|
cls,
|
|
name,
|
|
models.User.objects.create_user(name, "%s@localhost" % name, name),
|
|
)
|
|
getattr(cls, name).set_password(name)
|
|
user_group.user_set.add(getattr(cls, name))
|
|
|
|
# create test org
|
|
cls.org = models.Organization.objects.create(name="Test org", status="ok")
|
|
cls.org_other = models.Organization.objects.create(
|
|
name="Test org other", status="ok"
|
|
)
|
|
|
|
# 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(
|
|
network=cls.net,
|
|
visible="Users",
|
|
name="Contact Users",
|
|
phone="12345",
|
|
email="a@a.a",
|
|
status="ok",
|
|
)
|
|
models.NetworkContact.objects.create(
|
|
network=cls.net,
|
|
visible="Public",
|
|
name="Contact Public",
|
|
phone="12345",
|
|
email="a@a.a",
|
|
status="ok",
|
|
)
|
|
models.NetworkContact.objects.create(
|
|
network=cls.net,
|
|
visible="Private",
|
|
name="Contact Private",
|
|
phone="12345",
|
|
email="a@a.a",
|
|
status="ok",
|
|
)
|
|
|
|
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)
|
|
self.assertEqual(resp.status_code, 200)
|
|
assert resp.status_code == 200
|
|
assert TEXT_NOT_LOGGED_IN in resp.content.decode("utf-8")
|
|
|
|
# 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)
|
|
assert resp.status_code == 200
|
|
assert TEXT_NOT_VERIFIED in resp.content.decode("utf-8")
|
|
|
|
# test #3 - user logged in
|
|
c = Client()
|
|
c.login(username="user_a", password="user_a")
|
|
resp = c.get("/net/%d" % self.net.id)
|
|
assert resp.status_code == 200
|
|
assert TEXT_NOT_LOGGED_IN not in resp.content.decode("utf-8")
|
|
assert TEXT_NOT_VERIFIED not in resp.content.decode("utf-8")
|
|
|
|
def test_search_asn_redirect(self):
|
|
"""
|
|
When the user types AS*** or ASN*** and hits enter, if
|
|
a result is found it should redirect directly to the result
|
|
"""
|
|
|
|
c = Client()
|
|
|
|
for q in ["as1", "asn1", "AS1", "ASN1"]:
|
|
resp = c.get("/search?q={}".format(q), follow=True)
|
|
self.assertEqual(resp.status_code, 200)
|
|
self.assertEqual(resp.redirect_chain, [("/net/1", 302)])
|