Skip to content

Commit

Permalink
Merge branches 'split/test_api_admin_products.py-api/admin/products/t…
Browse files Browse the repository at this point in the history
…est_create.py', 'split/test_api_admin_products.py-api/admin/products/test_delete.py', 'split/test_api_admin_products.py-api/admin/products/test_list.py', 'split/test_api_admin_products.py-api/admin/products/test_retrieve.py' and 'split/test_api_admin_products.py-api/admin/products/test_update.py' into split/test-admin-api-product
  • Loading branch information
jbpenrath committed Nov 27, 2024
6 parents ecff910 + 674f4f2 + dd8d99d + 26155a4 + 1acd53d + f3506f1 commit b1cd2d9
Show file tree
Hide file tree
Showing 6 changed files with 543 additions and 458 deletions.
87 changes: 87 additions & 0 deletions src/backend/joanie/tests/core/api/admin/products/test_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""
Test suite for Product Admin API.
"""

from http import HTTPStatus

from django.test import TestCase

from joanie.core import factories


class ProductAdminApiCreateTest(TestCase):
"""
Test suite for the create Product Admin API endpoint.
"""

maxDiff = None

def test_admin_api_product_create(self):
"""
Staff user should be able to create a product.
"""
admin = factories.UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=admin.username, password="password")
data = {
"title": "Product 001",
"price": "100.00",
"price_currency": "EUR",
"type": "enrollment",
"call_to_action": "Purchase now",
"description": "This is a product description",
"instructions": "test instruction",
}

response = self.client.post("/api/v1.0/admin/products/", data=data)

self.assertEqual(response.status_code, HTTPStatus.CREATED)
content = response.json()
self.assertIsNotNone(content["id"])
self.assertEqual(content["title"], "Product 001")
self.assertEqual(content["instructions"], "test instruction")

def test_admin_api_product_create_with_blank_description(self):
"""
Staff user should be able to create a product with blank description.
"""
admin = factories.UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=admin.username, password="password")
data = {
"title": "Product 001",
"price": "100.00",
"price_currency": "EUR",
"type": "enrollment",
"call_to_action": "Purchase now",
"instructions": "Product instructions",
}

response = self.client.post("/api/v1.0/admin/products/", data=data)

self.assertEqual(response.status_code, HTTPStatus.CREATED)
content = response.json()
self.assertIsNotNone(content["id"])
self.assertEqual(content["title"], "Product 001")
self.assertEqual(content["description"], "")

def test_admin_api_product_create_with_blank_instructions(self):
"""
Staff user should be able to create a product with blank instructions.
"""
admin = factories.UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=admin.username, password="password")
data = {
"title": "Product 001",
"price": "100.00",
"price_currency": "EUR",
"type": "enrollment",
"description": "This is a product description",
"call_to_action": "Purchase now",
}

response = self.client.post("/api/v1.0/admin/products/", data=data)

self.assertEqual(response.status_code, HTTPStatus.CREATED)
content = response.json()
self.assertIsNotNone(content["id"])
self.assertEqual(content["title"], "Product 001")
self.assertEqual(content["instructions"], "")
29 changes: 29 additions & 0 deletions src/backend/joanie/tests/core/api/admin/products/test_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Test suite for Product Admin API.
"""

from http import HTTPStatus

from django.test import TestCase

from joanie.core import factories


class ProductAdminApiDeleteTest(TestCase):
"""
Test suite for the delete Product Admin API endpoint.
"""

maxDiff = None

def test_admin_api_product_delete(self):
"""
Staff user should be able to delete a product.
"""
admin = factories.UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=admin.username, password="password")
product = factories.ProductFactory()

response = self.client.delete(f"/api/v1.0/admin/products/{product.id}/")

