Skip to content

Commit

Permalink
tests: create of tests of process transaction and rename test file
Browse files Browse the repository at this point in the history
  • Loading branch information
jamoqs committed Oct 31, 2023
1 parent a36c448 commit 8e90c3d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 29 deletions.
115 changes: 86 additions & 29 deletions apps/billing/tests/test_process_transaction.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,115 @@
import factory
from django.contrib.auth import get_user_model
from django.test import TestCase
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient

from apps.billing.factories import TransactionFactory, TransactionItemFactory
from apps.billing.models import Transaction
from apps.billing.serializers import ProcessTransactionSerializer
from apps.billing.serializers import TransactionItemSerializer, TransactionSerializer


class ProcessTransactionTest(TestCase):
"""
A test case for the ProcessTransaction view.
"""

def setUp(self):
self.payload = {
"transaction_id": "a9k05000-227f-4500-b71f-9f00fba1cf5f",
"client_name": "Cliente name",
"email": "[email protected]",
"address_line_1": "Av. Liberdade",
"address_line_2": "",
"city": "Lisboa",
"postal_code": "1250-142",
"state": "",
"country_code": "PT",
"vat_identification_number": "PT220234835",
"vat_identification_country": "PT",
"total_amount_exclude_vat": 114.73,
"total_amount_include_vat": 149.00,
"currency": "EUR",
"item": {
"description": "The product/line text with a description of what have been bought. The field need to be a string.",
"quantity": 1,
"vat_tax": 114.73,
"amount_exclude_vat": 114.73,
"amount_include_vat": 149.00,
"organization_code": "UPorto",
"product_id": "course-v1:UPorto+CBNEEF+2023_T3",
"product_code": "CBNEEF",
},
}
"""
Set up the test case by creating a client, endpoint, transaction, transaction item, and payload.
Also create a new user and generate a token for that user.
"""
self.client = APIClient()
self.endpoint = "/api/billing/transaction-complete/"

self.transaction = factory.build(dict, FACTORY_CLASS=TransactionFactory)
self.transaction_item = factory.build(dict, FACTORY_CLASS=TransactionItemFactory, transaction=None)
self.transaction["item"] = self.transaction_item
self.payload = self.transaction

# Create a new user and generate a token for that user
self.user = get_user_model().objects.create_user(username="testuser", password="testpass")
self.token = Token.objects.create(user=self.user)

def test_create_transaction(self):
"""
Test that a transaction can be created with a valid token.
"""
self.client.credentials(HTTP_AUTHORIZATION="Token " + self.token.key)

response = self.client.post(self.endpoint, self.payload, format="json")
self.assertEqual(response.status_code, 201)

transaction = Transaction.objects.get(transaction_id=self.payload["transaction_id"])
serializer = ProcessTransactionSerializer(transaction)
transaction_data = TransactionSerializer(transaction).data
transaction_data["item"] = TransactionItemSerializer(transaction.transaction_items.all()[0]).data

self.assertEqual(response.data, serializer.data)
for key, value in response.data["data"].items():
if key == "item":
for item_key, item_value in response.data["data"]["item"].items():
self.assertEqual(item_value, transaction_data["item"][item_key])
self.assertEqual(value, transaction_data[key])

def test_create_transaction_without_token(self):
"""
Test that a transaction cannot be created without a valid token.
"""
response = self.client.post(self.endpoint, self.payload, format="json")
self.assertEqual(response.status_code, 401)

def test_create_transaction_with_duplicate_transaction_id(self):
"""
Test that a transaction cannot be created with a duplicate transaction ID.
"""
# Create a new transaction with the same transaction ID as the payload
TransactionFactory.create(transaction_id=self.payload["transaction_id"])

self.client.credentials(HTTP_AUTHORIZATION="Token " + self.token.key)

response = self.client.post(self.endpoint, self.payload, format="json")
self.assertEqual(response.status_code, 400)

def test_create_transaction_with_invalid_fields(self):
"""
Test that a transaction cannot be created if fields are invalid.
"""
# Set the 'amount' field to an invalid value
self.payload["amount"] = "invalid_amount"

self.client.credentials(HTTP_AUTHORIZATION="Token " + self.token.key)

response = self.client.post(self.endpoint, self.payload, format="json")
self.assertEqual(response.status_code, 400)

def test_create_transaction_with_missing_fields(self):
"""
Test that a transaction cannot be created if required fields are missing.
"""
# Remove the required 'transaction_id' field from the payload
del self.payload["transaction_id"]

self.client.credentials(HTTP_AUTHORIZATION="Token " + self.token.key)

response = self.client.post(self.endpoint, self.payload, format="json")
self.assertEqual(response.status_code, 400)

def test_create_transaction_with_invalid_token(self):
"""
Test that a transaction cannot be created with an invalid token.
"""
self.client.credentials(HTTP_AUTHORIZATION="Token " + "invalid_token")

response = self.client.post(self.endpoint, self.payload, format="json")
self.assertEqual(response.status_code, 401)

def test_create_transaction_with_missing_item_field(self):
"""
Test that a transaction cannot be created if the 'item' field is missing.
"""
# Remove the required 'item' field from the payload
del self.payload["item"]

self.client.credentials(HTTP_AUTHORIZATION="Token " + self.token.key)

response = self.client.post(self.endpoint, self.payload, format="json")
self.assertEqual(response.status_code, 400)
File renamed without changes.

0 comments on commit 8e90c3d

Please sign in to comment.