|
| 1 | +import pytest |
| 2 | +from factories import ProductFactory |
| 3 | + |
| 4 | + |
| 5 | +@pytest.mark.django_db |
| 6 | +def test_client_cannot_create_product(api_client, client_user, get_token): |
| 7 | + token = get_token(client_user) |
| 8 | + |
| 9 | + api_client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}") |
| 10 | + |
| 11 | + response = api_client.post( |
| 12 | + "/api/products/", |
| 13 | + { |
| 14 | + "name": "Producto Prohibido", |
| 15 | + "price": "100.00", |
| 16 | + "stock": 10, |
| 17 | + "is_public": True, |
| 18 | + }, |
| 19 | + format="json" |
| 20 | + ) |
| 21 | + |
| 22 | + assert response.status_code == 403 |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.django_db |
| 26 | +def test_staff_can_create_product(api_client, staff_user, get_token): |
| 27 | + token = get_token(staff_user) |
| 28 | + api_client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}") |
| 29 | + |
| 30 | + response = api_client.post( |
| 31 | + "/api/products/", |
| 32 | + { |
| 33 | + "name": "Producto Staff", |
| 34 | + "price": "50.00", |
| 35 | + "stock": 5, |
| 36 | + "is_public": True, |
| 37 | + }, |
| 38 | + format="json" |
| 39 | + ) |
| 40 | + |
| 41 | + assert response.status_code == 201 |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.django_db |
| 45 | +def test_staff_cannot_delete_product(api_client, staff_user, get_token): |
| 46 | + product = ProductFactory() |
| 47 | + |
| 48 | + token = get_token(staff_user) |
| 49 | + api_client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}") |
| 50 | + |
| 51 | + response = api_client.delete(f"/api/products/{product.id}/") |
| 52 | + |
| 53 | + assert response.status_code == 403 |
| 54 | + |
| 55 | +@pytest.mark.django_db |
| 56 | +def test_admin_can_delete_product(api_client, admin_user, get_token): |
| 57 | + product = ProductFactory() |
| 58 | + |
| 59 | + token = get_token(admin_user) |
| 60 | + api_client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}") |
| 61 | + |
| 62 | + response = api_client.delete(f"/api/products/{product.id}/") |
| 63 | + |
| 64 | + assert response.status_code == 204 |
0 commit comments