Skip to content

Commit

Permalink
add test coverage for existing tests and add extra command to run all…
Browse files Browse the repository at this point in the history
… the tests at once and renamed the existing test files for coverage to work
  • Loading branch information
carpecodeum committed Feb 11, 2020
1 parent d104d5b commit 70f6e2f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
46 changes: 46 additions & 0 deletions components/core/TestCommands.py
Original file line number Diff line number Diff line change
@@ -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()
27 changes: 27 additions & 0 deletions components/core/tests/tests_Bassa_endpoint_test.py
Original file line number Diff line number Diff line change
@@ -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()
38 changes: 38 additions & 0 deletions components/core/tests/tests_login_test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 70f6e2f

Please sign in to comment.