Ejemplo n.º 1
0
    def test_when_customer_does_not_exists_and_all_details_correct_creates_customer(
            self, database, payment_processors):
        assert Customer.find_by_email(self.email) is None

        customer = CustomerCreate.call(self.token, self.processor_name,
                                       self.details)

        assert customer is not None
        assert Customer.find_by_email(self.email) == customer
Ejemplo n.º 2
0
    def test_when_customer_does_not_exists_and_with_extra_details_creates_customer(
            self, database, payment_processors):
        assert Customer.find_by_email(self.email) is None

        customer = CustomerCreate.call(
            self.token, self.processor_name, {
                **self.details, 'extra-attr': 'something'
            })

        assert customer is not None
        assert Customer.find_by_email(self.email) == customer
Ejemplo n.º 3
0
    def test_finds_by_token_returns_the_expected_customer(self, database):
        customer = Customer.create(
            first_name = self.first_name,
            last_name = self.last_name,
            company = self.company,
            email = self.email,
            phone = self.phone,
            fax = self.fax,
            website = self.website
        )

        assert Customer.query.count() == 1
        assert Customer.find_by_token('somenonexistingtoken') is None
        assert Customer.find_by_token(customer.token) == customer
Ejemplo n.º 4
0
    def test_finds_by_email_returns_the_expected_customer(self, database):
        customer = Customer.create(
            first_name = self.first_name,
            last_name = self.last_name,
            company = self.company,
            email = self.email,
            phone = self.phone,
            fax = self.fax,
            website = self.website
        )

        assert Customer.query.count() == 1
        assert Customer.find_by_email('*****@*****.**') is None
        assert Customer.find_by_email(self.email) == customer
Ejemplo n.º 5
0
    def test_finds_by_id_returns_the_expected_customer(self, database):
        customer = Customer.create(
            first_name = self.first_name,
            last_name = self.last_name,
            company = self.company,
            email = self.email,
            phone = self.phone,
            fax = self.fax,
            website = self.website
        )

        assert Customer.query.count() == 1
        assert Customer.find_by_id(uuid.uuid4()) is None
        assert Customer.find_by_id(customer.id) == customer
Ejemplo n.º 6
0
    def create_customer(self):
        customer = Customer.find_by_token(self.customer_token)

        if customer is None:
            customer = Customer.find_by_email(self.details['email'])

        if customer is None:
            processor_information = self._register_customer_with_processor(
                self.processor_name, self.details)

            customer = Customer.create(**self.details)

            PaymentProcessorCustomerInformationCreate.call(
                self.processor_name, customer, processor_information)

        return customer
Ejemplo n.º 7
0
    def test_find_by_payment_mehod_id_and_processor_name_retrieves_the_expected_payment_processor_payment_information(
            self, database):
        name = 'stripetest'
        information = {'payment_token': 'sometoken'}

        customer = Customer.create(first_name='Test',
                                   last_name='Test',
                                   company='Really Cool LTD',
                                   email='*****@*****.**',
                                   phone='+1111111111',
                                   fax='+12222222222',
                                   website='https://www.reallycool.test')

        payment_method = PaymentMethod.create(
            customer=customer,
            details=self.details,
        )

        payment_processor_payment_information = PaymentProcessorPaymentInformation.create(
            name=name, payment_method=payment_method, information=information)

        assert PaymentProcessorPaymentInformation.find_by_payment_method_id_and_processor_name(
            uuid.uuid4(), name) is None
        assert PaymentProcessorPaymentInformation.find_by_payment_method_id_and_processor_name(
            payment_method.id, name) is not None
        assert PaymentProcessorPaymentInformation.find_by_payment_method_id_and_processor_name(
            payment_method.id, name) == payment_processor_payment_information
Ejemplo n.º 8
0
    def test_returns_an_error_when_missing_mandatory_field(
            self, testapp, database, invalid_detail, payment_processors):
        client = testapp.test_client()
        customer = Customer.create(
            **{
                'first_name': 'Test',
                'last_name': 'Test',
                'company': 'Really Cool LTD',
                'email': '*****@*****.**',
                'phone': '+1111111111'
            })
        customer_information = {'customer_id': 'someid'}

        payment_processor_customer_information = PaymentProcessorCustomerInformation.create(
            name='braintree',
            customer=customer,
            information=customer_information)
        params = {
            'cardholder_name': 'Test Coool',
            'number': '4005519200000004',
            'cvv': '111',
            'expiration_date': '12/99'
        }
        headers = {
            'Authorization': f'Bearer {customer.token}',
        }
        response = client.post('/payment_methods',
                               json={
                                   **params,
                                   **invalid_detail
                               },
                               headers=headers)

        assert response.status_code == HTTPStatus.BAD_REQUEST
        assert response.json == 'The payment method details are invalid. Please try new details'
