mirror of
				https://github.com/ae-utbm/sith.git
				synced 2025-10-28 07:33:53 +00:00 
			
		
		
		
	Remove GetCustomer API endpoint
This commit is contained in:
		| @@ -21,12 +21,11 @@ from ninja_extra import ControllerBase, api_controller, paginate, route | ||||
| from ninja_extra.pagination import PageNumberPaginationExtra | ||||
| from ninja_extra.schemas import PaginatedResponseSchema | ||||
|  | ||||
| from core.api_permissions import CanAccessLookup, CanView, IsLoggedInCounter, IsRoot | ||||
| from counter.models import Counter, Customer, Product | ||||
| from core.api_permissions import CanAccessLookup, CanView, IsRoot | ||||
| from counter.models import Counter, Product | ||||
| from counter.schemas import ( | ||||
|     CounterFilterSchema, | ||||
|     CounterSchema, | ||||
|     CustomerSchema, | ||||
|     ProductSchema, | ||||
|     SimplifiedCounterSchema, | ||||
| ) | ||||
| @@ -61,18 +60,6 @@ class CounterController(ControllerBase): | ||||
|         return filters.filter(Counter.objects.all()) | ||||
|  | ||||
|  | ||||
| @api_controller("/customer") | ||||
| class CustomerController(ControllerBase): | ||||
|     @route.get( | ||||
|         "{customer_id}", | ||||
|         response=CustomerSchema, | ||||
|         permissions=[IsLoggedInCounter], | ||||
|         url_name="get_customer", | ||||
|     ) | ||||
|     def get_customer(self, customer_id: int): | ||||
|         return self.get_object_or_exception(Customer, pk=customer_id) | ||||
|  | ||||
|  | ||||
| @api_controller("/product") | ||||
| class ProductController(ControllerBase): | ||||
|     @route.get( | ||||
|   | ||||
| @@ -4,7 +4,7 @@ from annotated_types import MinLen | ||||
| from ninja import Field, FilterSchema, ModelSchema | ||||
|  | ||||
| from core.schemas import SimpleUserSchema | ||||
| from counter.models import Counter, Customer, Product | ||||
| from counter.models import Counter, Product | ||||
|  | ||||
|  | ||||
| class CounterSchema(ModelSchema): | ||||
| @@ -16,12 +16,6 @@ class CounterSchema(ModelSchema): | ||||
|         fields = ["id", "name", "type", "club", "products"] | ||||
|  | ||||
|  | ||||
| class CustomerSchema(ModelSchema): | ||||
|     class Meta: | ||||
|         model = Customer | ||||
|         fields = ["user", "account_id", "amount", "recorded_products"] | ||||
|  | ||||
|  | ||||
| class CounterFilterSchema(FilterSchema): | ||||
|     search: Annotated[str, MinLen(1)] = Field(None, q="name__icontains") | ||||
|  | ||||
|   | ||||
| @@ -1,105 +0,0 @@ | ||||
| import pytest | ||||
| from django.contrib.auth.models import make_password | ||||
| from django.test.client import Client | ||||
| from django.urls import reverse | ||||
| from model_bakery import baker | ||||
|  | ||||
| from core.baker_recipes import board_user, subscriber_user | ||||
| from core.models import User | ||||
| from counter.models import Counter | ||||
|  | ||||
|  | ||||
| @pytest.fixture | ||||
| def customer_user() -> User: | ||||
|     return subscriber_user.make() | ||||
|  | ||||
|  | ||||
| @pytest.fixture | ||||
| def counter_bar() -> Counter: | ||||
|     return baker.make(Counter, type="BAR") | ||||
|  | ||||
|  | ||||
| @pytest.fixture | ||||
| def barmen(counter_bar: Counter) -> User: | ||||
|     user = subscriber_user.make(password=make_password("plop")) | ||||
|     counter_bar.sellers.add(user) | ||||
|     return user | ||||
|  | ||||
|  | ||||
| @pytest.fixture | ||||
| def board_member() -> User: | ||||
|     return board_user.make() | ||||
|  | ||||
|  | ||||
| @pytest.fixture | ||||
| def root_user() -> User: | ||||
|     return baker.make(User, is_superuser=True) | ||||
|  | ||||
|  | ||||
| @pytest.mark.django_db | ||||
| @pytest.mark.parametrize( | ||||
|     ("connected_user"), | ||||
|     [ | ||||
|         None,  # Anonymous user | ||||
|         "barmen", | ||||
|         "customer_user", | ||||
|         "board_member", | ||||
|         "root_user", | ||||
|     ], | ||||
| ) | ||||
| def test_get_customer_fail( | ||||
|     client: Client, | ||||
|     customer_user: User, | ||||
|     request: pytest.FixtureRequest, | ||||
|     connected_user: str | None, | ||||
| ): | ||||
|     if connected_user is not None: | ||||
|         client.force_login(request.getfixturevalue(connected_user)) | ||||
|     assert ( | ||||
|         client.get( | ||||
|             reverse("api:get_customer", kwargs={"customer_id": customer_user.id}) | ||||
|         ).status_code | ||||
|         == 403 | ||||
|     ) | ||||
|  | ||||
|  | ||||
| @pytest.mark.django_db | ||||
| def test_get_customer_from_bar_fail_wrong_referrer( | ||||
|     client: Client, customer_user: User, barmen: User, counter_bar: Counter | ||||
| ): | ||||
|     client.post( | ||||
|         reverse("counter:login", args=[counter_bar.pk]), | ||||
|         {"username": barmen.username, "password": "plop"}, | ||||
|     ) | ||||
|  | ||||
|     assert ( | ||||
|         client.get( | ||||
|             reverse("api:get_customer", kwargs={"customer_id": customer_user.id}) | ||||
|         ).status_code | ||||
|         == 403 | ||||
|     ) | ||||
|  | ||||
|  | ||||
| @pytest.mark.django_db | ||||
| def test_get_customer_from_bar_success( | ||||
|     client: Client, customer_user: User, barmen: User, counter_bar: Counter | ||||
| ): | ||||
|     client.post( | ||||
|         reverse("counter:login", args=[counter_bar.pk]), | ||||
|         {"username": barmen.username, "password": "plop"}, | ||||
|     ) | ||||
|  | ||||
|     response = client.get( | ||||
|         reverse("api:get_customer", kwargs={"customer_id": customer_user.id}), | ||||
|         HTTP_REFERER=reverse( | ||||
|             "counter:click", | ||||
|             kwargs={"counter_id": counter_bar.id, "user_id": customer_user.id}, | ||||
|         ), | ||||
|     ) | ||||
|     assert response.status_code == 200 | ||||
|     assert response.json() == { | ||||
|         "user": customer_user.id, | ||||
|         "account_id": customer_user.customer.account_id, | ||||
|         "amount": f"{customer_user.customer.amount:.2f}", | ||||
|         "recorded_products": customer_user.customer.recorded_products, | ||||
|     } | ||||
| @@ -72,9 +72,7 @@ class TestCounter(TestCase): | ||||
|             kwargs={"customer_id": self.richard.customer.pk}, | ||||
|         ) | ||||
|  | ||||
|         response = self.client.get( | ||||
|             response.get("location"), | ||||
|         ) | ||||
|         response = self.client.get(counter_url) | ||||
|         assert ">Richard Batsbak</" in str(response.content) | ||||
|  | ||||
|         self.client.post( | ||||
|   | ||||
		Reference in New Issue
	
	Block a user