Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test coverage and renamed existing test files to allow coverage to work #886

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this file do? is it a script?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file adds a command for running the tests without the coverage and with the coverge the respective commands being python TestCommands.py test and python TestCommands.py cov

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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line at the end of the file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay i will

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


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why there are two blank lines between code blocks instead of one?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No these are left because of keeping the code easy to read but if you think i will remove the extra line.

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()