created endpoint to get all categories
This commit is contained in:
@@ -44,6 +44,7 @@ urlpatterns = [
|
||||
path('api/v1/my_company/', company_views.my_company, name='my-company'),
|
||||
path('api/v1/companies/sample/', company_views.random_company_sample , name='company-sample'),
|
||||
path('api/v1/purchase_email/', product_views.purchase_email, name='purchase-email'),
|
||||
path('api/v1/products/all_categories/', product_views.all_categories, name='all-categories'),
|
||||
path('api/v1/stats/me/', stat_views.track_user, name='user-tracker'),
|
||||
path('api/v1/autocomplete/category-tag/', product_views.CategoryTagAutocomplete.as_view(), name='category-autocomplete'),
|
||||
path('api/v1/', include(router.urls)),
|
||||
|
||||
@@ -13,7 +13,7 @@ from rest_framework import status
|
||||
|
||||
from companies.factories import CompanyFactory
|
||||
from products.factories import ProductFactory, ActiveProductFactory
|
||||
from products.models import Product
|
||||
from products.models import Product, CategoryTag
|
||||
|
||||
from core.factories import CustomUserFactory
|
||||
from core.utils import get_tokens_for_user
|
||||
@@ -1258,3 +1258,37 @@ class PurchaseEmailTest(APITestCase):
|
||||
payload = response.json()
|
||||
self.assertTrue( 'email' in payload['error'])
|
||||
|
||||
|
||||
class AllCategoriesTest(APITestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Tests setup
|
||||
"""
|
||||
self.endpoint = '/api/v1/products/all_categories/'
|
||||
# self.factory = ProductFactory
|
||||
self.model = CategoryTag
|
||||
# create user
|
||||
self.email = f"user@mail.com"
|
||||
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
|
||||
self.user = CustomUserFactory(email=self.email, is_active=True)
|
||||
self.user.set_password(self.password)
|
||||
self.user.save()
|
||||
|
||||
def test_get_all_categories(self):
|
||||
# create instances
|
||||
instances = [
|
||||
self.model.objects.create(name='A'),
|
||||
self.model.objects.create(name='B'),
|
||||
self.model.objects.create(name='C'),
|
||||
self.model.objects.create(name='D'),
|
||||
self.model.objects.create(name='E'),
|
||||
]
|
||||
|
||||
response = self.client.get(self.endpoint)
|
||||
|
||||
# assertions
|
||||
self.assertEquals(response.status_code, 200)
|
||||
|
||||
payload = response.json()
|
||||
|
||||
self.assertEquals(len(instances), len(payload))
|
||||
|
||||
@@ -332,3 +332,13 @@ def purchase_email(request):
|
||||
|
||||
# response
|
||||
return Response()
|
||||
|
||||
|
||||
@api_view(['GET'])
|
||||
@permission_classes([AllowAny,])
|
||||
def all_categories(request):
|
||||
all_categories = []
|
||||
for instance in CategoryTag.objects.all():
|
||||
all_categories.append(instance.label)
|
||||
return Response(data=all_categories)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user