def test_bar_payment_user_uses_only_accepted_tabs( self, mock_bar_models_charge_source, mock_bar_models_authorize_source ): """ Ensure that a payment only use tabs that have been accepted For this test you need: - A bar - A user - Another user - A tab created by each user """ self.user.customer.default_source = "123" self.user.customer.save() mock_bar_models_authorize_source.return_value = {"id": "jnsdflkgj34r"} mock_bar_models_charge_source.return_value = {"id": "jnsdflkgj34r"} # Create another user user = UserProfile.new("*****@*****.**", "password", "Ryan", "Frahm", datetime.datetime.now()) user.groups.add(Group.objects.filter(name="Drinkers")[0]) # Create a 2 tabs # This one should not be accepted tab2 = Tab.new(20.00, self.user.email, "ijbwflgkbsdf", user) # This one should be automatically accepted tab1 = Tab.new(20.00, self.user.email, "ijbwflgkbsdf", self.user) url = reverse("api:bar-pay", args=(1,)) amount = 10 d = {"amount": amount} response = self.client.post(url, d, format="json") self.assertEqual(len(response.data.get("transactions")), 1) self.assertIsNotNone(response.data.get("sale")) # The user's tab should now be $10 self.assertEqual(response.data.get("tab"), 10) transactions = response.data.get("transactions") self.assertEqual(transactions[0]["status"], "authorized") self.assertEqual(transactions[0]["amount"], 10.00) self.assertEqual(transactions[0]["tab_id"], tab1.pk)
def test_bar_payment_user_uses_2_tabs(self, mock_bar_models_charge_source, mock_bar_models_authorize_source): """ Ensure that a user's 2 tabs are used if they have one For this test you need: - A bar - A user - A tab """ self.user.customer.default_source = "123" self.user.customer.save() mock_bar_models_authorize_source.return_value = {"id": "jnsdflkgj34r"} mock_bar_models_charge_source.return_value = {"id": "jnsdflkgj34r"} # Create a tab tab1 = Tab.new(20.00, self.user.email, "ijbwflgkbsdf", self.user) tab2 = Tab.new(20.00, self.user.email, "ijbwflgkbsdf", self.user) url = reverse("api:bar-pay", args=(1,)) amount = 30 d = {"amount": amount} response = self.client.post(url, d, format="json") self.assertEqual(len(response.data.get("transactions")), 2) self.assertIsNotNone(response.data.get("sale")) # The user's tab should now be $10 self.assertEqual(response.data.get("tab"), 10) transactions = response.data.get("transactions") self.assertEqual(transactions[0]["status"], "charged") self.assertEqual(transactions[0]["amount"], 20.00) self.assertEqual(transactions[1]["status"], "authorized") self.assertEqual(transactions[1]["amount"], 10.00)
def test_bar_payment_same_amount_as_tab(self, mock_bar_models_charge_source, mock_bar_models_authorize_source): """ Ensure that a user's tab is used even if it is the same size as the amount For this test you need: - A bar - A user - A tab """ self.user.customer.default_source = "123" self.user.customer.save() mock_bar_models_authorize_source.return_value = {"id": "jnsdflkgj34r"} mock_bar_models_charge_source.return_value = {"id": "jnsdflkgj34r"} # Create a tab tab = Tab.new(20, self.user.email, "ijbwflgkbsdf", self.user) url = reverse("api:bar-pay", args=(1,)) amount = 20 d = {"amount": amount} response = self.client.post(url, d, format="json") self.assertEqual(len(response.data.get("transactions")), 1) self.assertIsNotNone(response.data.get("sale")) # The user's tab should now be $10 self.assertEqual(response.data.get("tab"), 0) transactions = response.data.get("transactions") self.assertEqual(transactions[0]["status"], "charged") self.assertEqual(transactions[0]["amount"], amount)
def test_bar_payment_tab_is_removed_if_lt_minimum( self, mock_bar_models_charge_source, mock_bar_models_authorize_source ): """ Ensure that a tab is staged to be removed if it is less than the minimum For this test you need: - A bar - A user - A tab """ self.user.customer.default_source = "123" self.user.customer.save() mock_bar_models_authorize_source.return_value = {"id": "jnsdflkgj34r"} mock_bar_models_charge_source.return_value = {"id": "jnsdflkgj34r"} # Create a tab tabamt = 20.00 tab = Tab.new(20.00, self.user.email, "ijbwflgkbsdf", self.user) url = reverse("api:bar-pay", args=(1,)) amount = tabamt - settings.MIN_CARD_COST + 0.01 d = {"amount": amount} response = self.client.post(url, d, format="json") self.assertEqual(len(response.data.get("transactions")), 1) self.assertIsNotNone(response.data.get("sale")) # The user's tab should now be $10 self.assertEqual(response.data.get("tab"), tabamt - amount) transactions = response.data.get("transactions") self.assertEqual(transactions[0]["status"], "authorized") self.assertEqual(float(transactions[0]["amount"]), amount) self.assertEqual(len(response.data.get("removed")), 1)
def test_bar_payment_user_uses_tab_and_their_money_fails( self, mock_bar_models_charge_source, mock_bar_models_authorize_source ): """ Ensure that a request to tip fails if the next transaction is less than the minimum Scenario: I have a $20 tab. I buy a $19 drink. I tip $5. """ self.user.customer.default_source = "123" self.user.customer.save() mock_bar_models_authorize_source.return_value = {"id": "jnsdflkgj34r"} mock_bar_models_charge_source.return_value = {"id": "jnsdflkgj34r"} # Create a tab tab1 = Tab.new(20.00, self.user.email, "ijbwflgkbsdf", self.user) url = reverse("api:bar-sale", args=(1, 1)) amount = 19 tip = 5 d = {"tip": tip} sale = Sale(amount=amount, bar=self.bar, customer=self.user, tip=5) sale.save() t = Transaction(sale=sale, owner=self.user, tab=tab1, amount=amount, source="123") t.save() response = self.client.put(url, d, format="json") self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data.get("add"), 1.00)
def test_bar_payment_users_total_tab_updates_after_partial_tab_use( self, mock_bar_models_charge_source, mock_bar_models_authorize_source ): """ Ensure that a user's total tab amount updates after a partial tab is used Scenario: I open a $20 tab for myself. I buy a $19 drink. My total tab should be $1. """ self.user.customer.default_source = "123" self.user.customer.save() mock_bar_models_authorize_source.return_value = {"id": "jnsdflkgj34r"} mock_bar_models_charge_source.return_value = {"id": "jnsdflkgj34r"} # Create a tab tab = Tab.new(20.00, self.user.email, "ijbwflgkbsdf", self.user) url = reverse("api:bar-pay", args=(1,)) amount = 19 d = {"amount": amount} response = self.client.post(url, d, format="json") # The user's tab should now be $10 self.assertEqual(response.data.get("tab"), 1)
def test_bar_payment_returns_notification_of_more_money_needed( self, mock_bar_models_charge_source, mock_bar_models_authorize_source ): """ Ensure that a user's tab is used if they have one For this test you need: - A bar - A user - A tab """ self.user.customer.default_source = "123" self.user.customer.save() mock_bar_models_authorize_source.return_value = {"id": "jnsdflkgj34r"} mock_bar_models_charge_source.return_value = {"id": "jnsdflkgj34r"} # Create a tab tab = Tab.new(20.00, self.user.email, "ijbwflgkbsdf", self.user) url = reverse("api:bar-pay", args=(1,)) amount = 21 d = {"amount": amount} response = self.client.post(url, d, format="json") # The user's tab should now be $10 transactions = response.data.get("transactions") self.assertEqual(transactions[1]["amount_needed"], settings.MIN_CARD_COST - 1) self.assertTrue(transactions[1]["error"])