def when_offer_is_added_as_favorite_for_current_user(self, app): # Given user = users_factories.BeneficiaryGrant18Factory( email="*****@*****.**") offerer = create_offerer() venue = create_venue(offerer, postal_code="29100", siret="12345678912341") offer = create_offer_with_thing_product(venue, thumb_count=0) mediation = create_mediation(offer, is_active=True) repository.save(mediation) json = { "offerId": humanize(offer.id), "mediationId": humanize(mediation.id), } # When response = TestClient(app.test_client()).with_session_auth( user.email).post("/favorites", json=json) # Then assert response.status_code == 201 favorite = Favorite.query.one() assert favorite.offerId == offer.id assert favorite.mediationId == mediation.id assert favorite.userId == user.id # One call should be sent to batch, and one to sendinblue assert len(push_testing.requests) == 1 assert len(users_testing.sendinblue_requests) == 1 sendinblue_data = users_testing.sendinblue_requests[0] assert sendinblue_data["attributes"][ "LAST_FAVORITE_CREATION_DATE"] is not None
def when_user_is_logged_in_and_a_favorite_booked_offer_exist(self, app): # Given user = create_user() offerer = create_offerer() venue = create_venue(offerer, postal_code="29100", siret="12345678912341") offer = create_offer_with_thing_product(venue, thumb_count=0) mediation = create_mediation(offer, is_active=True) favorite = create_favorite(mediation=mediation, offer=offer, user=user) stock = create_stock(offer=offer, price=0) booking = create_booking(user=user, stock=stock) repository.save(booking, favorite) # When response = TestClient(app.test_client()).with_auth(user.email).get("/favorites") # Then assert response.status_code == 200 assert len(response.json) == 1 favorite = response.json[0] assert "offer" in favorite assert "venue" in favorite["offer"] assert "stocks" in favorite["offer"] assert stock.price == favorite["offer"]["stocks"][0]["price"] assert booking.quantity == favorite["booking"]["quantity"] assert humanize(booking.id) in favorite["booking"]["id"] assert "validationToken" not in favorite["offer"]["venue"]
def test_should_delete_product_when_related_offer_is_on_user_favorite_list( self, app): # Given isbn = "1111111111111" beneficiary = users_factories.BeneficiaryGrant18Factory() offerer = create_offerer(siren="775671464") venue = create_venue(offerer, name="Librairie Titelive", siret="77567146400110") product = create_product_with_thing_subcategory(id_at_providers=isbn) offer = create_offer_with_thing_product(venue, product=product) stock = create_stock(offer=offer, price=0) mediation = create_mediation(offer=offer) favorite = create_favorite(mediation=mediation, offer=offer, user=beneficiary) repository.save(venue, product, offer, stock, mediation, favorite) # When delete_unwanted_existing_product("1111111111111") # Then assert Product.query.count() == 0 assert Offer.query.count() == 0 assert Stock.query.count() == 0 assert Mediation.query.count() == 0 assert Favorite.query.count() == 0
def when_offer_is_added_as_favorite_for_current_user(self, app): # Given user = create_user(email="*****@*****.**") offerer = create_offerer() venue = create_venue(offerer, postal_code="29100", siret="12345678912341") offer = create_offer_with_thing_product(venue, thumb_count=0) mediation = create_mediation(offer, is_active=True) repository.save(user, mediation) json = { "offerId": humanize(offer.id), "mediationId": humanize(mediation.id), } # When response = TestClient(app.test_client()).with_auth( user.email).post("/favorites", json=json) # Then assert response.status_code == 201 favorite = FavoriteSQLEntity.query.one() assert favorite.offerId == offer.id assert favorite.mediationId == mediation.id assert favorite.userId == user.id
def when_user_is_logged_in_and_has_two_favorite_offers(self, app): # Given user = users_factories.BeneficiaryGrant18Factory() offerer = create_offerer() venue = create_venue(offerer, postal_code="29100", siret="12345678912341") offer1 = create_offer_with_thing_product(venue=venue, thumb_count=0) mediation1 = create_mediation(offer=offer1, is_active=True, idx=123) favorite1 = create_favorite(mediation=mediation1, offer=offer1, user=user) offer2 = create_offer_with_thing_product(venue=venue, thumb_count=0) favorite2 = create_favorite(offer=offer2, user=user) repository.save(user, favorite1, favorite2) # When response = TestClient(app.test_client()).with_session_auth( user.email).get("/favorites") # Then assert response.status_code == 200 assert len(response.json) == 2 first_favorite = response.json[0] assert "offer" in first_favorite assert "venue" in first_favorite["offer"] assert "mediationId" in first_favorite assert "validationToken" not in first_favorite["offer"]["venue"]
def when_expected_parameters_are_not_given(self, app): # Given user = create_user(email="*****@*****.**") offerer = create_offerer() venue = create_venue(offerer, postal_code="29100", siret="12345678912341") offer = create_offer_with_thing_product(venue, thumb_count=0) mediation = create_mediation(offer, is_active=True) favorite = create_favorite(mediation=mediation, offer=offer, user=user) repository.save(user, favorite) # When response = TestClient(app.test_client()).with_auth(user.email).delete("/favorites/1") # Then assert response.status_code == 404
def should_return_mediation_thumb_url_for_a_mediation_favorite(self): # Given offerer = create_offerer() venue = create_venue(offerer=offerer) offer = create_offer_with_thing_product(venue=venue) mediation = create_mediation(idx=123, offer=offer, thumb_count=1) favorite = create_domain_favorite(identifier=1, offer=offer, mediation=mediation) # When thumb_url = favorite.thumb_url # Then assert thumb_url == "http://localhost/storage/thumbs/mediations/PM"
def test_should_not_return_payload_when_offer_is_not_bookable(self, app): # Given offerer = create_offerer(validation_token="validation_token") venue = create_venue(offerer) product = create_product_with_event_type() offer = create_offer_with_event_product(venue, product=product) stock = create_stock(offer=offer) mediation = create_mediation(offer) repository.save(mediation, stock) # When offer_json_response = get_non_free_event_offer() # Then assert offer_json_response == {}
def test_returns_included_properties_on_joined_relationships(self, app): # given offerer = create_offerer() venue = create_venue(offerer) event_product = create_product_with_event_type(event_name="My Event") offer = create_offer_with_event_product(venue, product=event_product) mediation = create_mediation(offer) repository.save(mediation) EVENT_INCLUDES = [{"key": "mediations", "includes": ["thumbUrl"]}] # when dict_result = as_dict(offer, includes=EVENT_INCLUDES) # then assert "thumbUrl" in dict_result["mediations"][0]
def test_should_not_return_favorites_of_other_beneficiary(self, app): # given beneficiary = create_user() other_beneficiary = create_user() offerer = create_offerer() venue = create_venue(offerer=offerer) offer = create_offer_with_thing_product(venue=venue) mediation = create_mediation(offer=offer) favorite = create_favorite(mediation=mediation, offer=offer, user=other_beneficiary) repository.save(favorite) # when favorites = self.favorite_sql_repository.find_by_beneficiary(beneficiary.id) # then assert len(favorites) == 0
def test_does_not_return_excluded_keys_on_joined_relationships(self, app): # given offerer = create_offerer() venue = create_venue(offerer) event_product = create_product_with_event_subcategory( event_name="My Event") offer = create_offer_with_event_product(venue, product=event_product) mediation = create_mediation(offer) repository.save(mediation) EVENT_INCLUDES = [{"key": "mediations", "includes": ["-isActive"]}] # when dict_result = as_dict(offer, includes=EVENT_INCLUDES) # then assert "isActive" not in dict_result["mediations"][0]
def when_favorite_does_not_exist(self, app): # Given user = create_user(email="*****@*****.**") offerer = create_offerer() venue = create_venue(offerer, postal_code="29100", siret="12345678912341") offer = create_offer_with_thing_product(venue, thumb_count=0) mediation = create_mediation(offer, is_active=True) favorite = create_favorite(mediation=mediation, offer=offer, user=user) repository.save(user, favorite) # When response = TestClient(app.test_client()).with_auth(user.email).delete("/favorites/ABCD/ABCD") # Then assert response.status_code == 404 deleted_favorite = FavoriteSQLEntity.query.first() assert deleted_favorite == favorite
def test_should_not_return_booking_when_favorite_offer_booking_is_cancelled(self, app): # given beneficiary = create_user() offerer = create_offerer() venue = create_venue(offerer=offerer) offer = create_offer_with_thing_product(venue=venue) stock = create_stock_from_offer(idx=123, offer=offer, price=0) booking = create_booking(idx=321, stock=stock, venue=venue, user=beneficiary, is_cancelled=True) mediation = create_mediation(offer=offer) favorite = create_favorite(mediation=mediation, offer=offer, user=beneficiary) repository.save(favorite, booking) # when favorites = self.favorite_sql_repository.find_by_beneficiary(beneficiary.id) # then assert len(favorites) == 1 favorite = favorites[0] assert favorite.is_booked is False
def test_returns_a_list_of_beneficiary_favorites(self, app): # given beneficiary = create_user() offerer = create_offerer() venue = create_venue(offerer=offerer) offer_1 = create_offer_with_thing_product(venue=venue) mediation_1 = create_mediation(offer=offer_1) favorite_1 = create_favorite(mediation=mediation_1, offer=offer_1, user=beneficiary) offer_2 = create_offer_with_thing_product(venue=venue) favorite_2 = create_favorite(offer=offer_2, user=beneficiary) repository.save(favorite_1, favorite_2) # when favorites = self.favorite_sql_repository.find_by_beneficiary(beneficiary.id) # then assert len(favorites) == 2 assert isinstance(favorites[0], Favorite) assert isinstance(favorites[1], Favorite)
def when_mediation_is_not_found(self, app): # Given user = create_user(email="*****@*****.**") offerer = create_offerer() venue = create_venue(offerer, postal_code="29100", siret="12345678912341") offer = create_offer_with_thing_product(venue, thumb_count=0) mediation = create_mediation(offer, is_active=True) repository.save(user, mediation) json = { "offerId": humanize(offer.id), "mediationId": "ABCD", } # When response = TestClient(app.test_client()).with_auth( user.email).post("/favorites", json=json) # Then assert response.status_code == 404
def test_should_delete_product_when_related_offer_has_mediation(self, app): # Given isbn = "1111111111111" offerer = create_offerer(siren="775671464") venue = create_venue(offerer, name="Librairie Titelive", siret="77567146400110") product = create_product_with_thing_subcategory(id_at_providers=isbn) offer = create_offer_with_thing_product(venue, product=product) stock = create_stock(offer=offer, price=0) mediation = create_mediation(offer=offer) repository.save(venue, product, offer, stock, mediation) # When delete_unwanted_existing_product("1111111111111") # Then assert Product.query.count() == 0 assert Offer.query.count() == 0 assert Stock.query.count() == 0 assert Mediation.query.count() == 0
def create_industrial_mediations(offers_by_name): logger.info("create_industrial_mediations") mediations_with_asset = {} mediations_by_name = {} offer_items = list(offers_by_name.items()) offer_items_with_mediation = remove_every( offer_items, OFFERS_WITH_MEDIATION_REMOVE_MODULO) for (offer_with_mediation_name, offer_with_mediation) in offer_items_with_mediation: mediations_by_name[offer_with_mediation_name] = create_mediation( offer_with_mediation) repository.save(*mediations_by_name.values()) for mediation in mediations_by_name.values(): mediations_with_asset[ mediation.id] = store_public_object_from_sandbox_assets( "thumbs", mediation, mediation.offer.type) repository.save(*mediations_with_asset.values()) logger.info("created %d mediations", len(mediations_by_name))
def test_should_return_expected_payload_for_bookable_offer(self, app): # Given offerer = create_offerer() venue = create_venue(offerer) product = create_product_with_thing_type() offer = create_offer_with_thing_product(venue, product=product) stock = create_stock(offer=offer) mediation = create_mediation(offer) repository.save(mediation, stock) # When offer_json_response = get_non_free_thing_offer_with_active_mediation() # Then assert offer_json_response == { "mediationId": humanize(mediation.id), "offer": { "ageMax": None, "ageMin": None, "audioDisabilityCompliant": None, "bookingEmail": "*****@*****.**", "conditions": None, "dateCreated": format_into_utc_date(offer.dateCreated), "dateModifiedAtLastProvider": format_into_utc_date(offer.dateModifiedAtLastProvider), "description": None, "durationMinutes": None, "externalTicketOfficeUrl": None, "extraData": { "author": "Test Author" }, "fieldsUpdated": [], "id": humanize(offer.id), "idAtProviders": offer.idAtProviders, "isActive": True, "isDuo": False, "isNational": False, "lastProviderId": None, "mediaUrls": ["test/urls"], "mentalDisabilityCompliant": None, "motorDisabilityCompliant": None, "name": "Test Book", "productId": humanize(product.id), "thingName": "Test Book", "type": "ThingType.LIVRE_EDITION", "url": None, "venueCity": "Montreuil", "venueId": humanize(venue.id), "venueName": "La petite librairie", "visualDisabilityCompliant": None, "withdrawalDetails": None, }, }
def test_should_return_expected_payload_for_bookable_offer(self, app): # Given offerer = create_offerer() venue = create_venue(offerer) product = create_product_with_event_subcategory() offer = create_offer_with_event_product(venue, product=product) stock = create_stock(offer=offer) mediation = create_mediation(offer) repository.save(mediation, stock) # When offer_json_response = get_non_free_event_offer() # Then assert offer_json_response == { "mediationId": humanize(mediation.id), "offer": { "ageMax": None, "ageMin": None, "authorId": None, "audioDisabilityCompliant": None, "bookingEmail": "*****@*****.**", "conditions": None, "dateCreated": format_into_utc_date(offer.dateCreated), "dateModifiedAtLastProvider": format_into_utc_date(offer.dateModifiedAtLastProvider), "description": None, "durationMinutes": 60, "externalTicketOfficeUrl": None, "extraData": None, "fieldsUpdated": [], "id": humanize(offer.id), "idAtProvider": None, "idAtProviders": offer.idAtProviders, "isActive": True, "isDuo": False, "isEducational": False, "isNational": False, "dateUpdated": format_into_utc_date(offer.dateUpdated), "lastProviderId": None, "lastValidationDate": None, "mediaUrls": [], "mentalDisabilityCompliant": None, "motorDisabilityCompliant": None, "name": "Test event", "productId": humanize(product.id), "rankingWeight": None, "status": "ACTIVE", "subcategoryId": "SPECTACLE_REPRESENTATION", "thingName": "Test event", "url": None, "validation": "APPROVED", "venueCity": "Montreuil", "venueId": humanize(venue.id), "venueName": "La petite librairie", "visualDisabilityCompliant": None, "withdrawalDetails": None, }, "stockCount": 1, }