# Path: ptp/libptp/constants.py
# UNKNOWN = 0
# 
# INFO = 1
# 
# LOW = 2
# 
# MEDIUM = 3
# 
# HIGH = 4
# 
# Path: ptp/tools/robots/parser.py
# class RobotsParser(LineParser):
#     """Robots specialized parser."""
# 
#     __tool__ = 'robots'
#     __format__ = 'txt'
# 
#     def __init__(self, pathname, filename='*robots.txt', light=False, first=True):
#         LineParser.__init__(self, pathname, filename, light=light, first=first)
# 
#     @classmethod
#     def is_mine(cls, pathname, filename='*robots.txt', light=False, first=True):
#         """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(('User-agent:', 'Disallow:', 'Allow:')):  # 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
# 
#         """
#         return {}  # No metadata to retrieve in robots.txt
# 
#     def parse_report(self):
#         """Parser the results of a Robots.txt report.
# 
#         :return: List of dicts where each one represents a vuln.
#         :rtype: :class:`list`
# 
#         """
#         disallowed_entries = [line.lstrip('Disallow: ') for line in self.stream if line.startswith('Disallow')]
#         if not disallowed_entries:
#             return []
#         self.vulns = [
#             {'ranking': SIGNATURES.get(entry, UNKNOWN)}
#             for entry in disallowed_entries]
#         return self.vulns


import mock
import unittest
from hamcrest import assert_that, has_items, is_not, has_item
from ptp.libptp.constants import UNKNOWN, INFO, LOW, MEDIUM, HIGH
from ptp.tools.robots.parser import RobotsParser
        from .robots_reports import report_info
        from .robots_reports import report_info
        from .robots_reports import report_info
        from .robots_reports import report_unknown
            RobotsParser.is_mine('foo.bar')

    @mock.patch('ptp.libptp.parser.LineParser.handle_file', side_effect=OSError)
    def test_parser_robots_is_mine_oserror(self, mock_handle):
        with self.assertRaises(OSError):
            RobotsParser.is_mine('foo.bar')

    ###
    # RobotsParser.parse_metadata
    ###
    @mock.patch('ptp.libptp.parser.LineParser.handle_file', side_effect=handle_file)
    def test_parser_robots_parse_metadata(self, mock_handle):
        RobotsParser.__format__ = ''
        my_robots = RobotsParser(report_info)
        self.assertTrue(my_robots.parse_metadata() == {})

    ###
    # RobotsParser.parse_report
    ###
    @mock.patch('ptp.libptp.parser.LineParser.handle_file', side_effect=handle_file)
    def test_parser_robots_parse_report(self, mock_handle):
        RobotsParser.__format__ = ''
        my_robots = RobotsParser(report_info)
        report = my_robots.parse_report()
        assert_that(report, has_items(*[{'ranking': UNKNOWN}] * 5))
        assert_that(report, has_items(*[{'ranking': INFO}] * 1))
        RobotsParser.__format__ = ''
        my_robots = RobotsParser(report_unknown)
        report = my_robots.parse_report()
        assert_that(report, is_not(has_item([{'ranking': UNKNOWN}])))

# Path: ptp/libptp/constants.py
# UNKNOWN = 0
# 
# INFO = 1
# 
# LOW = 2
# 
# MEDIUM = 3
# 
# HIGH = 4
# 
# Path: ptp/tools/robots/parser.py
# class RobotsParser(LineParser):
#     """Robots specialized parser."""
# 
#     __tool__ = 'robots'
#     __format__ = 'txt'
# 
#     def __init__(self, pathname, filename='*robots.txt', light=False, first=True):
#         LineParser.__init__(self, pathname, filename, light=light, first=first)
# 
#     @classmethod
#     def is_mine(cls, pathname, filename='*robots.txt', light=False, first=True):
#         """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(('User-agent:', 'Disallow:', 'Allow:')):  # 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
# 
#         """
#         return {}  # No metadata to retrieve in robots.txt
# 
#     def parse_report(self):
#         """Parser the results of a Robots.txt report.
# 
#         :return: List of dicts where each one represents a vuln.
#         :rtype: :class:`list`
# 
#         """
#         disallowed_entries = [line.lstrip('Disallow: ') for line in self.stream if line.startswith('Disallow')]
#         if not disallowed_entries:
#             return []
#         self.vulns = [
#             {'ranking': SIGNATURES.get(entry, UNKNOWN)}
#             for entry in disallowed_entries]
#         return self.vulns


# Path: tests/tools/robots/test_parser.py
import mock
import unittest
from hamcrest import assert_that, has_items, is_not, has_item
from ptp.libptp.constants import UNKNOWN, INFO, LOW, MEDIUM, HIGH
from ptp.tools.robots.parser import RobotsParser
        from .robots_reports import report_info
        from .robots_reports import report_info
        from .robots_reports import report_info
        from .robots_reports import report_unknown

            RobotsParser.is_mine('foo.bar')

    @mock.patch('ptp.libptp.parser.LineParser.handle_file', side_effect=OSError)
    def test_parser_robots_is_mine_oserror(self, mock_handle):
        with self.assertRaises(OSError):
            RobotsParser.is_mine('foo.bar')

    ###
    # RobotsParser.parse_metadata
    ###
    @mock.patch('ptp.libptp.parser.LineParser.handle_file', side_effect=handle_file)
    def test_parser_robots_parse_metadata(self, mock_handle):
        RobotsParser.__format__ = ''
        my_robots = RobotsParser(report_info)
        self.assertTrue(my_robots.parse_metadata() == {})

    ###
    # RobotsParser.parse_report
    ###
    @mock.patch('ptp.libptp.parser.LineParser.handle_file', side_effect=handle_file)
    def test_parser_robots_parse_report(self, mock_handle):
        RobotsParser.__format__ = ''
        my_robots = RobotsParser(report_info)
        report = my_robots.parse_report()
        assert_that(report, has_items(*[{'ranking': UNKNOWN}] * 5))
        assert_that(report, has_items(*[{'ranking': INFO}] * 1))
        RobotsParser.__format__ = ''
        my_robots = RobotsParser(report_unknown)
        report = my_robots.parse_report()
        assert_that(report, is_not(has_item([{'ranking': UNKNOWN}])))

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