slinky/tests/test_web.py

70 lines
2.1 KiB
Python
Raw 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
from flask_bootstrap import Bootstrap # type: ignore[import]
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:
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()
Bootstrap(self.app)
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
"""
response = self.client.get('/egie')
2021-12-27 18:34:31 +00:00
self.assertEqual(response.status_code, 302)
self.assertEqual(response.location, 'https://example.com')
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
"""
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
"""
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
"""
with mock.patch('slinky.web.random_string', return_value='egie'):
response = self.client.get(
'/_/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
"""
with mock.patch('slinky.web.random_string', side_effect=['egie', 'egiz']):
response = self.client.get(
'/_/add',
headers={'x-forwarded-for': '127.0.0.1'},
)
self.assertEqual(response.status_code, 200)