slinky/tests/test_web.py

70 lines
2.1 KiB
Python

"""
Test Slinky web interface
"""
from unittest import TestCase, mock
from flask import Flask
from flask_bootstrap import Bootstrap # type: ignore[import]
from slinky.web import slinky_webapp
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:
"""
Ensure simple redirect works
"""
response = self.client.get('/egie')
self.assertEqual(response.status_code, 302)
self.assertEqual(response.location, 'https://example.com')
def test_fixed_views(self) -> None:
"""
Ensure depleted fixed views returns a 404
"""
response = self.client.get('/egig')
self.assertEqual(response.status_code, 404)
def test_expiry(self) -> None:
"""
Ensure expired redirect returns a 404
"""
response = self.client.get('/egif')
self.assertEqual(response.status_code, 404)
def test_no_unique_shortcode(self) -> None:
"""
Ensure non-unique shortcode generation returns a 500 error
"""
with mock.patch('slinky.web.random_string', return_value='egie'):
response = self.client.get(
'/_/add', headers={'x-forwarded-for': '127.0.0.1'}
)
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)