self.assertEqual(response.status_code, HTTPStatus.NO_CONTENT)
128 changes: 128 additions & 0 deletions src/backend/joanie/tests/core/api/admin/products/test_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""
Test suite for Product Admin API.
"""

import random
from http import HTTPStatus

from django.test import TestCase

from joanie.core import enums, factories, models


class ProductAdminApiListTest(TestCase):
"""
Test suite for the list Product Admin API endpoint.
"""

maxDiff = None

def test_admin_api_product_request_without_authentication(self):
"""
Anonymous users should not be able to request products endpoint.
"""
response = self.client.get("/api/v1.0/admin/products/")

self.assertEqual(response.status_code, HTTPStatus.UNAUTHORIZED)
content = response.json()
self.assertEqual(
content["detail"], "Authentication credentials were not provided."
)

def test_admin_api_product_request_with_lambda_user(self):
"""
Lambda user should not be able to request products endpoint.
"""
admin = factories.UserFactory(is_staff=False, is_superuser=False)
self.client.login(username=admin.username, password="password")

response = self.client.get("/api/v1.0/admin/products/")

self.assertEqual(response.status_code, HTTPStatus.FORBIDDEN)
content = response.json()
self.assertEqual(
content["detail"], "You do not have permission to perform this action."
)

def test_admin_api_product_list(self):
"""
Staff user should be able to get paginated list of products
"""
admin = factories.UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=admin.username, password="password")
product_count = random.randint(1, 10)
factories.ProductFactory.create_batch(product_count)

response = self.client.get("/api/v1.0/admin/products/")

self.assertEqual(response.status_code, HTTPStatus.OK)
content = response.json()
self.assertEqual(content["count"], product_count)

def test_admin_api_product_list_filter_by_type(self):
"""
Staff user should be able to get paginated list of products filtered by
type
"""
admin = factories.UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=admin.username, password="password")

for [product_type, _] in enums.PRODUCT_TYPE_CHOICES:
factories.ProductFactory.create(type=product_type)

for [product_type, _] in enums.PRODUCT_TYPE_CHOICES:
response = self.client.get(f"/api/v1.0/admin/products/?type={product_type}")

product = models.Product.objects.get(type=product_type)
self.assertEqual(response.status_code, HTTPStatus.OK)
content = response.json()
self.assertEqual(content["count"], 1)
self.assertEqual(content["results"][0]["id"], str(product.id))

def test_admin_api_product_list_filter_by_invalid_type(self):
"""
Staff user should be able to get paginated list of products filtered by
type but an error should be returned if the type is not valid
"""
admin = factories.UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=admin.username, password="password")

response = self.client.get("/api/v1.0/admin/products/?type=invalid_type")

self.assertContains(
response,
'{"type":["'
"Select a valid choice. invalid_type is not one of the available choices."
'"]}',
status_code=HTTPStatus.BAD_REQUEST,
)

def test_admin_api_product_list_filter_by_id(self):
"""
Staff user should be able to get paginated list of products filtered by
id
"""
admin = factories.UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=admin.username, password="password")

products = factories.ProductFactory.create_batch(3)

response = self.client.get("/api/v1.0/admin/products/")
self.assertEqual(response.status_code, HTTPStatus.OK)
content = response.json()
self.assertEqual(content["count"], 3)

response = self.client.get(f"/api/v1.0/admin/products/?ids={products[0].id}")
self.assertEqual(response.status_code, HTTPStatus.OK)
content = response.json()
self.assertEqual(content["count"], 1)
self.assertEqual(content["results"][0]["id"], str(products[0].id))

response = self.client.get(
f"/api/v1.0/admin/products/?ids={products[0].id}&ids={products[1].id}"
)
self.assertEqual(response.status_code, HTTPStatus.OK)
content = response.json()
self.assertEqual(content["count"], 2)
self.assertEqual(content["results"][0]["id"], str(products[1].id))
self.assertEqual(content["results"][1]["id"], str(products[0].id))
Loading

0 comments on commit b1cd2d9

Please sign in to comment.