<commit_before><commit_msg>Add decorator tests<commit_after>"""Test jinja filters."""

import unittest

from flask import Flask

from flask_extras import FlaskExtras
from flask_extras import decorators

app = Flask('test_flask_jsondash')
app.debug = True
app.secret_key = 'Foo'
FlaskExtras(app)


@app.route('/xhr-custom')
@decorators.xhr_only(status_code=400)
def foo_xhr_custom():
    return ''


@app.route('/xhr')
@decorators.xhr_only()
def foo_xhr():
    return ''


@app.route('/args')
@decorators.require_args(params=['foo', 'bar'])
def foo_args():
    return ''


@app.route('/cookies')
@decorators.require_cookies(['foo'])
def foo_cookies():
    return ''


@app.route('/headers')
@decorators.require_headers(headers=['X-Foo'])
def foo_headers():
    return ''

client = app.test_client()


class DecoratorTest(unittest.TestCase):
    """All tests for title function."""


class XhsTest(DecoratorTest):
    """All tests for title function."""

    def test_invalid_xhr_decorator(self):
        """Test the function with None argument."""
        with app.app_context():
            res = client.get('/xhr')
            self.assertEqual(res.status_code, 415)

    def test_invalid_xhr_custom_decorator(self):
        """Test the function with None argument."""
        with app.app_context():
            res = client.get('/xhr-custom')
            self.assertEqual(res.status_code, 400)


class RequireArgsTest(DecoratorTest):
    """All tests for title function."""

    def test_invalid_require_args_decorator(self):
        """Test the function with None argument."""
        with self.assertRaises(ValueError):
            with app.app_context():
                client.get('/args')

    def test_valid_require_args_decorator(self):
        """Test the function with None argument."""
        with app.app_context():
            res = client.get('/args?foo=1&bar=1')
            self.assertEqual(res.status_code, 200)


class RequireCookiesTest(DecoratorTest):
    """All tests for title function."""

    def test_invalid_require_cookies_decorator(self):
        """Test the function with None argument."""
        with self.assertRaises(ValueError):
            with app.app_context():
                client.get('/cookies')


class RequireHeadersTest(DecoratorTest):
    """All tests for title function."""

    def test_invalid_require_headers_decorator(self):
        """Test the function with None argument."""
        with self.assertRaises(ValueError):
            with app.app_context():
                client.get('/headers')

    def test_valid_require_headers_decorator(self):
        """Test the function with None argument."""
        with app.app_context():
            res = client.get('/headers', headers={'X-Foo': 'Foo'})
            self.assertEqual(res.status_code, 200)

{'fraction_non_alphanumeric': 0.1326676176890157, 'fraction_numerical': 0.006062767475035664, 'mean_word_length': 3.25, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5597110', 'n_tokens_mistral': 863, 'n_tokens_neox': 817, 'n_words': 186}