Ejemplo n.º 9
0
    def test_converts_payment_method_to_dict(self, database):
        customer = Customer.create(first_name='Test',
                                   last_name='Coool',
                                   company='Really Cool LTD',
                                   email='*****@*****.**',
                                   phone='+1111111111',
                                   fax='+12222222222',
                                   website='https://www.reallycool.test')

        details = {
            'card_holder_name': 'Test Coool',
            'number': '1111' * 4,
            'cvv': '111',
            'expiration_date': '12/99'
        }

        payment_method = PaymentMethod.create(
            customer=customer,
            details=details,
        )

        expected_dict = {
            'token': payment_method.token,
            'updated_at': int(payment_method.created_at.timestamp()),
            'created_at': int(payment_method.created_at.timestamp())
        }

        assert payment_method.as_dict() == expected_dict
Ejemplo n.º 10
0
    def test_creates_payment_method_with_all_attributes_correct(
            self, database):
        customer = Customer.create(first_name='Test',
                                   last_name='Coool',
                                   company='Really Cool LTD',
                                   email='*****@*****.**',
                                   phone='+1111111111',
                                   fax='+12222222222',
                                   website='https://www.reallycool.test')

        details = {
            'card_holder_name': 'Test Coool',
            'number': '1111' * 4,
            'cvv': '111',
            'expiration_date': '12/99'
        }

        payment_method = PaymentMethod.create(
            customer=customer,
            details=details,
        )

        assert PaymentMethod.query.filter_by(
            customer_id=customer.id).first().id == payment_method.id

        assert payment_method.id is not None

        assert PaymentMethod.query.filter_by(
            customer_id=customer.id).first().token == payment_method.token

        assert payment_method.token is not None

        assert payment_method.customer_id == customer.id
        assert payment_method.details == details
Ejemplo n.º 11
0
    def test_creates_payment_method_when_all_details_are_valid(
            self, testapp, database, payment_processors):
        client = testapp.test_client()
        customer = Customer.create(
            **{
                'first_name': 'Test',
                'last_name': 'Test',
                'company': 'Really Cool LTD',
                'email': '*****@*****.**',
                'phone': '+1111111111'
            })

        customer_information = {'customer_id': 'someid'}

        payment_processor_customer_information = PaymentProcessorCustomerInformation.create(
            name='braintree',
            customer=customer,
            information=customer_information)

        params = {
            'cardholder_name': 'Test Coool',
            'number': '4005519200000004',
            'cvv': '111',
            'expiration_date': '12/99'
        }

        headers = {
            'Authorization': f'Bearer {customer.token}',
        }
        response = client.post('/payment_methods',
                               json=params,
                               headers=headers)

        assert response.status_code == HTTPStatus.CREATED
        assert PaymentMethod.find_by_token(response.json['token']) is not None
Ejemplo n.º 12
0
    def test_creates_customer_with_all_expected_attributes(self, database):
        name = 'braintreetest'
        information = {
            'payment_token': 'sometoken',
            'nonce_token': 'someothertoken'
        }

        customer = Customer.create(first_name='Test',
                                   last_name='Test',
                                   company='Really Cool LTD',
                                   email='*****@*****.**',
                                   phone='+1111111111',
                                   fax='+12222222222',
                                   website='https://www.reallycool.test')

        payment_method = PaymentMethod.create(
            customer=customer,
            details=self.details,
        )

        payment_processor_payment_information = PaymentProcessorPaymentInformation.create(
            name=name, payment_method=payment_method, information=information)

        assert PaymentProcessorPaymentInformation.query.filter_by(
            name=name).first().id == payment_processor_payment_information.id

        assert payment_processor_payment_information.id is not None

        assert payment_processor_payment_information.name == name
        assert payment_processor_payment_information.information == information
        assert payment_processor_payment_information.payment_method_id == payment_method.id
