def test_delete_cascade_venue_should_remove_synchronization_to_provider():
    # Given
    venue = offers_factories.VenueFactory()
    venue_to_delete = offers_factories.VenueFactory()
    offerers_factories.VenueProviderFactory(venue=venue_to_delete)
    offerers_factories.VenueProviderFactory(venue=venue)

    # When
    delete_cascade_venue_by_id(venue_to_delete.id)

    # Then
    assert Offerer.query.count() == 2
    assert Venue.query.count() == 1
    assert VenueProvider.query.count() == 1
    assert Provider.query.count() > 0
    def test_get_list_with_valid_venue_id(self, app):
        # given
        user = users_factories.ProFactory()
        titelive_things_provider = get_provider_by_local_class("TiteLiveThings")
        venue_provider = offerers_factories.VenueProviderFactory(
            venue__name="Librairie Titelive", provider=titelive_things_provider, lastSyncDate=datetime(2021, 8, 16)
        )

        # test that nOffers only containe active offers
        offers_factories.OfferFactory(
            venue=venue_provider.venue,
            lastProviderId=venue_provider.provider.id,
            idAtProviders="testIdAtProviders1",
            isActive=True,
        )
        offers_factories.OfferFactory(
            venue=venue_provider.venue,
            lastProviderId=venue_provider.provider.id,
            idAtProviders="testIdAtProviders2",
            isActive=False,
        )

        # when
        auth_request = TestClient(app.test_client()).with_session_auth(email=user.email)
        response = auth_request.get("/venueProviders?venueId=" + humanize(venue_provider.venue.id))

        # then
        assert response.status_code == 200
        assert response.json["venue_providers"][0].get("nOffers") == 1
        assert response.json["venue_providers"][0].get("id") == humanize(venue_provider.id)
        assert response.json["venue_providers"][0].get("venueId") == humanize(venue_provider.venue.id)
        assert response.json["venue_providers"][0].get("lastSyncDate") == "2021-08-16T00:00:00Z"
    def test_should_call_sync_venue_provider_for_expected_venue_provider(
            self, mock_synchronize_venue_provider):
        # Given
        provider = offerers_factories.ProviderFactory(
            localClass="TiteLiveStocks")
        vp1 = offerers_factories.VenueProviderFactory(provider=provider)
        vp2 = offerers_factories.VenueProviderFactory(provider=provider)
        offerers_factories.VenueProviderFactory(provider=provider,
                                                isActive=False)
        other_provider = offerers_factories.ProviderFactory()
        offerers_factories.VenueProviderFactory(provider=other_provider)

        # When
        update_venues_for_specific_provider(provider.id)

        # Then
        assert mock_synchronize_venue_provider.call_args_list == [
            mock.call(vp1),
            mock.call(vp2),
        ]
示例#4
0
    def test_does_not_update_objects_when_venue_provider_is_not_active(self, next_function):
        # Given
        offerers_factories.AllocineProviderFactory(localClass="TestLocalProvider")
        providable_info = create_providable_info(date_modified=datetime(2018, 1, 1))
        venue_provider = offerers_factories.VenueProviderFactory(isActive=False)
        local_provider = provider_test_utils.TestLocalProvider(venue_provider)
        next_function.side_effect = [[providable_info]]

        # When
        local_provider.updateObjects()

        # Then
        assert Product.query.count() == 0
    def when_listing_all_venues_without_venue_id_argument(self, app):
        # given
        user = users_factories.ProFactory()
        titelive_things_provider = get_provider_by_local_class("TiteLiveThings")
        offerers_factories.VenueProviderFactory(
            venue__name="Librairie Titelive",
            provider=titelive_things_provider,
        )

        # when
        auth_request = TestClient(app.test_client()).with_session_auth(email=user.email)
        response = auth_request.get("/venueProviders")

        # then
        assert response.status_code == 400
示例#6
0
def test_change_venue_provider():
    api_url = "https://example.com/provider/api"
    provider = offerers_factories.APIProviderFactory()
    venue_provider = offerers_factories.VenueProviderFactory(provider=provider)
    venue = venue_provider.venue
    stock = StockFactory(quantity=10, offer__venue=venue, offer__idAtProviders="1")
    BookingFactory(stock=stock)
    new_provider = offerers_factories.APIProviderFactory(apiUrl=api_url)

    api.change_venue_provider(venue_provider, new_provider.id)

    # Check venue_provider has change provider and sync date reset
    assert len(venue.venueProviders) == 1
    assert venue.venueProviders[0].providerId == new_provider.id
    assert venue.venueProviders[0].lastSyncDate == None
    # Check that the quantity of existing stocks has been reset.
    assert stock.quantity == 1
    assert stock.offer.lastProviderId == new_provider.id
