<commit_before>import unittest

import sys
from mock import patch

from honeybadger import honeybadger
from honeybadger.config import Configuration
from honeybadger.contrib.flask import FlaskPlugin, FlaskHoneybadger

PY3_2 = sys.version_info[0:2] == (3, 2)


class FlaskPluginTestCase(unittest.TestCase):
    def setUp(self):
        if PY3_2:
            self.skipTest(
                'Flask requires Python3 > 3.2. More info at http://flask.pocoo.org/docs/0.12/python3/#requirements')
        import flask

        self.config = Configuration()

        self.app = flask.Flask(__name__)
        self.app.secret_key = 'safe'
        self.app.config.update({
            'HONEYBADGER_ENVIRONMENT': 'production_flask'
        })
        self.plugin = FlaskPlugin()

        @self.app.route('/test',  methods=['GET', 'POST', 'PUT'])
        def foo():
            return 'bar'

    def test_supports_in_request_context(self):
        with self.app.test_request_context():
            self.assertTrue(self.plugin.supports(self.config, {}))

    def test_supports_no_request_context(self):
        self.assertFalse(self.plugin.supports(self.config, {}))

    def test_get_request_with_headers(self):
        with self.app.test_request_context('/test', 'http://server:1234/path', 'a=1&a=2&foo=bar',
                                           headers={
                                               'X-Wizard-Color': 'grey'
                                           }):
            payload = self.plugin.generate_payload(self.config, {'k': 'value'})

            self.assertEqual(payload['url'], 'http://server:1234/path/test')
            self.assertEqual(payload['component'], 'honeybadger.tests.contrib.test_flask')
            self.assertEqual(payload['action'], 'foo')
            self.assertDictEqual(payload['params'], {'a': ['1', '2'], 'foo': ['bar']})
            self.assertDictEqual(payload['session'], {})
            self.assertDictEqual(payload['cgi_data'], {
                'Content-Length': '0',
                'Host': 'server:1234',
                'X-Wizard-Color': 'grey',
                'REQUEST_METHOD': 'GET'
            })
            self.assertDictEqual(payload['context'], {'k': 'value'})

    def test_get_request_with_session(self):
        with self.app.test_request_context('/test', 'http://server:1234/path') as ctx:
            ctx.session['answer'] = 42
            ctx.session['password'] = '<PASSWORD>'

            payload = self.plugin.generate_payload(self.config, {'k': 'value'})

            self.assertEqual(payload['url'], 'http://server:1234/path/test')
            self.assertEqual(payload['component'], 'honeybadger.tests.contrib.test_flask')
            self.assertEqual(payload['action'], 'foo')
            self.assertDictEqual(payload['params'], {})
            self.assertDictEqual(payload['session'], {'answer': 42, 'password': '[FILTERED]'})
            self.assertDictEqual(payload['cgi_data'], {
                'Content-Length': '0',
                'Host': 'server:1234',
                'REQUEST_METHOD': 'GET'
            })
            self.assertDictEqual(payload['context'], {'k': 'value'})

    def test_post_request(self):
        with self.app.test_request_context('/test', 'http://server:1234/path', method='POST',
                                           data={'foo': 'bar', 'password': '<PASSWORD>'}):

            payload = self.plugin.generate_payload(self.config, {'k': 'value'})

            self.assertEqual(payload['url'], 'http://server:1234/path/test')
            self.assertEqual(payload['component'], 'honeybadger.tests.contrib.test_flask')
            self.assertEqual(payload['action'], 'foo')
            self.assertDictEqual(payload['params'], {'foo': ['bar'], 'password': '[FILTERED]'})
            self.assertDictEqual(payload['session'], {})
            self.assertDictEqual(payload['cgi_data'], {
                'Host': 'server:1234',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': '29',
                'REQUEST_METHOD': 'POST'
            })
            self.assertDictEqual(payload['context'], {'k': 'value'})

    def test_put_request(self):
        with self.app.test_request_context('/test', 'http://server:1234/path', method='PUT',
                                           data={'foo': 'bar', 'password': '<PASSWORD>'}):

            payload = self.plugin.generate_payload(self.config, {'k': 'value'})

            self.assertEqual(payload['url'], 'http://server:1234/path/test')
            self.assertEqual(payload['component'], 'honeybadger.tests.contrib.test_flask')
            self.assertEqual(payload['action'], 'foo')
            self.assertDictEqual(payload['params'], {'foo': ['bar'], 'password': '[<PASSWORD>]'})
            self.assertDictEqual(payload['session'], {})
            self.assertDictEqual(payload['cgi_data'], {
                'Host': 'server:1234',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': '29',
                'REQUEST_METHOD': 'PUT'
            })
            self.assertDictEqual(payload['context'], {'k': 'value'})


class FlaskHoneybadgerTestCase(unittest.TestCase):

    def setUp(self):
        if PY3_2:<commit_msg>Fix failing flask test<commit_after>import unittest

import sys
from mock import patch

from honeybadger import honeybadger
from honeybadger.config import Configuration
from honeybadger.contrib.flask import FlaskPlugin, FlaskHoneybadger