Ejemplo n.º 13
0
    def test_creates_customers_when_all_details_are_valid(
            self, testapp, database, payment_processors):
        client = testapp.test_client()
        response = client.post('/customers', json=self.params)
        customer = Customer.find_by_email(self.params['email'])

        assert response.status_code == HTTPStatus.CREATED
        assert response.json == customer.as_dict()
Ejemplo n.º 14
0
    def test_when_customer_exists_and_no_token_provided_returns_existing_customer(
            self, database, payment_processors):
        existing_customer = Customer.create(**self.details)

        customer = CustomerCreate.call(self.token, self.processor_name,
                                       self.details)

        assert existing_customer is not None
        assert existing_customer.id == customer.id
Ejemplo n.º 15
0
    def test_returns_existing_customers_when_token_specified(
            self, testapp, database, payment_processors):
        client = testapp.test_client()
        email = '*****@*****.**'
        customer = Customer.create(**self.params)
        headers = {'Authorization': f'Bearer {customer.token}'}
        response = client.post('/customers', json=self.params, headers=headers)

        assert response.status_code == HTTPStatus.CREATED
        assert response.json == customer.as_dict()
Ejemplo n.º 16
0
    def test_when_customer_details_are_valid_and_optional_present_creates_customer(
            self, database, optional_details, payment_processors):
        customer_counter = Customer.query.count()

        customer = CustomerCreate.call(self.token, self.processor_name, {
            **self.details,
            **optional_details
        })

        assert Customer.query.count() == customer_counter + 1
        assert customer is not None
        assert Customer.find_by_email(self.email) == customer
Ejemplo n.º 17
0
    def test_creates_payment_processor_customer_information_when_all_details_correct(
            self, database):
        customer = Customer.create(**self.customer_details)

        before_count = PaymentProcessorCustomerInformation.query.count()

        payment_processor_customer_information = PaymentProcessorCustomerInformationCreate.call(
            self.processor_name, customer,
            self.payment_processor_customer_information)

        assert PaymentProcessorCustomerInformation.query.count(
        ) == before_count + 1
        assert payment_processor_customer_information.name == self.processor_name
        assert payment_processor_customer_information.customer_id == customer.id
        assert payment_processor_customer_information.information == self.payment_processor_customer_information
Ejemplo n.º 18
0
    def test_returns_existing_payment_processor_customer_information_when_customer_exists(
            self, database):
        customer = Customer.create(**self.customer_details)

        prev_payment_processor_customer_information = PaymentProcessorCustomerInformationCreate.call(
            self.processor_name, customer,
            self.payment_processor_customer_information)

        before_count = PaymentProcessorCustomerInformation.query.count()

        payment_processor_customer_information = PaymentProcessorCustomerInformationCreate.call(
            self.processor_name, customer,
            self.payment_processor_customer_information)

        assert PaymentProcessorCustomerInformation.query.count(
        ) == before_count
        assert payment_processor_customer_information.id == prev_payment_processor_customer_information.id
Ejemplo n.º 19
0
    def test_converts_customer_to_dict(self, database):
        customer = Customer.create(
            first_name = self.first_name,
            last_name = self.last_name,
            company = self.company,
            email = self.email,
            phone = self.phone,
            fax = self.fax,
            website = self.website
        )

        expected_dict = {
            'first_name': customer.first_name,
            'last_name': customer.last_name,
            'email': customer.email,
            'token': customer.token,
            'updated_at': int(customer.created_at.timestamp()),
            'created_at': int(customer.created_at.timestamp())
        }

        assert customer.as_dict() == expected_dict
Ejemplo n.º 20
0
    def test_does_not_creates_payment_processor_customer_information_with_invalid_customer(
            self, database):
        name = 'braintreetest'
        information = {'customer_id': 'someid'}

        customer = Customer(id=uuid.uuid4(),
                            first_name='None',
                            last_name='Existing',
                            company='Really Cool LTD',
                            email='*****@*****.**',
                            phone='+1111111111',
                            fax='+12222222222',
                            website='https://www.reallycool.test')

        with pytest.raises(InvalidCustomer):
            PaymentProcessorCustomerInformation.create(name=name,
                                                       customer=customer,
                                                       information=information)

        assert PaymentProcessorCustomerInformation.query.filter_by(
            customer_id=customer.id).first() is None
