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

add unittest TestAppBase, including setup the app and add the admin user for further test #26

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Config file for automatic testing at travis-ci.org
sudo: false # http://docs.travis-ci.com/user/migrating-from-legacy/
language: python
python:
- 2.7
install:
- ./travis_install.sh
before_script:
- ./tests/installTestDb.sh
script:
- nose2 -v
branches:
only:
- unittest
102 changes: 102 additions & 0 deletions tests/TestAppBase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import os
import sys
import mock
import unittest
from mock import patch

# Need to append server root path to ensure we can import the necessary files.
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import tests.config
from collections import namedtuple
from flask import g, current_app
from database import create_session, Base
from mod_auth.models import User, Role, Page, PageAccess
from mod_config.models import Service, Notification, Actions, Conditions, Rule
from mod_honeypot.models import Profile, PiModels, PiPotReport, ProfileService, \
CollectorTypes, Deployment


def generate_keys(tempdir):
secret_csrf_path = os.path.join(tempdir, "secret_csrf")
secret_key_path = os.path.join(tempdir, "secret_key")
if not os.path.exists(secret_csrf_path):
secret_csrf_cmd = "head -c 24 /dev/urandom > {path}".format(path=secret_csrf_path)
os.system(secret_csrf_cmd)
if not os.path.exists(secret_key_path):
secret_key_cmd = "head -c 24 /dev/urandom > {path}".format(path=secret_key_path)
os.system(secret_key_cmd)

return {'secret_csrf_path': secret_csrf_path, 'secret_key_path': secret_key_path}


def load_config(tempdir):
key_paths = generate_keys(tempdir)
with open(key_paths['secret_key_path'], 'rb') as secret_key_file:
secret_key = secret_key_file.read()
with open(key_paths['secret_csrf_path'], 'rb') as secret_csrf_file:
secret_csrf = secret_csrf_file.read()

return {
'TESTING': True,
'WTF_CSRF_ENABLED': False,
'SQLALCHEMY_POOL_SIZE': 1,
'SECRET_KEY': secret_key,
'CSRF_SESSION_KEY': secret_csrf,
'SERVER_IP': '127.0.0.1',
'SERVER_PORT': 443,
'INSTANCE_NAME': 'testInstance',
'APPLICATION_ROOT': '/',
'CSRF_ENABLED': False,
'DATABASE_URI': tests.config.DATABASE_URI,
'COLLECTOR_UDP_PORT': 1234,
'COLLECTOR_SSL_PORT': 1235
}


class BasicTestCase(unittest.TestCase):
tempdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "temp")

def create_app(self):
with patch('config_parser.parse_config', return_value=load_config(self.tempdir)):
from run import app
return app

def create_admin(self):
# test if there is admin existed
if not self.admin_is_created():
db = create_session(self.app.config['DATABASE_URI'], drop_tables=False)
role = Role(name="Admin")
db.add(role)
db.commit()
admin_user = User(role_id=role.id, name="Admin", password="admin", email="admin@sample.com")
db.add(admin_user)
db.commit()
db.remove()
return admin_user

def setUp(self):
if not os.path.exists(self.tempdir):
os.mkdir(self.tempdir)
self.app = self.create_app()
self.create_admin()

def test_app_is_running(self):
self.assertFalse(current_app is None)

def test_app_is_testing(self):
self.assertTrue(self.app.config['TESTING'])

def admin_is_created(self):
db = create_session(self.app.config['DATABASE_URI'], drop_tables=False)
admin_row = Role.query.filter(Role.is_admin).first()
admin = User.query.filter(User.role_id == admin_row.id).first()
db.remove()
return admin is not None

def test_create_admin(self):
self.assertTrue(self.admin_is_created())


if __name__ == '__main__':
unittest.main()
15 changes: 12 additions & 3 deletions tests/TestNestedMenus.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import unittest
import os
import sys

from mock import patch, call
from server.decorators import get_menu_entries, get_permissible_entries
# Need to append server root path to ensure we can import the necessary files.
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from decorators import get_menu_entries, get_permissible_entries


class TestGetMenuEntries(unittest.TestCase):

@patch('mod_auth.models.User')
@patch('server.decorators.get_permissible_entries')
@patch('decorators.get_permissible_entries')
def test_get_menu_entries_with_simple_entries(self, mock_permissible_entries, mock_user):
"""
Passing a menu entry to get_menu_entries() when all the
Expand Down Expand Up @@ -59,7 +64,7 @@ def side_effect(*args):
mock_permissible_entries.assert_has_calls(calls) # Check that mocked function correctly called

@patch('mod_auth.models.User')
@patch('server.decorators.get_permissible_entries')
@patch('decorators.get_permissible_entries')
def test_get_menu_entries_with_no_permissions(self, mock_permissible_entries, mock_user):
"""
Passing a menu entry to get_menu_entries() when user is not
Expand Down Expand Up @@ -158,3 +163,7 @@ def side_effect(*args):
'entries': [{'title': 'Honeypot services', 'route': 'config.services', 'icon': 'sliders'}],
'icon': 'bell-o'}], 'icon': 'bell-o'}
self.assertDictEqual(entries, correct_entries)


