Exemplo n.º 1
0
 def test_compressed_json_from_url(self, mock_url_call, mock_gzip_read):
     mock_gzip_read.return_value = "{ \"name\":\"John\", \"age\":31, \"city\":\"New York\" }"
     mock_url_call.return_value = mock.MagicMock(status_code=200,
                                                 headers={'content-type':"application/x-gzip"})
     resource = Resource("http://simple.com")
     data = resource.read()
     self.assertEqual(json.loads("{ \"name\":\"John\", \"age\":31, \"city\":\"New York\" }"), data)
Exemplo n.º 2
0
 def test_compressed_text_from_url(self, mock_url_call, mock_gzip_read):
     mock_gzip_read.return_value = "Hello World"
     mock_url_call.return_value = mock.MagicMock(status_code=200,
                                                 headers={'content-type':"application/x-gzip"})
     resource = Resource("http://simple.com")
     data = resource.read()
     self.assertEqual("Hello World", data)
Exemplo n.º 3
0
    def test_text_from_url(self, mock_url_call):
        mock_url_call.return_value = mock.MagicMock(status_code=200,
                                                    headers={'content-type':"text/plain"},
                                                    content="Hello World")

        resource = Resource("http://simple.com")
        data = resource.read()
        self.assertEqual("Hello World", data)
Exemplo n.º 4
0
    def update_cves(self):
        cves = []

        update_resoure = Resource(self.after_url())
        new_data = update_resoure.read()

        for cve in new_data:
            cves.append(cve['CVE'])
        return cves
Exemplo n.º 5
0
    def test_open_from_directory(self):
        test_data = ['files.csv',
                     'platforms/linux/local/one_cve.py',
                     'platforms/linux/remote/invalid_cve.py',
                     'platforms/linux/remote/no_cve.php',
                     'platforms/linux/remote/two_different_cves.txt',
                     'platforms/linux/remote/two_different_one_same_cve.txt',
                     'platforms/linux/remote/two_same_cves.c']
        resource = Resource(os.path.join(self.test_data_path, 'exploit-source'))

        data = resource.read()
        common_prefix = os.path.commonprefix(data)
        self.assertTrue(isinstance(data, list))
        for filename in data:
            self.assertIn(filename.replace(common_prefix, ''), test_data)
Exemplo n.º 6
0
class TestSecurityApi(unittest.TestCase):
    def setUp(self):
        self.test_data_path = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), '..', 'test_data')
        self.url = 'https://access.redhat.com/labs/securitydataapi/cves.json'
        self.mock_data_file = os.path.join(self.test_data_path, 'security_api',
                                           'redhat_security_api.json')
        self.mock_resource = Resource(self.mock_data_file)

        self.update_url = 'https://access.redhat.com/labs/securitydataapi/cves.json?after=2017-10-17'
        self.mock_update_file = os.path.join(
            self.test_data_path, 'security_api',
            'red_hat_security_api_after_2017_10_17.json')
        self.mock_update_resource = Resource(self.mock_update_file)

        self.sapi = SecurityApiSource('redhat', self.url)

    @mock.patch('requests.get')
    def test_from_url(self, mock_url_call):
        mock_url_call.return_value = mock.MagicMock(
            status_code=200,
            headers={'content-type': "application/json; charset=utf-8"},
            content=self.mock_resource.read())

        self.assertEqual(self.url, self.sapi.location)
        self.assertEqual('redhat', self.sapi.name)
        data = self.sapi.read()
        self.assertTrue(isinstance(data, list))
        self.assertEqual(self.sapi.latest_date(),
                         dateutil.parser.parse('10-16-2017').date())

    # test cases for not yet implemented functionality in security api source
    # @mock.patch('requests.get')
    # def test_caching(self, mock_url_call):
    #     mock_url_call.return_value = mock.MagicMock(status_code=200,
    #                                                 headers={'content-type':"application/json; charset=utf-8"},
    #                                                 content=self.mock_resource.read())
    #     cache_file = os.path.join(self.test_data_path, 'cache/redhat.json')
    #     self.sapi.configure_cache(cache_file)
    #     self.assertTrue(self.sapi.cache)
    #
    #     self.assertFalse(os.path.isfile(cache_file))
    #     self.assertNotEqual(self.url, self.sapi.location)
    #     self.assertEqual('redhat', self.sapi.name)
    #     data = self.sapi.read()
    #     self.assertTrue(os.path.isdir(self.sapi.cache_path))
    #     self.assertTrue(isinstance(data, list))
    #     data_from_cache = self.sapi.read()
    #     self.assertEqual(data, data_from_cache)
    #
    # @mock.patch('requests.get')
    # def test_detele_cache(self, mock_url_call):
    #     mock_url_call.return_value = mock.MagicMock(status_code=200,
    #                                                 headers={'content-type':"application/json; charset=utf-8"},
    #                                                 content=self.mock_resource.read())
    #     cache_file = os.path.join(self.test_data_path, 'cache/redhat.json')
    #     self.sapi.configure_cache(cache_file)
    #
    #     self.sapi.read()
    #     self.assertTrue(os.path.isfile(cache_file))
    #     self.sapi.delete_cache()
    #     self.assertFalse(os.path.isfile(cache_file))

    @mock.patch('requests.get')
    def test_updates(self, mock_url_call):
        mock_url_call.side_effect = [
            mock.MagicMock(
                status_code=200,
                headers={'content-type': "application/json; charset=utf-8"},
                content=self.mock_resource.read()),
            mock.MagicMock(
                status_code=200,
                headers={'content-type': "application/json; charset=utf-8"},
                content=self.mock_update_resource.read())
        ]
        self.assertEqual(self.sapi.after_date(),
                         dateutil.parser.parse('10-17-2017').date())
        self.assertEqual(self.sapi.after_url(), self.update_url)

        cves = self.sapi.update_cves()
        self.assertEqual(65, len(cves))

    @mock.patch('requests.get')
    def test_cves(self, mock_url_call):
        mock_url_call.return_value = mock.MagicMock(
            status_code=200,
            headers={'content-type': "application/json; charset=utf-8"},
            content=self.mock_resource.read())
        cves_to_test = self.sapi.cves()
        for cve in cves_to_test:
            self.assertEqual(len(cves_to_test[cve].keys()), 4)
            self.assertIn('published_date', cves_to_test[cve].keys())
            self.assertIn('source_name', cves_to_test[cve].keys())
            self.assertIn('source_type', cves_to_test[cve].keys())
            self.assertIsInstance(cves_to_test[cve]['source_type'], int)
            self.assertEqual(cves_to_test[cve]['source_type'], 0)

    def tearDown(self):
        cache_file = os.path.join(self.test_data_path, 'cache/redhat.json')
        if os.path.isfile(cache_file):
            os.remove(cache_file)
Exemplo n.º 7
0
 def test_open_json_from_file(self):
     file_resource = os.path.join(self.test_data_path, 'simple.json')
     resource = Resource(file_resource)
     data = resource.read()
     self.assertEqual(json.loads("{ \"name\":\"John\", \"age\":31, \"city\":\"New York\" }"), data)
Exemplo n.º 8
0
 def test_open_text_from_file(self):
     file_resource = os.path.join(self.test_data_path, 'simple.txt')
     resource = Resource(file_resource)
     data = resource.read()
     self.assertEqual("This is a simple test.\n", data)