<commit_before>import copy
import os
import unittest

from pymongo import MongoClient
from webob.headers import ResponseHeaders

from hel.utils.messages import Messages
from hel.utils.tests import sample_packages as s_pkgs


deleted = False
auth_headers = None
mongodb_url = 'mongodb://localhost:37017'
if 'HEL_TESTING_MONGODB_ADDR' in os.environ:
    mongodb_url = os.environ['HEL_TESTING_MONGODB_ADDR']


class FunctionalTests(unittest.TestCase):

    user = {
        'nickname': 'root',
        'password': <PASSWORD>',
        'email': '<EMAIL>'
    }

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)
        global deleted
        if not deleted:
            mongo = MongoClient(mongodb_url)
            db = mongo.hel
            users = db['users']
            users.delete_many({})
            mongo.close()
            deleted = True

    def setUp(self):
        settings = {
            'activation.length': 64,
            'activation.time': 10,
            'mongo_db_url': mongodb_url
        }
        from hel import main
        app = main({}, **settings)
        from webtest import TestApp
        self.test_app = TestApp(app)

    def test_home(self):
        res = self.test_app.get('/', status=200)
        self.assertTrue(len(res.forms) > 0)

    def test_unexisting_page(self):
        self.test_app.get('/thispagedoesnotexist', status=404)

    def test_bad_log_in(self):
        res = self.test_app.post('/', {
                'password': 'hi',
                'log-in': True
            }, status=200)
        self.assertIsNone(res.html.find(id='log-out'))
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.bad_request)

    def test_failed_log_in(self):
        res = self.test_app.post('/', {
                'password': 'totally-random-passwd',
                'nickname': '-n0-such-user',
                'log-in': True
            }, status=200)
        self.assertIsNone(res.html.find(id='log-out'))

    def test_failed_log_in_empty_nick(self):
        res = self.test_app.post('/', {
                'password': 'hi',
                'nickname': '',
                'log-in': True
            }, status=200)
        self.assertIsNone(res.html.find(id='log-out'))
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.empty_nickname)

    def test_failed_log_in_empty_pass(self):
        res = self.test_app.post('/', {
                'password': '',
                'nickname': 'test',
                'log-in': True
            }, status=200)
        self.assertIsNone(res.html.find(id='log-out'))
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.empty_password)

    def test_bad_reg(self):
        res = self.test_app.post('/', {
                'email': '',
                'nickname': '',
                'passwd-confirm': '',
                'register': True
            }, status=200)
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.bad_request)

    def test_failed_reg_empty_nick(self):
        res = self.test_app.post('/', {
                'email': '',
                'nickname': '',
                'password': '',
                'passwd-confirm': '',
                'register': True
            }, status=200)
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.empty_nickname)

    def test_failed_reg_empty_email(self):
        res = self.test_app.post('/', {
                'email': '',
                'nickname': self.user['nickname'],
                'password': '',
                'passwd-confirm': '',
                'register': True
            }, status=200)
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.empty_email)

    def test_failed_reg_empty_password(self):
        res = self.test_app.post('/', {
                'email': self.user['email'],
                'nickname': self.user['nickname'],
                'password': '',
                'passwd-confirm': '',
                'register': True
            }, status=200)
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.empty_password)

    def test_failed_reg_nick_use(self):
        data = copy.copy(self.user)
        data['register'] = True
        data['passwd-confirm'] = 'hi'
        res = self.test_app.post('/', data, status=200)
        self.assertIsNone(res.html.find(id='log-out'))
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.password_mismatch)

    def test_reg_success(self):
        data = copy.copy(self.user)
        data['register'] = True
        data['passwd-confirm'] = data['password']
        res = self.test_app.post('/', data, status=200)
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.account_created_success)
        client = MongoClient(mongodb_url)
        users = [x for x in client.hel['users'].find()]
        client.close()
        self.assertEqual(len(users), 1)
        self.assertEqual(users[0]['nickname'], data['nickname'])

    def test_no_action_auth(self):
        res = self.test_app.post_json('/auth', {
            'oh': 'well'
        }, status=400)
        self.assertEqual(res.json['info'], Messages.bad_request)

    def test_bad_json_auth(self):
        res = self.test_app.post('/auth', """hey there
            'oh': 'well'
        },""", status=400)
        self.assertEqual(res.json['info'], Messages.bad_request)

    def test_teapot(self):
        self.test_app.post('/teapot', status=418)

    def test_cors_headers(self):
        res = self.test_app.post('/', status=200)
        self.assertTrue(any(v[0] == 'Access-Control-Allow-Origin' and
                            v[1] == '*' for v in res.headerlist))


class FunctionalAuthTests(unittest.TestCase):

    user = FunctionalTests.user

    setUp = FunctionalTests.setUp
    tearDown = FunctionalTests.tearDown

    def test_failed_log_in_pass(self):
        data = copy.copy(self.user)
        data['log-in'] = True
        data['password'] = '<PASSWORD>'
        res = self.test_app.post('/', data, status=200)
        self.assertIsNone(res.html.find(id='log-out'))<commit_msg>Access the right field in tests<commit_after>import copy
import os
import unittest

from pymongo import MongoClient
from webob.headers import ResponseHeaders

from hel.utils.messages import Messages
from hel.utils.tests import sample_packages as s_pkgs


