1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-23 00:50:00 +00:00

Update tests, add tests structure for views

This commit is contained in:
Kfir Hadas 2017-07-15 02:49:23 +03:00
parent a758f5f078
commit d7d8d8ef14
11 changed files with 219 additions and 103 deletions

View file

@ -14,7 +14,6 @@ class NyaaTestCase(unittest.TestCase):
def setUpClass(cls): def setUpClass(cls):
app = create_app('config') app = create_app('config')
app.config['TESTING'] = True app.config['TESTING'] = True
cls.app_context = app.app_context()
# Use a seperate database for testing # Use a seperate database for testing
# if USE_MYSQL: # if USE_MYSQL:
@ -28,8 +27,11 @@ class NyaaTestCase(unittest.TestCase):
# app.config['USE_MYSQL'] = USE_MYSQL # app.config['USE_MYSQL'] = USE_MYSQL
# app.config['SQLALCHEMY_DATABASE_URI'] = db_uri # app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
cls.app = app
cls.app_context = app.app_context()
with cls.app_context: with cls.app_context:
cls.app = app.test_client() cls.client = app.test_client()
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):

View file

@ -18,14 +18,14 @@ class ApiHandlerTests(NyaaTestCase):
def test_no_authorization(self): def test_no_authorization(self):
""" Test that API is locked unless you're logged in """ """ Test that API is locked unless you're logged in """
rv = self.app.get('/api/info/1') rv = self.client.get('/api/info/1')
data = json.loads(rv.get_data()) data = json.loads(rv.get_data())
self.assertDictEqual({'errors': ['Bad authorization']}, data) self.assertDictEqual({'errors': ['Bad authorization']}, data)
@unittest.skip('Not yet implemented') @unittest.skip('Not yet implemented')
def test_bad_credentials(self): def test_bad_credentials(self):
""" Test that API is locked unless you're logged in """ """ Test that API is locked unless you're logged in """
rv = self.app.get('/api/info/1') rv = self.client.get('/api/info/1')
data = json.loads(rv.get_data()) data = json.loads(rv.get_data())
self.assertDictEqual({'errors': ['Bad authorization']}, data) self.assertDictEqual({'errors': ['Bad authorization']}, data)

View file

@ -1,55 +0,0 @@
import os
import unittest
import tempfile
import nyaa
class NyaaTestCase(unittest.TestCase):
nyaa_app = nyaa.create_app('config')
def setUp(self):
self.db, self.nyaa_app.config['DATABASE'] = tempfile.mkstemp()
self.nyaa_app.config['TESTING'] = True
self.app = self.nyaa_app.test_client()
with self.nyaa_app.app_context():
nyaa.db.create_all()
def tearDown(self):
os.close(self.db)
os.unlink(self.nyaa_app.config['DATABASE'])
def test_index_url(self):
rv = self.app.get('/')
assert b'Browse :: Nyaa' in rv.data
assert b'Guest' in rv.data
def test_upload_url(self):
rv = self.app.get('/upload')
assert b'Upload Torrent' in rv.data
assert b'You are not logged in, and are uploading anonymously.' in rv.data
def test_rules_url(self):
rv = self.app.get('/rules')
assert b'Site Rules' in rv.data
def test_help_url(self):
rv = self.app.get('/help')
assert b'Using the Site' in rv.data
def test_rss_url(self):
rv = self.app.get('/?page=rss')
assert b'/xmlns/nyaa' in rv.data
def test_login_url(self):
rv = self.app.get('/login')
assert b'Username or email address' in rv.data
def test_registration_url(self):
rv = self.app.get('/register')
assert b'Username' in rv.data
assert b'Password' in rv.data
if __name__ == '__main__':
unittest.main()

View file

