<commit_before># -*- coding: utf-8 -*-

import urlparse
import base64
import logging
import unittest
from StringIO import StringIO

logging.basicConfig(level=logging.DEBUG)

JSON_HEADERS = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
}

class BaseTestCase(unittest.TestCase):

    def setUp(self):
        self.log = StringIO()        
        unittest.TestCase.setUp(self)

    def assertInLog(self, msg):
        self.assertTrue(msg in self.log.getvalue())
        
    def assertNotInLog(self, msg):
        self.assertFalse(msg in self.log.getvalue())

class BaseFlaskTestCase(BaseTestCase):
    
    CONFIG = "mongrey.web.settings.Test"
    
    def _create_app(self):
        raise NotImplementedError
        
    def setUp(self):
        BaseTestCase.setUp(self)
        self.flask_app = self._create_app()
        self.client = self.flask_app.test_client()
        self._ctx = self.flask_app.test_request_context()
        self._ctx.push()
        from flask import json
        self.json_mod = json

    def tearDown(self):
        BaseTestCase.tearDown(self)
        _ctx = getattr(self, '_ctx', None)
        if self._ctx:
            self._ctx.pop()

    def login_basic_headers(self, username=None, password=None):
        headers = {}
        #'Authorization': 'Basic ' + b64encode("{0}:{1}".format(username, password))
        headers['Authorization'] = 'Basic ' + base64.b64encode(('%s:%s' % (username, password)).encode('latin1')).strip().decode('latin1')
        return headers

    def logout(self, url=None):
        url = '/logout'
        return self.client.get(url)

    def assertStatusCode(self, response, status_code):
        self.assertEquals(status_code, response.status_code)
        return response

    def assertOk(self, response):
        return self.assertStatusCode(response, 200)
    assert_200 = assertOk

    def assertBadRequest(self, response):
        return self.assertStatusCode(response, 400)
    assert_400 = assertBadRequest

    def assertRequireAuth(self, response):
        return self.assertStatusCode(response, 401)
    assert_401 = assertRequireAuth<commit_msg>add features for web tests<commit_after># -*- coding: utf-8 -*-

import urlparse
import base64
import logging
import unittest
from StringIO import StringIO

logging.basicConfig(level=logging.DEBUG)

JSON_HEADERS = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
}

class BaseTestCase(unittest.TestCase):

    def setUp(self):
        self.log = StringIO()        
        unittest.TestCase.setUp(self)

    def assertInLog(self, msg):
        self.assertTrue(msg in self.log.getvalue())
        
    def assertNotInLog(self, msg):
        self.assertFalse(msg in self.log.getvalue())

class BaseFlaskTestCase(BaseTestCase):
    
    CONFIG = "mongrey.web.settings.Test"
    
    def _create_app(self):
        raise NotImplementedError
        
    def setUp(self):
        BaseTestCase.setUp(self)
        
        self.flask_app = self._create_app()
        self.client = self.flask_app.test_client()
        self._ctx = self.flask_app.test_request_context()
        self._ctx.push()

        from flask import json
        from flask import template_rendered
        self.json_mod = json
        self.templates = []
        template_rendered.connect(self._add_template)

    def tearDown(self):
        BaseTestCase.tearDown(self)
        from flask import template_rendered
        template_rendered.disconnect(self._add_template)
        _ctx = getattr(self, '_ctx', None)
        if self._ctx:
            self._ctx.pop()

    def login_basic_headers(self, username=None, password=None):
        headers = {}
        #'Authorization': 'Basic ' + b64encode("{0}:{1}".format(username, password))
        headers['Authorization'] = 'Basic ' + base64.b64encode(('%s:%s' % (username, password)).encode('latin1')).strip().decode('latin1')
        return headers

    def logout(self, url=None):
        url = '/logout'
        return self.client.get(url)

    def get_context_variable(self, name):
    
        for template, context in self.templates:
            if name in context:
                return context[name]

    def _add_template(self, app, template, context, **kwargs):

        if len(self.templates) > 0:
            self.templates = []
        
        self.templates.append((template, context))
        
    def assertStatusCode(self, response, status_code):
        self.assertEquals(status_code, response.status_code)
        return response

    def assertOk(self, response):
        return self.assertStatusCode(response, 200)
    assert_200 = assertOk

    def assertBadRequest(self, response):
        return self.assertStatusCode(response, 400)
    assert_400 = assertBadRequest

    def assertRequireAuth(self, response):
        return self.assertStatusCode(response, 401)
    assert_401 = assertRequireAuth
{'fraction_non_alphanumeric': 0.11020574455082502, 'fraction_numerical': 0.012833570992055409, 'mean_word_length': 2.7885802469135803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '7518816', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1422, 'n_words': 298}