text: import requests_mock
  
  import demistomock as demisto
  import CommonServerPython
  import pytest
  import json
  
  
  
  class ResponseMock:
      def __init__(self, response):
          self._json = response
          self.content = json.dumps(response)
  
      def json(self):
          return self._json
  
  
  class ObjectMocker(dict):
      __getattr__ = dict.__getitem__
      __setattr__ = dict.__setitem__
      __delattr__ = dict.__delitem__
  
  
  def run_command_test(command_func, args, response_path, expected_result_path, mocker):
      with open(response_path, 'r') as response_f:
          response = ResponseMock(json.load(response_f))
      mocker.patch('Zscaler_v2.http_request', return_value=response)
      if command_func.__name__ == 'url_lookup':
          res = command_func(args)
      else:
          res = command_func(**args)
      with open(expected_result_path, 'r') as ex_f:
          expected_result = json.load(ex_f)
          assert expected_result == res
  
  
  @pytest.fixture(autouse=True)
  def init_tests(mocker):
      params = {
          'cloud': 'http://cloud',
          'credentials': {
              'identifier': 'security',
              'password': 'ninja'
          },
          'key': 'api'
      }
      mocker.patch.object(demisto, 'params', return_value=params)
  
  
  def test_get_saml_attr(mocker):
      """zscaler-saml_attr"""
      import Zscaler_v2
      run_command_test(command_func=Zscaler_v2.get_saml_attr,
                       args={'customerId': 0},
                       response_path='../Zscaler_v2/test_data/responses/saml_attr.json',
                       expected_result_path='../Zscaler_v2/test_data/results/saml_attr.json',
                       mocker=mocker)
  
  
  def test_get_saml_attr_details(mocker):
      """zscaler-saml_attr_details"""
      import Zscaler_v2
      run_command_test(command_func=Zscaler_v2.get_saml_attr_details,
                       args={'customerId': 0, 'attrId': 2},
                       response_path='../Zscaler_v2/test_data/responses/saml_attr.json',
                       expected_result_path='../Zscaler_v2/test_data/results/saml_attr.json',
                       mocker=mocker)
  
  
  def test_get_policy_set_bypass(mocker):
      """zscaler-policy_set_bypass"""
      import Zscaler_v2
      run_command_test(command_func=Zscaler_v2.get_policy_set_bypass,
                       args={'customerId': 0},
                       response_path='../Zscaler_v2/test_data/responses/policy_set.json',
                       expected_result_path='../Zscaler_v2/test_data/results/policy_set.json',
                       mocker=mocker)
  
  
  def test_get_segment_group_details(mocker):
      """zscaler-segment_group_details"""
      import Zscaler_v2
      run_command_test(command_func=Zscaler_v2.get_segment_group_details,
                       args={'customerId': 0, 'segmentGroupId': 2},
                       response_path='../Zscaler_v2/test_data/responses/segment_group.json',
                       expected_result_path='../Zscaler_v2/test_data/results/segment_group.json',
                       mocker=mocker)
  
  
  def test_segment_group_update(mocker):
      """zscaler-update_segment_group"""
      import Zscaler_v2
      run_command_test(command_func=Zscaler_v2.update_segment_group,
                       args={'customerId': 0, 'segmentGroupId': 2},
                       response_path='../Zscaler_v2/test_data/responses/segment_group.json',
                       expected_result_path='../Zscaler_v2/test_data/results/segment_group.json',
                       mocker=mocker)
  
  
  # disable-secrets-detection-start
  test_data = [
      ('https://madeup.fake.com/css?family=blah:1,2,3', 'true', ['madeup.fake.com/css?family=blah:1', '2', '3']),
      ('https://madeup.fake.com/css?family=blah:1,2,3', 'false', ['madeup.fake.com/css?family=blah:1,2,3'])
  ]
  
  
  # disable-secrets-detection-end
  @pytest.mark.parametrize('url,multiple,expected_data', test_data)
  def test_url_multiple_arg(url, multiple, expected_data):
      '''Scenario: Submit a URL with commas in it
  
      Given
      - A URL with commas in it
      When
      - case A: 'multiple' argument is set to "true" (the default)
      - case B: 'multiple' argument is set to "false"
      Then
      - case A: Ensure the URL is interpreted as multiple values to be sent in the subsequent API call
      - case B: Ensure the URL is interpreted as a single value to be sent in the subsequent API call
  
      Args:
          url (str): The URL to submit.
          multiple (str): "true" or "false" - whether to interpret the 'url' argument as multiple comma separated values.
          expected_data (list): The data expected to be sent in the API call.
      '''
      import Zscaler_v2
      with requests_mock.mock() as m:
          # 'fake_resp_content' doesn't really matter here since we are checking the data being sent in the call,
          # not what it is that we expect to get in response
          fake_resp_content = b'[{"url": "blah", "urlClassifications": [], "urlClassificationsWithSecurityAlert": []}]'
          m.post(Zscaler_v2.BASE_URL + '/urlLookup', content=fake_resp_content)
          args = {
              'url': url,
              'multiple': multiple
          }
          Zscaler_v2.url_lookup(args)
      assert m.called
      assert m.call_count == 1
      request_data = m.last_request.json()
      assert len(request_data) == len(expected_data)
      assert request_data == expected_data
  
  
  def test_login__active_session(mocker):
      """
      Scenario: test login with an active session
  
      Given:
       - User has authorization to login
       - There is an active session
      When:
       - login is called
      Then:
       - No login request is done
       - Result is as expected
      """
      import Zscaler_v2
      mock_id = 'mock_id'
      mocker.patch.object(demisto, 'debug')
      mocker.patch.object(Zscaler_v2, 'get_integration_context', return_value={Zscaler_v2.SESSION_ID_KEY: mock_id})
      mocker.patch.object(Zscaler_v2, 'test_module')
      Zscaler_v2.login()
  
      assert Zscaler_v2.DEFAULT_HEADERS['cookie'] == mock_id
  
  
  def test_login__no_active_session(mocker):
      """
      Scenario: test login when there is no active session
  
      Given:
       - User has authorization to login
       - There is an active session
      When:
       - User wishes to login using login command
      Then:
       - Result is as expected
      """
      import Zscaler_v2
      mock_header = 'JSESSIONID=MOCK_ID; Path=/; Secure; HttpOnly'
      Zscaler_v2.API_KEY = 'Lcb38EvjtZVc'
      mocker.patch.object(demisto, 'debug')
      mocker.patch.object(Zscaler_v2, 'get_integration_context', return_value={})
      mocker.patch.object(Zscaler_v2, 'test_module')
      mocker.patch.object(Zscaler_v2, 'http_request', return_value=ObjectMocker({'headers': {'Set-Cookie': mock_header}}))
      Zscaler_v2.login()
  
      assert Zscaler_v2.DEFAULT_HEADERS['cookie'] == 'JSESSIONID=MOCK_ID'
  
  
  def test_login_command(mocker):
      """
      Scenario: test successful login command
  
      Given:
       - User provided valid credentials
       - Integration context is empty
      When:
       - zscaler-login command is called
      Then:
       - Ensure logout is not called
       - Ensure login is called
       - Ensure readable output is as expected
      """
      import Zscaler_v2
      mocker.patch.object(demisto, 'debug')
      mocker.patch.object(Zscaler_v2, 'get_integration_context', return_value={})
      logout_mock = mocker.patch.object(Zscaler_v2, 'logout')
      login_mock = mocker.patch.object(Zscaler_v2, 'login')
      raw_res = Zscaler_v2.login_command()
  
      assert not logout_mock.called
      assert login_mock.called
      assert raw_res.readable_output == 'Zscaler session created successfully.'
  
  
  def test_login_command__load_from_context(mocker):
      """
      Scenario: test successful login command attempt with load from the context
  
      Given:
       - User has provided valid credentials
       - Integration context has a previous session
      When:
       - zscaler-login command is called
      Then:
       - Ensure logout is called
       - Ensure login is called
       - Ensure readable output is as expected
      """
      import Zscaler_v2
      mocker.patch.object(demisto, 'debug')
      mocker.patch.object(Zscaler_v2, 'get_integration_context', return_value={Zscaler_v2.SESSION_ID_KEY: 'test_key'})
      logout_mock = mocker.patch.object(Zscaler_v2, 'logout')
      login_mock = mocker.patch.object(Zscaler_v2, 'login')
      raw_res = Zscaler_v2.login_command()
  
      assert logout_mock.called
      assert login_mock.called
      assert raw_res.readable_output == 'Zscaler session created successfully.'
  
  
  def test_logout_command__no_context(mocker):
      """
      Scenario: logout when there's no active session
  
      Given:
       - There is no active session
      When:
       - logout command is performed
      Then:
       - Return readable output detailing no action was performed
      """
      import Zscaler_v2
      mocker.patch.object(Zscaler_v2, 'get_integration_context', return_value={})
      raw_res = Zscaler_v2.logout_command()
      assert raw_res.readable_output == 'No API session was found. No action was performed.'
  
  
  def test_logout_command__happy_context(mocker):
      """
      Scenario: logout when there's an active session
  
      Given:
       - There is an active session
      When:
       - logout command is performed
      Then:
       - Return readable output detailing logout was performed
      """
      import Zscaler_v2
      mocker.patch.object(Zscaler_v2, 'get_integration_context', return_value={Zscaler_v2.SESSION_ID_KEY: 'test_key'})
      mocker.patch.object(Zscaler_v2, 'logout', return_value=ResponseMock({}))
      raw_res = Zscaler_v2.logout_command()
      assert raw_res.readable_output == "API session logged out of Zscaler successfully."
  
  
  def test_logout_command__context_expired(mocker):
      """
      Scenario: fail to logout with AuthorizationError when there's an active session
  
      Given:
       - There is an active session
      When:
       - logout command is performed
       - logout action fails with AuthorizationError
      Then:
       - Return readable output detailing no logout was done
      """
      import Zscaler_v2
      mocker.patch.object(Zscaler_v2, 'get_integration_context', return_value={Zscaler_v2.SESSION_ID_KEY: 'test_key'})
      mocker.patch.object(Zscaler_v2, 'logout', side_effect=Zscaler_v2.AuthorizationError(''))
      raw_res = Zscaler_v2.logout_command()
      assert raw_res.readable_output == "API session is not authenticated. No action was performed."
  