def test_that_when_there_are_other_deposit_claims_finalize_payment_substract_them_from_currently_processed_claim(self):
        self.deposit_claim = DepositClaim()
        self.deposit_claim.subtask_id = self._get_uuid('1')
        self.deposit_claim.payer_deposit_account = self.deposit_account
        self.deposit_claim.payee_ethereum_address = self.task_to_compute.provider_ethereum_address
        self.deposit_claim.concent_use_case = ConcentUseCase.FORCED_ACCEPTANCE
        self.deposit_claim.amount = 2
        self.deposit_claim.clean()
        # Save twice because we want two claims.
        self.deposit_claim.save()
        self.deposit_claim.pk = None
        self.deposit_claim.subtask_id = generate_uuid()
        self.deposit_claim.save()

        with mock.patch('core.payments.service.get_deposit_value', return_value=5) as get_deposit_value:
            with mock.patch('core.payments.service.force_subtask_payment', return_value=MOCK_TRANSACTION_HASH) as force_subtask_payment:
                returned_value = finalize_payment(self.deposit_claim)

        self.assertEqual(returned_value, MOCK_TRANSACTION_HASH)

        get_deposit_value.assert_called_with(
            client_eth_address=self.deposit_claim.payer_deposit_account.ethereum_address,
        )
        force_subtask_payment.assert_called_once_with(
            requestor_eth_address=self.deposit_claim.payer_deposit_account.ethereum_address,
            provider_eth_address=self.deposit_claim.payee_ethereum_address,
            # 5 - (2 + 2) | deposit_value - sum_of_other_claims
            value=1,
            subtask_id=self.deposit_claim.subtask_id,
        )
示例#2
0
    def wrapper(request: HttpRequest, *args: Any, **kwargs: Any) -> Union[HttpResponse, JsonResponse]:
        if request.content_type == '':
            log(logger, 'error: Content-Type is missing')
            return JsonResponse({'error': 'Content-Type is missing.'}, status = 400)
        elif request.content_type == 'application/octet-stream':
            try:
                auth_message = load_without_public_key(request.body)
                if isinstance(auth_message, message.concents.ClientAuthorization):
                    if is_golem_message_signed_with_key(
                        auth_message.client_public_key,
                        auth_message,
                    ):
                        log(
                            logger,
                            f'A message has been received in `{request.resolver_match.view_name if request.resolver_match is not None else "-not available"}`.'
                            f'Message type: {auth_message.__class__.__name__}.',
                            client_public_key=auth_message.client_public_key
                        )
                    else:
                        log(
                            logger,
                            f'ClientAuthorization message is not signed with public key {auth_message.client_public_key}.',
                            client_public_key=auth_message.client_public_key
                        )
                        return JsonResponse(
                            {
                                'error': f'ClientAuthorization message is not signed with public key {auth_message.client_public_key}.',
                                'error_code': ErrorCode.MESSAGE_SIGNATURE_WRONG.value,
                            },
                            status=400
                        )
                else:
                    log(logger, 'error: Client Authentication message not included')
                    return JsonResponse({'error': 'Client Authentication message not included'}, status = 400)
            except FieldError as exception:
                log(logger, 'Golem Message contains wrong fields.', exception.__class__.__name__)
                return JsonResponse({'error': join_messages('Golem Message contains wrong fields.', str(exception))}, status = 400)
            except MessageFromFutureError as exception:
                log(logger, 'Message timestamp too far in the future.', exception.__class__.__name__)
                return JsonResponse({'error': join_messages('Message timestamp too far in the future.', str(exception))}, status = 400)
            except MessageTooOldError as exception:
                log(logger, 'Message is too old.', exception.__class__.__name__)
                return JsonResponse({'error': join_messages('Message is too old.', str(exception))}, status = 400)
            except TimestampError as exception:
                log(logger, 'Error:', exception.__class__.__name__)
                return JsonResponse({'error': f'{exception}'}, status = 400)
            except MessageError as exception:
                request_id = generate_uuid()
                log(logger, f'uuid: {request_id}', ERROR_IN_GOLEM_MESSAGE, exception.__class__.__name__, logging_level=logging.LoggingLevel.EXCEPTION)
                return JsonResponse({'error': join_messages(f'uuid: {request_id}', ERROR_IN_GOLEM_MESSAGE, exception.__class__.__name__)}, status=400)
        else:
            log(logger, 'error: Concent supports only application/octet-stream.')
            return JsonResponse({'error': "Concent supports only application/octet-stream."}, status = 415)

        return view(request, auth_message, auth_message.client_public_key, *args, *kwargs)
示例#3
0
    def wrapper(request: HttpRequest, *args: Any, **kwargs: Any) -> Union[HttpResponse, JsonResponse]:
        if request.content_type == '':
            log(logger, 'error: Content-Type is missing')
            return JsonResponse({'error': 'Content-Type is missing.'}, status = 400)
        elif request.content_type == 'application/octet-stream':
            try:
                golem_message = load_without_public_key(request.body)
                assert golem_message is not None
                client_public_key = get_validated_client_public_key_from_client_message(golem_message)
                log(
                    logger,
                    f'A message has been received in `{request.resolver_match.view_name if request.resolver_match is not None else "-not available"}`.'
                    f'Message type: {golem_message.__class__.__name__}.',
                    f'Content type: {request.META["CONTENT_TYPE"] if "CONTENT_TYPE" in request.META.keys() else "-not available"}'
                    f'TASK_ID: {golem_message.task_id if "task_id" in dir(golem_message) else "-not available"}',
                    subtask_id=golem_message.subtask_id if 'subtask_id' in dir(golem_message) else None,
                    client_public_key=client_public_key
                )
            except ConcentValidationError as exception:
                log(logger, f"error_code: {exception.error_code.value} error: {exception.error_message} ")
                return JsonResponse(
                    {
                        'error': f'{exception.error_message}',
                        'error_code': exception.error_code.value,
                    },
                    status=400
                )
            except FieldError as exception:
                log(logger, 'Golem Message contains wrong fields.', exception.__class__.__name__)
                return JsonResponse({'error': join_messages('Golem Message contains wrong fields.', str(exception))}, status = 400)
            except MessageFromFutureError as exception:
                log(logger, 'Message timestamp too far in the future.', exception.__class__.__name__)
                return JsonResponse({'error': join_messages('Message timestamp too far in the future.', str(exception))}, status = 400)
            except MessageTooOldError as exception:
                log(logger, 'Message is too old.', exception.__class__.__name__)
                return JsonResponse({'error': join_messages('Message is too old.', str(exception))}, status = 400)
            except TimestampError as exception:
                log(logger, 'Error:', exception.__class__.__name__)
                return JsonResponse({'error': f'{exception}'}, status = 400)
            except MessageError as exception:
                request_id = generate_uuid()
                log(logger, f'uuid: {request_id}', ERROR_IN_GOLEM_MESSAGE, exception.__class__.__name__, logging_level=logging.LoggingLevel.EXCEPTION)
                return JsonResponse({'error': join_messages(f'uuid: {request_id}', ERROR_IN_GOLEM_MESSAGE, exception.__class__.__name__)}, status=400)
        else:
            log(logger, 'error: Concent supports only application/octet-stream.')
            return JsonResponse({'error': "Concent supports only application/octet-stream."}, status = 415)

        return view(request, golem_message, client_public_key, *args, *kwargs)