-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(PC-34083)[API] fix: pro: draft offer: always check if EAN is used
Before publishing or updating a draft offer, the EAN should always be checked: if it is already used by another published (managed by the same venue), the edit or updated operation should fail.
- Loading branch information
1 parent
463f66b
commit 3b9f3e8
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
import pcapi.core.providers.factories as providers_factories | ||
from pcapi.core.providers.repository import get_provider_by_local_class | ||
import pcapi.core.users.factories as users_factories | ||
from pcapi.models.offer_mixin import OfferValidationStatus | ||
from pcapi.utils.date import format_into_utc_date | ||
|
||
|
||
|
@@ -674,6 +675,36 @@ def when_trying_to_patch_product(self, client): | |
assert response.status_code == 400 | ||
assert response.json["product_id"] == ["Vous ne pouvez pas changer cette information"] | ||
|
||
def test_cannot_edit_details_if_ean_is_already_used(self, client): | ||
ean = "0000000000001" | ||
email = "[email protected]" | ||
|
||
user_offerer = offerers_factories.UserOffererFactory(user__email=email) | ||
venue = offerers_factories.VenueFactory( | ||
managingOfferer=user_offerer.offerer, venueTypeCode=VenueTypeCode.RECORD_STORE | ||
) | ||
|
||
product = offers_factories.ProductFactory(subcategoryId=subcategories.LIVRE_PAPIER.id, extraData={"ean": ean}) | ||
offers_factories.OfferFactory( | ||
subcategoryId=subcategories.LIVRE_PAPIER.id, | ||
venue=venue, | ||
product=product, | ||
validation=OfferValidationStatus.APPROVED, | ||
extraData={"ean": ean}, | ||
) | ||
|
||
offer = offers_factories.OfferFactory( | ||
venue=venue, isActive=False, validation=OfferValidationStatus.DRAFT, product=product, extraData={"ean": ean} | ||
) | ||
|
||
data = {"name": "some other name"} | ||
response = client.with_session_auth(email).patch(f"offers/draft/{offer.id}", json=data) | ||
|
||
assert response.status_code == 400 | ||
assert response.json == { | ||
"ean": ["Une offre avec cet EAN existe déjà. Vous pouvez la retrouver dans l’onglet Offres."] | ||
} | ||
|
||
|
||
@pytest.mark.usefixtures("db_session") | ||
class Returns403Test: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
from pcapi.core.categories import subcategories_v2 as subcategories | ||
import pcapi.core.offerers.factories as offerers_factories | ||
from pcapi.core.offerers.schemas import VenueTypeCode | ||
import pcapi.core.offers.factories as offers_factories | ||
import pcapi.core.offers.models as offers_models | ||
from pcapi.core.testing import assert_num_queries | ||
|
@@ -242,3 +243,40 @@ def test_patch_publish_future_offer( | |
assert response.json["publication_date"] == ["Impossible de sélectionner une date de publication dans le passé"] | ||
offer = offers_models.Offer.query.get(stock.offerId) | ||
assert offer.validation == OfferValidationStatus.DRAFT | ||
|
||
def test_cannot_publish_offer_if_ean_is_already_used( | ||
self, | ||
client, | ||
): | ||
ean = "0000000000001" | ||
email = "[email protected]" | ||
|
||
user_offerer = offerers_factories.UserOffererFactory(user__email=email) | ||
venue = offerers_factories.VenueFactory( | ||
managingOfferer=user_offerer.offerer, venueTypeCode=VenueTypeCode.RECORD_STORE | ||
) | ||
|
||
product = offers_factories.ProductFactory(subcategoryId=subcategories.LIVRE_PAPIER.id, extraData={"ean": ean}) | ||
offers_factories.OfferFactory( | ||
subcategoryId=subcategories.LIVRE_PAPIER.id, | ||
venue=venue, | ||
product=product, | ||
validation=OfferValidationStatus.APPROVED, | ||
extraData={"ean": ean}, | ||
) | ||
|
||
offer = offers_factories.StockFactory( | ||
offer__venue=venue, | ||
offer__isActive=False, | ||
offer__validation=OfferValidationStatus.DRAFT, | ||
offer__product=product, | ||
offer__extraData={"ean": ean}, | ||
).offer | ||
|
||
client = client.with_session_auth(email) | ||
response = client.patch("/offers/publish", json={"id": offer.id}) | ||
|
||
assert response.status_code == 400 | ||
assert response.json == { | ||
"ean": ["Une offre avec cet EAN existe déjà. Vous pouvez la retrouver dans l’onglet Offres."] | ||
} |