<commit_before>#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import os
import unittest
import urllib2
import pprint
from flask.ext.testing import LiveServerTestCase, TestCase

sys.path.append('{0}/..'.format(os.path.join(os.path.dirname(os.path.realpath(__file__)))))

from burpui import app, bui, login_manager, init as BUIinit

class BurpuiLiveTestCase(LiveServerTestCase):

	def create_app(self):
		conf = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../burpui.cfg')
		app.config['TESTING'] = True
		app.config['LOGIN_DISABLED'] = True
		app.config['LIVESERVER_PORT'] = 5001
		app.config['CFG'] = conf
		bui.setup(conf)
		login_manager.init_app(app)
		return app

	def setUp(self):
		print '\nBegin Test 1\n'

	def tearDown(self):
		print '\nTest 1 Finished!\n'

	def test_server_is_up_and_running(self):
		response = urllib2.urlopen(self.get_server_url())
		self.assertEqual(response.code, 200)

class BurpuiAPITestCase(TestCase):

	def setUp(self):
		print '\nBegin Test 2\n'
	
	def tearDown(self):
		print '\nTest 2 Finished!\n'
	
	def create_app(self):
		conf = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test2.cfg')
		app.config['TESTING'] = True
		app.config['LOGIN_DISABLED'] = True
		app.config['CFG'] = conf
		bui.setup(conf)
		login_manager.init_app(app)
		return app

	def test_no_clients(self):
		response = self.client.get('/api/clients.json')
		self.assertEquals(response.json, {u'notif':[[2, u'Cannot contact burp server at 127.0.0.1:9999']]})
	
	def test_config_parsing(self):
		response = self.client.get('/api/server-config')
		print response.json
		self.assertEquals(response.json, dict(results=[],
												 boolean=bui.cli.get_parser_attr('boolean'),
												 server_doc=bui.cli.get_parser_attr('server_doc'),
												 string=bui.cli.get_parser_attr('string'),
#												 multi=bui.cli.get_parser_attr('multi'),
												 suggest=bui.cli.get_parser_attr('values_server'),
												 defaults=bui.cli.get_parser_attr('defaults_server')))
	
	def test_restore(self):
		response = self.client.post('/api/restore/dummy/1', data=dict(strip=False))
		self.assert500(response)
	
	def test_running_clients(self):
		response = self.client.get('/api/running-clients.json')
		self.assertEquals(response.json, dict(results=[]))
	
	def test_live_rendering(self):
		response = self.client.get('/api/render-live-template/toto')
		self.assert404(response)
		response = self.client.get('/api/render-live-template')
		self.assert500(response)

#	def test_servers_json(self):
#		response = self.client.get('/api/servers.json')
#		self.assertEquals(response.json, dict(results=[]))

	def test_live(self):
		response = self.client.get('/api/live.json')
		self.assertEquals(response.json, dict(results=[]))

class BurpuiRoutesTestCase(TestCase):

	def setUp(self):
		print '\nBegin Test 3\n'

	def tearDown(self):
		print '\nTest 3 Finished!\n'

	def create_app(self):
		conf = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../burpui.cfg')
		app.config['TESTING'] = True
		app.config['LOGIN_DISABLED'] = True
		app.config['LIVESERVER_PORT'] = 5001
		app.config['CFG'] = conf
		bui.setup(conf)
		bui.cli.port = 9999
		login_manager.init_app(app)
		return app
	
	def test_config_render(self):
		response = self.client.get('/settings')
		assert 'Burp Configuration' in response.data

class BurpuiLoginTestCase(TestCase):

	def setUp(self):
		print '\nBegin Test 4\n'

	def tearDown(self):
		print '\nTest 4 Finished!\n'

	def login(self, username, password):
		return self.client.post('/login', data=dict(
			username=username,
			password=password
		), follow_redirects=True)

	def create_app(self):
		conf = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../burpui.cfg')
		BUIinit(conf, False, False)
		app.config['TESTING'] = True
		app.config['LIVESERVER_PORT'] = 5001
		app.config['WTF_CSRF_ENABLED'] = False
		bui.cli.port = 9999
		login_manager.init_app(app)
		return app

	def test_login_ok(self):
		rv = self.login('admin', 'admin')
		print rv.data
		assert 'Logged in successfully' in rv.data
	
	def test_login_ko(self):
		rv = self.login('admin', 'toto')
		assert 'Wrong username or password' in rv.data

	def test_login_no_user(self):
		rv = self.login('toto', 'toto')
		assert 'Wrong username or password' in rv.data

if __name__ == '__main__':
	unittest.main()<commit_msg>improve tests coverage<commit_after>#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import os
import unittest
import urllib2
import pprint
from flask.ext.testing import LiveServerTestCase, TestCase

sys.path.append('{0}/..'.format(os.path.join(os.path.dirname(os.path.realpath(__file__)))))

from burpui import app, bui, login_manager, init as BUIinit

class BurpuiLiveTestCase(LiveServerTestCase):

	def create_app(self):
		conf = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../burpui.cfg')
		app.config['TESTING'] = True
		app.config['LOGIN_DISABLED'] = True
		app.config['LIVESERVER_PORT'] = 5001
		app.config['CFG'] = conf
		bui.setup(conf)
		login_manager.init_app(app)
		return app

	def setUp(self):
		print '\nBegin Test 1\n'

	def tearDown(self):
		print '\nTest 1 Finished!\n'

	def test_server_is_up_and_running(self):
		response = urllib2.urlopen(self.get_server_url())
		self.assertEqual(response.code, 200)

