# Path: ptp/libptp/constants.py
# UNKNOWN = 0
# 
# INFO = 1
# 
# LOW = 2
# 
# MEDIUM = 3
# 
# HIGH = 4
# 
# Path: ptp/tools/owasp/cm008/parser.py
# class OWASPCM008Parser(LineParser):
#     """OWASPCM008 specialized parser."""
# 
#     __tool__ = 'owasp-cm-008'
# 
#     @classmethod
#     def is_mine(cls, pathname, filename='*', light=True, first=False):
#         """Check if it can handle the report file.
# 
#         :param str pathname: Path to the report directory.
#         :param str filename: Regex matching the report file.
#         :param bool light: `True` to only parse the ranking of the findings from the report.
#         :param bool first: Only process first file (``True``) or each file that matched (``False``).
# 
#         :raises IOError: when the report file cannot be found.
#         :raises OSError: when the report file cannot be found.
# 
#         :return: `True` if it supports the report, `False` otherwise.
#         :rtype: :class:`bool`
# 
#         """
#         stream = cls.handle_file(pathname, filename, first=first)
#         if stream and stream[0].startswith('HTTP'):  # FIXME: Weak check here...
#             return True
#         return False
# 
#     def parse_metadata(self):
#         """Parse the metadata of the report.
# 
#         :return: The metadata of the report.
#         :rtype: dict
# 
#         """
#         self.metadata = {'date': line.lstrip('Date: ') for line in self.stream if line.startswith('Date')}
#         return self.metadata
# 
#     def parse_report(self):
#         """Parser the results of OWASP-CM-008 results.
# 
#         :return: List of dicts where each one represents a discovery.
#         :rtype: :class:`list`
# 
#         """
#         allowed_methods = [
#             line.lstrip('Allow: ').split(', ')
#             for line in self.stream
#             if line.startswith('Allow')]
#         if not allowed_methods:
#             return []
#         self.vulns = [
#             {'ranking': SIGNATURES.get(method, UNKNOWN)}
#             for methods in allowed_methods
#             for method in methods]
#         return self.vulns


import mock
import unittest
from hamcrest import assert_that, has_entry, has_item, has_items, is_not
from ptp.libptp.constants import UNKNOWN, INFO, LOW, MEDIUM, HIGH
from ptp.tools.owasp.cm008.parser import OWASPCM008Parser
        from .owasp_cm008_reports import report_low
        from .owasp_cm008_reports import report_medium
        from .owasp_cm008_reports import report_high
        from .owasp_cm008_reports import report_low
        from .owasp_cm008_reports import report_medium
        from .owasp_cm008_reports import report_high
        from .owasp_cm008_reports import report_low
        from .owasp_cm008_reports import report_medium
        from .owasp_cm008_reports import report_high
        from .owasp_cm008_reports import report_no_allow
        assert_that(my_cm008.parse_metadata(), has_entry('date', 'Tue, 12 Aug 2014 20:26:16 GMT'))
        my_cm008 = OWASPCM008Parser(report_medium)
        assert_that(my_cm008.parse_metadata(), has_entry('date', 'Tue, 12 Aug 2014 20:26:16 GMT'))
        my_cm008 = OWASPCM008Parser(report_high)
        assert_that(my_cm008.parse_metadata(), has_entry('date', 'Tue, 12 Aug 2014 20:26:16 GMT'))

    ###
    # OWASPCM008Parser.parse_report
    ###
    @mock.patch('ptp.libptp.parser.LineParser.handle_file', side_effect=handle_file)
    def test_parser_owasp_cm008_parse_report(self, mock_handle):
        my_cm008 = OWASPCM008Parser(report_low)
        report = my_cm008.parse_report()
        assert_that(report, has_items(*[{'ranking': UNKNOWN}] * 2))
        assert_that(report, has_items(*[{'ranking': LOW}] * 2))
        assert_that(report, is_not(has_item([{'ranking': INFO}])))
        assert_that(report, is_not(has_item([{'ranking': MEDIUM}])))
        assert_that(report, is_not(has_item([{'ranking': HIGH}])))
        my_cm008 = OWASPCM008Parser(report_medium)
        report = my_cm008.parse_report()
        assert_that(report, has_items(*[{'ranking': UNKNOWN}] * 1))
        assert_that(report, has_items(*[{'ranking': MEDIUM}] * 1))
        assert_that(report, is_not(has_item([{'ranking': INFO}])))
        assert_that(report, is_not(has_item([{'ranking': LOW}])))
        assert_that(report, is_not(has_item([{'ranking': HIGH}])))
        my_cm008 = OWASPCM008Parser(report_high)
        report = my_cm008.parse_report()
        assert_that(report, has_items(*[{'ranking': UNKNOWN}] * 2))
        assert_that(report, has_items(*[{'ranking': LOW}] * 2))
        assert_that(report, has_items(*[{'ranking': HIGH}] * 1))

