示例#1
0
 def test_ww_enabled_from_ww_evening(self):
     dt = TIMEZONE.localize(datetime(2019, 6, 8, 18, 1))
     now = TIMEZONE.localize(datetime(2019, 6, 10, 18))
     result = working_days_count_since(dt,
                                       now=now,
                                       working_weekends_enabled=True)
     self.assertEqual(result, 1)
示例#2
0
def query_payment_report_failed(message_ids_include=None,
                                message_ids_date_from=None,
                                message_ids_date_to=None,
                                message_ids_exclude=None,
                                **kwargs):
    filters = []
    if message_ids_include is not None:
        filters.append({"messages.message_id": {"$in": message_ids_include}})
    messages_match_filter = {}
    if message_ids_include is not None:
        messages_match_filter.update(
            {"message_id": {
                "$in": message_ids_include
            }})
    if message_ids_date_from is not None and message_ids_date_to is not None:
        message_ids_date_from = UTC.normalize(
            TIMEZONE.localize(message_ids_date_from))
        message_ids_date_to = UTC.normalize(
            TIMEZONE.localize(message_ids_date_to))
        messages_match_filter.update({
            "createdAt": {
                "$gte": message_ids_date_from,
                "$lt": message_ids_date_to
            }
        })
    filters.append({"messages": {"$elemMatch": messages_match_filter}})
    if message_ids_exclude is not None:
        filters.append(
            {"messages.message_id": {
                "$not": {
                    "$in": message_ids_exclude
                }
            }})
    return query_combined_and(filters) if filters else {}
示例#3
0
    def test_working_time(self):
        now = TIMEZONE.localize(datetime(2019, 3, 19, 14, 30, 45, 12))

        result = get_working_datetime(now)

        self.assertEqual(result,
                         TIMEZONE.localize(datetime(2019, 3, 19, 14, 31)))
示例#4
0
    def test_custom_working_time(self):
        now = TIMEZONE.localize(datetime(2019, 3, 19, 16, 30, 45, 12))

        custom_work_time = dict(
            start=(7, 30),
            end=(16, 0),
        )
        result = get_working_datetime(now, custom_wd=custom_work_time)

        self.assertEqual(result,
                         TIMEZONE.localize(datetime(2019, 3, 20, 7, 30)))
    def test_check_request_fail_status(self, requests_mock,
                                       decode_and_save_data_mock, retry_mock,
                                       get_wt_mock):
        get_wt_mock.return_value = datetime(2007, 1, 2, 13, 30)
        retry_mock.side_effect = Retry

        request_data = "aGVsbG8="
        supplier = {
            "tender_id": "f" * 32,
            "award_id": "c" * 32,
        }

        requests_mock.post.return_value = Mock(status_code=502,
                                               text="Bad Gateway",
                                               headers={"Retry-After": 10})

        with patch("fiscal_bot.tasks.working_days_count_since",
                   lambda *_, **k: 0):
            with self.assertRaises(Retry):
                check_for_response_file(request_data=request_data,
                                        supplier=supplier,
                                        request_time=TIMEZONE.localize(
                                            datetime(2019, 3, 29, 15, 47)),
                                        requests_reties=0)

        retry_mock.assert_called_once_with(countdown=10)
        requests_mock.post.assert_called_once_with(
            '{}/cabinet/public/api/exchange/kvt_by_id'.format(FISCAL_API_HOST),
            data=request_data,
            timeout=(CONNECT_TIMEOUT, READ_TIMEOUT),
            proxies=FISCAL_API_PROXIES,
        )
        decode_and_save_data_mock.delay.assert_not_called()
    def test_check_request_fail_request(self, requests_mock,
                                        decode_and_save_data_mock, retry_mock,
                                        get_wt_mock):
        get_wt_mock.return_value = datetime(2007, 1, 2, 13, 30)
        retry_mock.side_effect = Retry

        request_data = "aGVsbG8="
        supplier = {
            "tender_id": "f" * 32,
            "award_id": "c" * 32,
        }
        request_time = TIMEZONE.localize(datetime(2019, 3, 29, 15, 47))
        requests_mock.post.side_effect = requests.exceptions.ConnectionError()

        with patch("fiscal_bot.tasks.working_days_count_since"
                   ) as working_days_count_since_mock:
            working_days_count_since_mock.return_value = 0
            with self.assertRaises(Retry):
                check_for_response_file(request_data=request_data,
                                        supplier=supplier,
                                        request_time=request_time,
                                        requests_reties=0)
        working_days_count_since_mock.assert_called_once_with(
            request_time, working_weekends_enabled=True)
        retry_mock.assert_called_once_with(exc=requests_mock.post.side_effect)
        requests_mock.post.assert_called_once_with(
            '{}/cabinet/public/api/exchange/kvt_by_id'.format(FISCAL_API_HOST),
            data=request_data,
            timeout=(CONNECT_TIMEOUT, READ_TIMEOUT),
            proxies=FISCAL_API_PROXIES,
        )
        decode_and_save_data_mock.delay.assert_not_called()
