def _check_if_uuid(branch_ids):
    for branch_id in branch_ids:
        try:
            UUID(branch_id)
        except ValueError:
            message = 'os IDs das lojas devem ser do tipo UUID'
            log.error(message)
            abort(400, message) def _get_card_name(card_type, provider_short_name):
    card_name = ' '.join([card_type_map[card_type], provider_short_name]).upper()
    return card_name_map.get(card_name, card_name) def _get_card_description(card_type, provider_short_name):
    return _get_card_name(card_type, provider_short_name) def _get_category_info(sellable):
    return {
        'idGrupo': sellable.category and sellable.category.id,
        'codigo': sellable.category and sellable.category.id,
        'descricao': sellable.category and sellable.category.description,
        'idGrupoPai': sellable.category and sellable.category.category_id,
        'dataAlteracao': sellable.category and
        sellable.category.te.te_server.strftime('%Y-%m-%d %H:%M:%S -0300'),
        'ativo': True,
    } def _get_payment_method_with_provider_code(card_type, provider):
    return (card_type + '_' + provider.short_name.replace(" ", "")).lower() def _get_credit_provider_description(credit_provider):
    return credit_provider.short_name def _get_payment_method_description(payment_method):
    return _get_payment_method_name(payment_method.method_name) def _get_payment_method_name(payment_method_name):
    return method_name_map[payment_method_name] def _get_person_names(person):
    name = person.name
    if ' ' in name:
        firstname, lastname = name.split(' ', maxsplit=1)
    else:
        firstname = name
        lastname = '.'

    return {
        'primeiroNome': firstname,
        'segundoNome': None,
        'sobrenome': lastname,
        'apelido': firstname,
    } def generate_b1food_token(size=128):
    import string
    import random

    chars = string.ascii_uppercase + string.ascii_lowercase + string.digits
    return ''.join(random.choice(chars) for _ in range(size))
import json
import pytest
from unittest import mock
from datetime import datetime
from storm.expr import Ne, Select
from stoqlib.domain.overrides import ProductBranchOverride
from stoqlib.domain.payment.card import CreditCardData, CreditProvider
from stoqlib.domain.payment.method import PaymentMethod
from stoqlib.domain.payment.payment import Payment
from stoqlib.domain.person import LoginUser
from stoqlib.domain.sale import Sale
from stoqlib.domain.sellable import Sellable
from stoqlib.domain.station import BranchStation
from stoqlib.domain.till import Till
from stoqlib.lib.formatters import raw_document
from stoqlib.lib.parameters import sysparam
from stoqserver.api.resources.b1food import (_check_if_uuid,
                                             _get_card_name,
                                             _get_card_description,
                                             _get_category_info,
                                             _get_payment_method_with_provider_code,
                                             _get_credit_provider_description,
                                             _get_payment_method_description,
                                             _get_payment_method_name,
                                             _get_person_names,
                                             generate_b1food_token)
    test_sale = example_creator.create_sale()
    test_sale.branch = current_station.branch
    test_sale.open_date = datetime.strptime('2020-01-02', '%Y-%m-%d')
    test_sale.confirm_date = datetime.strptime('2020-01-02', '%Y-%m-%d')
    sale_item = example_creator.create_sale_item(test_sale)
    sale_item.price = 10
    sale_item.base_price = 15
    sellable_category = example_creator.create_sellable_category(description='Category 1')
    sale_item.sellable.category = sellable_category
    sale_item.sellable.code = '1111111111111'
    client = example_creator.create_client()
    client.person.individual.cpf = '737.948.760-40'
    test_sale.client = client
    person = example_creator.create_person()
    person.login_user = current_user
    test_sale.salesperson.person = person
    payment = example_creator.create_payment(group=test_sale.group)
    payment.payment_type = Payment.TYPE_OUT
    payment.paid_value = 10
    sale_item.icms_info.v_icms = 1
    sale_item.icms_info.p_icms = 18

    return test_sale


@pytest.fixture
def cancelled_sale(example_creator, current_user, current_station):
    sale = example_creator.create_sale()
    sale.status = Sale.STATUS_CANCELLED
    sale.branch = current_station.branch
    sale.open_date = datetime.strptime('2020-01-02', '%Y-%m-%d')
    sale.confirm_date = datetime.strptime('2020-01-02', '%Y-%m-%d')
    sale.invoice.key = '33200423335270000159650830000000181790066862'
    sale_item = example_creator.create_sale_item(sale)
    sale_item.price = 10
    sale_item.base_price = 15
    sellable_category = example_creator.create_sellable_category(description='Category 1')
    sale_item.sellable.category = sellable_category
    sale_item.sellable.code = '22222'
    client = example_creator.create_client()
    client.person.individual.cpf = '737.948.760-40'
    sale.client = client
    person = example_creator.create_person()
    person.login_user = current_user
    sale.salesperson.person = person
    payment = example_creator.create_payment(group=sale.group)
    payment.payment_type = Payment.TYPE_IN
    payment.paid_value = 10
    sale_item.icms_info.v_icms = 1
    sale_item.icms_info.p_icms = 18

    return sale


@pytest.fixture
def sellable(example_creator):
    sellable = example_creator.create_sellable()

    return sellable


@pytest.fixture
def open_till(current_till, current_user):
    if current_till.status != Till.STATUS_OPEN:
        current_till.open_till(current_user)

    return current_till


@pytest.fixture
def close_till(open_till, current_user):
    open_till.close_till(current_user)
    open_till.opening_date = datetime.strptime('2020-01-02 08:00', '%Y-%m-%d %H:%M')
    open_till.closing_date = datetime.strptime('2020-01-02 18:00', '%Y-%m-%d %H:%M')
    return open_till


@pytest.fixture
def network():
    return {
        'id': '35868887-3fae-11eb-9f78-40b89ae8d341',
        'name': 'Company name'
    }


@pytest.fixture
def client_category(example_creator):
    return example_creator.create_client_category()


@pytest.fixture
def branch_with_active_station(example_creator):
    return example_creator.create_branch()


@pytest.fixture
def branch_with_inactive_station(example_creator):
    return example_creator.create_branch()


@pytest.fixture
def active_station(example_creator, branch_with_active_station):
    return example_creator.create_station(branch=branch_with_active_station,
                                          is_active=True, name='active station')


@pytest.fixture
def inactive_station(example_creator, branch_with_inactive_station):
    return example_creator.create_station(branch=branch_with_inactive_station,
                                          is_active=False, name='inactive station')


@pytest.fixture
def inactive_station2(example_creator, branch_with_active_station):
    return example_creator.create_station(branch=branch_with_active_station,
                                          is_active=False, name='inactive station 2')


@mock.patch('stoqserver.api.resources.b1food.UUID')
def test_check_if_uuid_valid(abort):

    _check_if_uuid(['123'])