PY3_2 = sys.version_info[0:2] == (3, 2)


class FlaskPluginTestCase(unittest.TestCase):
    def setUp(self):
        if PY3_2:
            self.skipTest(
                'Flask requires Python3 > 3.2. More info at http://flask.pocoo.org/docs/0.12/python3/#requirements')
        import flask

        self.config = Configuration()

        self.app = flask.Flask(__name__)
        self.app.secret_key = 'safe'
        self.app.config.update({
            'HONEYBADGER_ENVIRONMENT': 'production_flask'
        })
        self.plugin = FlaskPlugin()

        @self.app.route('/test',  methods=['GET', 'POST', 'PUT'])
        def foo():
            return 'bar'

    def test_supports_in_request_context(self):
        with self.app.test_request_context():
            self.assertTrue(self.plugin.supports(self.config, {}))

    def test_supports_no_request_context(self):
        self.assertFalse(self.plugin.supports(self.config, {}))

    def test_get_request_with_headers(self):
        with self.app.test_request_context(path='/test',
                                           base_url='http://server:1234/path',
                                           query_string='a=1&a=2&foo=bar',
                                           headers={
                                               'X-Wizard-Color': 'grey'
                                           }):
            payload = self.plugin.generate_payload(self.config, {'k': 'value'})

            self.assertEqual(payload['url'], 'http://server:1234/path/test')
            self.assertEqual(payload['component'], 'honeybadger.tests.contrib.test_flask')
            self.assertEqual(payload['action'], 'foo')
            self.assertDictEqual(payload['params'], {'a': ['1', '2'], 'foo': ['bar']})
            self.assertDictEqual(payload['session'], {})
            self.assertDictEqual(payload['cgi_data'], {
                'Content-Length': '0',
                'Host': 'server:1234',
                'X-Wizard-Color': 'grey',
                'REQUEST_METHOD': 'GET'
            })
            self.assertDictEqual(payload['context'], {'k': 'value'})

    def test_get_request_with_session(self):
        with self.app.test_request_context(path='/test', base_url='http://server:1234/path') as ctx:
            ctx.session['answer'] = 42
            ctx.session['password'] = '<PASSWORD>'

            payload = self.plugin.generate_payload(self.config, {'k': 'value'})

            self.assertEqual(payload['url'], 'http://server:1234/path/test')
            self.assertEqual(payload['component'], 'honeybadger.tests.contrib.test_flask')
            self.assertEqual(payload['action'], 'foo')
            self.assertDictEqual(payload['params'], {})
            self.assertDictEqual(payload['session'], {'answer': 42, 'password': '[FILTERED]'})
            self.assertDictEqual(payload['cgi_data'], {
                'Content-Length': '0',
                'Host': 'server:1234',
                'REQUEST_METHOD': 'GET'
            })
            self.assertDictEqual(payload['context'], {'k': 'value'})

    def test_post_request(self):
        with self.app.test_request_context(path='/test', base_url='http://server:1234/path', method='POST',
                                           data={'foo': 'bar', 'password': '<PASSWORD>'}):

            payload = self.plugin.generate_payload(self.config, {'k': 'value'})

            self.assertEqual(payload['url'], 'http://server:1234/path/test')
            self.assertEqual(payload['component'], 'honeybadger.tests.contrib.test_flask')
            self.assertEqual(payload['action'], 'foo')
            self.assertDictEqual(payload['params'], {'foo': ['bar'], 'password': '[FILTERED]'})
            self.assertDictEqual(payload['session'], {})
            self.assertDictEqual(payload['cgi_data'], {
                'Host': 'server:1234',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': '29',
                'REQUEST_METHOD': 'POST'
            })
            self.assertDictEqual(payload['context'], {'k': 'value'})

    def test_put_request(self):
        with self.app.test_request_context(path='/test', base_url='http://server:1234/path', method='PUT',
                                           data={'foo': 'bar', 'password': '<PASSWORD>'}):

            payload = self.plugin.generate_payload(self.config, {'k': 'value'})

            self.assertEqual(payload['url'], 'http://server:1234/path/test')
            self.assertEqual(payload['component'], 'honeybadger.tests.contrib.test_flask')
            self.assertEqual(payload['action'], 'foo')
            self.assertDictEqual(payload['params'], {'foo': ['bar'], 'password': '[<PASSWORD>]'})
            self.assertDictEqual(payload['session'], {})
            self.assertDictEqual(payload['cgi_data'], {
                'Host': 'server:1234',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': '29',
                'REQUEST_METHOD': 'PUT'
            })
            self.assertDictEqual(payload['context'], {'k': 'value'})


class FlaskHoneybadgerTestCase(unittest.TestCase):

    def setUp(self):
        if PY3_2:
{'fraction_non_alphanumeric': 0.1601233875490746, 'fraction_numerical': 0.014769115722564966, 'mean_word_length': 2.296056685150955, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4725563', 'n_tokens_mistral': 3104, 'n_tokens_neox': 2930, 'n_words': 454}