示例#7
0
def test_fully_sync_venue():
    api_url = "https://example.com/provider/api"
    provider = offerers_factories.APIProviderFactory(apiUrl=api_url)
    venue_provider = offerers_factories.VenueProviderFactory(provider=provider)
    venue = venue_provider.venue
    stock = offers_factories.StockFactory(quantity=10,
                                          offer__venue=venue,
                                          offer__idAtProviders="1")
    bookings_factories.BookingFactory(stock=stock)
    product2 = offers_factories.ProductFactory(
        idAtProviders="1234",
        extraData={"prix_livre": 10},
        subcategoryId=subcategories.LIVRE_PAPIER.id,
    )

    with requests_mock.Mocker() as mock:
        response = {
            "total": 1,
            "stocks": [{
                "ref": "1234",
                "available": 5
            }],
        }
        mock.get(f"{api_url}/{venue_provider.venueIdAtOfferProvider}",
                 [{
                     "json": response
                 }, {
                     "json": {
                         "stocks": []
                     }
                 }])
        fully_sync_venue.fully_sync_venue(venue)

    # Check that the quantity of existing stocks has been reset.
    assert stock.quantity == 1
    # Check that offers and stocks have been created or updated.
    offer2 = offers_models.Offer.query.filter_by(product=product2).one()
    assert offer2.stocks[0].quantity == 5
    def test_provider_is_not_allocine(self, app):
        # Given
        user_offerer = offer_factories.UserOffererFactory()
        user = user_offerer.user
        offerer = user_offerer.offerer
        venue = offer_factories.VenueFactory(managingOfferer=offerer)
        provider = offerers_factories.ProviderFactory()
        offerers_factories.VenueProviderFactory(venue=venue, provider=provider)

        updated_venue_provider_data = {
            "providerId": humanize(provider.id),
            "venueId": humanize(venue.id),
            "isDuo": True,
            "quantity": 77,
            "price": 64,
        }

        auth_request = TestClient(app.test_client()).with_session_auth(email=user.email)

        # When
        response = auth_request.put("/venueProviders", json=updated_venue_provider_data)

        # then
        assert response.status_code == 400
示例#9
0
    def test_execution(self):
        # Given
        venue_provider = offerers_factories.VenueProviderFactory(isActive=True)
        siret = venue_provider.venue.siret

        stock = create_stock(ISBNs[0], siret, quantity=20)
        offer = create_offer(ISBNs[1], siret)
        product = create_product(ISBNs[2])
        create_product(ISBNs[4])
        create_product(ISBNs[6], isGcuCompatible=False)

        stock_with_booking = create_stock(ISBNs[5], siret, quantity=20)
        BookingFactory(stock=stock_with_booking)
        BookingFactory(stock=stock_with_booking, quantity=2)

        # When
        with requests_mock.Mocker() as request_mock:
            request_mock.get(
                f"https://fnac_url/{siret}?limit=1000",
                [{
                    "json": r,
                    "headers": {
                        "content-type": "application/json"
                    }
                } for r in fnac_responses],
                request_headers={
                    "Authorization": "Basic fake_token",
                },
            )
            synchronize_fnac_stocks.synchronize_venue_stocks_from_fnac(
                venue_provider.venue)

        # Then
        # Test updates stock if already exists
        assert stock.quantity == 6

        # Test creates stock if does not exist
        assert len(offer.stocks) == 1
        created_stock = offer.stocks[0]
        assert created_stock.quantity == 4

        # Test creates offer if does not exist
        created_offer = Offer.query.filter_by(
            idAtProviders=f"{ISBNs[2]}@{siret}").one()
        assert created_offer.stocks[0].quantity == 18

        # Test doesn't create offer if product does not exist or not gcu compatible
        assert Offer.query.filter_by(
            idAtProviders=f"{ISBNs[3]}@{siret}").count() == 0
        assert Offer.query.filter_by(
            idAtProviders=f"{ISBNs[6]}@{siret}").count() == 0

        # Test second page is actually processed
        assert Offer.query.filter_by(
            idAtProviders=f"{ISBNs[4]}@{siret}").count() == 1

        # Test existing bookings are added to quantity
        assert stock_with_booking.quantity == 17 + 1 + 2

        # Test fill stock attributes
        assert created_stock.price == 30
        assert created_stock.idAtProviders == f"{ISBNs[1]}@{siret}"

        # Test override stock price attribute
        assert stock.price == 35

        # Test fill offers attributes
        assert created_offer.bookingEmail == venue_provider.venue.bookingEmail
        assert created_offer.description == product.description
        assert created_offer.extraData == product.extraData
        assert created_offer.name == product.name
        assert created_offer.productId == product.id
        assert created_offer.venueId == venue_provider.venue.id
        assert created_offer.type == product.type
        assert created_offer.idAtProviders == f"{ISBNs[2]}@{siret}"