slinky/tests/test_web.py
2021-12-29 14:39:20 +00:00

57 lines
1.7 KiB
Python

"""
Test Slinky
"""
from typing import Any
from unittest import TestCase, mock
from slinky import web
@mock.patch.dict('slinky.web.cfg', {'db': 'sqlite:///tests/test.db'})
class TestWeb(TestCase):
"""
Class to test Slinky code
"""
def test_simple_redirect(self, *_: Any) -> None:
"""
Ensure simple redirect works
"""
response = web.try_path_as_shortcode('egie')
self.assertEqual(response.status_code, 302)
self.assertEqual(response.location, 'https://example.com')
def test_fixed_views(self, *_: Any) -> None:
"""
Ensure simple redirect works
"""
response = web.try_path_as_shortcode('egig')
self.assertEqual(response.status_code, 404)
def test_expiry(self, *_: Any) -> None:
"""
Ensure simple redirect works
"""
response = web.try_path_as_shortcode('egif')
self.assertEqual(response.status_code, 404)
@mock.patch(
'slinky.web.AddForm',
return_value=mock.Mock(
shortcode=mock.Mock(data=''),
url=mock.Mock(data='https://example.com'),
fixed_views=mock.Mock(data=0),
expiry=mock.Mock(data='1970-01-01 00:00:00.000000'),
),
)
@mock.patch('slinky.random_string', return_value='egie')
def test_no_unique_shortcode(self, *_: Any) -> None:
"""
Ensure non-unique shortcode generation returns a 500 error
"""
request = mock.MagicMock()
request.headers = {'X-Forwarded-For': '127.0.0.1'}
with mock.patch("slinky.web.request", request):
response = web.add()
self.assertEqual(response.status_code, 500)