2021-12-27 18:34:31 +00:00
|
|
|
"""
|
2022-05-16 10:45:39 +01:00
|
|
|
Test Slinky web interface
|
2021-12-27 18:34:31 +00:00
|
|
|
"""
|
2022-05-16 10:45:39 +01:00
|
|
|
|
2021-12-27 18:34:31 +00:00
|
|
|
from unittest import TestCase, mock
|
|
|
|
|
2022-05-16 10:45:39 +01:00
|
|
|
from flask import Flask
|
2024-11-27 09:50:37 +00:00
|
|
|
|
2022-05-16 10:45:39 +01: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
|
|
|
|
"""
|
|
|
|
|
2022-05-16 10:45:39 +01:00
|
|
|
def setUp(self) -> None:
|
2024-11-27 09:50:37 +00:00
|
|
|
self.app = Flask(__name__, template_folder="../templates")
|
2022-05-16 10:45:39 +01:00
|
|
|
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()
|
2022-05-16 10:45:39 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-05-16 10:45:39 +01:00
|
|
|
def test_fixed_views(self) -> None:
|
2021-12-27 18:34:31 +00:00
|
|
|
"""
|
2022-05-16 10:45:39 +01: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)
|
|
|
|
|
2022-05-16 10:45:39 +01:00
|
|
|
def test_expiry(self) -> None:
|
2021-12-27 18:34:31 +00:00
|
|
|
"""
|
2022-05-16 10:45:39 +01: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
|
|
|
|
2022-05-16 10:45:39 +01: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"):
|
2022-05-16 10:45:39 +01:00
|
|
|
response = self.client.get(
|
2024-11-27 09:50:37 +00:00
|
|
|
"/_/add", headers={"x-forwarded-for": "127.0.0.1"}
|
2022-05-16 10:45:39 +01:00
|
|
|
)
|
2021-12-28 13:51:57 +00:00
|
|
|
self.assertEqual(response.status_code, 500)
|
2022-05-16 10:45:39 +01:00
|
|
|
|
|
|
|
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"]):
|
2022-05-16 10:45:39 +01:00
|
|
|
response = self.client.get(
|
2024-11-27 09:50:37 +00:00
|
|
|
"/_/add",
|
|
|
|
headers={"x-forwarded-for": "127.0.0.1"},
|
2022-05-16 10:45:39 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|