HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/tests/agent/test_models.py
from datetime import datetime

import pytest
import pytz
from sqlalchemy import and_

from buyercall.blueprints.partnership.models import PartnershipAccount
from buyercall.blueprints.agents.models import Agent, AgentSchedule


class TestAgent(object):

    def test_agent_create(self, agents):
        partnership_account = PartnershipAccount.query\
            .filter(PartnershipAccount.name == 'Airstrip One')\
            .first()

        if partnership_account is not None:
            params = {
                'user_id': None,
                'firstname': 'agent first name',
                'lastname': 'agent last name',
                'title': 'mr',
                'email': 'agent.test@buyercall.com',
                'phonenumber': '1234567890',
                'mobile': '1234567890',
                'extension': 0,
                'department': 'None',
                'timezone': 'US/Central',
                'partnership_account_id': partnership_account.id,
                'all_hours': False
            }

            result = Agent.create(params)

            assert result > 0
        else:
            assert False

    @pytest.mark.skip(reason="method not currently used in system")
    def test_agent_update(self, agents):
        agent = Agent.query\
            .filter(Agent.firstname == 'tbd first name')\
            .first()

        if agent is not None:
            result = agent.update(agent.partnership_account_id, 'ms', 'updated first name',
                                  'updated last name', 0, '0987654321', '1122334455',
                                  'test.agent@buyercall.com', 'BDC', True, 'America/Los_Angeles')

            if result:
                agent_check = Agent.query\
                    .filter(Agent.id == agent.id)\
                    .first()

                if agent_check.title == 'ms' and agent_check.firstname == 'updated first name' and \
                    agent_check.lastname == 'updated last name' and agent_check.extension == 0 and \
                    agent_check.phonenumber == '0987654321' and agent_check.mobile == '1122334455' and \
                    agent_check.email == 'test.agent@buyercall.com' and agent_check.department == 'BDC' and \
                    agent_check.all_hours and agent_check.timezone == 'America/Los_Angeles':
                    assert True
            else:
                assert False
        else:
            assert False

    @pytest.mark.skip(reason="method not currently used in system")
    def test_agent_search(self, agents):
        agents = Agent.query \
            .filter(Agent.search('updated first name')).count()

        if agents is not None and len(agents) == 1:
            assert True
        else:
            assert False

    def test_agent_deactivate(self, agents):
        agent = Agent.query\
            .filter(Agent.is_deactivated == False)\
            .first()

        if agent is not None:
            result = Agent.deactivate(agent.id, agent.partnership_account_id)

            if result:
                agent_check = Agent.query\
                    .filter(Agent.id == agent.id)\
                    .first()

                agent_schedule_check = AgentSchedule.query\
                    .filter(and_(AgentSchedule.agent_id == agent.id, AgentSchedule.is_active == False))\
                    .count()

                if agent_check is not None and agent_check.is_deactivated and agent_check.deactivated_on is not None:
                    if agent_schedule_check is not None and int(agent_schedule_check) == 7:
                        assert True
                    else:
                        assert False
                else:
                    assert False
            else:
                assert False
        else:
            assert False

    def test_agent_available_now(self, agents):
        agent = Agent.query\
            .filter(and_(Agent.firstname == 'tbd first name',
                         Agent.lastname == 'tbd last name'))\
            .first()

        timezone = pytz.timezone('US/Central')
        date = datetime.now(pytz.utc)
        local_date = timezone.normalize(date.astimezone(timezone))
        weekday = (local_date.weekday() + 1) % 7
        hour = local_date.hour

        if agent:
            if weekday < 7 and 5 <= hour <= 23:
                assert agent.available_now
            else:
                assert not agent.available_now
        else:
            assert False

    def test_agent_available_not_now(self, agents):
        agent = Agent.query \
            .filter(and_(Agent.firstname == 'the first name',
                         Agent.lastname == 'the last name')) \
            .first()

        if agent and agent.available_now == False:
            assert True
        else:
            assert False

    def test_agent_full_name(self, agents):
        agent = Agent.query \
            .filter(and_(Agent.firstname == 'tbd first name',
                         Agent.lastname == 'tbd last name')) \
            .first()

        assert agent.full_name == 'tbd first name tbd last name'

    def test_agent_contact_leads(self, agents):
        agent = Agent.query \
            .filter(and_(Agent.firstname == 'tbd first name',
                         Agent.lastname == 'tbd last name')) \
            .first()
        assert len(agent.contact_leads) == 1

    def test_agent_outbound_calls(self, agents):
        agent = Agent.query \
            .filter(and_(Agent.firstname == 'tbd first name',
                         Agent.lastname == 'tbd last name')) \
            .first()

        assert len(agent.outbound_calls) == 1

    @pytest.mark.skip(reason="method not currently used in system")
    def test_agent_outbound_messages(self):
        assert True

    def test_agent_type_agent(self, agents):
        agent = Agent.query \
            .filter(and_(Agent.firstname == 'tbd first name',
                         Agent.lastname == 'tbd last name')) \
            .first()

        assert agent.type == 'Agent'

    def test_agent_type_group(self, agents):
        agent = Agent.query \
            .filter(and_(Agent.firstname == 'the first name',
                         Agent.lastname == 'the last name')) \
            .first()

        assert agent.type == 'Group'


