# Path: esteid/mobileid/signer.py
# class MobileIdSigner(Signer):
#     phone_number: str
#     id_code: str
#     language: str
# 
#     SessionData = MobileIdSessionData
#     session_data: MobileIdSessionData
# 
#     def setup(self, initial_data: dict = None):
#         """
#         Receives user input via POST: `id_code`, `phone_number`, `language`
#         """
#         if not isinstance(initial_data, dict):
#             raise InvalidParameters("Missing required parameters")
# 
#         user_input = UserInput(**initial_data)
# 
#         try:
#             user_input.is_valid()
#         except (InvalidIdCode, InvalidParameter):
#             # Just to be explicit
#             raise
#         except ValueError as e:
#             raise InvalidParameters("Invalid parameters") from e
# 
#         self.id_code = user_input.id_code
#         self.phone_number = user_input.phone_number
#         self.language = user_input.language
# 
#     def prepare(self, container=None, files: List[DataFile] = None) -> dict:
#         container = self.open_container(container, files)
# 
#         service = TranslatedMobileIDService.get_instance()
# 
#         certificate = service.get_certificate(self.id_code, self.phone_number)
# 
#         xml_sig = container.prepare_signature(certificate)
# 
#         sign_session = service.sign(self.id_code, self.phone_number, xml_sig.signed_data(), language=self.language)
# 
#         self.save_session_data(
#             digest=sign_session.digest,
#             container=container,
#             xml_sig=xml_sig,
#             session_id=sign_session.session_id,
#         )
# 
#         return {
#             "verification_code": sign_session.verification_code,
#         }
# 
#     def finalize(self, data: dict = None) -> Container:
#         digest = self.session_data.digest
#         session_id = self.session_data.session_id
# 
#         temp_signature_file = self.session_data.temp_signature_file
#         temp_container_file = self.session_data.temp_container_file
# 
#         with open(temp_signature_file, "rb") as f:
#             xml_sig = XmlSignature(f.read())
# 
#         service = TranslatedMobileIDService.get_instance()
# 
#         try:
#             status = service.sign_status(session_id, xml_sig.get_certificate_value(), digest)
#         except ActionInProgress:
#             # Just to be explicit about the InProgress status
#             raise
# 
#         xml_sig.set_signature_value(status.signature)
# 
#         self.finalize_xml_signature(xml_sig)
# 
#         container = Container.open(temp_container_file)
#         container.add_signature(xml_sig)
# 
#         return container
# 
#     def save_session_data(self, *, digest: bytes, container: Container, xml_sig: XmlSignature, session_id: str):
#         data_obj = self.session_data
#         data_obj.session_id = session_id
# 
#         super().save_session_data(digest=digest, container=container, xml_sig=xml_sig)
# 
# Path: esteid/mobileid/signer.py
# class MobileIdSigner(Signer):
#     def setup(self, initial_data: dict = None):
#     def prepare(self, container=None, files: List[DataFile] = None) -> dict:
#     def finalize(self, data: dict = None) -> Container:
#     def save_session_data(self, *, digest: bytes, container: Container, xml_sig: XmlSignature, session_id: str):
# 
# Path: esteid/exceptions.py
# class InvalidIdCode(InvalidParameter):
#     """Invalid ID code (format or checksum don't match)"""
# 
#     default_message = _("Invalid ID code.")
# 
#     def __init__(self, message=None, *, param="id_code", **kwargs):
#         super().__init__(message, param=param, **kwargs)
# 
# class InvalidParameter(InvalidParameters):
#     """Invalid initialization parameter PARAM received from the request.
# 
#     Takes a `param` kwarg
#     """
# 
#     default_message = _("Invalid value for parameter {param}.")
# 
#     def __init__(self, message=None, *, param, **kwargs):
#         super().__init__(message)
#         self.kwargs = {**kwargs, "param": param}
# 
# class InvalidParameters(EsteidError):
#     """Invalid initialization parameters received from the request."""
# 
#     status = HTTPStatus.BAD_REQUEST
# 
#     default_message = _("Invalid parameters.")
# 
# class SigningSessionDoesNotExist(SigningSessionError):
#     """A signing session does not exist while expected to exist"""
# 
#     default_message = _("No signing session found.")


import base64
import os
import pytest
from tempfile import NamedTemporaryFile
from time import time
from unittest.mock import patch
from esteid.mobileid import MobileIdSigner
from esteid.mobileid import signer as signer_module
from ...exceptions import InvalidIdCode, InvalidParameter, InvalidParameters, SigningSessionDoesNotExist





@pytest.fixture()
def mobileidsigner():
    signer = MobileIdSigner({}, initial=True)
    with patch.object(signer, "open_container"), patch.object(signer, "save_session_data"):
        yield signer


@pytest.fixture()
def mobileid_session_data():
    with NamedTemporaryFile("wb", delete=False) as f:
        f.write(b"xml signature data")
    yield {
        "digest_b64": "MTIz",
        "temp_signature_file": f.name,
        "temp_container_file": "...",
        "session_id": "...",
        "timestamp": int(time()),
    }
    os.remove(f.name)


@pytest.fixture()
def mobileidservice():

