dashboard/billing/tests.py

116 lines
4.7 KiB
Python
Raw Normal View History

# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import time
from io import StringIO
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.urls import reverse
from django.test import TestCase, Client, override_settings
from django.utils.http import urlencode
from django.contrib.auth import authenticate
from django.conf import settings
from payments import get_payment_model, RedirectNeeded, PaymentStatus
from accounts.tests import register_util, login_util
from dash.tests import create_configurations, create_instance_util
from .models import Payment
class BillingTest(TestCase):
"""
Tests billing system
"""
def setUp(self):
self.username = "billing_user"
register_util(t=self, username=self.username)
create_configurations(t=self)
def test_payments(self):
c = Client()
login_util(self, c, "accounts.home")
instance_name = "test_create_instance_renders"
create_instance_util(
t=self, c=c, instance_name=instance_name, config=self.instance_config[0]
)
payment_uri = reverse("billing.invoice.generate", args=(instance_name,))
# generate invoice
resp = c.get(payment_uri)
self.assertEqual(resp.status_code, 302)
invoice_uri = resp.headers["Location"]
self.assertEqual("invoice/payment/" in invoice_uri, True)
# try to generate duplicate invoice, but should get redirected to previous invoice
resp = c.get(payment_uri)
self.assertEqual(resp.status_code, 302)
self.assertEqual(invoice_uri == resp.headers["Location"], True)
# check if invoice details page is displaying the invoice
# if payment is yet to be made:
# template will show payment button
# else:
# template will show payment date
resp = c.get(invoice_uri)
self.assertEqual(str.encode(instance_name) in resp.content, True)
self.assertEqual(
str.encode(str(self.instance_config[0].rent)) in resp.content, True
)
self.assertEqual(str.encode("Paid on") in resp.content, False)
# check if the unpaid invoice is displayed in the pending invoice view
resp = c.get(reverse("billing.invoice.pending"))
self.assertEqual(str.encode(invoice_uri) in resp.content, True)
# simulate payment. There's probably a better way to do this
payment = get_payment_model().objects.get(paid_by=self.user)
payment.status = PaymentStatus.CONFIRMED
payment.save()
# check if paid invoice is listed in paid invoice list view
resp = c.get(reverse("billing.invoice.paid"))
self.assertEqual(str.encode(invoice_uri) in resp.content, True)
# check if the paid invoice is displayed in the pending invoice view, should not be displayed
resp = c.get(reverse("billing.invoice.pending"))
self.assertEqual(str.encode(invoice_uri) in resp.content, False)
# check if the invoice details view is rendering paid invoice version
resp = c.get(invoice_uri)
self.assertEqual(str.encode(instance_name) in resp.content, True)
self.assertEqual(
str.encode(str(self.instance_config[0].rent)) in resp.content, True
)
self.assertEqual(str.encode("Paid on") in resp.content, True)
# try to generate an invoice for the second time on the same VM
# shouldn't be possible since payment is already made for the duration
resp = c.get(payment_uri)
self.assertEqual(resp.status_code, 400)
## payment success page; no real functionality but user is redirected here
# by stripe if payment is successful
resp = c.get(reverse("billing.invoice.success", args=(payment.public_ref,)))
self.assertEqual(b"success" in resp.content, True)
## payment failure page; no real functionality but user is redirected here
# by stripe if payment is successful
resp = c.get(reverse("billing.invoice.fail", args=(payment.public_ref,)))
self.assertEqual(b"failed" in resp.content, True)