示例#7
0
    def test_prepare_request_exception(self, requests_mock,
                                       check_for_response_file_mock,
                                       retry_mock):
        retry_mock.side_effect = Retry
        uid = 265970448191511
        supplier = {
            "tender_id": "f" * 32,
            "award_id": "c" * 32,
        }
        request_time = TIMEZONE.localize(datetime(2019, 3, 29, 15, 47))

        requests_mock.post.side_effect = requests.exceptions.ConnectionError(
            "You shall not pass!")

        with self.assertRaises(Retry):
            prepare_check_request(
                uid,
                supplier=supplier,
                request_time=request_time,
                requests_reties=0,
            )

        requests_mock.post.assert_called_once_with(
            "{}/encrypt_fiscal/file".format(API_SIGN_HOST),
            files={'file': (str(uid), str(uid).encode())},
            auth=(API_SIGN_USER, API_SIGN_PASSWORD),
            timeout=(CONNECT_TIMEOUT, READ_TIMEOUT),
        )
        retry_mock.assert_called_once_with(exc=requests_mock.post.side_effect)
        check_for_response_file_mock.delay.assert_not_called()
示例#8
0
    def test_prepare_request_status_error(self, requests_mock,
                                          check_for_response_file_mock,
                                          retry_mock):
        retry_mock.side_effect = Retry
        uid = 265970448191511
        supplier = {
            "tender_id": "f" * 32,
            "award_id": "c" * 32,
        }
        request_time = TIMEZONE.localize(datetime(2019, 3, 29, 15, 47))

        requests_mock.post.return_value = Mock(status_code=500,
                                               text="Bad Gateway",
                                               headers={"Retry-After": 16})

        with self.assertRaises(Retry):
            prepare_check_request(
                uid,
                supplier=supplier,
                request_time=request_time,
                requests_reties=0,
            )

        requests_mock.post.assert_called_once_with(
            "{}/encrypt_fiscal/file".format(API_SIGN_HOST),
            files={'file': (str(uid), str(uid).encode())},
            auth=(API_SIGN_USER, API_SIGN_PASSWORD),
            timeout=(CONNECT_TIMEOUT, READ_TIMEOUT),
        )
        retry_mock.assert_called_once_with(countdown=16)
        check_for_response_file_mock.delay.assert_not_called()
示例#9
0
    def test_prepare_request_success(self, requests_mock,
                                     check_for_response_file_mock):
        uid = 265970448191511
        supplier = {
            "tender_id": "f" * 32,
            "award_id": "c" * 32,
        }
        encoded_content = b"Hello"
        request_time = TIMEZONE.localize(datetime(2019, 3, 29, 15, 47))

        requests_mock.post.return_value = Mock(
            status_code=200,
            content=encoded_content,
        )

        prepare_check_request(
            uid,
            supplier=supplier,
            request_time=request_time,
            requests_reties=0,
        )

        requests_mock.post.assert_called_once_with(
            "{}/encrypt_fiscal/file".format(API_SIGN_HOST),
            files={'file': (str(uid), str(uid).encode())},
            auth=(API_SIGN_USER, API_SIGN_PASSWORD),
            timeout=(CONNECT_TIMEOUT, READ_TIMEOUT),
        )
        check_for_response_file_mock.delay.assert_called_once_with(
            request_data=base64.b64encode(encoded_content).decode(),
            supplier=supplier,
            request_time=request_time,
            requests_reties=0,
        )
