|
@@ -2,7 +2,7 @@ from misago.categories.models import Category
|
|
|
from misago.core.testutils import MisagoTestCase
|
|
|
|
|
|
from .. import testutils
|
|
|
-from ..utils import add_categories_to_threads
|
|
|
+from ..utils import add_categories_to_threads, get_thread_id_from_url
|
|
|
|
|
|
|
|
|
class AddCategoriesToThreadsTests(MisagoTestCase):
|
|
@@ -144,3 +144,117 @@ class AddCategoriesToThreadsTests(MisagoTestCase):
|
|
|
|
|
|
self.assertEqual(thread.top_category, self.category_a)
|
|
|
self.assertEqual(thread.category, self.category_d)
|
|
|
+
|
|
|
+
|
|
|
+class MockRequest(object):
|
|
|
+ def __init__(self, scheme, host, wsgialias=''):
|
|
|
+ self.scheme = scheme
|
|
|
+ self.host = host
|
|
|
+
|
|
|
+ self.path_info = '/api/threads/123/merge/'
|
|
|
+ self.path = '{}'.format(wsgialias.rstrip('/'), self.path_info)
|
|
|
+
|
|
|
+ def get_host(self):
|
|
|
+ return self.host
|
|
|
+
|
|
|
+ def is_secure(self):
|
|
|
+ return self.scheme == 'https'
|
|
|
+
|
|
|
+
|
|
|
+class GetThreadIdFromUrlTests(MisagoTestCase):
|
|
|
+ def test_get_thread_id_from_valid_urls(self):
|
|
|
+ """get_thread_id_from_url extracts thread pk from valid urls"""
|
|
|
+ TEST_CASES = (
|
|
|
+ {
|
|
|
+ # perfect match
|
|
|
+ 'request': MockRequest('https', 'testforum.com', '/discuss/'),
|
|
|
+ 'url': 'https://testforum.com/discuss/thread/test-thread-123/',
|
|
|
+ 'pk': 123
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # we don't validate scheme in case site recently moved to https
|
|
|
+ # but user still has old url's saved somewhere
|
|
|
+ 'request': MockRequest('http', 'testforum.com', '/discuss/'),
|
|
|
+ 'url': 'http://testforum.com/discuss/thread/test-thread-432/post/12321/',
|
|
|
+ 'pk': 432
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # we can extract thread id from other thread urls
|
|
|
+ 'request': MockRequest('https', 'testforum.com', '/discuss/'),
|
|
|
+ 'url': 'http://testforum.com/discuss/thread/test-thread-432/post/12321/',
|
|
|
+ 'pk': 432
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # we can extract thread id from thread page url
|
|
|
+ 'request': MockRequest('http', 'testforum.com', '/discuss/'),
|
|
|
+ 'url': 'http://testforum.com/discuss/thread/test-thread-432/123/',
|
|
|
+ 'pk': 432
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # we can extract thread id from thread last post url with relative schema
|
|
|
+ 'request': MockRequest('http', 'testforum.com', '/discuss/'),
|
|
|
+ 'url': '//testforum.com/discuss/thread/test-thread-18/last/',
|
|
|
+ 'pk': 18
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # we can extract thread id from url that lacks scheme
|
|
|
+ 'request': MockRequest('http', 'testforum.com', ''),
|
|
|
+ 'url': 'testforum.com/thread/test-thread-12/last/',
|
|
|
+ 'pk': 12
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # we can extract thread id from schemaless thread last post url
|
|
|
+ 'request': MockRequest('http', 'testforum.com', '/discuss/'),
|
|
|
+ 'url': 'testforum.com/discuss/thread/test-thread-18/last/',
|
|
|
+ 'pk': 18
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # we can extract thread id from url that lacks scheme and hostname
|
|
|
+ 'request': MockRequest('http', 'testforum.com', ''),
|
|
|
+ 'url': '/thread/test-thread-13/',
|
|
|
+ 'pk': 13
|
|
|
+ }
|
|
|
+ )
|
|
|
+
|
|
|
+ for case in TEST_CASES:
|
|
|
+ pk = get_thread_id_from_url(case['request'], case['url'])
|
|
|
+ self.assertEqual(
|
|
|
+ pk, case['pk'], 'get_thread_id_from_url for {} should return {}'.format(case['url'], case['pk']))
|
|
|
+
|
|
|
+ def test_get_thread_id_from_invalid_urls(self):
|
|
|
+ TEST_CASES = (
|
|
|
+ {
|
|
|
+ # invalid wsgi alias
|
|
|
+ 'request': MockRequest('https', 'testforum.com'),
|
|
|
+ 'url': 'http://testforum.com/discuss/thread/test-thread-123/'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # invalid hostname
|
|
|
+ 'request': MockRequest('http', 'misago-project.org', '/discuss/'),
|
|
|
+ 'url': 'https://testforum.com/discuss/thread/test-thread-432/post/12321/'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # non-thread url
|
|
|
+ 'request': MockRequest('http', 'testforum.com'),
|
|
|
+ 'url': 'https://testforum.com/user/bobboberson-123/'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # rubbish url
|
|
|
+ 'request': MockRequest('http', 'testforum.com'),
|
|
|
+ 'url': 'asdsadsasadsaSA&das8as*S(A*sa'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # blank url
|
|
|
+ 'request': MockRequest('http', 'testforum.com'),
|
|
|
+ 'url': '/'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ # empty url
|
|
|
+ 'request': MockRequest('http', 'testforum.com'),
|
|
|
+ 'url': ''
|
|
|
+ }
|
|
|
+ )
|
|
|
+
|
|
|
+ for case in TEST_CASES:
|
|
|
+ pk = get_thread_id_from_url(case['request'], case['url'])
|
|
|
+ self.assertIsNone(pk, 'get_thread_id_from_url for {} should fail'.format(case['url']))
|