deleted = False
auth_headers = None
mongodb_url = 'mongodb://localhost:37017'
if 'HEL_TESTING_MONGODB_ADDR' in os.environ:
    mongodb_url = os.environ['HEL_TESTING_MONGODB_ADDR']


class FunctionalTests(unittest.TestCase):

    user = {
        'nickname': 'root',
        'password': <PASSWORD>',
        'email': '<EMAIL>'
    }

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)
        global deleted
        if not deleted:
            mongo = MongoClient(mongodb_url)
            db = mongo.hel
            users = db['users']
            users.delete_many({})
            mongo.close()
            deleted = True

    def setUp(self):
        settings = {
            'activation.length': 64,
            'activation.time': 10,
            'mongo_db_url': mongodb_url
        }
        from hel import main
        app = main({}, **settings)
        from webtest import TestApp
        self.test_app = TestApp(app)

    def test_home(self):
        res = self.test_app.get('/', status=200)
        self.assertTrue(len(res.forms) > 0)

    def test_unexisting_page(self):
        self.test_app.get('/thispagedoesnotexist', status=404)

    def test_bad_log_in(self):
        res = self.test_app.post('/', {
                'password': 'hi',
                'log-in': True
            }, status=200)
        self.assertIsNone(res.html.find(id='log-out'))
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.bad_request)

    def test_failed_log_in(self):
        res = self.test_app.post('/', {
                'password': '<PASSWORD>',
                'nickname': '-n0-such-user',
                'log-in': True
            }, status=200)
        self.assertIsNone(res.html.find(id='log-out'))

    def test_failed_log_in_empty_nick(self):
        res = self.test_app.post('/', {
                'password': 'hi',
                'nickname': '',
                'log-in': True
            }, status=200)
        self.assertIsNone(res.html.find(id='log-out'))
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.empty_nickname)

    def test_failed_log_in_empty_pass(self):
        res = self.test_app.post('/', {
                'password': '',
                'nickname': 'test',
                'log-in': True
            }, status=200)
        self.assertIsNone(res.html.find(id='log-out'))
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.empty_password)

    def test_bad_reg(self):
        res = self.test_app.post('/', {
                'email': '',
                'nickname': '',
                'passwd-confirm': '',
                'register': True
            }, status=200)
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.bad_request)

    def test_failed_reg_empty_nick(self):
        res = self.test_app.post('/', {
                'email': '',
                'nickname': '',
                'password': '',
                'passwd-confirm': '',
                'register': True
            }, status=200)
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.empty_nickname)

    def test_failed_reg_empty_email(self):
        res = self.test_app.post('/', {
                'email': '',
                'nickname': self.user['nickname'],
                'password': '',
                'passwd-confirm': '',
                'register': True
            }, status=200)
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.empty_email)

    def test_failed_reg_empty_password(self):
        res = self.test_app.post('/', {
                'email': self.user['email'],
                'nickname': self.user['nickname'],
                'password': '',
                'passwd-confirm': '',
                'register': True
            }, status=200)
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.empty_password)

    def test_failed_reg_nick_use(self):
        data = copy.copy(self.user)
        data['register'] = True
        data['passwd-confirm'] = 'hi'
        res = self.test_app.post('/', data, status=200)
        self.assertIsNone(res.html.find(id='log-out'))
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.password_mismatch)

    def test_reg_success(self):
        data = copy.copy(self.user)
        data['register'] = True
        data['passwd-confirm'] = data['password']
        res = self.test_app.post('/', data, status=200)
        message = res.html.find(id='login-message')
        self.assertIsNotNone(message)
        self.assertEqual(message.string, Messages.account_created_success)
        client = MongoClient(mongodb_url)
        users = [x for x in client.hel['users'].find()]
        client.close()
        self.assertEqual(len(users), 1)
        self.assertEqual(users[0]['nickname'], data['nickname'])

    def test_no_action_auth(self):
        res = self.test_app.post_json('/auth', {
            'oh': 'well'
        }, status=400)
        self.assertEqual(res.json['message'], Messages.bad_request)

    def test_bad_json_auth(self):
        res = self.test_app.post('/auth', """hey there
            'oh': 'well'
        },""", status=400)
        self.assertEqual(res.json['message'], Messages.bad_request)

    def test_teapot(self):
        self.test_app.post('/teapot', status=418)

    def test_cors_headers(self):
        res = self.test_app.post('/', status=200)
        self.assertTrue(any(v[0] == 'Access-Control-Allow-Origin' and
                            v[1] == '*' for v in res.headerlist))


class FunctionalAuthTests(unittest.TestCase):

    user = FunctionalTests.user

    setUp = FunctionalTests.setUp
    tearDown = FunctionalTests.tearDown

    def test_failed_log_in_pass(self):
        data = copy.copy(self.user)
        data['log-in'] = True
        data['password'] = '<PASSWORD>'
        res = self.test_app.post('/', data, status=200)
        self.assertIsNone(res.html.find(id='log-out'))
{'fraction_non_alphanumeric': 0.13983955251343197, 'fraction_numerical': 0.009715168911459484, 'mean_word_length': 2.1629422718808193, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, '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': '1964977', 'n_tokens_mistral': 4261, 'n_tokens_neox': 4049, 'n_words': 666}