示例#10
0
 def test_next_working_day_with_custom_day_2(self):
     dt = TIMEZONE.localize(datetime(2019, 3, 19, 14, 30, 45, 12))
     now = dt + timedelta(days=1, hours=2)
     custom_work_time = dict(
         start=(7, 30),
         end=(15, 0),
     )
     result = working_days_count_since(dt,
                                       now=now,
                                       custom_wd=custom_work_time)
     self.assertEqual(result, 2)
示例#11
0
    def test_saved_request_success(self, decode_and_save_mock, prepare_check_request_mock,
                                   get_result_mock, save_result_mock):
        filename = "test.xml"
        request_data = "whatever"
        supplier = dict(
            name="Python Monty Иванович",
            identifier="AA426097",
            tender_id="f" * 32,
            award_id="c" * 32,
            tenderID="UA-2019-01-31-000147-a",
        )
        fiscal_response = {
            "id": "fa" * 16,
            "status": "OK",
            "kvt1Fname": "Response.xml",
            "kvt1Base64": "Y29udGVudA==",
        }
        get_result_mock.return_value = fiscal_response

        with patch("fiscal_bot.tasks.get_now") as get_now_mock:
            get_now_mock.return_value = TIMEZONE.localize(datetime(2019, 3, 28, 12))

            with patch("fiscal_bot.tasks.requests") as requests_mock:
                send_request_receipt(
                    request_data=request_data, filename=filename,
                    supplier=supplier, requests_reties=1
                )

        get_result_mock.assert_called_once_with(
            send_request_receipt,
            (supplier, 1)
        )
        requests_mock.post.assert_not_called()
        save_result_mock.assert_not_called()
        decode_and_save_mock.apply_async.assert_called_once_with(
            kwargs=dict(
                name="Response.xml",
                data="Y29udGVudA==",
                tender_id=supplier["tender_id"],
                award_id=supplier["award_id"],
            )
        )
        prepare_check_request_mock.apply_async.assert_called_once_with(
            eta=get_now_mock.return_value + timedelta(hours=1),
            kwargs=dict(
                uid="fa" * 16,
                supplier=supplier,
                request_time=get_now_mock.return_value,
                requests_reties=1,
            )
        )
    def test_check_on_3rd_wd_3(self, requests_mock,
                               prepare_receipt_request_mock,
                               decode_and_save_data_mock):
        request_data = "aGVsbG8="
        supplier = {
            "tender_id": "f" * 32,
            "award_id": "c" * 32,
        }

        with patch("fiscal_bot.tasks.working_days_count_since",
                   lambda *_, **k: 3):
            check_for_response_file(request_data=request_data,
                                    supplier=supplier,
                                    request_time=TIMEZONE.localize(
                                        datetime(2019, 3, 29, 15, 47)),
                                    requests_reties=REQUEST_MAX_RETRIES)

        requests_mock.post.assert_not_called()
        decode_and_save_data_mock.assert_not_called()
        prepare_receipt_request_mock.delay.assert_not_called()
    def test_check_request_success(self, requests_mock,
                                   decode_and_save_data_mock):
        request_data = "aGVsbG8="
        supplier = {
            "tender_id": "f" * 32,
            "award_id": "c" * 32,
        }

        requests_mock.post.return_value = Mock(
            status_code=200,
            json=lambda: self.response,
        )

        with patch("fiscal_bot.tasks.working_days_count_since",
                   lambda *_, **k: 0):
            check_for_response_file(request_data=request_data,
                                    supplier=supplier,
                                    request_time=TIMEZONE.localize(
                                        datetime(2019, 3, 29, 15, 47)),
                                    requests_reties=0)

        self.assertEqual(requests_mock.post.call_args_list, [
            call(
                '{}/cabinet/public/api/exchange/kvt_by_id'.format(
                    FISCAL_API_HOST),
                data=request_data,
                timeout=(CONNECT_TIMEOUT, READ_TIMEOUT),
                proxies=FISCAL_API_PROXIES,
            )
        ])

        self.assertEqual(decode_and_save_data_mock.delay.call_args_list, [
            call(
                self.response["kvtList"][-1]["kvtFname"],
                self.response["kvtList"][-1]["kvtBase64"],
                supplier["tender_id"],
                supplier["award_id"],
            )
        ])
    def test_prepare_request_success(self, build_receipt_mock,
                                     send_request_receipt_mock):
        build_receipt_mock.return_value = "hallo.xml", b"unencrypted contents"

        supplier = dict(
            name="Python Monty Иванович",
            identifier="AA426097",
            tender_id="f" * 32,
            award_id="c" * 32,
            tenderID="UA-2019-01-31-000147-a",
            lot_index=None,
        )

        with patch("fiscal_bot.tasks.get_now") as get_now_mock:
            get_now_mock.return_value = TIMEZONE.localize(
                datetime(2019, 4, 1, 16, 1))  # Monday 16:01

            with patch("fiscal_bot.tasks.requests") as requests_mock:
                requests_mock.post.side_effect = [
                    Mock(
                        status_code=200,
                        content=b"content",
                    ),
                ]
                prepare_receipt_request(supplier=supplier)

            requests_mock.post.assert_called_once_with(
                "{}/encrypt_fiscal/file".format(API_SIGN_HOST),
                files={'file': ("hallo.xml", b"unencrypted contents")},
                auth=(API_SIGN_USER, API_SIGN_PASSWORD),
                timeout=(CONNECT_TIMEOUT, READ_TIMEOUT),
            )

            send_request_receipt_mock.apply_async.assert_called_once_with(
                kwargs=dict(request_data=base64.b64encode(b"content").decode(),
                            filename="hallo.xml",
                            supplier=supplier,
                            requests_reties=0))
