1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/tests/test_maintenance.py

84 lines
2.5 KiB
Python
Raw Normal View History

2019-01-04 10:02:28 +00:00
import py.test
import pytest
import json
from rest_framework.test import APIClient
from django.test import Client, TestCase
from peeringdb_server import maintenance, settings
from peeringdb_server.models import REFTAG_MAP, User
import peeringdb_server.views as views
from .util import ClientCase
2019-01-04 10:02:28 +00:00
2019-12-05 16:57:52 +00:00
class TestMaintenanceMode(ClientCase):
2019-01-04 10:02:28 +00:00
@classmethod
def setUpTestData(cls):
super(TestMaintenanceMode, cls).setUpTestData()
2019-12-05 16:57:52 +00:00
cls.superuser = User.objects.create_user(
"su", "su@localhost", "su", is_superuser=True
)
cls.org = REFTAG_MAP["org"].objects.create(name="Test Org", status="ok")
2019-01-04 10:02:28 +00:00
@pytest.fixture(autouse=True)
def init_lockfile(self, tmpdir):
settings.MAINTENANCE_MODE_LOCKFILE = str(tmpdir.join("maintenance.lock"))
def test_signup(self):
"""
user signup should be blocked during maintenance
"""
maintenance.on()
client = Client()
resp = client.post("/register", data={})
assert resp.status_code == 503
assert "maintenance mode" in resp.content.decode()
2019-01-04 10:02:28 +00:00
maintenance.off()
def test_api(self):
"""
test that maintenance mode on blocks all write ops to the rest api
"""
# set maintenance on
maintenance.on()
# init api client
self.client = APIClient()
self.client.force_authenticate(self.superuser)
err_str = "in maintenance mode"
# GET requests should work as expected
r = self.client.get("/api/org/1", format="json")
content = json.loads(r.content)
assert r.status_code == 200
# POST should be blocked
2019-12-05 16:57:52 +00:00
r = self.client.post(
"/api/net", {"org_id": 1, "name": "Test net", "asn": 9000000}, format="json"
)
2019-01-04 10:02:28 +00:00
content = json.loads(r.content)
assert r.status_code == 503
assert err_str in content["meta"]["error"]
2019-01-04 10:02:28 +00:00
net = {"id": 1}
# PUT should be blocked
2019-12-05 16:57:52 +00:00
r = self.client.put("/api/net/{}".format(net["id"]), net, format="json")
2019-01-04 10:02:28 +00:00
content = json.loads(r.content)
assert r.status_code == 503
assert err_str in content["meta"]["error"]
2019-01-04 10:02:28 +00:00
# DELETE should be blocked
2019-12-05 16:57:52 +00:00
r = self.client.delete("/api/net/{}".format(net["id"]), {}, format="json")
2019-01-04 10:02:28 +00:00
content = json.loads(r.content)
assert r.status_code == 503
assert err_str in content["meta"]["error"]
2019-01-04 10:02:28 +00:00
# set maintenance mode off
maintenance.off()