51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
from rest_framework import status
|
|
from rest_framework.test import APITestCase
|
|
|
|
from config.tests.mixins import TestUserAuthenticationMixin
|
|
from shop.models import Brand
|
|
|
|
|
|
class TestBrandsAPI(APITestCase, TestUserAuthenticationMixin):
|
|
model_name = "brand"
|
|
|
|
def setUp(self):
|
|
self.create_user()
|
|
|
|
def create_brands(self):
|
|
Brand.objects.create(
|
|
name="Marca",
|
|
)
|
|
|
|
def test_fetch_brands(self):
|
|
self.create_brands()
|
|
self.login()
|
|
response = self.client.get("/api/v1/shop/brands/")
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert len(response.data.get("results")) == 1
|
|
|
|
def test_create_retrieve_brand(self):
|
|
self.login()
|
|
response = self.client.post("/api/v1/shop/brands/", {"name": "Marca 1"})
|
|
assert response.status_code == status.HTTP_201_CREATED
|
|
pk = response.data.get("id")
|
|
|
|
response = self.client.get(f"/api/v1/shop/brands/{pk}/")
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
def test_create_update_brand(self):
|
|
self.login()
|
|
response = self.client.post("/api/v1/shop/brands/", {"name": "Marca 1"})
|
|
assert response.status_code == status.HTTP_201_CREATED
|
|
pk = response.data.get("id")
|
|
|
|
response = self.client.get(f"/api/v1/shop/brands/{pk}/")
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
response = self.client.patch(f"/api/v1/shop/brands/{pk}/", {"name": "Marca 2"})
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
response = self.client.get(f"/api/v1/shop/brands/{pk}/")
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert response.data.get("name") == "Marca 2"
|