<commit_before>import datetime
import simplejson
import random
import functools
import time

from django.db import IntegrityError
from django.test import TestCase, LiveServerTestCase
from django.http import HttpResponse, Http404

from tracker.models import (Tbluser,
                            TrackingEntry,
                            Tblauthorization)

from utils.calendar_utils import (validate_time, parse_time,
                                              delete_user, useredit,
                                              mass_holidays, ajax_delete_entry,
                                              gen_calendar, ajax_change_entry,
                                              ajax_error)
from utils.datemaps import pad, float_to_time, generate_select, ABSENT_CHOICES
from utils.error_codes import DUPLICATE_ENTRY

try:
    from selenium.webdriver.firefox.webdriver import WebDriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver.common.keys import Keys
    SELENIUM_AVAILABLE = True
except ImportError:
    SELENIUM_AVAILABLE = False

def check_selenium(f):
    if not SELENIUM_AVAILABLE:
        return
    @functools.wraps(f)
    def inner(args):
        f(args)
    return inner

def create_users(cls):
    # we create users which will be linked,
    # to test how the automatic retrieval
    # of the links works
    cls.linked_manager = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="ADMIN",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

    cls.linked_user = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="RUSER",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

    cls.linked_teamlead = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="TEAML",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

    # create the links for the linked users
    cls.authorization = Tblauthorization.objects.create(admin=cls.linked_manager)
    cls.authorization.save()
    cls.authorization.users.add(cls.linked_user, cls.linked_teamlead)
    cls.authorization.save()

    cls.unlinked_manager = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="ADMIN",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

    cls.unlinked_user = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="RUSER",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

    cls.unlinked_teamlead = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="TEAML",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

def delete_users(cls):
    [user.delete() for user in Tbluser.objects.all()]

class BaseUserTest(TestCase):

    def setUp(self):

        create_users(self)

        # create a new_user dict to share among tests
        self.new_user = {
            'mode': "false",
            'user_id': "<EMAIL>",
            'firstname': "New",
            'lastname': "Test",
            'user_type': "RUSER",
            'market': "BK",
            'process': "AR",
            'start_date': "2012-01-01",
            'breaklength': "00:15:00",
            'shiftlength': "08:00:00",
            'job_code': "ABCDE",
            'holiday_balance': 20
        }

        # create some random holiday data
        holidays = {
            self.linked_manager.id: list(),
            self.linked_user.id: list(),
            }
        holidays_empty = {
            self.linked_manager.id: list(),
            self.linked_user.id: list(),
            }
        for day in range(1, 32):
            holidays[self.linked_manager.id].append(random.choice(ABSENT_CHOICES)[0])
            holidays[self.linked_user.id].append(random.choice(ABSENT_CHOICES)[0])
            holidays_empty[self.linked_manager.id].append("empty")
            holidays_empty[self.linked_user.id].append("empty")
        self.holiday_data = simplejson.dumps(holidays)
        self.holiday_data_empty = simplejson.dumps(holidays_empty)

        class Request(object):
            def __init__(self, model_id):
                self.session = {
                    'user_id': model_id
                    }

                self.POST = {}


            def is_ajax(self):
                return True

        self.linked_manager_request = Request(self.linked_manager.id)
        self.linked_teamlead_request = Request(self.linked_teamlead.id)
        self.linked_user_request = Request(self.linked_user.id)
        self.unlinked_manager_request = Request(self.unlinked_manager.id)
        self.unlinked_teamlead_request = Request(self.unlinked_teamlead.id)
        self.unlinked_user_request = Request(self.unlinked_user.id)

    def tearDown(self):
        del(self.linked_manager_request)
        delete_users(self)
        [holiday.delete() for holiday in TrackingEntry.objects.all()]

class UserTestCase(BaseUserTest):
    '''
    Tests the methods attached to user instances
    '''

    def testIsAdmin(self):
        '''
        Only managers and team leaders are admins
        '''
        self.assertEquals(self.linked_manager.is_admin(), True)
        self.assertNotEquals(self.linked_manager.is_admin(), False)
        self.assertEquals(self.linked_user.is_admin(), False)
        self.assertNotEquals(self.linked_user.is_admin(), True)

    def testName(self):
        '''
        Should return the full name
        '''
        self.assertEquals(self.linked_user.name(), "test case")
        self.assertEquals(self.linked_manager.name(), "test case")

    def testUserType(self):
        '''
        Make sure our types return what we set them up as
        '''
        self.assertEquals(self.linked_manager.display_user_type(), "ADMIN")
        self.assertEquals(self.linked_user.display_user_type(), "RUSER")

    def testHolidayBalanceMix(self):
        '''
        Test to make sure that the holiday balance calculates correctly
        '''<commit_msg>More tests.<commit_after>import datetime
