99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
import pytest
|
|
from django.utils import timezone
|
|
|
|
from dpdapi.models import Domain
|
|
|
|
FIELDS = {"id", "created", "updated", "name"}
|
|
|
|
|
|
def test_list(api, domain):
|
|
Domain.objects.create(name="other.org")
|
|
res = api.get("/api/domains/")
|
|
assert res.status_code == 200
|
|
assert sorted(d["name"] for d in res.json()) == ["example.com", "other.org"]
|
|
assert set(res.json()[0]) == FIELDS
|
|
|
|
|
|
def test_filter_by_name(api, domain):
|
|
Domain.objects.create(name="other.org")
|
|
res = api.get("/api/domains/", {"name": "other.org"})
|
|
assert [d["name"] for d in res.json()] == ["other.org"]
|
|
|
|
|
|
def test_retrieve(api, domain):
|
|
res = api.get(f"/api/domains/{domain.pk}/")
|
|
assert res.status_code == 200
|
|
assert res.json()["name"] == "example.com"
|
|
assert res.json()["id"] == domain.pk
|
|
|
|
|
|
def test_timestamps_are_local_iso8601(api, domain):
|
|
body = api.get(f"/api/domains/{domain.pk}/").json()
|
|
assert body["created"] == timezone.localtime(domain.created).isoformat()
|
|
assert body["updated"] == timezone.localtime(domain.updated).isoformat()
|
|
|
|
|
|
def test_retrieve_missing(api, db):
|
|
assert api.get("/api/domains/999/").status_code == 404
|
|
|
|
|
|
def test_create(api, db):
|
|
res = api.post("/api/domains/", {"name": "New.ORG"}, content_type="application/json")
|
|
assert res.status_code == 201
|
|
assert res.json()["name"] == "new.org"
|
|
assert set(res.json()) == FIELDS
|
|
assert str(Domain.objects.get()) == "new.org"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"data", [{}, {"name": ""}, {"name": "example.com"}, {"name": "Example.COM"}, {"name": "x" * 255}]
|
|
)
|
|
def test_create_invalid(api, domain, data):
|
|
res = api.post("/api/domains/", data, content_type="application/json")
|
|
assert res.status_code == 400
|
|
assert Domain.objects.count() == 1
|
|
|
|
|
|
def test_update(api, domain):
|
|
res = api.put(f"/api/domains/{domain.pk}/", {"name": "Renamed.com"}, content_type="application/json")
|
|
assert res.status_code == 200
|
|
assert res.json()["name"] == "renamed.com"
|
|
domain.refresh_from_db()
|
|
assert domain.name == "renamed.com"
|
|
|
|
|
|
def test_update_invalid(api, domain):
|
|
res = api.put(f"/api/domains/{domain.pk}/", {}, content_type="application/json")
|
|
assert res.status_code == 400
|
|
|
|
|
|
def test_partial_update(api, domain):
|
|
res = api.patch(f"/api/domains/{domain.pk}/", {"name": "patched.com"}, content_type="application/json")
|
|
assert res.status_code == 200
|
|
assert res.json()["name"] == "patched.com"
|
|
|
|
|
|
def test_partial_update_empty(api, domain):
|
|
res = api.patch(f"/api/domains/{domain.pk}/", {}, content_type="application/json")
|
|
assert res.status_code == 200
|
|
assert res.json()["name"] == "example.com"
|
|
|
|
|
|
def test_partial_update_null(api, domain):
|
|
res = api.patch(f"/api/domains/{domain.pk}/", {"name": None}, content_type="application/json")
|
|
assert res.status_code == 400
|
|
|
|
|
|
def test_update_missing(api, db):
|
|
res = api.put("/api/domains/999/", {"name": "x.com"}, content_type="application/json")
|
|
assert res.status_code == 404
|
|
|
|
|
|
def test_delete(api, domain):
|
|
assert api.delete(f"/api/domains/{domain.pk}/").status_code == 204
|
|
assert not Domain.objects.exists()
|
|
|
|
|
|
def test_delete_missing(api, db):
|
|
assert api.delete("/api/domains/999/").status_code == 404
|