From 1aa7b24b3ff67db1386379a42317a5737663a496 Mon Sep 17 00:00:00 2001 From: the catalyst Date: Wed, 12 Feb 2020 01:05:47 +0530 Subject: [PATCH] add test coverage for existing tests and add extra command to run all the tests at once and renamed the existing test files for coverage to work --- components/core/TestCommands.py | 46 +++++++++++++++++++ .../core/tests/tests_Bassa_endpoint_test.py | 27 +++++++++++ components/core/tests/tests_login_test.py | 38 +++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 components/core/TestCommands.py create mode 100644 components/core/tests/tests_Bassa_endpoint_test.py create mode 100644 components/core/tests/tests_login_test.py diff --git a/components/core/TestCommands.py b/components/core/TestCommands.py new file mode 100644 index 00000000..195f1ed8 --- /dev/null +++ b/components/core/TestCommands.py @@ -0,0 +1,46 @@ +import unittest +import os +import coverage +from initializer import server +from flask_script import Manager + +manager = Manager(server) + +@manager.command +def test(): + """Runs the unit tests without coverage.""" + tests = unittest.TestLoader().discover('tests') + result = unittest.TextTestRunner(verbosity=2).run(tests) + if result.wasSuccessful(): + return 0 + else: + sys.exit("Tests have failed") + + +@manager.command +def cov(): + """Runs the unit tests with coverage.""" + cov = coverage.coverage( + branch=True, + include='./*' + ) + cov.start() + tests = unittest.TestLoader().discover('tests') + result = unittest.TextTestRunner(verbosity=2).run(tests) + cov.stop() + cov.save() + print('Coverage Summary:') + cov.report() + basedir = os.path.abspath(os.path.dirname(__file__)) + covdir = os.path.join(basedir, 'coverage') + cov.html_report(directory=covdir) + cov.erase() + if result.wasSuccessful(): + return 0 + else: + sys.exit("Tests have failed") + + + +if __name__ == '__main__': + manager.run() \ No newline at end of file diff --git a/components/core/tests/tests_Bassa_endpoint_test.py b/components/core/tests/tests_Bassa_endpoint_test.py new file mode 100644 index 00000000..a97101e8 --- /dev/null +++ b/components/core/tests/tests_Bassa_endpoint_test.py @@ -0,0 +1,27 @@ +import unittest +import requests + +headers = { +'Host': 'localhost:5000', +'Accept': 'application/json, text/plain, */*', +'Accept-Language': 'en-US,en;q=0.5', +'Accept-Encoding': 'gzip, deflate', +'Content-Type': 'application/x-www-form-urlencoded', +'Origin': 'http://localhost:3000' +} +correct_username="rand" +correct_password="pass" +incorrect_username="admin" +incorrect_password="admin" +correct_string="user_name="+correct_username+"&password="+correct_password +incorrect_string="user_name="+incorrect_username+"&password="+incorrect_password +payload = {''} +class TestFlaskAPIUsingRequests(unittest.TestCase): + def test_api_login_returns_auth_level(self): + resp = requests.post('http://localhost:5000/api/login',correct_string,headers=headers) + self.assertEqual(resp.json(),{u'auth': u'0'}) + def test_api_login_incorrectly_return_403(self): + resp = requests.post('http://localhost:5000/api/login',incorrect_string,headers=headers) + self.assertEqual(resp.status_code,403) +if __name__ == "__main__": + unittest.main() diff --git a/components/core/tests/tests_login_test.py b/components/core/tests/tests_login_test.py new file mode 100644 index 00000000..72620fa6 --- /dev/null +++ b/components/core/tests/tests_login_test.py @@ -0,0 +1,38 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +from UserManager import * +import unittest + + +def username(): + return 'admin' + + +def password(): + return 'admin' + + +def usernamer(): + return 'rand' + + +def passwordr(): + return 'pass' + + +class Test(unittest.TestCase): + + def test_incorrect_login(self): + self.assertEqual(False, user_login(username(), password())) + + def test_correct_login(self): + self.assertEqual(True, user_login(usernamer(), passwordr())) + + def test_incorrect_check_approved(self): + self.assertEqual(False, check_approved(username(), password())) + + def test_correct_check_approved(self): + self.assertEqual(True, check_approved(usernamer(), passwordr())) + +if __name__ == "__main__": + unittest.main()