示例#15
0
 def test_from_holiday_to_monday_evening(self):
     dt = TIMEZONE.localize(datetime(2019, 3, 8, 9, 1))
     now = TIMEZONE.localize(datetime(2019, 3, 11, 23, 59))
     result = working_days_count_since(dt, now=now)
     self.assertEqual(result, 1)
示例#16
0
 def test_week(self):
     dt = TIMEZONE.localize(datetime(2019, 3, 11, 6))
     now = TIMEZONE.localize(datetime(2019, 3, 17, 23, 59))
     result = working_days_count_since(dt, now=now)
     self.assertEqual(result, 5)
示例#17
0
 def test_week_with_a_holiday(self):
     dt = TIMEZONE.localize(datetime(2019, 3, 4, 6))
     now = TIMEZONE.localize(datetime(2019, 3, 10, 23, 59))
     result = working_days_count_since(dt, now=now)
     self.assertEqual(result, 4)
示例#18
0
 def test_saturday_sunday(self):
     dt = TIMEZONE.localize(datetime(2019, 3, 23, 9, 1))
     now = TIMEZONE.localize(datetime(2019, 3, 24, 18))
     result = working_days_count_since(dt, now=now)
     self.assertEqual(result, 0)
示例#19
0
 def test_next_working_day(self):
     dt = TIMEZONE.localize(datetime(2019, 3, 19, 14, 30, 45, 12))
     now = dt + timedelta(days=1, hours=2)
     result = working_days_count_since(dt, now=now)
     self.assertEqual(result, 2)
示例#20
0
 def test_same_working_day(self):
     dt = TIMEZONE.localize(datetime(2019, 3, 19, 14, 30, 45, 12))
     now = dt + timedelta(seconds=10)
     result = working_days_count_since(dt, now=now)
     self.assertEqual(result, 1)
示例#21
0
    def test_working_weekends_enabled(self):
        now = TIMEZONE.localize(datetime(2019, 6, 7, 18, 30))

        result = get_working_datetime(now, working_weekends_enabled=True)

        self.assertEqual(result, TIMEZONE.localize(datetime(2019, 6, 8, 9)))
示例#22
0
 def test_evening_friday_to_monday(self):
     dt = TIMEZONE.localize(datetime(2019, 3, 22, 18, 1))
     now = TIMEZONE.localize(datetime(2019, 3, 25, 9))
     result = working_days_count_since(dt, now=now)
     self.assertEqual(result, 1)