import simplejson
import random
import functools
import time

from django.db import IntegrityError
from django.test import TestCase, LiveServerTestCase
from django.http import HttpResponse, Http404

from tracker.models import (Tbluser,
                            TrackingEntry,
                            Tblauthorization)

from utils.calendar_utils import (validate_time, parse_time,
                                              delete_user, useredit,
                                              mass_holidays, ajax_delete_entry,
                                              gen_calendar, ajax_change_entry,
                                              ajax_error)
from utils.datemaps import pad, float_to_time, generate_select, ABSENT_CHOICES
from utils.error_codes import DUPLICATE_ENTRY

try:
    from selenium.webdriver.firefox.webdriver import WebDriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver.common.keys import Keys
    SELENIUM_AVAILABLE = True
except ImportError:
    SELENIUM_AVAILABLE = False

def check_selenium(f):
    if not SELENIUM_AVAILABLE:
        return
    @functools.wraps(f)
    def inner(args):
        f(args)
    return inner

def create_users(cls):
    # we create users which will be linked,
    # to test how the automatic retrieval
    # of the links works

    cls.linked_super_user = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="SUPER",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )


    cls.linked_manager = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="ADMIN",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="<PASSWORD>",
        holiday_balance=20
        )

    cls.linked_user = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="RUSER",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

    users = [
    Tbluser.objects.create(
            user_id="<EMAIL>" % userid,
            firstname="test",
            lastname="case",
            password="password",
            user_type="RUSER",
            market="BG",
            process="AP",
            start_date=datetime.datetime.today(),
            breaklength="00:15:00",
            shiftlength="08:00:00",
            job_code="00F20G",
            holiday_balance=20
            )
    for userid in range(5)]
    users.append(
        Tbluser.objects.create(
            user_id="<EMAIL>",
            firstname="test",
            lastname="case",
            password="password",
            user_type="RUSER",
            market="BG",
            process="AO",
            start_date=datetime.datetime.today(),
            breaklength="00:15:00",
            shiftlength="08:00:00",
            job_code="00F20G",
            holiday_balance=20
            )
        )

    cls.linked_teamlead = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="TEAML",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

    # create the links for the linked users
    cls.authorization = Tblauthorization.objects.create(admin=cls.linked_manager)
    cls.authorization.save()
    cls.authorization.users.add(cls.linked_user, cls.linked_teamlead)
    [cls.authorization.users.add(user) for user in users]
    cls.authorization.save()

    cls.supauthorization = Tblauthorization.objects.create(admin=cls.linked_super_user)
    cls.supauthorization.users.add(cls.linked_manager, cls.linked_teamlead, cls.linked_user)
    cls.supauthorization.save()

    cls.unlinked_super_user = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="SUPER",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

    cls.unlinked_manager = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="ADMIN",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

    cls.unlinked_user = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="RUSER",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

    cls.unlinked_teamlead = Tbluser.objects.create(
        user_id="<EMAIL>",
        firstname="test",
        lastname="case",
        password="password",
        user_type="TEAML",
        market="BG",
        process="AP",
        start_date=datetime.datetime.today(),
        breaklength="00:15:00",
        shiftlength="08:00:00",
        job_code="00F20G",
        holiday_balance=20
        )

def delete_users(cls):
    [user.delete() for user in Tbluser.objects.all()]

class BaseUserTest(TestCase):

    def setUp(self):

        create_users(self)

        # create a new_user dict to share among tests
        self.new_user = {
            'mode': "false",
            'user_id': "<EMAIL>",
            'firstname': "New",
            'lastname': "Test",
            'user_type': "RUSER",
            'market': "BK",
            'process': "AR",
            'start_date': "2012-01-01",
            'breaklength': "00:15:00",
            'shiftlength': "08:00:00",
            'job_code': "ABCDE",
            'holiday_balance': 20
        }

        # create some random holiday data
        holidays = {
            self.linked_manager.id: list(),
            self.linked_user.id: list(),
            }
        holidays_empty = {
            self.linked_manager.id: list(),
            self.linked_user.id: list(),
            }
        for day in range(1, 32):
            holidays[self.linked_manager.id].append(random.choice(ABSENT_CHOICES)[0])
            holidays[self.linked_user.id].append(random.choice(ABSENT_CHOICES)[0])
            holidays_empty[self.linked_manager.id].append("empty")
            holidays_empty[self.linked_user.id].append("empty")
        self.holiday_data = simplejson.dumps(holidays)
        self.holiday_data_empty = simplejson.dumps(holidays_empty)

        class Request(object):
            def __init__(self, model_id):
                self.session = {
                    'user_id': model_id
                    }

                self.POST = {}


            def is_ajax(self):
                return True

        self.linked_manager_request = Request(self.linked_manager.id)
        self.linked_teamlead_request = Request(self.linked_teamlead.id)
        self.linked_user_request = Request(self.linked_user.id)
        self.unlinked_manager_request = Request(self.unlinked_manager.id)
        self.unlinked_teamlead_request = Request(self.unlinked_teamlead.id)
        self.unlinked_user_request = Request(self.unlinked_user.id)

    def tearDown(self):
        del(self.linked_manager_request)
        delete_users(self)
        [holiday.delete() for holiday in TrackingEntry.objects.all()]