Ejemplo n.º 21
0
    def test_find_by_customer_id_and_processor_name_retrieves_the_expected_payment_processor_customer_information(
            self, database):
        name = 'stripetest'
        information = {'payment_token': 'sometoken', 'customer_id': 'someid'}

        customer = Customer.create(first_name='Test',
                                   last_name='Coool',
                                   company='Really Cool LTD',
                                   email='*****@*****.**',
                                   phone='+1111111111',
                                   fax='+12222222222',
                                   website='https://www.reallycool.test')

        payment_processor_customer_information = PaymentProcessorCustomerInformation.create(
            name=name, customer=customer, information=information)

        assert PaymentProcessorCustomerInformation.find_by_customer_id_and_processor_name(
            uuid.uuid4(), name) is None
        assert PaymentProcessorCustomerInformation.find_by_customer_id_and_processor_name(
            customer.id, name) is not None
        assert PaymentProcessorCustomerInformation.find_by_customer_id_and_processor_name(
            customer.id, name) == payment_processor_customer_information
Ejemplo n.º 22
0
    def test_creates_customer_with_all_expected_attributes(self, database):
        customer = Customer.create(
            first_name = self.first_name,
            last_name = self.last_name,
            company = self.company,
            email = self.email,
            phone = self.phone,
            fax = self.fax,
            website = self.website
        )

        assert Customer.query.filter_by(
            email = self.email
        ).first().id == customer.id

        assert customer.id is not None

        assert Customer.query.filter_by(
            email = self.email
        ).first().token == customer.token
        assert customer.token is not None

        assert Customer.query.filter_by(
            email = self.email
        ).first().created_at == customer.created_at
        assert customer.created_at is not None

        assert Customer.query.filter_by(
            email = self.email
        ).first().updated_at == customer.updated_at
        assert customer.updated_at is not None

        assert customer.first_name == self.first_name
        assert customer.last_name == self.last_name
        assert customer.company == self.company
        assert customer.email == self.email
        assert customer.phone == self.phone
        assert customer.fax == self.fax
        assert customer.website == self.website
Ejemplo n.º 23
0
    def test_find_by_id_retrieves_the_expected_payment_method(self, database):
        customer = Customer.create(first_name='Test',
                                   last_name='Coool2',
                                   company='Really Cool LTD',
                                   email='*****@*****.**',
                                   phone='+1111111111',
                                   fax='+12222222222',
                                   website='https://www.reallycool.test')

        details = {
            'card_holder_name': 'Test Coool',
            'number': '1111' * 4,
            'cvv': '111',
            'expiration_date': '12/99'
        }

        payment_method = PaymentMethod.create(customer=customer,
                                              details=details)

        assert PaymentMethod.find_by_id(uuid.uuid4()) is None
        assert PaymentMethod.find_by_id(payment_method.id) is not None
        assert PaymentMethod.find_by_id(payment_method.id) == payment_method
Ejemplo n.º 24
0
    def test_creates_payment_processor_customer_information_with_all_expected_attributes(
            self, database):
        name = 'braintreetest'
        information = {'customer_id': 'someid'}

        customer = Customer.create(first_name='Test',
                                   last_name='Coool',
                                   company='Really Cool LTD',
                                   email='*****@*****.**',
                                   phone='+1111111111',
                                   fax='+12222222222',
                                   website='https://www.reallycool.test')

        payment_processor_customer_information = PaymentProcessorCustomerInformation.create(
            name=name, customer=customer, information=information)

        assert PaymentProcessorCustomerInformation.query.filter_by(
            name=name).first().id == payment_processor_customer_information.id

        assert payment_processor_customer_information.id is not None

        assert payment_processor_customer_information.name == name
        assert payment_processor_customer_information.information == information
Ejemplo n.º 25
0
    def test_does_not_create_payment_method_with_invalid_customer(
            self, database):
        customer = Customer(id=uuid.uuid4(),
                            first_name='None',
                            last_name='Existing',
                            company='Really Cool LTD',
                            email='*****@*****.**',
                            phone='+1111111111',
                            fax='+12222222222',
                            website='https://www.reallycool.test')

        details = {
            'card_holder_name': 'Test Coool',
            'number': '1111' * 4,
            'cvv': '111',
            'expiration_date': '12/99'
        }

        with pytest.raises(InvalidCustomer):
            payment_method = PaymentMethod.create(customer=customer,
                                                  details=details)

        assert PaymentMethod.query.filter_by(
            customer_id=customer.id).first() is None
Ejemplo n.º 26
0
    def _find_customer(self, token: str):
        self.customer = Customer.find_by_token(token)

        if self.customer is None:
            raise InvalidCustomer