Пример #1
0
 def test_list_empty_dir_successful(self):
     mock_response = MockHTTPResponse(200, json.dumps({}))
     requests.get = MagicMock(return_value=mock_response)
     file_manager = GithubRepositoryFileManager()
     response = file_manager.list("", "", "")
     expect = []
     self.assertEqual(response, expect, "It should return en empty dict.")
Пример #2
0
 def test_get_file_call_with_args(self):
     args = ("owner", "repo", "/path")
     mock_response = MockHTTPResponse(200, json.dumps({}))
     requests.get = MagicMock(return_value=mock_response)
     file_manager = GithubRepositoryFileManager()
     response = file_manager.getFile(*args)
     expected_url = (
         f"https://raw.githubusercontent.com/{args[0]}/{args[1]}/master{args[2]}"
     )
     requests.get.assert_called_with(expected_url)
Пример #3
0
 def test_get_file_successful(self):
     file_ = {"name": "filename"}
     mock_response = MockHTTPResponse(200, json.dumps(file_))
     requests.get = MagicMock(return_value=mock_response)
     file_manager = GithubRepositoryFileManager()
     response = file_manager.getFile("", "", "")
     expect = json.dumps(file_)
     self.assertEqual(
         response, expect, "It should return file content in txt format"
     )
Пример #4
0
 def test_list_dir_successful(self):
     mock_response = MockHTTPResponse(200, json.dumps(repo_structure))
     requests.get = MagicMock(return_value=mock_response)
     file_manager = GithubRepositoryFileManager()
     response = file_manager.list("", "", "")
     expect = [
         {"isFile": True, "name": "file_0.json"},
         {"isFile": True, "name": "file_1.json"},
         {"isFile": True, "name": "file_2.json"},
         {"isFile": False, "name": "folder"},
     ]
     self.assertEqual(
         response, expect, "It should return a file manager result dict type"
     )
Пример #5
0
def getMalwareRecords(path: str, keys: List[str]) -> List[Dict[str, Any]]:
    """Returns an object with the found key-values of all the JSON files found in the path given,
    taking the mitre/cti repository as root"""

    owner = "mitre"
    repo = "cti"

    repository = RepositoryNavigator(owner, repo,
                                     GithubRepositoryFileManager())

    files = repository.listRepository(path)
    files = list(filter(lambda f: f["isFile"] == True, files))
    files = list(filter(lambda f: ".json" in f["name"], files))

    threads: List[Thread] = []
    response: List[Dict[str, Any]] = []

    def parseFile(filename: str):
        file_content = repository.getFileFromRepository(f"{path}/{filename}")
        parser = DataParser(file_content, JSONStringExtractor())
        response.append(parser.parse(keys))

    for f in files:
        filename = f["name"]
        t = Thread(target=parseFile, args=(filename, ))
        threads.append(t)

    for t in threads:
        t.start()
    for t in threads:
        t.join()

    return response
Пример #6
0
 def test_get_file_not_found(self):
     mock_response = MockHTTPResponse(404, json.dumps({}))
     requests.get = MagicMock(return_value=mock_response)
     file_manager = GithubRepositoryFileManager()
     with self.assertRaises(Exception):
         file_manager.getFile("", "", "")