From d7d8d8ef1431fefa22a44d5b53282fe412c80f4c Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Sat, 15 Jul 2017 02:49:23 +0300 Subject: [PATCH] Update tests, add tests structure for views --- tests/__init__.py | 6 ++- tests/test_api_handler.py | 4 +- tests/test_nyaa.py | 55 ------------------------ tests/test_template_utils.py | 81 ++++++++++++++++-------------------- tests/views/__init__.py | 0 tests/views/test_account.py | 35 ++++++++++++++++ tests/views/test_admin.py | 18 ++++++++ tests/views/test_main.py | 23 ++++++++++ tests/views/test_site.py | 26 ++++++++++++ tests/views/test_torrents.py | 48 +++++++++++++++++++++ tests/views/test_users.py | 26 ++++++++++++ 11 files changed, 219 insertions(+), 103 deletions(-) delete mode 100644 tests/test_nyaa.py create mode 100644 tests/views/__init__.py create mode 100644 tests/views/test_account.py create mode 100644 tests/views/test_admin.py create mode 100644 tests/views/test_main.py create mode 100644 tests/views/test_site.py create mode 100644 tests/views/test_torrents.py create mode 100644 tests/views/test_users.py diff --git a/tests/__init__.py b/tests/__init__.py index 4663455..e300651 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -14,7 +14,6 @@ class NyaaTestCase(unittest.TestCase): def setUpClass(cls): app = create_app('config') app.config['TESTING'] = True - cls.app_context = app.app_context() # Use a seperate database for testing # if USE_MYSQL: @@ -28,8 +27,11 @@ class NyaaTestCase(unittest.TestCase): # app.config['USE_MYSQL'] = USE_MYSQL # app.config['SQLALCHEMY_DATABASE_URI'] = db_uri + cls.app = app + cls.app_context = app.app_context() + with cls.app_context: - cls.app = app.test_client() + cls.client = app.test_client() @classmethod def tearDownClass(cls): diff --git a/tests/test_api_handler.py b/tests/test_api_handler.py index 13897e8..c2e0bc9 100644 --- a/tests/test_api_handler.py +++ b/tests/test_api_handler.py @@ -18,14 +18,14 @@ class ApiHandlerTests(NyaaTestCase): def test_no_authorization(self): """ 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()) self.assertDictEqual({'errors': ['Bad authorization']}, data) @unittest.skip('Not yet implemented') def test_bad_credentials(self): """ 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()) self.assertDictEqual({'errors': ['Bad authorization']}, data) diff --git a/tests/test_nyaa.py b/tests/test_nyaa.py deleted file mode 100644 index e0f87ad..0000000 --- a/tests/test_nyaa.py +++ /dev/null @@ -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() diff --git a/tests/test_template_utils.py b/tests/test_template_utils.py index b529144..17b406a 100644 --- a/tests/test_template_utils.py +++ b/tests/test_template_utils.py @@ -10,50 +10,9 @@ from nyaa.template_utils import (_jinja2_filter_rfc822, _jinja2_filter_rfc822_es class TestTemplateUtils(NyaaTestCase): - # def setUp(self): - # self.db, nyaa.app.config['DATABASE'] = tempfile.mkstemp() - # nyaa.app.config['TESTING'] = True - # 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') + def test_create_magnet_from_es_info(self): + pass @unittest.skip('Not yet implemented') def test_static_cachebuster(self): @@ -93,6 +52,40 @@ class TestTemplateUtils(NyaaTestCase): self.assertEqual(category_name('1_100'), '???') 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__': unittest.main() diff --git a/tests/views/__init__.py b/tests/views/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/views/test_account.py b/tests/views/test_account.py new file mode 100644 index 0000000..5754787 --- /dev/null +++ b/tests/views/test_account.py @@ -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() diff --git a/tests/views/test_admin.py b/tests/views/test_admin.py new file mode 100644 index 0000000..e8ef576 --- /dev/null +++ b/tests/views/test_admin.py @@ -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() diff --git a/tests/views/test_main.py b/tests/views/test_main.py new file mode 100644 index 0000000..a0dc9f2 --- /dev/null +++ b/tests/views/test_main.py @@ -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() diff --git a/tests/views/test_site.py b/tests/views/test_site.py new file mode 100644 index 0000000..e25156d --- /dev/null +++ b/tests/views/test_site.py @@ -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() diff --git a/tests/views/test_torrents.py b/tests/views/test_torrents.py new file mode 100644 index 0000000..1e9be4d --- /dev/null +++ b/tests/views/test_torrents.py @@ -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() diff --git a/tests/views/test_users.py b/tests/views/test_users.py new file mode 100644 index 0000000..57b9efa --- /dev/null +++ b/tests/views/test_users.py @@ -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()