Skip to content

Commit

Permalink
fix iterate issue on Enum class due to python3 update, add test to co…
Browse files Browse the repository at this point in the history
…ver the change (#35)
  • Loading branch information
VertexC authored and canihavesomecoffee committed Aug 13, 2019
1 parent 25d0c4d commit 8675148
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 44 deletions.
11 changes: 5 additions & 6 deletions mod_honeypot/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,11 @@ def profiles_id(id):
def manage():
from run import app
new_deploy = NewDeploymentForm()
new_deploy.rpi_model.choices = [(key, value) for key, value in PiModels]
new_deploy.rpi_model.choices = [(item.name, item.value) for item in PiModels]
new_deploy.profile_id.choices = [
(p.id, p.name) for p in Profile.query.all()]
new_deploy.collector_type.choices = [(key, value) for key, value in
new_deploy.collector_type.choices = [(item.name, item.value) for item in
CollectorTypes]

if request.is_xhr:
result = {
'status': 'error',
Expand All @@ -158,19 +157,19 @@ def manage():
deployment = Deployment(
new_deploy.name.data, new_deploy.profile_id.data,
instance_key, mac_key, encryption_key,
PiModels.from_string(new_deploy.rpi_model.data),
PiModels[new_deploy.rpi_model.data],
new_deploy.server_ip.data, new_deploy.interface.data,
new_deploy.wlan_configuration.data, new_deploy.hostname.data,
new_deploy.rootpw.data, new_deploy.debug.data,
CollectorTypes.from_string(new_deploy.collector_type.data)
CollectorTypes[new_deploy.collector_type.data]
)
g.db.add(deployment)
g.db.commit()
result['status'] = 'success'
result['id'] = deployment.id
else:
errors = []
for err in new_deploy.errors.itervalues():
for _, err in new_deploy.errors.items():
errors.extend(err)
result['errors'] = errors
return jsonify(result)
Expand Down
75 changes: 37 additions & 38 deletions tests/testDashboardDataReport.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def setUp(self):
def tearDown(self):
super(TestServiceManagement, self).tearDown()

def testDynamicDataReport(self):
def test_dynamic_data_report(self):
deployment_id = 1
profile_id = 1
report_type = 'General data'
Expand Down Expand Up @@ -57,45 +57,44 @@ def testDynamicDataReport(self):
report_data = PiPotReport(deployment_id=1, message="test", timestamp=time_before_a_week)
db.add(report_data)
db.commit()
db.remove()
# request without the number of data specified
data_num = -1
with self.app.test_client() as client:
data = dict(
deployment=deployment_id,
service=0,
report_type=report_type,
data_num=data_num
)
response = client.post('/dashboard/load', data=data, follow_redirects=False)
self.assertEqual(response.get_json()['status'], 'success')
self.assertEqual(response.get_json()['data_num'], data_num_within_a_week)
# request with the number of data specified
data_num = min(data_num_within_a_week + 10, data_num_before_a_week + data_num_within_a_week)
with self.app.test_client() as client:
data = dict(
deployment=deployment_id,
service=0,
report_type=report_type,
data_num=data_num
)
response = client.post('/dashboard/load', data=data, follow_redirects=False)
self.assertEqual(response.get_json()['status'], 'success')
self.assertEqual(response.get_json()['data_num'], data_num)
data_num = data_num_before_a_week + data_num_within_a_week + 10
with self.app.test_client() as client:
data = dict(
deployment=deployment_id,
service=0,
report_type=report_type,
data_num=data_num
)
response = client.post('/dashboard/load', data=data, follow_redirects=False)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json()['status'], 'success')
self.assertEqual(response.get_json()['data_num'], data_num_before_a_week + data_num_within_a_week)
finally:
db.remove()
# request without the number of data specified
data_num = -1
with self.app.test_client() as client:
data = dict(
deployment=deployment_id,
service=0,
report_type=report_type,
data_num=data_num
)
response = client.post('/dashboard/load', data=data, follow_redirects=False)
self.assertEqual(response.get_json()['status'], 'success')
self.assertEqual(response.get_json()['data_num'], data_num_within_a_week)
# request with the number of data specified
data_num = min(data_num_within_a_week + 10, data_num_before_a_week + data_num_within_a_week)
with self.app.test_client() as client:
data = dict(
deployment=deployment_id,
service=0,
report_type=report_type,
data_num=data_num
)
response = client.post('/dashboard/load', data=data, follow_redirects=False)
self.assertEqual(response.get_json()['status'], 'success')
self.assertEqual(response.get_json()['data_num'], data_num)
data_num = data_num_before_a_week + data_num_within_a_week + 10
with self.app.test_client() as client:
data = dict(
deployment=deployment_id,
service=0,
report_type=report_type,
data_num=data_num
)
response = client.post('/dashboard/load', data=data, follow_redirects=False)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json()['status'], 'success')
self.assertEqual(response.get_json()['data_num'], data_num_before_a_week + data_num_within_a_week)


if __name__ == '__main__':
Expand Down
66 changes: 66 additions & 0 deletions tests/testHoneypotDeployment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
import sys
import mock
import unittest
import json
import datetime
from mock import patch

from flask import request, jsonify

import tests.authMock
from database import create_session
from mod_config.models import Service
from mod_honeypot.models import Profile, PiModels, PiPotReport, ProfileService, \
CollectorTypes, Deployment
from tests.testAppBase import TestAppBase


class TestHoneypotDeployment(TestAppBase):

def setUp(self):
super(TestHoneypotDeployment, self).setUp()

def tearDown(self):
super(TestHoneypotDeployment, self).tearDown()

def test_honeypot_deployment(self):
# create service, profile, delpoyment
try:
db = create_session(self.app.config['DATABASE_URI'], drop_tables=False)
profile = Profile(name='test-profile', description="test")
db.add(profile)
db.commit()
profile_id = profile.id
finally:
db.remove()
name = 'test_delpoyment'
with self.app.test_client() as client:
data = dict(
profile_id=profile_id,
name='test_delpoyment',
rpi_model='one',
server_ip='127.0.0.1',
interface='eth0',
debug=True,
hostname='admin',
rootpw='123',
collector_type='tcp',
wlan_configuration=''
)
response = client.post('manage', data=data, follow_redirects=False,
headers=[('X-Requested-With', 'XMLHttpRequest')])
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json()['status'], 'success')
# check the deloyment is created is the database
try:
db = create_session(self.app.config['DATABASE_URI'], drop_tables=False)
deloyment_instance = db.query(Deployment.name).first()
delpoyment_name = deloyment_instance.name
finally:
db.remove()
self.assertEqual(delpoyment_name, name)


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

0 comments on commit 8675148

Please sign in to comment.