class BurpuiAPITestCase(TestCase):

	def setUp(self):
		print '\nBegin Test 2\n'
	
	def tearDown(self):
		print '\nTest 2 Finished!\n'
	
	def create_app(self):
		conf = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test2.cfg')
		app.config['TESTING'] = True
		app.config['LOGIN_DISABLED'] = True
		app.config['CFG'] = conf
		bui.setup(conf)
		login_manager.init_app(app)
		return app

	def test_no_clients(self):
		response = self.client.get('/api/clients.json')
		self.assertEquals(response.json, {u'notif':[[2, u'Cannot contact burp server at 127.0.0.1:9999']]})
	
	def test_config_parsing(self):
		response = self.client.get('/api/server-config')
		self.assertEquals(response.json, dict(results=[],
												 boolean=bui.cli.get_parser_attr('boolean'),
												 server_doc=bui.cli.get_parser_attr('server_doc'),
												 string=bui.cli.get_parser_attr('string'),
#												 multi=bui.cli.get_parser_attr('multi'),
												 suggest=bui.cli.get_parser_attr('values_server'),
												 defaults=bui.cli.get_parser_attr('defaults_server')))
	
	def test_restore(self):
		response = self.client.post('/api/restore/dummy/1', data=dict(strip=False))
		self.assert500(response)
	
	def test_running_clients(self):
		response = self.client.get('/api/running-clients.json')
		self.assertEquals(response.json, dict(results=[]))
	
	def test_live_rendering(self):
		response = self.client.get('/api/render-live-template/toto')
		self.assert404(response)
		response = self.client.get('/api/render-live-template')
		self.assert500(response)

#	def test_servers_json(self):
#		response = self.client.get('/api/servers.json')
#		self.assertEquals(response.json, dict(results=[]))

	def test_live(self):
		response = self.client.get('/api/live.json')
		self.assertEquals(response.json, dict(results=[]))
	
	def test_running(self):
		response = self.client.get('/api/running.json')
		self.assertEquals(response.json, dict(results=False))
	
	def test_client_tree(self):
		response = self.client.get('/api/client-tree.json/toto/1')
		self.assertEquals(response.json, {u'notif':[[2, u'Cannot contact burp server at 127.0.0.1:9999']]})
	
	def test_clients_report_json(self):
		response = self.client.get('/api/clients-report.json')
		self.assertEquals(response.json, {u'notif':[[2, u'Cannot contact burp server at 127.0.0.1:9999']]})
	
	def test_client_stat_json(self):
		response = self.client.get('/api/client-stat.json/toto')
		self.assertEquals(response.json, {u'notif':[[2, u'Cannot contact burp server at 127.0.0.1:9999']]})
		response = self.client.get('/api/client-stat.json/toto/1')
		self.assertEquals(response.json, {u'notif':[[2, u'Cannot contact burp server at 127.0.0.1:9999']]})
	
	def test_client_json(self):
		response = self.client.get('/api/client.json/toto')
		self.assertEquals(response.json, {u'notif':[[2, u'Cannot contact burp server at 127.0.0.1:9999']]})

class BurpuiRoutesTestCase(TestCase):

	def setUp(self):
		print '\nBegin Test 3\n'

	def tearDown(self):
		print '\nTest 3 Finished!\n'

	def create_app(self):
		conf = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../burpui.cfg')
		app.config['TESTING'] = True
		app.config['LOGIN_DISABLED'] = True
		app.config['LIVESERVER_PORT'] = 5001
		app.config['CFG'] = conf
		bui.setup(conf)
		bui.cli.port = 9999
		login_manager.init_app(app)
		return app
	
	def test_config_render(self):
		response = self.client.get('/settings')
		assert 'Burp Configuration' in response.data
	
	def test_live_monitor(self):
		response = self.client.get('/live-monitor', follow_redirects=True)
		assert 'Sorry, there are no running backups' in response.data

class BurpuiLoginTestCase(TestCase):

	def setUp(self):
		print '\nBegin Test 4\n'

	def tearDown(self):
		print '\nTest 4 Finished!\n'

	def login(self, username, password):
		return self.client.post('/login', data=dict(
			username=username,
			password=password
		), follow_redirects=True)

	def create_app(self):
		conf = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../burpui.cfg')
		BUIinit(conf, False, False)
		app.config['TESTING'] = True
		app.config['LIVESERVER_PORT'] = 5001
		app.config['WTF_CSRF_ENABLED'] = False
		bui.cli.port = 9999
		login_manager.init_app(app)
		return app

	def test_login_ok(self):
		rv = self.login('admin', 'admin')
		assert 'Logged in successfully' in rv.data
	
	def test_login_ko(self):
		rv = self.login('admin', 'toto')
		assert 'Wrong username or password' in rv.data

	def test_login_no_user(self):
		rv = self.login('toto', 'toto')
		assert 'Wrong username or password' in rv.data

if __name__ == '__main__':
	unittest.main()
{'fraction_non_alphanumeric': 0.15466613032984713, 'fraction_numerical': 0.017196299275945293, 'mean_word_length': 4.549665178571429, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 7, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '71420', 'n_tokens_mistral': 4008, 'n_tokens_neox': 3545, 'n_words': 633}