class TestAgentSchedule(object):
    def test_agent_schedule_create(self):
        agent = Agent.query\
            .filter(and_(Agent.firstname == 'new first name', Agent.lastname == 'new last name'))\
            .first()

        if agent is not None:

            day0param = {
                'day': 0,
                'available_from': '05:00 AM',
                'available_to': '06:00 AM',
                'is_active': True,
                'partnership_account_id': agent.partnership_account_id,
                'agent_id': agent.id
            }

            day1param = {
                'day': 1,
                'available_from': '05:00 AM',
                'available_to': '06:00 AM',
                'is_active': True,
                'partnership_account_id': agent.partnership_account_id,
                'agent_id': agent.id
            }

            day2param = {
                'day': 2,
                'available_from': '05:00 AM',
                'available_to': '06:00 AM',
                'is_active': True,
                'partnership_account_id': agent.partnership_account_id,
                'agent_id': agent.id
            }

            day3param = {
                'day': 3,
                'available_from': '05:00 AM',
                'available_to': '06:00 AM',
                'is_active': True,
                'partnership_account_id': agent.partnership_account_id,
                'agent_id': agent.id
            }

            day4param = {
                'day': 4,
                'available_from': '05:00 AM',
                'available_to': '06:00 AM',
                'is_active': True,
                'partnership_account_id': agent.partnership_account_id,
                'agent_id': agent.id
            }

            day5param = {
                'day': 5,
                'available_from': '05:00 AM',
                'available_to': '06:00 AM',
                'is_active': True,
                'partnership_account_id': agent.partnership_account_id,
                'agent_id': agent.id
            }

            day6param = {
                'day': 6,
                'available_from': '05:00 AM',
                'available_to': '06:00 AM',
                'is_active': True,
                'partnership_account_id': agent.partnership_account_id,
                'agent_id': agent.id
            }

            days = [day0param, day1param, day2param, day3param, day4param, day5param, day6param]

            result = AgentSchedule.create(*days)

            agent_schedule_count = AgentSchedule.query.filter(AgentSchedule.agent_id == agent.id).count()

            if result and agent_schedule_count == 7:
                assert True
            else:
                assert False
        else:
            assert False

    def test_agent_schedule_update(self):
        agent = Agent.query.filter(Agent.firstname == 'the first name').first()

        if agent is not None:
            agent_schedule = AgentSchedule.query\
                .filter(and_(AgentSchedule.agent_id == agent.id, AgentSchedule.day == 0))\
                .first()

            if agent_schedule is not None:

                AgentSchedule.update(
                    agent_schedule.id,
                    '01:00 AM',
                    '02:00 AM',
                    False,
                    agent.partnership_account_id
                )

                agent_schedule = AgentSchedule.query \
                    .filter(and_(AgentSchedule.agent_id == agent.id, AgentSchedule.day == 0)) \
                    .first()

                if agent_schedule.available_from == '01:00 AM' and \
                    agent_schedule.available_to == '02:00 AM' and \
                    agent_schedule.is_active == False:

                    assert True
                else:
                    assert False
            else:
                assert False
        else:
            assert False

    def test_agent_schedule_deactivate_agent_found(self):
        agent = Agent.query\
            .filter(Agent.firstname == 'the first name')\
            .first()

        if agent is not None:
            result = AgentSchedule.deactivate(agent.id, agent.partnership_account_id)

            agent_schedules = AgentSchedule.query\
                .filter(and_(AgentSchedule.agent_id == agent.id, AgentSchedule.is_active == False)).count()

            if result and agent_schedules == 7:
                assert True
            else:
                assert False
        else:
            assert False

    def test_agent_schedule_deactivate_agent_not_found(self):
        result = AgentSchedule.deactivate(69, 69)

        agent_schedules = AgentSchedule.query \
            .filter(and_(AgentSchedule.agent_id == 69, AgentSchedule.is_active == False)).count()

        if not result and agent_schedules == 0:
            assert True
        else:
            assert False