if __name__ == "__main__":
unittest.main()
50 changes: 50 additions & 0 deletions tests/installTestDb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
clear
date=`date +%Y-%m-%d`
install_log="${dir}/TestDbSetUp_${date}_log.txt"
read -e -p "Password of the 'root' user of MySQL: " -i "" db_root_password
# Verify password
while ! mysql -u root --password="${db_root_password}" -e ";" ; do
read -e -p "Invalid password, please retry: " -i "" db_root_password
done
db_user="pipot"
db_name="pipotTest"
mysql -u root --password="${db_root_password}" -e "CREATE DATABASE IF NOT EXISTS ${db_name};" >> "$install_log" 2>&1
# Check if DB exists
db_exists=`mysql -u root --password="${db_root_password}" -se"USE ${db_name};" 2>&1`
if [ ! "${db_exists}" == "" ]; then
echo "Failed to create the database! Please check the installation log!"
exit -1
fi
# Check if user exists
db_user_exists=`mysql -u root --password="${db_root_password}" -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${db_user}')"`
db_user_password=""
if [ ${db_user_exists} = 0 ]; then
rand_pass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)
read -e -p "Password for ${db_user} (will be created): " -i "${rand_pass}" db_user_password
# Attempt to create the user
mysql -u root --password="$db_root_password" -e "CREATE USER '${db_user}'@'localhost' IDENTIFIED BY '${db_user_password}';" >> "$install_log" 2>&1
db_user_exists=`mysql -u root --password="$db_root_password" -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$db_user')"`
if [ ${db_user_exists} = 0 ]; then
echo "Failed to create the user! Please check the installation log!"
exit -1
fi
else
read -e -p "Password for ${db_user}: " db_user_password
# Check if we have access
while ! mysql -u "${db_user}" --password="${db_user_password}" -e ";" ; do
read -e -p "Invalid password, please retry: " -i "" db_user_password
done
fi
# Grant user access to database
mysql -u root --password="${db_root_password}" -e "GRANT ALL ON ${db_name}.* TO '${db_user}'@'localhost';" >> "$install_log" 2>&1
# Check if user has access
db_access=`mysql -u "${db_user}" --password="${db_user_password}" -se"USE ${db_name};" 2>&1`
if [ ! "${db_access}" == "" ]; then
echo "Failed to grant user access to database! Please check the installation log!"
exit -1
fi

echo "# Auto-generated configuration by installTestDb.sh
DATABASE_URI = 'mysql+pymysql://${db_user}:${db_user_password}@localhost:3306/${db_name}''
" > "${dir}/config.py"
102 changes: 102 additions & 0 deletions tests/testAppBase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import os
import sys
import mock
import unittest
from mock import patch

# Need to append server root path to ensure we can import the necessary files.
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import tests.config
from collections import namedtuple
from flask import g, current_app
from database import create_session, Base
from mod_auth.models import User, Role, Page, PageAccess
from mod_config.models import Service, Notification, Actions, Conditions, Rule
from mod_honeypot.models import Profile, PiModels, PiPotReport, ProfileService, \
CollectorTypes, Deployment


def generate_keys(tempdir):
secret_csrf_path = os.path.join(tempdir, "secret_csrf")
secret_key_path = os.path.join(tempdir, "secret_key")
if not os.path.exists(secret_csrf_path):
secret_csrf_cmd = "head -c 24 /dev/urandom > {path}".format(path=secret_csrf_path)
os.system(secret_csrf_cmd)
if not os.path.exists(secret_key_path):
secret_key_cmd = "head -c 24 /dev/urandom > {path}".format(path=secret_key_path)
os.system(secret_key_cmd)

return {'secret_csrf_path': secret_csrf_path, 'secret_key_path': secret_key_path}


def load_config(tempdir):
key_paths = generate_keys(tempdir)
with open(key_paths['secret_key_path'], 'rb') as secret_key_file:
secret_key = secret_key_file.read()
with open(key_paths['secret_csrf_path'], 'rb') as secret_csrf_file:
secret_csrf = secret_csrf_file.read()

return {
'TESTING': True,
'WTF_CSRF_ENABLED': False,
'SQLALCHEMY_POOL_SIZE': 1,
'SECRET_KEY': secret_key,
'CSRF_SESSION_KEY': secret_csrf,
'SERVER_IP': '127.0.0.1',
'SERVER_PORT': 443,
'INSTANCE_NAME': 'testInstance',
'APPLICATION_ROOT': '/',
'CSRF_ENABLED': False,
'DATABASE_URI': tests.config.DATABASE_URI,
'COLLECTOR_UDP_PORT': 1234,
'COLLECTOR_SSL_PORT': 1235
}


class BasicTestCase(unittest.TestCase):
tempdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "temp")

def create_app(self):
with patch('config_parser.parse_config', return_value=load_config(self.tempdir)):
from run import app
return app

def create_admin(self):
# test if there is admin existed
if not self.admin_is_created():
db = create_session(self.app.config['DATABASE_URI'], drop_tables=False)
role = Role(name="Admin")
db.add(role)
db.commit()
admin_user = User(role_id=role.id, name="Admin", password="admin", email="admin@sample.com")
db.add(admin_user)
db.commit()
db.remove()
return admin_user

def setUp(self):
if not os.path.exists(self.tempdir):
os.mkdir(self.tempdir)
self.app = self.create_app()
self.create_admin()

def test_app_is_running(self):
self.assertFalse(current_app is None)

def test_app_is_testing(self):
self.assertTrue(self.app.config['TESTING'])

def admin_is_created(self):
db = create_session(self.app.config['DATABASE_URI'], drop_tables=False)
admin_row = Role.query.filter(Role.is_admin).first()
admin = User.query.filter(User.role_id == admin_row.id).first()
db.remove()
return admin is not None

def test_create_admin(self):
self.assertTrue(self.admin_is_created())


if __name__ == '__main__':
unittest.main()
Loading