Files
links/tests/test_links_api.py
T
2026-03-21 21:00:38 +11:00

66 lines
2.2 KiB
Python

"""
Integration tests for the Links feature.
Links are managed via Django template views, tested through the Django test client.
"""
import pytest
from links.models import Link
@pytest.mark.django_db
class TestLinksUI:
def test_list_page_returns_200(self, client):
response = client.get("/", follow=True)
assert response.status_code == 200
def test_create_link(self, client):
response = client.post(
"/create/",
{
"alias": "testlink",
"original_url": "https://example.com",
"link_type": Link.LinkType.LINK,
"description": "",
"text": "",
},
follow=True,
)
assert response.status_code == 200
assert Link.objects.filter(alias="testlink").exists()
def test_redirect_follows_alias(self, client):
Link.objects.create(alias="gh", original_url="https://github.com")
response = client.get("/gh/")
assert response.status_code == 302
assert response["Location"] == "https://github.com"
def test_redirect_unknown_alias_goes_to_create(self, client):
response = client.get("/definitely-not-a-real-alias/")
assert response.status_code == 302
assert "create" in response["Location"]
def test_delete_link(self, client):
link = Link.objects.create(alias="del-me", original_url="https://example.com")
response = client.post(f"/delete/{link.pk}/")
assert response.status_code in (200, 302)
assert not Link.objects.filter(pk=link.pk).exists()
def test_link_with_template_url(self, client):
Link.objects.create(
alias="gosearch",
original_url="https://google.com/search?q={query,default=hello}",
)
response = client.get("/gosearch/")
assert response.status_code == 302
assert "q=hello" in response["Location"]
def test_link_with_template_param_override(self, client):
Link.objects.create(
alias="wiki",
original_url="https://en.wikipedia.org/wiki/{topic,default=Python}",
)
response = client.get("/wiki/Django/")
assert response.status_code == 302
assert "Django" in response["Location"]