1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/tests/test_views.py
Matt Griswold 5147028bee clean up / format / poetry (#1000)
* stub in poetry for pipenv

* re-add tester image

* add pre-commit / formatting

* fix ghactions

* revert test data whitespace, exclude tests/data

* revert ws

* decruft, rm tox/pipenv

* install dev packages for base image

* add lgtm config to force to py3
2021-07-10 10:12:35 -05:00

136 lines
3.7 KiB
Python

import pytest
from django.test import Client
from rest_framework.test import APIClient
from peeringdb_server.models import (
Network,
Organization,
User,
UserOrgAffiliationRequest,
)
URL = "/affiliate-to-org"
@pytest.fixture
def client():
user = User.objects.create(
username="test",
email="test@localhost",
)
user.set_password("test1234")
user.save()
client = APIClient()
client.login(username="test", password="test1234")
return client
@pytest.fixture
def org():
org = Organization.objects.create(
name="Test Org",
)
return org
@pytest.fixture
def network(org):
net = Network.objects.create(name="test network", org=org, asn=123, status="ok")
return net
def assert_passing_affiliation_request(data, client):
response = client.post(URL, data)
assert response.status_code == 200
assert UserOrgAffiliationRequest.objects.count() == 1
def assert_failing_affiliation_request(data, client):
response = client.post(URL, data)
assert response.status_code == 400
assert "You already requested affiliation to this ASN/org" in str(response.content)
assert UserOrgAffiliationRequest.objects.count() == 1
"""
The following tests are for issue 931:
Limit the number of requests
for affiliation to an ASN/org to 1
"""
@pytest.mark.django_db
def test_affiliate_to_org_multiple(client, org):
assert_passing_affiliation_request({"org": org.id}, client)
assert_failing_affiliation_request({"org": org.id}, client)
@pytest.mark.django_db
def test_affiliate_to_asn_multiple(client, network):
assert_passing_affiliation_request({"asn": 123}, client)
assert_failing_affiliation_request({"asn": 123}, client)
@pytest.mark.django_db
def test_affiliate_to_org_then_asn(client, network, org):
assert_passing_affiliation_request({"org": org.id}, client)
assert_failing_affiliation_request({"asn": 123}, client)
@pytest.mark.django_db
def test_affiliate_to_asn_then_org(client, network, org):
assert_passing_affiliation_request({"org": org.id}, client)
assert_failing_affiliation_request({"asn": 123}, client)
@pytest.mark.django_db
def test_affiliate_to_org_id_takes_precedence_over_asn(client, org):
assert_passing_affiliation_request({"org": org.id, "asn": 2020}, client)
assert_failing_affiliation_request({"org": org.id, "asn": 2111}, client)
@pytest.mark.django_db
def test_affiliate_to_asn_takes_precendence_over_org_name(client, network, org):
assert_passing_affiliation_request({"org": "test name", "asn": 123}, client)
assert_failing_affiliation_request({"org": "different", "asn": 123}, client)
@pytest.mark.django_db
def test_affiliate_to_nonexisting_org_multiple(client):
"""
Multiple affiliations to nonexisting orgs should still get
caught if the provided org name is repetitive
"""
data = {
"org": "Nonexistent org",
}
assert_passing_affiliation_request(data, client)
assert_failing_affiliation_request(data, client)
# If we change the org name we can affiliate to that one as well
other_data = {
"org": "Second nonexistent org",
}
response = client.post(URL, other_data)
assert response.status_code == 200
assert UserOrgAffiliationRequest.objects.count() == 2
@pytest.mark.django_db
def test_adv_search_init():
client = Client()
response = client.get("/advanced_search")
assert response.status_code == 200
user = User.objects.create(
username="test",
email="test@localhost",
)
user.set_password("test1234")
user.save()
client.login(username="test", password="test1234")
response = client.get("/advanced_search")
assert response.status_code == 200