def test_create_hire_service_with_valid_coupon_and_coupon_is_invalidated( self): self.customer = FAKE_CUSTOMER.create_for_user(self.final) Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) data = { "client": self.final.pk, "professional": self.professional.pk, "date": "2028-10-10", "time": "18:00:00", "address": "Aligustre 20", "city": "Madrid", "region": "Madrid", "country": "Spain", "zip_code": "28039", "services": [self.service_1.pk, self.service_3.pk], "comments": "Hola", "total": 9.00, "credit_card": FAKE_CARD["id"], "coupon": self.coupon.code } client = APIClient() client.force_authenticate(user=self.final) response = client.post("/1.0/hire-services/", json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue(Coupon.objects.get(code=self.coupon.code).valid) response = self.client.get("/api/1.0/coupon/{0}".format( self.coupon.code)) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_remove_card_by_customer( self, customer_retrieve_source_mock, customer_retrieve_mock, card_retrieve_mock, card_delete_mock, ): stripe_card = Card._api_create(customer=self.customer, source=FAKE_CARD["id"]) Card.sync_from_stripe_data(stripe_card) self.assertEqual(1, self.customer.legacy_cards.count()) # remove card card = self.customer.legacy_cards.all()[0] card.remove() self.assertEqual(0, self.customer.legacy_cards.count()) api_key = card.default_api_key stripe_account = card._get_stripe_account_id(api_key) card_delete_mock.assert_called_once_with(self.customer.id, card.id, api_key=api_key, stripe_account=stripe_account)
def test_create_hire_service_ko_credit_card_card_not_exist(self): superuser = User.objects.get(username='******') token_jwt_1 = jwt_encode(superuser) self.customer = FAKE_CUSTOMER.create_for_user(self.final) Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) data = { "client": self.final.pk, "professional": self.professional.pk, "date": "2028-10-10", "time": "18:00:00", "address": "Aligustre 20", "city": "Madrid", "region": "Madrid", "country": "Spain", "zip_code": "28039", "services": [self.service_1.pk, self.service_3.pk], "comments": "Hola", "total": 10.00, "credit_card": FAKE_CARD_II["id"] } client = APIClient() response = client.post( "/1.0/hire-services/", data=data, format='json', HTTP_AUTHORIZATION='Token {}'.format(token_jwt_1)) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data.get('credit_card')[0], _("Credit card id is not included in app."))
def test_create_hire_service_several_same_services(self): self.customer = FAKE_CUSTOMER.create_for_user(self.final) Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) data = { "client": self.final.pk, "professional": self.professional.pk, "date": "2028-10-10", "time": "18:00:00", "address": "Aligustre 20", "city": "Madrid", "region": "Madrid", "country": "Spain", "zip_code": "28039", "services": [self.service_1.pk, self.service_1.pk], "comments": "Hola", "total": 6.00, "credit_card": FAKE_CARD["id"] } client = APIClient() client.force_authenticate(user=self.final) response = client.post("/1.0/hire-services/", json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_create_hire_service_with_invalid_price_and_coupon(self): self.customer = FAKE_CUSTOMER.create_for_user(self.final) Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) data = { "client": self.final.pk, "professional": self.professional.pk, "date": "2028-10-10", "time": "18:00:00", "address": "Aligustre 20", "city": "Madrid", "region": "Madrid", "country": "Spain", "zip_code": "28039", "services": [self.service_1.pk, self.service_3.pk], "comments": "Hola", "total": 10.00, "credit_card": FAKE_CARD["id"], "coupon": self.coupon.code } client = APIClient() client.force_authenticate(user=self.final) response = client.post("/1.0/hire-services/", json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data.get("non_field_errors", [''])[0], "Invalid price")
def test_remove_card_by_account(self, account_retrieve_mock): stripe_card = Card._api_create(account=self.custom_account, source=FAKE_CARD_IV["id"]) Card.sync_from_stripe_data(stripe_card) # remove card count, _ = Card.objects.filter(id=stripe_card["id"]).delete() self.assertEqual(1, count)
def test_remove_already_deleted_card(self, customer_retrieve_mock): stripe_card = Card._api_create(customer=self.customer, source=FAKE_CARD["id"]) Card.sync_from_stripe_data(stripe_card) self.assertEqual(self.customer.sources.count(), 1) card_object = self.customer.sources.first() Card.objects.filter(stripe_id=stripe_card["id"]).delete() self.assertEqual(self.customer.sources.count(), 0) card_object.remove() self.assertEqual(self.customer.sources.count(), 0)
def test_remove_already_deleted_card(self, customer_retrieve_mock): stripe_card = Card._api_create(customer=self.customer, source=FAKE_CARD["id"]) Card.sync_from_stripe_data(stripe_card) self.assertEqual(self.customer.legacy_cards.count(), 1) card_object = self.customer.legacy_cards.first() Card.objects.filter(id=stripe_card["id"]).delete() self.assertEqual(self.customer.legacy_cards.count(), 0) card_object.remove() self.assertEqual(self.customer.legacy_cards.count(), 0)
def test_remove(self, customer_retrieve_mock, card_retrieve_mock, card_delete_mock): stripe_card = Card._api_create(customer=self.customer, source=FAKE_CARD["id"]) Card.sync_from_stripe_data(stripe_card) self.assertEqual(1, self.customer.sources.count()) card = self.customer.sources.all()[0] card.remove() self.assertEqual(0, self.customer.sources.count()) self.assertTrue(card_delete_mock.called)
def test_remove(self, customer_retrieve_mock, card_retrieve_mock, card_delete_mock): stripe_card = Card._api_create(customer=self.customer, source=FAKE_CARD["id"]) Card.sync_from_stripe_data(stripe_card) self.assertEqual(1, self.customer.legacy_cards.count()) card = self.customer.legacy_cards.all()[0] card.remove() self.assertEqual(0, self.customer.legacy_cards.count()) self.assertTrue(card_delete_mock.called)
def test_remove_unexpected_exception(self, customer_retrieve_mock, card_delete_mock): stripe_card = Card._api_create(customer=self.customer, source=FAKE_CARD["id"]) Card.sync_from_stripe_data(stripe_card) card_delete_mock.side_effect = InvalidRequestError("Unexpected Exception", "blah") self.assertEqual(1, self.customer.legacy_cards.count()) card = self.customer.legacy_cards.all()[0] with self.assertRaisesMessage(InvalidRequestError, "Unexpected Exception"): card.remove()
def test_remove_unexpected_exception(self, customer_retrieve_mock, card_delete_mock): stripe_card = Card._api_create(customer=self.customer, source=FAKE_CARD["id"]) Card.sync_from_stripe_data(stripe_card) card_delete_mock.side_effect = InvalidRequestError("Unexpected Exception", "blah") self.assertEqual(1, self.customer.sources.count()) card = self.customer.sources.all()[0] with self.assertRaisesMessage(InvalidRequestError, "Unexpected Exception"): card.remove()
def test_remove_no_such_customer(self, customer_retrieve_mock, card_delete_mock): stripe_card = Card._api_create(customer=self.customer, source=FAKE_CARD["id"]) Card.sync_from_stripe_data(stripe_card) card_delete_mock.side_effect = InvalidRequestError("No such customer:", "blah") self.assertEqual(1, self.customer.sources.count()) card = self.customer.sources.all()[0] card.remove() self.assertEqual(0, self.customer.sources.count()) self.assertTrue(card_delete_mock.called)
def test_remove_no_such_customer(self, customer_retrieve_mock, card_delete_mock): stripe_card = Card._api_create(customer=self.customer, source=FAKE_CARD["id"]) Card.sync_from_stripe_data(stripe_card) card_delete_mock.side_effect = InvalidRequestError("No such customer:", "blah") self.assertEqual(1, self.customer.legacy_cards.count()) card = self.customer.legacy_cards.all()[0] card.remove() self.assertEqual(0, self.customer.legacy_cards.count()) self.assertTrue(card_delete_mock.called)
def test_update_hire_service_not_exist_ko(self): final = self.final token_jwt_1 = jwt_encode(final) self.customer = FAKE_CUSTOMER.create_for_user(self.final) Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) data = {"credit_card": FAKE_CARD_II['id']} client = APIClient() response = client.patch( "/1.0/hire-services/0/", data=data, format='json', HTTP_AUTHORIZATION='Token {}'.format(token_jwt_1)) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_remove_already_deleted_card_by_account(self, account_retrieve_mock, card_delete_mock): stripe_card = Card._api_create(account=self.custom_account, source=FAKE_CARD_IV["id"]) card = Card.sync_from_stripe_data(stripe_card) self.assertEqual(1, Card.objects.filter(id=stripe_card["id"]).count()) # remove card card.remove() self.assertEqual(0, Card.objects.filter(id=stripe_card["id"]).count()) # remove card again count, _ = Card.objects.filter(id=stripe_card["id"]).delete() self.assertEqual(0, count) api_key = card.default_api_key stripe_account = card._get_stripe_account_id(api_key) card_delete_mock.assert_called_once_with( self.custom_account.id, card.id, api_key=api_key, stripe_account=stripe_account, )
def test_create_card_finds_account_with_customer_absent(self): # deepcopy the CardDict object FAKE_CARD_DICT = deepcopy(FAKE_CARD) # Add account and remove customer FAKE_CARD_DICT["account"] = self.standard_account.id FAKE_CARD_DICT["customer"] = None card = Card.sync_from_stripe_data(FAKE_CARD_DICT) self.assertEqual(self.standard_account, card.account) self.assertEqual( card.get_stripe_dashboard_url(), f"https://dashboard.stripe.com/{card.account.id}/settings/payouts", ) self.assert_fks( card, expected_blank_fks={ "djstripe.Card.customer", "djstripe.BankAccount.account", "djstripe.Customer.coupon", "djstripe.Customer.default_payment_method", "djstripe.Customer.default_source", }, )
def test_create_card_finds_customer(self): card = Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) self.assertEqual(self.customer, card.customer) self.assertEqual( card.get_stripe_dashboard_url(), self.customer.get_stripe_dashboard_url() )
def test_create_card_finds_customer(self): card = Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) self.assertEqual(self.customer, card.customer) self.assertEqual( card.get_stripe_dashboard_url(), self.customer.get_stripe_dashboard_url() )
def test_update_hire_service_final__ok(self): final = self.final token_jwt_1 = jwt_encode(final) self.customer = FAKE_CUSTOMER.create_for_user(self.final) Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) data = {"credit_card": FAKE_CARD['id']} client = APIClient() response = client.patch( "/1.0/hire-services/{0}/".format(self.hire_service_1.pk), data=data, format='json', HTTP_AUTHORIZATION='Token {}'.format(token_jwt_1)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( HireService.objects.get(pk=1).credit_card, FAKE_CARD['id'])
def setUp(self): self.superuser = User.objects.get(pk=1) self.professional = User.objects.get(pk=3) self.final = User.objects.get(pk=4) self.professional_2 = User.objects.get(pk=5) self.hire_service_1 = HireService.objects.get(pk=1) self.hire_service_2 = HireService.objects.get(pk=2) self.hire_service_3 = HireService.objects.get(pk=3) self.hire_service_4 = HireService.objects.get(pk=4) # Add credit card to user_final self.customer = FAKE_CUSTOMER.create_for_user(self.final) Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) # Add credit card for hire service self.hire_service_2.credit_card = FAKE_CARD['id'] self.hire_service_2.save()
def test_update_hire_service_final_ko_credit_card_invalid(self): final = self.final token_jwt_1 = jwt_encode(final) self.customer = FAKE_CUSTOMER.create_for_user(self.final) Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) data = {"credit_card": "aaaaaaa"} client = APIClient() response = client.patch( "/1.0/hire-services/{0}/".format(self.hire_service_1.pk), data=data, format='json', HTTP_AUTHORIZATION='Token {}'.format(token_jwt_1)) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data.get('credit_card')[0], _("Credit card id is not included in app."))
def test_update_hire_service_professional__ko(self): professional = self.professional token_jwt_1 = jwt_encode(professional) self.customer = FAKE_CUSTOMER.create_for_user(self.final) Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) data = {"credit_card": FAKE_CARD['id']} client = APIClient() response = client.patch( "/1.0/hire-services/{0}/".format(self.hire_service_1.pk), data=data, format='json', HTTP_AUTHORIZATION='Token {}'.format(token_jwt_1)) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data.get('non_field_errors')[0], _("Only client for the hire services can update credit card"))
def test_str(self): fake_card = deepcopy(FAKE_CARD) card = Card.sync_from_stripe_data(fake_card) self.assertEqual( "<brand={brand}, last4={last4}, exp_month={exp_month}, exp_year={exp_year}, id={id}>" .format(brand=fake_card["brand"], last4=fake_card["last4"], exp_month=fake_card["exp_month"], exp_year=fake_card["exp_year"], id=fake_card["id"]), str(card))
def test_api_retrieve_by_customer_equals_retrieval_by_account( self, customer_retrieve_source_mock, account_retrieve_external_account_mock, customer_retrieve_mock, ): # deepcopy the CardDict object FAKE_CARD_DICT = deepcopy(FAKE_CARD) card = Card.sync_from_stripe_data(deepcopy(FAKE_CARD_DICT)) card_by_customer = card.api_retrieve() # Add account FAKE_CARD_DICT["account"] = FAKE_CUSTOM_ACCOUNT["id"] FAKE_CARD_DICT["customer"] = None card = Card.sync_from_stripe_data(FAKE_CARD_DICT) card_by_account = card.api_retrieve() # assert the same card object gets retrieved self.assertCountEqual(card_by_customer, card_by_account)
def test_create_hire_service_ko_datetime_less_than_two_hours(self): superuser = User.objects.get(username='******') token_jwt_1 = jwt_encode(superuser) self.customer = FAKE_CUSTOMER.create_for_user(self.final) Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) self.customer_2 = FAKE_CUSTOMER_II.create_for_user(self.professional) Card.sync_from_stripe_data(deepcopy(FAKE_CARD_II)) service_datetime = datetime.datetime.now() time = datetime.datetime.now() + datetime.timedelta(hours=1) data = { "client": self.final.pk, "professional": self.professional.pk, "date": service_datetime.strftime('%Y-%m-%d'), "time": time.strftime('%H:%M:%S'), "address": "Aligustre 20", "city": "Madrid", "region": "Madrid", "country": "Spain", "zip_code": "28039", "services": [self.service_1.pk, self.service_3.pk], "comments": "Hola", "total": 10.00, "credit_card": FAKE_CARD["id"] } client = APIClient() response = client.post( "/1.0/hire-services/", data=data, format='json', HTTP_AUTHORIZATION='Token {}'.format(token_jwt_1)) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data.get('non_field_errors')[0], _("The selected date is not enough to hire services."))
def test_str(self): fake_card = deepcopy(FAKE_CARD) card = Card.sync_from_stripe_data(fake_card) self.assertEqual( "<brand={brand}, last4={last4}, exp_month={exp_month}, exp_year={exp_year}, stripe_id={stripe_id}>".format( brand=fake_card["brand"], last4=fake_card["last4"], exp_month=fake_card["exp_month"], exp_year=fake_card["exp_year"], stripe_id=fake_card["id"] ), str(card) )
def test_create_card_finds_customer(self): card = Card.sync_from_stripe_data(deepcopy(FAKE_CARD)) self.assertEqual(self.customer, card.customer) self.assertEqual(card.get_stripe_dashboard_url(), self.customer.get_stripe_dashboard_url()) self.assert_fks( card, expected_blank_fks={ "djstripe.BankAccount.account", "djstripe.Customer.coupon", "djstripe.Customer.default_source", }, )
def test_str(self): fake_card = deepcopy(FAKE_CARD) card = Card.sync_from_stripe_data(fake_card) self.assertEqual( "<brand={brand}, last4={last4}, exp_month={exp_month}, exp_year={exp_year}, id={id}>".format( brand=fake_card["brand"], last4=fake_card["last4"], exp_month=fake_card["exp_month"], exp_year=fake_card["exp_year"], id=fake_card["id"], ), str(card), ) self.assert_fks(card, expected_blank_fks={"djstripe.Customer.coupon"})
def test_str(self): fake_card = deepcopy(FAKE_CARD) card = Card.sync_from_stripe_data(fake_card) self.assertEqual( "<brand={brand}, last4={last4}, exp_month={exp_month}, exp_year={exp_year}, id={id}>".format( brand=fake_card["brand"], last4=fake_card["last4"], exp_month=fake_card["exp_month"], exp_year=fake_card["exp_year"], id=fake_card["id"], ), str(card), ) self.assert_fks(card, expected_blank_fks={"djstripe.Customer.coupon"})
def test__str__(self, fake_stripe_data, has_account, has_customer, monkeypatch): def mock_customer_get(*args, **kwargs): data = deepcopy(FAKE_CUSTOMER) data["default_source"] = None data["sources"] = [] return data def mock_account_get(*args, **kwargs): return deepcopy(FAKE_CUSTOM_ACCOUNT) # monkeypatch stripe.Account.retrieve and stripe.Customer.retrieve calls to return # the desired json response. monkeypatch.setattr(stripe.Account, "retrieve", mock_account_get) monkeypatch.setattr(stripe.Customer, "retrieve", mock_customer_get) card = Card.sync_from_stripe_data(fake_stripe_data) default = False if has_account: account = Account.objects.filter( id=fake_stripe_data["account"]).first() default = fake_stripe_data["default_for_currency"] assert ( f"{enums.CardBrand.humanize(fake_stripe_data['brand'])} {account.default_currency} {'Default' if default else ''} {fake_stripe_data['last4']}" == str(card)) if has_customer: customer = Customer.objects.filter( id=fake_stripe_data["customer"]).first() default_source = customer.default_source default_payment_method = customer.default_payment_method if (default_payment_method and fake_stripe_data["id"] == default_payment_method.id ) or (default_source and fake_stripe_data["id"] == default_source.id): # current card is the default payment method or source default = True assert ( f"{enums.CardBrand.humanize(fake_stripe_data['brand'])} {fake_stripe_data['last4']} {'Default' if default else ''} Expires {fake_stripe_data['exp_month']} {fake_stripe_data['exp_year']}" == str(card))
def test_create_card_finds_customer_with_account_present(self): # deepcopy the CardDict object FAKE_CARD_DICT = deepcopy(FAKE_CARD) # Add account FAKE_CARD_DICT["account"] = self.standard_account.id card = Card.sync_from_stripe_data(FAKE_CARD_DICT) self.assertEqual(self.customer, card.customer) self.assertEqual(self.standard_account, card.account) self.assertEqual( card.get_stripe_dashboard_url(), self.customer.get_stripe_dashboard_url(), ) self.assert_fks( card, expected_blank_fks={ "djstripe.BankAccount.account", "djstripe.Customer.coupon", "djstripe.Customer.default_payment_method", "djstripe.Customer.default_source", }, )
def test_attach_objects_hook_without_customer(self): with self.assertRaisesMessage(ValidationError, "A customer was not attached to this card."): Card.sync_from_stripe_data(deepcopy(FAKE_CARD_III))
def test_attach_objects_hook_without_customer(self): with self.assertRaisesMessage(ValidationError, "A customer was not attached to this card."): Card.sync_from_stripe_data(deepcopy(FAKE_CARD_III))
def test_attach_objects_hook_without_customer(self): card = Card.sync_from_stripe_data(deepcopy(FAKE_CARD_III)) self.assertEqual(card.customer, None)
def test_attach_objects_hook_without_customer(self): FAKE_CARD_DICT = deepcopy(FAKE_CARD) FAKE_CARD_DICT["customer"] = None card = Card.sync_from_stripe_data(FAKE_CARD_DICT) self.assertEqual(card.customer, None)
def test_attach_objects_hook_without_account(self): card = Card.sync_from_stripe_data(FAKE_CARD) self.assertEqual(card.account, None)
def test_attach_objects_hook_without_customer(self): card = Card.sync_from_stripe_data(deepcopy(FAKE_CARD_III)) self.assertEqual(card.customer, None)