# Path: ptp/libptp/constants.py
# UNKNOWN = 0
# 
# INFO = 1
# 
# LOW = 2
# 
# MEDIUM = 3
# 
# HIGH = 4
# 
# Path: ptp/tools/owasp/cm008/parser.py
# class OWASPCM008Parser(LineParser):
#     """OWASPCM008 specialized parser."""
# 
#     __tool__ = 'owasp-cm-008'
# 
#     @classmethod
#     def is_mine(cls, pathname, filename='*', light=True, first=False):
#         """Check if it can handle the report file.
# 
#         :param str pathname: Path to the report directory.
#         :param str filename: Regex matching the report file.
#         :param bool light: `True` to only parse the ranking of the findings from the report.
#         :param bool first: Only process first file (``True``) or each file that matched (``False``).
# 
#         :raises IOError: when the report file cannot be found.
#         :raises OSError: when the report file cannot be found.
# 
#         :return: `True` if it supports the report, `False` otherwise.
#         :rtype: :class:`bool`
# 
#         """
#         stream = cls.handle_file(pathname, filename, first=first)
#         if stream and stream[0].startswith('HTTP'):  # FIXME: Weak check here...
#             return True
#         return False
# 
#     def parse_metadata(self):
#         """Parse the metadata of the report.
# 
#         :return: The metadata of the report.
#         :rtype: dict
# 
#         """
#         self.metadata = {'date': line.lstrip('Date: ') for line in self.stream if line.startswith('Date')}
#         return self.metadata
# 
#     def parse_report(self):
#         """Parser the results of OWASP-CM-008 results.
# 
#         :return: List of dicts where each one represents a discovery.
#         :rtype: :class:`list`
# 
#         """
#         allowed_methods = [
#             line.lstrip('Allow: ').split(', ')
#             for line in self.stream
#             if line.startswith('Allow')]
#         if not allowed_methods:
#             return []
#         self.vulns = [
#             {'ranking': SIGNATURES.get(method, UNKNOWN)}
#             for methods in allowed_methods
#             for method in methods]
#         return self.vulns


# Path: tests/tools/owasp/test_parser.py
import mock
import unittest
from hamcrest import assert_that, has_entry, has_item, has_items, is_not
from ptp.libptp.constants import UNKNOWN, INFO, LOW, MEDIUM, HIGH
from ptp.tools.owasp.cm008.parser import OWASPCM008Parser
        from .owasp_cm008_reports import report_low
        from .owasp_cm008_reports import report_medium
        from .owasp_cm008_reports import report_high
        from .owasp_cm008_reports import report_low
        from .owasp_cm008_reports import report_medium
        from .owasp_cm008_reports import report_high
        from .owasp_cm008_reports import report_low
        from .owasp_cm008_reports import report_medium
        from .owasp_cm008_reports import report_high
        from .owasp_cm008_reports import report_no_allow

        assert_that(my_cm008.parse_metadata(), has_entry('date', 'Tue, 12 Aug 2014 20:26:16 GMT'))
        my_cm008 = OWASPCM008Parser(report_medium)
        assert_that(my_cm008.parse_metadata(), has_entry('date', 'Tue, 12 Aug 2014 20:26:16 GMT'))
        my_cm008 = OWASPCM008Parser(report_high)
        assert_that(my_cm008.parse_metadata(), has_entry('date', 'Tue, 12 Aug 2014 20:26:16 GMT'))

    ###
    # OWASPCM008Parser.parse_report
    ###
    @mock.patch('ptp.libptp.parser.LineParser.handle_file', side_effect=handle_file)
    def test_parser_owasp_cm008_parse_report(self, mock_handle):
        my_cm008 = OWASPCM008Parser(report_low)
        report = my_cm008.parse_report()
        assert_that(report, has_items(*[{'ranking': UNKNOWN}] * 2))
        assert_that(report, has_items(*[{'ranking': LOW}] * 2))
        assert_that(report, is_not(has_item([{'ranking': INFO}])))
        assert_that(report, is_not(has_item([{'ranking': MEDIUM}])))
        assert_that(report, is_not(has_item([{'ranking': HIGH}])))
        my_cm008 = OWASPCM008Parser(report_medium)
        report = my_cm008.parse_report()
        assert_that(report, has_items(*[{'ranking': UNKNOWN}] * 1))
        assert_that(report, has_items(*[{'ranking': MEDIUM}] * 1))
        assert_that(report, is_not(has_item([{'ranking': INFO}])))
        assert_that(report, is_not(has_item([{'ranking': LOW}])))
        assert_that(report, is_not(has_item([{'ranking': HIGH}])))
        my_cm008 = OWASPCM008Parser(report_high)
        report = my_cm008.parse_report()
        assert_that(report, has_items(*[{'ranking': UNKNOWN}] * 2))
        assert_that(report, has_items(*[{'ranking': LOW}] * 2))
        assert_that(report, has_items(*[{'ranking': HIGH}] * 1))

        assert_that(report, is_not(has_item([{'ranking': INFO}])))