Beispiel #1
0
    def test_returns_existing_payment_method_when_token_provided(
            self, database, payment_processors):
        customer = CustomerCreate.call(None, self.processor_name,
                                       self.customer_details)

        prev_payment_method = PaymentMethodCreate.call(self.processor_name,
                                                       customer.token, None,
                                                       self.payment_details)
        prev_payment_information = PaymentProcessorPaymentInformation.find_by_payment_method_id_and_processor_name(
            prev_payment_method.id, self.processor_name)

        before_count_payment_method = PaymentMethod.query.count()
        before_count_payment_information = PaymentProcessorPaymentInformation.query.count(
        )

        payment_method = PaymentMethodCreate.call(self.processor_name,
                                                  customer.token,
                                                  prev_payment_method.token,
                                                  self.payment_details)
        payment_information = PaymentProcessorPaymentInformation.find_by_payment_method_id_and_processor_name(
            payment_method.id, self.processor_name)

        assert PaymentMethod.query.count() == before_count_payment_method
        assert PaymentProcessorPaymentInformation.query.count(
        ) == before_count_payment_information

        assert prev_payment_method.id == payment_method.id
        assert prev_payment_information.id == payment_information.id
Beispiel #2
0
    def test_returns_an_error_when_missing_mandatory_token(self, testapp, database, invalid_header, payment_processors):
        client = testapp.test_client()
        customer = CustomerCreate.call(
            None,
            self.processor_name,
            self.customer_details
        )

        payment_method = PaymentMethodCreate.call(
            'braintree',
            customer.token,
            None,
            self.payment_method_details
        )

        headers = {
            'Authorization': f'Bearer {customer.token}',
            'X-pay-token': f'{payment_method.token}'
        }

        headers.pop(invalid_header)

        response = client.post('/sales', json=self.details, headers=headers)

        assert response.status_code == HTTPStatus.BAD_REQUEST
        assert response.json == 'The customer or/and the payment token are invalid. Please try new tokens.'
Beispiel #3
0
    def test_when_payment_details_are_valid_and_no_customer_token_provided_does_not_create_sale(
            self, database, payment_processors):

        customer = CustomerCreate.call(None, self.processor_name,
                                       self.customer_details)
        payment_method = PaymentMethodCreate.call(self.processor_name,
                                                  customer.token, None,
                                                  self.payment_details)

        with pytest.raises(InvalidCustomer):
            SaleCreate.call(self.processor_name, None, payment_method.token,
                            self.sale_details)
Beispiel #4
0
def create():
    customer_token = get_token(request.headers)
    payment_token = get_payment_token(request.headers)
    try:
        payment_method = PaymentMethodCreate.call(PAYMENT_PROCESSOR,
                                                  customer_token,
                                                  payment_token, request.json)
    except (InvalidPaymentMethod, InvalidPaymentProcessorPaymentInformation):
        return jsonify(
            'The payment method details are invalid. Please try new details'
        ), HTTPStatus.BAD_REQUEST

    return jsonify(payment_method.as_dict()), HTTPStatus.CREATED
Beispiel #5
0
    def test_when_payment_details_are_invalid_does_not_create_sale(
            self, database, invalid_detail, payment_processors):

        customer = CustomerCreate.call(None, self.processor_name,
                                       self.customer_details)
        payment_method = PaymentMethodCreate.call(self.processor_name,
                                                  customer.token, None,
                                                  self.payment_details)

        with pytest.raises(InvalidSale):
            SaleCreate.call(self.processor_name, customer.token,
                            payment_method.token, {
                                **self.sale_details,
                                **invalid_detail
                            })
Beispiel #6
0
    def test_creates_sale_when_all_details_are_valid_and_valid_tokens_provided(
            self, database, payment_processors):
        customer = CustomerCreate.call(None, self.processor_name,
                                       self.customer_details)
        payment_method = PaymentMethodCreate.call(self.processor_name,
                                                  customer.token, None,
                                                  self.payment_details)

        sale = SaleCreate.call(self.processor_name, customer.token,
                               payment_method.token, self.sale_details)

        assert sale == {
            'message':
            f"transaction was made successfully for amount {self.sale_details['amount']}"
        }
Beispiel #7
0
    def test_when_payment_details_are_invalid_does_not_create_payment_method(
            self, database, invalid_detail, payment_processors):

        customer = CustomerCreate.call(None, self.processor_name,
                                       self.customer_details)

        before_count_payment_method = PaymentMethod.query.count()
        before_count_payment_information = PaymentProcessorPaymentInformation.query.count(
        )

        with pytest.raises(InvalidPaymentMethod):
            payment_method = PaymentMethodCreate.call(
                self.processor_name, customer.token, None, {
                    **self.payment_details,
                    **invalid_detail
                })

        assert PaymentMethod.query.count() == before_count_payment_method
        assert PaymentProcessorPaymentInformation.query.count(
        ) == before_count_payment_information
Beispiel #8
0
    def test_creates_payment_method_when_all_details_valid_and_does_not_exist(
            self, database, payment_processors):
        customer = CustomerCreate.call(None, self.processor_name,
                                       self.customer_details)

        before_count = PaymentMethod.query.count()

        payment_method = PaymentMethodCreate.call(self.processor_name,
                                                  customer.token, None,
                                                  self.payment_details)

        assert PaymentMethod.query.count() == before_count + 1

        assert payment_method.customer_id == customer.id
        assert payment_method.details == self.payment_details

        payment_information = PaymentProcessorPaymentInformation.find_by_payment_method_id_and_processor_name(
            payment_method.id, self.processor_name)

        assert payment_information.information == {
            'payment_token': 'sometoken'
        }
Beispiel #9
0
    def test_creates_sale_when_all_details_are_valid(self, testapp, database, payment_processors):
        client = testapp.test_client()
        customer = CustomerCreate.call(
            None,
            self.processor_name,
            self.customer_details
        )

        payment_method = PaymentMethodCreate.call(
            'braintree',
            customer.token,
            None,
            self.payment_method_details
        )

        headers = {
            'Authorization': f'Bearer {customer.token}',
            'X-pay-token': f'{payment_method.token}'
        }
        response = client.post('/sales', json=self.details, headers=headers)

        assert response.status_code == HTTPStatus.CREATED
        assert response.json == { 'message': 'transaction was made successfully for amount 100.00' }
Beispiel #10
0
    def test_returns_an_error_when_missing_mandatory_field(self, testapp, database, invalid_detail, payment_processors):
        client = testapp.test_client()
        customer = CustomerCreate.call(
            None,
            self.processor_name,
            self.customer_details
        )

        payment_method = PaymentMethodCreate.call(
            self.processor_name,
            customer.token,
            None,
            self.payment_method_details
        )

        headers = {
            'Authorization': f'Bearer {customer.token}',
            'X-pay-token': f'{payment_method.token}'
        }

        response = client.post('/sales', json={**self.details, **invalid_detail}, headers=headers)

        assert response.status_code == HTTPStatus.BAD_REQUEST
        assert response.json == 'The camount is not provided. Please try new amount.'