# Path: dftimewolf/lib/errors.py
# class DFTimewolfError(Exception):
# class BadConfigurationError(DFTimewolfError):
# class RecipeParseError(DFTimewolfError):
# class CommandLineParseError(DFTimewolfError):
# class CriticalError(DFTimewolfError):
#   def __init__(self,
#                message: Optional[str]=None,
#                name: Optional[str]=None,
#                stacktrace: Optional[str]=None,
#                critical: bool=False,
#                unexpected: bool=False) -> None:
# 
# Path: dftimewolf/lib/collectors/grr_base.py
# class GRRBaseModule(object):
#   _CHECK_APPROVAL_INTERVAL_SEC = 10
#   def __init__(self) -> None:
#   def GrrSetUp(
#       self,
#       reason: str,
#       grr_server_url: str,
#       grr_username: str,
#       grr_password: str,
#       approvers: Optional[str]=None,
#       verify: bool=True) -> None:
#   def _WrapGRRRequestWithApproval(
#       self,
#       grr_object: Union[Hunt, Client],
#       grr_function: Callable,  # type: ignore[type-arg]
#       logger: WolfLogger,
#       *args: Any,
#       **kwargs: Any
#   ) -> Union[Flow, Hunt]:


import unittest
import logging
import mock
from grr_api_client import errors as grr_errors
from dftimewolf.lib import errors
from dftimewolf.lib.collectors import grr_base
ACCESS_FORBIDDEN_MAX = 3


class MockGRRObject(object):
  """Fake GRR object that will be used in the access forbidden wrapper test"""
  _access_forbidden_counter = 0
  CreateApproval = mock.MagicMock()
  ClientApproval = mock.MagicMock()
  ClientApproval.client_id = "abcd"
  ClientApproval.approval_id = "dcba"
  ClientApproval.username = "nobody"
  CreateApproval.return_value = ClientApproval

  hunt_id = "123"
  client_id = "321"

  # pylint: disable=unused-argument
  def ForbiddenFunction(self, random1, random2, random3=None, random4=None):
    """Will raise a grr_errors.AccessForbiddenError three times, and return."""
    while ACCESS_FORBIDDEN_MAX > self._access_forbidden_counter:
      self._access_forbidden_counter += 1
      raise grr_errors.AccessForbiddenError
    return 4


class GRRBaseModuleTest(unittest.TestCase):
  """Tests for the GRR base collector."""

  def testInitialization(self):
    """Tests that the collector can be initialized."""

# Path: dftimewolf/lib/errors.py
# class DFTimewolfError(Exception):
# class BadConfigurationError(DFTimewolfError):
# class RecipeParseError(DFTimewolfError):
# class CommandLineParseError(DFTimewolfError):
# class CriticalError(DFTimewolfError):
#   def __init__(self,
#                message: Optional[str]=None,
#                name: Optional[str]=None,
#                stacktrace: Optional[str]=None,
#                critical: bool=False,
#                unexpected: bool=False) -> None:
# 
# Path: dftimewolf/lib/collectors/grr_base.py
# class GRRBaseModule(object):
#   _CHECK_APPROVAL_INTERVAL_SEC = 10
#   def __init__(self) -> None:
#   def GrrSetUp(
#       self,
#       reason: str,
#       grr_server_url: str,
#       grr_username: str,
#       grr_password: str,
#       approvers: Optional[str]=None,
#       verify: bool=True) -> None:
#   def _WrapGRRRequestWithApproval(
#       self,
#       grr_object: Union[Hunt, Client],
#       grr_function: Callable,  # type: ignore[type-arg]
#       logger: WolfLogger,
#       *args: Any,
#       **kwargs: Any
#   ) -> Union[Flow, Hunt]:


# Path: tests/lib/collectors/grr_base.py
import unittest
import logging
import mock
from grr_api_client import errors as grr_errors
from dftimewolf.lib import errors
from dftimewolf.lib.collectors import grr_base

ACCESS_FORBIDDEN_MAX = 3


class MockGRRObject(object):
  """Fake GRR object that will be used in the access forbidden wrapper test"""
  _access_forbidden_counter = 0
  CreateApproval = mock.MagicMock()
  ClientApproval = mock.MagicMock()
  ClientApproval.client_id = "abcd"
  ClientApproval.approval_id = "dcba"
  ClientApproval.username = "nobody"
  CreateApproval.return_value = ClientApproval

  hunt_id = "123"
  client_id = "321"

  # pylint: disable=unused-argument
  def ForbiddenFunction(self, random1, random2, random3=None, random4=None):
    """Will raise a grr_errors.AccessForbiddenError three times, and return."""
    while ACCESS_FORBIDDEN_MAX > self._access_forbidden_counter:
      self._access_forbidden_counter += 1
      raise grr_errors.AccessForbiddenError
    return 4


class GRRBaseModuleTest(unittest.TestCase):
  """Tests for the GRR base collector."""

  def testInitialization(self):
    """Tests that the collector can be initialized."""

    grr_base_module = grr_base.GRRBaseModule()