|
3 | 3 |
|
4 | 4 | import json |
5 | 5 | import os |
| 6 | +import time |
6 | 7 | import unittest |
7 | 8 | from base64 import b64encode |
8 | 9 | from urllib.parse import urljoin |
@@ -787,13 +788,43 @@ def test_vulners(self): |
787 | 788 |
|
788 | 789 | def test_wikidata(self): |
789 | 790 | query = {"module": "wiki", "text": "Google"} |
790 | | - response = self.misp_modules_post(query) |
| 791 | + retryable_errors = {"Something went wrong, look in the server logs for details"} |
| 792 | + max_attempts = 4 |
| 793 | + wait_seconds = 2 |
| 794 | + |
| 795 | + last_response = None |
| 796 | + last_exception = None |
| 797 | + for attempt in range(1, max_attempts + 1): |
| 798 | + try: |
| 799 | + last_response = self.misp_modules_post(query) |
| 800 | + except requests.exceptions.RequestException as request_exception: |
| 801 | + last_exception = request_exception |
| 802 | + if attempt < max_attempts: |
| 803 | + time.sleep(wait_seconds) |
| 804 | + continue |
| 805 | + self.fail(f"Wikidata request failed after {max_attempts} attempts: {request_exception}") |
| 806 | + |
| 807 | + try: |
| 808 | + self.assertEqual(self.get_values(last_response), "http://www.wikidata.org/entity/Q95") |
| 809 | + return |
| 810 | + except Exception: |
| 811 | + try: |
| 812 | + error_message = self.get_errors(last_response) |
| 813 | + except Exception: |
| 814 | + error_message = None |
| 815 | + |
| 816 | + if error_message not in retryable_errors or attempt == max_attempts: |
| 817 | + break |
| 818 | + |
| 819 | + time.sleep(wait_seconds) |
| 820 | + |
| 821 | + if last_response is None and last_exception is not None: |
| 822 | + self.fail(f"Wikidata request failed after {max_attempts} attempts: {last_exception}") |
| 823 | + |
791 | 824 | try: |
792 | | - self.assertEqual(self.get_values(response), "http://www.wikidata.org/entity/Q95") |
793 | | - except KeyError: |
794 | | - self.assertEqual(self.get_errors(response), "Something went wrong, look in the server logs for details") |
| 825 | + self.assertEqual(self.get_values(last_response), "No additional data found on Wikidata") |
795 | 826 | except Exception: |
796 | | - self.assertEqual(self.get_values(response), "No additional data found on Wikidata") |
| 827 | + self.assertEqual(self.get_errors(last_response), "Something went wrong, look in the server logs for details") |
797 | 828 |
|
798 | 829 | def test_xforceexchange(self): |
799 | 830 | module_name = "xforceexchange" |
|
0 commit comments