class UserTestCase(BaseUserTest):
    '''
    Tests the methods attached to user instances
    '''

    def test_Sup_TL_or_Admin(self):
        self.assertEquals(self.linked_super_user.sup_tl_or_admin(), True)
        self.assertEquals(self.linked_manager.sup_tl_or_admin(), True)
        self.assertEquals(self.linked_teamlead.sup_tl_or_admin(), True)
        self.assertEquals(self.linked_user.sup_tl_or_admin(), False)

    def test_IsSuper(self):
        self.assertEquals(self.linked_super_user.is_super(), True)
        self.assertEquals(self.linked_manager.is_super(), False)
        self.assertEquals(self.linked_teamlead.is_super(), False)
        self.assertEquals(self.linked_user.is_super(), False)

    def test_Super_Or_Admin(self):
        self.assertEquals(self.linked_super_user.super_or_admin(), True)
        self.assertEquals(self.linked_manager.super_or_admin(), True)
        self.assertEquals(self.linked_teamlead.super_or_admin(), False)
        self.assertEquals(self.linked_user.super_or_admin(), False)

    def test_IsAdmin(self):
        self.assertEquals(self.linked_super_user.is_admin(), False)
        self.assertEquals(self.linked_manager.is_admin(), True)
        self.assertEquals(self.linked_teamlead.is_admin(), False)
        self.assertEquals(self.linked_user.is_admin(), False)

    def test_Admin_or_TL(self):
        self.assertEquals(self.linked_super_user.admin_or_tl(), False)
        self.assertEquals(self.linked_manager.admin_or_tl(), True)
        self.assertEquals(self.linked_teamlead.admin_or_tl(), True)
        self.assertEquals(self.linked_user.admin_or_tl(), False)

    def test_IsTL(self):
        self.assertEquals(self.linked_super_user.is_tl(), False)
        self.assertEquals(self.linked_manager.is_tl(), False)
        self.assertEquals(self.linked_teamlead.is_tl(), True)
        self.assertEquals(self.linked_user.is_tl(), False)

    def test_IsUSER(self):
        self.assertEquals(self.linked_super_user.is_user(), False)
        self.assertEquals(self.linked_manager.is_user(), False)
        self.assertEquals(self.linked_teamlead.is_user(), False)
        self.assertEquals(self.linked_user.is_user(), True)

    def test_get_subordinates(self):
        self.assertEquals(len(self.linked_super_user.get_subordinates()), 3)
        self.assertEquals(len(self.linked_manager.get_subordinates()), 8)
        self.assertEquals(len(self.linked_teamlead.get_subordinates()), 8)
        self.assertEquals(len(self.linked_user.get_subordinates()), 7)

    def testName(self):
        '''
        Should return the full name
        '''
        self.assertEquals(self.linked_user.name(), "test case")
        self.assertEquals(self.linked_manager.name(), "test case")

    def testUserType(self):
        '''
        Make sure our types return what we set them up as
        '''
        self.assertEquals(self.linked_super_user.user_type, "SUPER")
        self.assertEquals(self.linked_manager.user_type, "ADMIN")
        self.assertEquals(self.linked_teamlead.user_type, "TEAML")
        self.assertEquals(self.linked_user.user_type, "RUSER")

    def testHolidayBalanceMix(self):
        '''
        Test to make sure that the holiday balance calculates correctly
        '''
{'fraction_non_alphanumeric': 0.11464766429136976, 'fraction_numerical': 0.0184217471628398, 'mean_word_length': 2.3157157857892896, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1600077', 'n_tokens_mistral': 5800, 'n_tokens_neox': 5534, 'n_words': 890}