# Path: esteid/constants.py
# OCSP_DEMO_URL = "http://demo.sk.ee/ocsp"
# 
# TSA_DEMO_URL = "http://demo.sk.ee/tsa/"
# 
# Path: esteid/exceptions.py
# class ActionInProgress(EsteidError):
#     """
#     Raised when a request to the service is still in pending status,
#     or when it is necessary to move on to the next step while passing some data on to the user
#     (like after successful auth, we initiate a signing request and display verification code)
#     """
# 
#     status = HTTPStatus.ACCEPTED
# 
#     default_message = _("Operation is in progress.")
# 
#     def __init__(self, message=None, data: dict = None):
#         super().__init__(message)
#         self.data = data or {}
# 
# Path: esteid/mobileid/types.py
# PHONE_NUMBER_REGEXP = settings.MOBILE_ID_PHONE_NUMBER_REGEXP
# class UserInput(PredictableDict):
# class MobileIdSessionData(InterimSessionData):
#     def is_valid(self, raise_exception=True):


from time import sleep, time
from esteid_certificates import get_certificate
from pyasice import Container, XmlSignature
from pyasice.ocsp import OCSP
from pyasice.tsa import TSA
from ...constants import OCSP_DEMO_URL, TSA_DEMO_URL
from ...exceptions import ActionInProgress
from ..types import SignResult, SignStatusResult
import pytest



def run_sign_flow(demo_mid_api, certificate, id_number=None, phone_number=None, signed_data=None, timeout=60):
    """Run full sign flow w/ a hash algorithm

    test data from:
    https://github.com/SK-EID/MID/wiki/Test-number-for-automated-testing-in-DEMO

    :param MobileIDService demo_mid_api:
    :param bytes certificate: obtained by authentication, required to validate the signature
    :param str id_number:
    :param str phone_number:
    :param bytes signed_data: content to sign
    :param int timeout: how long to wait for a result. NOTE: if user input is expected, ensure at least 1 minute
    :rtype: SignStatusResult
    """
    if signed_data is None:
        signed_data = b"Test"
    res = demo_mid_api.sign(id_number, phone_number, signed_data)
    assert isinstance(res, SignResult)

    assert res.session_id

    status_res = None  # type: SignStatusResult

    end_time = time() + timeout
    while status_res is None and time() < end_time:
        try:
            status_res = demo_mid_api.sign_status(res.session_id, certificate, res.digest)

# Path: esteid/constants.py
# OCSP_DEMO_URL = "http://demo.sk.ee/ocsp"
# 
# TSA_DEMO_URL = "http://demo.sk.ee/tsa/"
# 
# Path: esteid/exceptions.py
# class ActionInProgress(EsteidError):
#     """
#     Raised when a request to the service is still in pending status,
#     or when it is necessary to move on to the next step while passing some data on to the user
#     (like after successful auth, we initiate a signing request and display verification code)
#     """
# 
#     status = HTTPStatus.ACCEPTED
# 
#     default_message = _("Operation is in progress.")
# 
#     def __init__(self, message=None, data: dict = None):
#         super().__init__(message)
#         self.data = data or {}
# 
# Path: esteid/mobileid/types.py
# PHONE_NUMBER_REGEXP = settings.MOBILE_ID_PHONE_NUMBER_REGEXP
# class UserInput(PredictableDict):
# class MobileIdSessionData(InterimSessionData):
#     def is_valid(self, raise_exception=True):


# Path: esteid/mobileid/tests/test_signing.py
from time import sleep, time
from esteid_certificates import get_certificate
from pyasice import Container, XmlSignature
from pyasice.ocsp import OCSP
from pyasice.tsa import TSA
from ...constants import OCSP_DEMO_URL, TSA_DEMO_URL
from ...exceptions import ActionInProgress
from ..types import SignResult, SignStatusResult
import pytest




def run_sign_flow(demo_mid_api, certificate, id_number=None, phone_number=None, signed_data=None, timeout=60):
    """Run full sign flow w/ a hash algorithm

    test data from:
    https://github.com/SK-EID/MID/wiki/Test-number-for-automated-testing-in-DEMO

    :param MobileIDService demo_mid_api:
    :param bytes certificate: obtained by authentication, required to validate the signature
    :param str id_number:
    :param str phone_number:
    :param bytes signed_data: content to sign
    :param int timeout: how long to wait for a result. NOTE: if user input is expected, ensure at least 1 minute
    :rtype: SignStatusResult
    """
    if signed_data is None:
        signed_data = b"Test"
    res = demo_mid_api.sign(id_number, phone_number, signed_data)
    assert isinstance(res, SignResult)

    assert res.session_id

    status_res = None  # type: SignStatusResult

    end_time = time() + timeout
    while status_res is None and time() < end_time:
        try:
            status_res = demo_mid_api.sign_status(res.session_id, certificate, res.digest)

        except ActionInProgress: