def process(self, encrypted_jwt, tx_id=None):

        self.bound_logger = self.bound_logger.bind(tx_id=tx_id)
        self.bound_logger.debug("Message Received")
        try:
            self.bound_logger.info("Decrypting message")
            decrypted_payload = self._decrypt(encrypted_jwt, tx_id)

            self.bound_logger.info("Extracting file")

            payload = self.extract_file(decrypted_payload, tx_id)
            self.bound_logger = self.bound_logger.bind(
                case_id=payload.case_id, survey_id=payload.survey_id)

            if settings.ANTI_VIRUS_ENABLED:
                av_check = AntiVirusCheck(tx_id=tx_id)
                av_check.send_for_av_scan(payload)

            file_path = self._get_ftp_file_path(payload.survey_id)
            self.bound_logger.info("Sent to ftp server.",
                                   filename=payload.file_name)
            self._send_to_ftp(payload.decoded_contents, file_path,
                              payload.file_name, tx_id)

        except QuarantinableError:
            self.bound_logger.error("Unable to process message")
            raise
        except TypeError:
            self.bound_logger.exception()
            raise

        self.bound_logger = self.bound_logger.try_unbind(
            "survey_id", "case_id", "tx_id")
    def test_send_for_av_scan_request_exception(self):
        responses.add(responses.POST, settings.ANTI_VIRUS_BASE_URL, body=requests.RequestException())

        anti_virus = AntiVirusCheck(tx_id=1)

        payload = Payload(decoded_contents="test", file_name="test", case_id="1", survey_id="1")

        with self.assertRaises(RetryableError):
            anti_virus.send_for_av_scan(payload)
    def test_send_for_av_scan_service_unavailable(self):
        responses.add(responses.POST, settings.ANTI_VIRUS_BASE_URL, status=503)

        anti_virus = AntiVirusCheck(tx_id=1)

        payload = Payload(decoded_contents="test", file_name="test", case_id="1", survey_id="1")

        with self.assertRaises(RetryableError):
            anti_virus.send_for_av_scan(payload)
    def test_send_for_av_scan_bad_request(self):
        responses.add(responses.POST, settings.ANTI_VIRUS_BASE_URL, status=400)

        anti_virus = AntiVirusCheck(tx_id=1)

        payload = Payload(decoded_contents="test", file_name="test", case_id="1", survey_id="1")

        with self.assertRaises(BadMessageError):
            anti_virus.send_for_av_scan(payload)
    def test_send_for_av_scan_type_error(self):
        # should be json not raw text so this should throw a retryable error
        responses.add(responses.POST, settings.ANTI_VIRUS_BASE_URL, body='test', status=200)

        anti_virus = AntiVirusCheck(tx_id=1)

        payload = Payload(decoded_contents="test", file_name="test", case_id="1", survey_id="1")

        with self.assertRaises(RetryableError):
            anti_virus.send_for_av_scan(payload)
    def test_get_results_returns_request_exception(self):
        data_id = '123'
        responses.add(responses.POST, settings.ANTI_VIRUS_BASE_URL, json={'data_id': data_id}, status=200)

        responses.add(responses.GET, settings.ANTI_VIRUS_BASE_URL + "/" + data_id, body=requests.RequestException())

        anti_virus = AntiVirusCheck(tx_id=1)

        payload = Payload(decoded_contents="test", file_name="test", case_id="1", survey_id="1")

        with self.assertRaises(RetryableError):
            self.assertTrue(anti_virus.send_for_av_scan(payload))
    def test_send_for_av_scan_success(self):
        data_id = '123'
        responses.add(responses.POST, settings.ANTI_VIRUS_BASE_URL, json={'data_id': data_id}, status=200)

        responses.add(responses.GET, settings.ANTI_VIRUS_BASE_URL + "/" + data_id,
                      json={
                          'scan_results': {'scan_all_result_i': 0},
                          'process_info': {'progress_percentage': 100, 'result': 'Allowed'}
                      }, status=200)

        anti_virus = AntiVirusCheck(tx_id=1)

        payload = Payload(decoded_contents="test", file_name="test", case_id="1", survey_id="1")

        self.assertTrue(anti_virus.send_for_av_scan(payload))
    def test_send_for_av_scan_causes_type_error(self):
        data_id = '123'
        responses.add(responses.POST, settings.ANTI_VIRUS_BASE_URL, json={'data_id': data_id}, status=200)

        responses.add(responses.GET, settings.ANTI_VIRUS_BASE_URL + "/" + data_id,
                      json={
                          'scan_results': {'scan_all_result_i': 0},
                          'process_info': {'progress_percentage': 'incorrect-value', 'result': 'Allowed'}
                      }, status=200)

        anti_virus = AntiVirusCheck(tx_id=1)

        payload = Payload(decoded_contents="test", file_name="test", case_id="1", survey_id="1")

        with self.assertRaises(RetryableError):
            anti_virus.send_for_av_scan(payload)
    def test_send_for_av_scan_not_ready_hits_max_attempts(self):
        settings.ANTI_VIRUS_WAIT_TIME = 0.1
        data_id = '123'
        responses.add(responses.POST, settings.ANTI_VIRUS_BASE_URL, json={'data_id': data_id}, status=200)

        responses.add(responses.GET, settings.ANTI_VIRUS_BASE_URL + "/" + data_id,
                      json={
                          'scan_results': {'scan_all_result_i': 0},
                          'process_info': {'progress_percentage': 50, 'result': 'Allowed'}
                      }, status=200)

        anti_virus = AntiVirusCheck(tx_id=1)

        payload = Payload(decoded_contents="test", file_name="test", case_id="1", survey_id="1")

        with self.assertRaises(RetryableError):
            self.assertTrue(anti_virus.send_for_av_scan(payload))