1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/tests/test_api_mfa.py
Stefan Pratter 5eb925e319 Support 202301 (#1329)
* fix next redirect when using U2F 2FA auth (#1191)

* Added self identifier to API

* fix migrations hierarchy after merging in previous support branch

* campus object

Co-authored-by: Stefan Pratter <stefan@20c.com>

* fix out of bound error message
add city / country to campus view

* fix tests

* relock poetry

* linting

* linting

* fix docs regen

* regen docs

* linting

* refactor self entity view to support carrier and campus object types and also make it easier to support additional object types in the future

* remove debug message

---------

Co-authored-by: Gajanan Patil <dipaksavaliya.python@gmail.com>
2023-02-15 07:55:01 +00:00

93 lines
2.6 KiB
Python

from base64 import b64encode
import pytest
from django_security_keys.models import SecurityKey
from rest_framework.test import APIClient
from peeringdb_server.models import Network, Organization, User
from .util import reset_group_ids
@pytest.mark.django_db
def test_mfa_basic_auth_block_writes():
reset_group_ids()
user = User.objects.create_user(
username="user", password="password", email="user@localhost"
)
org = Organization.objects.create(name="Test", status="ok")
net = Network.objects.create(name="Test", asn=63311, status="ok", org=org)
net_2 = Network.objects.create(name="Test 2", asn=63312, status="ok", org=org)
user.set_verified()
org.admin_usergroup.user_set.add(user)
client = APIClient()
basic_auth = b64encode(b"user:password").decode("ascii")
client.credentials(HTTP_AUTHORIZATION=f"Basic {basic_auth}")
# test 1: no MFA added, POST, PUT, DELETE should return BadRequest as we
# are supplying no data
response = client.post("/api/net", data={})
assert response.status_code == 400
response = client.put("/api/net/1", data={})
assert response.status_code == 400
response = client.delete("/api/net/2", data={})
assert response.status_code == 204
# test 2: add MFA, POST PUT DELETE should return permission error
SecurityKey.objects.create(
name="test",
type="security-key",
user=user,
credential_id="1234",
credential_public_key="deadbeef",
)
response = client.post("/api/net", data={})
assert response.status_code == 403
assert (
response.json()["meta"]["error"]
== "Cannot perform write operations with a MFA enabled account when authenticating with Basic authentication."
)
response = client.put("/api/net/1", data={})
assert response.status_code == 403
assert (
response.json()["meta"]["error"]
== "Cannot perform write operations with a MFA enabled account when authenticating with Basic authentication."
)
response = client.delete("/api/net/1", data={})
assert response.status_code == 403
assert (
response.json()["meta"]["error"]
== "Cannot perform write operations with a MFA enabled account when authenticating with Basic authentication."
)
# test 3: remove MFA
SecurityKey.objects.all().delete()
response = client.post("/api/net", data={})
assert response.status_code == 400
response = client.put("/api/net/1", data={})
assert response.status_code == 400
response = client.delete("/api/net/1", data={})
assert response.status_code == 204