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
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
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
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
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'
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
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
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()
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
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
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
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
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
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
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
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
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
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
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