@ -10,50 +10,9 @@ from nyaa.template_utils import (_jinja2_filter_rfc822, _jinja2_filter_rfc822_es
class TestTemplateUtils(NyaaTestCase): class TestTemplateUtils(NyaaTestCase):
# def setUp(self): @unittest.skip('Not yet implemented')
# self.db, nyaa.app.config['DATABASE'] = tempfile.mkstemp() def test_create_magnet_from_es_info(self):
# nyaa.app.config['TESTING'] = True pass
# self.app = nyaa.app.test_client()
# with nyaa.app.app_context():
# nyaa.db.create_all()
#
# def tearDown(self):
# os.close(self.db)
# os.unlink(nyaa.app.config['DATABASE'])
def test_filter_rfc822(self):
# test with timezone UTC
test_date = datetime.datetime(2017, 2, 15, 11, 15, 34, 100, datetime.timezone.utc)
self.assertEqual(_jinja2_filter_rfc822(test_date), 'Wed, 15 Feb 2017 11:15:34 -0000')
def test_filter_rfc822_es(self):
# test with local timezone
test_date_str = '2017-02-15T11:15:34'
# this is in order to get around local time zone issues
expected = formatdate(float(datetime.datetime(2017, 2, 15, 11, 15, 34, 100).timestamp()))
self.assertEqual(_jinja2_filter_rfc822_es(test_date_str), expected)
def test_get_utc_timestamp(self):
# test with local timezone
test_date_str = '2017-02-15T11:15:34'
self.assertEqual(get_utc_timestamp(test_date_str), 1487157334)
def test_get_display_time(self):
# test with local timezone
test_date_str = '2017-02-15T11:15:34'
self.assertEqual(get_display_time(test_date_str), '2017-02-15 11:15')
def test_timesince(self):
now = datetime.datetime.utcnow()
self.assertEqual(timesince(now), 'just now')
self.assertEqual(timesince(now - datetime.timedelta(seconds=5)), '5 seconds ago')
self.assertEqual(timesince(now - datetime.timedelta(minutes=1)), '1 minute ago')
self.assertEqual(
timesince(now - datetime.timedelta(minutes=38, seconds=43)), '38 minutes ago')
self.assertEqual(
timesince(now - datetime.timedelta(hours=2, minutes=38, seconds=51)), '2 hours ago')
bigger = now - datetime.timedelta(days=3)
self.assertEqual(timesince(bigger), bigger.strftime('%Y-%m-%d %H:%M UTC'))
@unittest.skip('Not yet implemented') @unittest.skip('Not yet implemented')
def test_static_cachebuster(self): def test_static_cachebuster(self):
@ -93,6 +52,40 @@ class TestTemplateUtils(NyaaTestCase):
self.assertEqual(category_name('1_100'), '???') self.assertEqual(category_name('1_100'), '???')
self.assertEqual(category_name('0_0'), '???') self.assertEqual(category_name('0_0'), '???')
def test_get_utc_timestamp(self):
# test with local timezone
test_date_str = '2017-02-15T11:15:34'
self.assertEqual(get_utc_timestamp(test_date_str), 1487157334)
def test_get_display_time(self):
# test with local timezone
test_date_str = '2017-02-15T11:15:34'
self.assertEqual(get_display_time(test_date_str), '2017-02-15 11:15')
def test_filter_rfc822(self):
# test with timezone UTC
test_date = datetime.datetime(2017, 2, 15, 11, 15, 34, 100, datetime.timezone.utc)
self.assertEqual(_jinja2_filter_rfc822(test_date), 'Wed, 15 Feb 2017 11:15:34 -0000')
def test_filter_rfc822_es(self):
# test with local timezone
test_date_str = '2017-02-15T11:15:34'
# this is in order to get around local time zone issues
expected = formatdate(float(datetime.datetime(2017, 2, 15, 11, 15, 34, 100).timestamp()))
self.assertEqual(_jinja2_filter_rfc822_es(test_date_str), expected)
def test_timesince(self):
now = datetime.datetime.utcnow()
self.assertEqual(timesince(now), 'just now')
self.assertEqual(timesince(now - datetime.timedelta(seconds=5)), '5 seconds ago')
self.assertEqual(timesince(now - datetime.timedelta(minutes=1)), '1 minute ago')
self.assertEqual(
timesince(now - datetime.timedelta(minutes=38, seconds=43)), '38 minutes ago')
self.assertEqual(
timesince(now - datetime.timedelta(hours=2, minutes=38, seconds=51)), '2 hours ago')
bigger = now - datetime.timedelta(days=3)
self.assertEqual(timesince(bigger), bigger.strftime('%Y-%m-%d %H:%M UTC'))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

0
tests/views/__init__.py Normal file
View file

View file

@ -0,0 +1,35 @@
import unittest
from tests import NyaaTestCase
class AccountTestCase(NyaaTestCase):
""" Tests for nyaa.views.account """
def test_login(self):
rv = self.client.get('/login')
self.assertIn(b'Username or email address', rv.data)
def test_logout(self):
rv = self.client.get('/logout')
self.assertIn(b'Redirecting...', rv.data)
def test_register(self):
rv = self.client.get('/register')
self.assertIn(b'Username', rv.data)
self.assertIn(b'Password', rv.data)
@unittest.skip('Not yet implemented')
def test_profile(self):
pass
@unittest.skip('Not yet implemented')
def test_redirect_url(self):
pass
@unittest.skip('Not yet implemented')
def test_send_verification_email(self):
pass
if __name__ == '__main__':
unittest.main()

18
tests/views/test_admin.py Normal file
View file

@ -0,0 +1,18 @@
import unittest
from tests import NyaaTestCase
class AdminTestCase(NyaaTestCase):
""" Tests for nyaa.views.admin """
@unittest.skip('Not yet implemented')
def test_adminlog(self):
pass
@unittest.skip('Not yet implemented')
def test_reports(self):
pass
if __name__ == '__main__':
unittest.main()

23
tests/views/test_main.py Normal file
View file

@ -0,0 +1,23 @@
import unittest
from tests import NyaaTestCase
class MainTestCase(NyaaTestCase):
""" Tests for nyaa.views.main """
def test_index_url(self):
rv = self.client.get('/')
self.assertIn(b'Browse :: Nyaa', rv.data)
self.assertIn(b'Guest', rv.data)
def test_rss_url(self):
rv = self.client.get('/?page=rss')
self.assertIn(b'/xmlns/nyaa', rv.data)
def test_invalid_url(self):
rv = self.client.get('/notarealpage')
self.assertIn(b'404 Not Found', rv.data)
if __name__ == '__main__':
unittest.main()

26
tests/views/test_site.py Normal file
View file

@ -0,0 +1,26 @@
import unittest
from tests import NyaaTestCase
class SiteTestCase(NyaaTestCase):
""" Tests for nyaa.views.site """
# def test_about_url(self):
# rv = self.client.get('/about')
# self.assertIn(b'About', rv.data)
def test_rules_url(self):
rv = self.client.get('/rules')
self.assertIn(b'Site Rules', rv.data)
def test_help_url(self):
rv = self.client.get('/help')
self.assertIn(b'Using the Site', rv.data)
def test_xmlns_url(self):
rv = self.client.get('/xmlns/nyaa')
self.assertIn(b'Nyaa XML Namespace', rv.data)
if __name__ == '__main__':
unittest.main()

View file

@ -0,0 +1,48 @@
import unittest
from tests import NyaaTestCase
class TorrentsTestCase(NyaaTestCase):
""" Tests for nyaa.views.torrents """
@unittest.skip('Not yet implemented')
def test_view_url(self):
pass
@unittest.skip('Not yet implemented')
def test_edit_url(self):
pass
@unittest.skip('Not yet implemented')
def test_redirect_magnet(self):
pass
@unittest.skip('Not yet implemented')
def test_download_torrent(self):
pass
@unittest.skip('Not yet implemented')
def test_delete_comment(self):
pass
@unittest.skip('Not yet implemented')
def test_submit_report(self):
pass
def test_upload_url(self):
rv = self.client.get('/upload')
self.assertIn(b'Upload Torrent', rv.data)
self.assertIn(b'You are not logged in, and are uploading anonymously.', rv.data)
@unittest.skip('Not yet implemented')
def test__create_upload_category_choices(self):
pass
@unittest.skip('Not yet implemented')
def test__get_cached_torrent_file(self):
pass
if __name__ == '__main__':
unittest.main()

26
tests/views/test_users.py Normal file
View file

@ -0,0 +1,26 @@
import unittest
from tests import NyaaTestCase
class UsersTestCase(NyaaTestCase):
""" Tests for nyaa.views.users """
@unittest.skip('Not yet implemented')
def test_view_user(self):
pass
@unittest.skip('Not yet implemented')
def test_activate_user(self):
pass
@unittest.skip('Not yet implemented')
def test__create_user_class_choices(self):
pass
@unittest.skip('Not yet implemented')
def test__get_activation_link(self):
pass
if __name__ == '__main__':
unittest.main()