# Path: esteid/mobileid/signer.py
# class MobileIdSigner(Signer):
#     phone_number: str
#     id_code: str
#     language: str
# 
#     SessionData = MobileIdSessionData
#     session_data: MobileIdSessionData
# 
#     def setup(self, initial_data: dict = None):
#         """
#         Receives user input via POST: `id_code`, `phone_number`, `language`
#         """
#         if not isinstance(initial_data, dict):
#             raise InvalidParameters("Missing required parameters")
# 
#         user_input = UserInput(**initial_data)
# 
#         try:
#             user_input.is_valid()
#         except (InvalidIdCode, InvalidParameter):
#             # Just to be explicit
#             raise
#         except ValueError as e:
#             raise InvalidParameters("Invalid parameters") from e
# 
#         self.id_code = user_input.id_code
#         self.phone_number = user_input.phone_number
#         self.language = user_input.language
# 
#     def prepare(self, container=None, files: List[DataFile] = None) -> dict:
#         container = self.open_container(container, files)
# 
#         service = TranslatedMobileIDService.get_instance()
# 
#         certificate = service.get_certificate(self.id_code, self.phone_number)
# 
#         xml_sig = container.prepare_signature(certificate)
# 
#         sign_session = service.sign(self.id_code, self.phone_number, xml_sig.signed_data(), language=self.language)
# 
#         self.save_session_data(
#             digest=sign_session.digest,
#             container=container,
#             xml_sig=xml_sig,
#             session_id=sign_session.session_id,
#         )
# 
#         return {
#             "verification_code": sign_session.verification_code,
#         }
# 
#     def finalize(self, data: dict = None) -> Container:
#         digest = self.session_data.digest
#         session_id = self.session_data.session_id
# 
#         temp_signature_file = self.session_data.temp_signature_file
#         temp_container_file = self.session_data.temp_container_file
# 
#         with open(temp_signature_file, "rb") as f:
#             xml_sig = XmlSignature(f.read())
# 
#         service = TranslatedMobileIDService.get_instance()
# 
#         try:
#             status = service.sign_status(session_id, xml_sig.get_certificate_value(), digest)
#         except ActionInProgress:
#             # Just to be explicit about the InProgress status
#             raise
# 
#         xml_sig.set_signature_value(status.signature)
# 
#         self.finalize_xml_signature(xml_sig)
# 
#         container = Container.open(temp_container_file)
#         container.add_signature(xml_sig)
# 
#         return container
# 
#     def save_session_data(self, *, digest: bytes, container: Container, xml_sig: XmlSignature, session_id: str):
#         data_obj = self.session_data
#         data_obj.session_id = session_id
# 
#         super().save_session_data(digest=digest, container=container, xml_sig=xml_sig)
# 
# Path: esteid/mobileid/signer.py
# class MobileIdSigner(Signer):
#     def setup(self, initial_data: dict = None):
#     def prepare(self, container=None, files: List[DataFile] = None) -> dict:
#     def finalize(self, data: dict = None) -> Container:
#     def save_session_data(self, *, digest: bytes, container: Container, xml_sig: XmlSignature, session_id: str):
# 
# Path: esteid/exceptions.py
# class InvalidIdCode(InvalidParameter):
#     """Invalid ID code (format or checksum don't match)"""
# 
#     default_message = _("Invalid ID code.")
# 
#     def __init__(self, message=None, *, param="id_code", **kwargs):
#         super().__init__(message, param=param, **kwargs)
# 
# class InvalidParameter(InvalidParameters):
#     """Invalid initialization parameter PARAM received from the request.
# 
#     Takes a `param` kwarg
#     """
# 
#     default_message = _("Invalid value for parameter {param}.")
# 
#     def __init__(self, message=None, *, param, **kwargs):
#         super().__init__(message)
#         self.kwargs = {**kwargs, "param": param}
# 
# class InvalidParameters(EsteidError):
#     """Invalid initialization parameters received from the request."""
# 
#     status = HTTPStatus.BAD_REQUEST
# 
#     default_message = _("Invalid parameters.")
# 
# class SigningSessionDoesNotExist(SigningSessionError):
#     """A signing session does not exist while expected to exist"""
# 
#     default_message = _("No signing session found.")


# Path: esteid/mobileid/tests/test_mobileidsigner.py
import base64
import os
import pytest
from tempfile import NamedTemporaryFile
from time import time
from unittest.mock import patch
from esteid.mobileid import MobileIdSigner
from esteid.mobileid import signer as signer_module
from ...exceptions import InvalidIdCode, InvalidParameter, InvalidParameters, SigningSessionDoesNotExist






@pytest.fixture()
def mobileidsigner():
    signer = MobileIdSigner({}, initial=True)
    with patch.object(signer, "open_container"), patch.object(signer, "save_session_data"):
        yield signer


@pytest.fixture()
def mobileid_session_data():
    with NamedTemporaryFile("wb", delete=False) as f:
        f.write(b"xml signature data")
    yield {
        "digest_b64": "MTIz",
        "temp_signature_file": f.name,
        "temp_container_file": "...",
        "session_id": "...",
        "timestamp": int(time()),
    }
    os.remove(f.name)


@pytest.fixture()
def mobileidservice():

    with patch.object(signer_module, "TranslatedMobileIDService") as service_cls: