slinky/tests/test_web.py

54 lines
1.5 KiB
Python
Raw Normal View History

2021-12-27 18:34:31 +00:00
"""
Test Slinky
"""
from typing import Any
from unittest import TestCase, mock
from slinky import web
2021-12-28 13:51:57 +00:00
2021-12-27 18:34:31 +00:00
@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)
2021-12-28 13:51:57 +00:00
@mock.patch(
'slinky.web.ShortURLForm',
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:
"""
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
"""
response = web.add()
self.assertEqual(response.status_code, 500)