slinky/tests/test_web.py

68 lines
2 KiB
Python
Raw Permalink Normal View History

2021-12-27 18:34:31 +00:00
"""
Test Slinky web interface
2021-12-27 18:34:31 +00:00
"""
2021-12-27 18:34:31 +00:00
from unittest import TestCase, mock
from flask import Flask
2024-11-27 09:50:37 +00:00
from slinky.web import slinky_webapp
2021-12-27 18:34:31 +00:00
2021-12-28 13:51:57 +00:00
2021-12-27 18:34:31 +00:00
class TestWeb(TestCase):
"""
Class to test Slinky code
"""
def setUp(self) -> None:
2024-11-27 09:50:37 +00:00
self.app = Flask(__name__, template_folder="../templates")
self.app.register_blueprint(slinky_webapp)
self.app_context = self.app.app_context()
self.app_context.push()
self.client = self.app.test_client()
2024-11-27 09:50:37 +00:00
mock.patch.dict("slinky.web.cfg", {"db": "sqlite:///tests/test.db"}).start()
def test_simple_redirect(self) -> None:
2021-12-27 18:34:31 +00:00
"""
Ensure simple redirect works
"""
2024-11-27 09:50:37 +00:00
response = self.client.get("/egie")
2021-12-27 18:34:31 +00:00
self.assertEqual(response.status_code, 302)
2024-11-27 09:50:37 +00:00
self.assertEqual(response.location, "https://example.com")
2021-12-27 18:34:31 +00:00
def test_fixed_views(self) -> None:
2021-12-27 18:34:31 +00:00
"""
Ensure depleted fixed views returns a 404
2021-12-27 18:34:31 +00:00
"""
2024-11-27 09:50:37 +00:00
response = self.client.get("/egig")
2021-12-27 18:34:31 +00:00
self.assertEqual(response.status_code, 404)
def test_expiry(self) -> None:
2021-12-27 18:34:31 +00:00
"""
Ensure expired redirect returns a 404
2021-12-27 18:34:31 +00:00
"""
2024-11-27 09:50:37 +00:00
response = self.client.get("/egif")
2021-12-27 18:34:31 +00:00
self.assertEqual(response.status_code, 404)
2021-12-28 13:51:57 +00:00
def test_no_unique_shortcode(self) -> None:
2021-12-28 13:51:57 +00:00
"""
2021-12-28 15:13:45 +00:00
Ensure non-unique shortcode generation returns a 500 error
2021-12-28 13:51:57 +00:00
"""
2024-11-27 09:50:37 +00:00
with mock.patch("slinky.web.random_string", return_value="egie"):
response = self.client.get(
2024-11-27 09:50:37 +00:00
"/_/add", headers={"x-forwarded-for": "127.0.0.1"}
)
2021-12-28 13:51:57 +00:00
self.assertEqual(response.status_code, 500)
def test_conflicting_random_string(self) -> None:
"""
Test the condition where the random_string() returns an existing shortcode
"""
2024-11-27 09:50:37 +00:00
with mock.patch("slinky.web.random_string", side_effect=["egie", "egiz"]):
response = self.client.get(
2024-11-27 09:50:37 +00:00
"/_/add",
headers={"x-forwarded-for": "127.0.0.1"},
)
self.assertEqual(response.status_code, 200)