<commit_before>#!/opt/tufin/securitysuite/ps/python/bin/python3.4
import os
import time
import types
import unittest
from unittest.mock import patch

import sys

from pytos.securechange.xml_objects.restapi.step.access_request.verifier import AccessRequestVerifierResult
from pytos.common.definitions import xml_tags
from pytos.securechange.helpers import Secure_Change_Helper
from pytos.securechange.xml_objects.rest import Ticket, Ticket_History_Activities, User, User_List, TicketList
from pytos.common.exceptions import REST_Bad_Request_Error


def fake_request_response(rest_file):
    full_path = os.path.dirname(os.path.abspath(__file__))
    sub_resources_dir = sys._getframe(1).f_locals['self'].__class__.__name__.lower()
    resource_file = os.path.join(full_path, "resources", sub_resources_dir, "{}.xml".format(rest_file))
    with open(resource_file, mode='rb') as f:
        return f.read()


class TestSecureChangeHelper(unittest.TestCase):
    def setUp(self):
        self.helper = Secure_Change_Helper("localhost", ("username", "password"))
        self.patcher = patch('pytos.common.rest_requests.requests.Session.send')
        self.mock_get_uri = self.patcher.start()
        self.mock_get_uri.return_value.status_code = 200

    def tearDown(self):
        self.patcher.stop()

    def test_01_post_ticket(self):
        self.mock_get_uri.return_value.headers = {'location': '1'}
        self.mock_get_uri.return_value.status_code = 201
        full_path = os.path.dirname(os.path.abspath(__file__))
        sub_resources_dir = sys._getframe(1).f_locals['self'].__class__.__name__.lower()
        resource_file = os.path.join(full_path, "resources", sub_resources_dir, "{}.xml".format('new_ticket'))
        ticket_obj = self.helper.read_ticket_template(resource_file)
        with patch('pytos.common.rest_requests.requests.Request') as mock_post_uri:
            self.helper.post_ticket(ticket_obj)
            mock_post_uri.assert_called_with(
                'POST',
                'https://localhost/securechangeworkflow/api/securechange/tickets/',
                auth=('username', 'password'),
                data=ticket_obj.to_xml_string().decode("utf-8"),
                headers={'Content-Type': 'application/xml'}<commit_msg>SC Tests - post a ticket<commit_after>#!/opt/tufin/securitysuite/ps/python/bin/python3.4
import os
import time
import types
import unittest
from unittest.mock import patch

import sys

from pytos.securechange.xml_objects.restapi.step.access_request.verifier import AccessRequestVerifierResult
from pytos.common.definitions import xml_tags
from pytos.securechange.helpers import Secure_Change_Helper
from pytos.securechange.xml_objects.rest import Ticket, Ticket_History_Activities, User, User_List, TicketList
from pytos.common.exceptions import REST_Bad_Request_Error


def fake_request_response(rest_file):
    full_path = os.path.dirname(os.path.abspath(__file__))
    sub_resources_dir = sys._getframe(1).f_locals['self'].__class__.__name__.lower()
    resource_file = os.path.join(full_path, "resources", sub_resources_dir, "{}.xml".format(rest_file))
    with open(resource_file, mode='rb') as f:
        return f.read()


class TestSecureChangeHelper(unittest.TestCase):
    def setUp(self):
        self.helper = Secure_Change_Helper("localhost", ("username", "password"))
        self.patcher = patch('pytos.common.rest_requests.requests.Session.send')
        self.mock_get_uri = self.patcher.start()
        self.mock_get_uri.return_value.status_code = 200

    def tearDown(self):
        self.patcher.stop()

    def test_01_post_ticket(self):
        self.mock_get_uri.return_value.headers = {'location': '1'}
        self.mock_get_uri.return_value.status_code = 201
        full_path = os.path.dirname(os.path.abspath(__file__))
        sub_resources_dir = sys._getframe(1).f_locals['self'].__class__.__name__.lower()
        resource_file = os.path.join(full_path, "resources", sub_resources_dir, "{}.xml".format('new_ticket'))
        ticket_obj = self.helper.read_ticket_template(resource_file)
        with patch('pytos.common.rest_requests.requests.Request') as mock_post_uri:
            self.helper.post_ticket(ticket_obj)
            mock_post_uri.assert_called_with(
                'POST',
                'https://localhost/securechangeworkflow/api/securechange/tickets/',
                auth=('username', 'password'),
                data=ticket_obj.to_xml_string().encode(),
                headers={'Content-Type': 'application/xml'}
{'fraction_non_alphanumeric': 0.11499336576735958, 'fraction_numerical': 0.005970809376382132, 'mean_word_length': 3.9377729257641922, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '979896', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1393, 'n_words': 213}