示例#23
0
    def test_sunday(self):
        now = TIMEZONE.localize(datetime(2019, 3, 24, 6, 30))

        result = get_working_datetime(now)

        self.assertEqual(result, TIMEZONE.localize(datetime(2019, 3, 25, 9)))
示例#24
0
    def test_evening(self):
        now = TIMEZONE.localize(datetime(2019, 3, 19, 18, 0, 0, 1))

        result = get_working_datetime(now)

        self.assertEqual(result, TIMEZONE.localize(datetime(2019, 3, 20, 9)))
示例#25
0
    def test_morning(self):
        now = TIMEZONE.localize(datetime(2019, 3, 19, 6, 30, 1, 1))

        result = get_working_datetime(now)

        self.assertEqual(result, TIMEZONE.localize(datetime(2019, 3, 19, 9)))
示例#26
0
 def test_from_holiday_to_sunday(self):
     dt = TIMEZONE.localize(datetime(2019, 3, 8, 9, 1))
     now = TIMEZONE.localize(datetime(2019, 3, 10, 18))
     result = working_days_count_since(dt, now=now)
     self.assertEqual(result, 0)
示例#27
0
 def test_ww_disabled(self):
     dt = TIMEZONE.localize(datetime(2019, 6, 7, 9, 1))
     now = TIMEZONE.localize(datetime(2019, 6, 10, 18))
     result = working_days_count_since(dt, now=now)
     self.assertEqual(result, 2)
示例#28
0
    def test_request_success(self, decode_and_save_mock, prepare_check_request_mock,
                                     get_result_mock, save_result_mock):
        get_result_mock.return_value = None
        filename = "test.xml"
        request_data = "whatever"
        supplier = dict(
            name="Python Monty Иванович",
            identifier="AA426097",
            tender_id="f" * 32,
            award_id="c" * 32,
            tenderID="UA-2019-01-31-000147-a",
        )
        fiscal_response = {
            "id": "fa" * 16,
            "status": "OK",
            "kvt1Fname": "Response.xml",
            "kvt1Base64": "Y29udGVudA==",
        }

        with patch("fiscal_bot.tasks.get_now") as get_now_mock:
            get_now_mock.return_value = TIMEZONE.localize(datetime(2019, 3, 28, 21))

            with patch("fiscal_bot.tasks.requests") as requests_mock:
                requests_mock.post.side_effect = [
                    Mock(
                        status_code=200,
                        json=lambda: fiscal_response,
                    ),
                ]
                send_request_receipt(
                    request_data=request_data, filename=filename,
                    supplier=supplier, requests_reties=1
                )

        requests_mock.post.assert_called_once_with(
            '{}/cabinet/public/api/exchange/report'.format(FISCAL_API_HOST),
            json=[{'contentBase64': request_data, 'fname': filename}],
            proxies=FISCAL_API_PROXIES,
        )
        get_result_mock.assert_called_once_with(
            send_request_receipt,
            (supplier, 1)
        )
        save_result_mock.assert_called_once_with(
            send_request_receipt,
            fiscal_response,
            (supplier, 1)
        )
        decode_and_save_mock.apply_async.assert_called_once_with(
            kwargs=dict(
                name="Response.xml",
                data="Y29udGVudA==",
                tender_id=supplier["tender_id"],
                award_id=supplier["award_id"],
            )
        )

        prepare_check_request_mock.apply_async.assert_called_once_with(
            eta=TIMEZONE.localize(datetime(2019, 3, 29, 9)),
            kwargs=dict(
                uid="fa" * 16,
                supplier=supplier,
                request_time=get_now_mock.return_value,
                requests_reties=1,
            )
        )
示例#29
0
    def test_holiday(self):
        now = TIMEZONE.localize(datetime(2019, 3, 8, 12, 30))

        result = get_working_datetime(now)

        self.assertEqual(result, TIMEZONE.localize(datetime(2019, 3, 11, 9)))
示例#30
0
 def test_week_with_a_holiday_and_weekends(self):
     dt = TIMEZONE.localize(datetime(2019, 3, 1, 18, 1))
     now = TIMEZONE.localize(datetime(2019, 3, 11, 8, 59))
     result = working_days_count_since(dt, now=now)
     self.assertEqual(result, 4)